Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_urllib2_localnet.cpython-35.opt-1.pyc



��Yfs`�
@s+ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZejd�Z
yddlZWnek
r�dZYnXejje�Zejjed�Zejjed�ZGdd�dejj�ZGdd	�d	e
j�ZGd
d�d�ZGdd
�d
ejj�ZGdd�dejj�Zej e
d�Gdd�dej!��Z"ej e
d�Gdd�dej!��Z#dd�Z$ej e
d�Gdd�dej!��Z%da&dd�Z'dd�Z(e)dkr'ej*�dS)�N)�support�	threadingzkeycert.pemzkeycert2.pemc@s.eZdZdZdd�Zdd�ZdS)�LoopbackHttpServerz^HTTP server w/ a few modifications that make it useful for
    loopback testing purposes.
    cCs-tjjj|||�|jjd�dS)Ng�������?)�http�server�
HTTPServer�__init__�socket�
settimeout)�selfZserver_addressZRequestHandlerClass�r�?/opt/alt/python35/lib64/python3.5/test/test_urllib2_localnet.pyr!szLoopbackHttpServer.__init__cCs,|jj�\}}|jd�||fS)zHTTPServer method, overridden.g$@)r	Zacceptr
)r�requestZclient_addressrrr
�get_request*s
zLoopbackHttpServer.get_requestN)�__name__�
__module__�__qualname__�__doc__rrrrrr
rs	rc@s:eZdZdZdd�Zdd�Zdd�ZdS)	�LoopbackHttpServerThreadz2Stoppable thread that runs a loopback http server.cCsVtjj|�d|_tj�|_d|_td|�|_|jj	|_
dS)NFzHTTP/1.0�	127.0.0.1r)rr)r�Threadr�_stop_serverZEvent�readyZprotocol_versionr�httpdZserver_port�port)r�request_handlerrrr
r9s		z!LoopbackHttpServerThread.__init__cCs$d|_|j�|jj�dS)z.Stops the webserver if it's currently running.TN)r�joinrZserver_close)rrrr
�stopBs	
zLoopbackHttpServerThread.stopcCs.|jj�x|js)|jj�qWdS)N)r�setrr�handle_request)rrrr
�runJs
zLoopbackHttpServerThread.runN)rrrrrrr rrrr
r6s	rc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)�DigestAuthHandlerz-Handler for performing digest authentication.cCs1d|_g|_i|_d|_d|_dS)Nrz
Test Realm�auth)�_request_num�_nonces�_users�_realm_name�_qop)rrrr
rTs
				zDigestAuthHandler.__init__cCs
||_dS)N)r')rZqoprrr
�set_qop[szDigestAuthHandler.set_qopcCs
||_dS)N)r%)rZusersrrr
�	set_users^szDigestAuthHandler.set_userscCs
||_dS)N)r&)rZrealmrrr
�	set_realmbszDigestAuthHandler.set_realmcCsJ|jd7_tjt|j�jd��j�}|jj|�|S)N��ascii)r#�hashlib�md5�str�encode�	hexdigestr$�append)r�noncerrr
�_generate_noncees'z!DigestAuthHandler._generate_noncecCs�|jd�}||dd�}|jd�}i}xx|D]p}|jd�\}}|j�}|ddkr�|ddkr�|dd�}n|j�}|||<q?W|S)	N� r+�,�=r�"���r9)�find�split�strip)rZauth_strZfirst_space_index�parts�	auth_dict�part�name�valuerrr
�_create_auth_dictks
 z#DigestAuthHandler._create_auth_dictcCs�i}|j|�||d<||d<||d<d|}tj|jd��j�}d|}tj|jd��j�}	||d<|	|d<d	|}
tj|
jd��j�}||d
kS)N�password�method�uriz#%(username)s:%(realm)s:%(password)sr,z%(method)s:%(uri)s�HA1�HA2z3%(HA1)s:%(nonce)s:%(nc)s:%(cnonce)s:%(qop)s:%(HA2)s�response)�updater-r.r0r1)rr>rCrDrEZ
final_dictZHA1_strrFZHA2_strrGZresponse_strrHrrr
�_validate_auth|s







z DigestAuthHandler._validate_authcCsg|jdd�|jdd�|jdd|j|j|j�f�|j�|jjd�dS)	Ni�zProxy Authentication RequiredzContent-Typez	text/htmlzProxy-Authenticatez(Digest realm="%s", qop="%s",nonce="%s", sProxy Authentication Required.F)�
send_response�send_headerr&r'r4�end_headers�wfile�write)rrrrr
�_return_auth_challenge�s
z(DigestAuthHandler._return_auth_challengecCst|j�dkrdSd|jkr5|j|�S|j|jd�}|d|jkrr|j|d}n
|j|�S|jd�|jkr�|j|�S|jj|d�d}x;|j|j	gD]'}|j
|||j|�r�d}q�W|s|j|�SdSdS)aPerforms digest authentication on the given HTTP request
        handler.  Returns True if authentication was successful, False
        otherwise.

        If no users have been set, then digest auth is effectively
        disabled and this method will always return True.
        rTzProxy-AuthorizationZusernamer3FN)�lenr%�headersrPrB�getr$�remove�path�
short_pathrJZcommand)rrr>rCZauth_validatedrUrrr
r�s,	


		

z DigestAuthHandler.handle_requestN)
rrrrrr(r)r*r4rBrJrPrrrrr
r!Qsr!c@s�eZdZdZdZdZdZdeefZej	ej
d��jd�Zdd�Z
d	d
�Zdd�Zd
d�Zdd�ZdS)�BasicAuthHandlerz,Handler for performing basic authentication.�testUser�testPass�Testz%s:%sr,cOstjjj|||�dS)N)rr�BaseHTTPRequestHandlerr)r�args�kwargsrrr
r�szBasicAuthHandler.__init__cGsdS)Nr)r�formatr\rrr
�log_message�szBasicAuthHandler.log_messagecCs+|jd�|jdd�|j�dS)N��zContent-typez	text/html)rKrLrM)rrrr
�do_HEAD�s
zBasicAuthHandler.do_HEADcCsB|jd�|jdd|j�|jdd�|j�dS)Ni�zWWW-AuthenticatezBasic realm="%s"zContent-typez	text/html)rKrL�REALMrM)rrrr
�do_AUTHHEAD�s
zBasicAuthHandler.do_AUTHHEADcCs�|jjdd�s2|j�|jjd�nV|jjdd�d|jkr~|jd�|j�|jjd�n
|j�dS)NZ
Authorization�sNo Auth header receivedzBasic r`sIt works)rRrSrcrNrO�ENCODED_AUTHrKrM)rrrr
�do_GET�s
	

zBasicAuthHandler.do_GETN)rrrr�USER�PASSWDrbZUSER_PASSWD�base64Z	b64encoder0�decodererr_rarcrfrrrr
rW�s!rWc@s:eZdZdZdd�Zdd�Zdd�ZdS)	�FakeProxyHandlerz�This is a 'fake proxy' that makes it look like the entire
    internet has gone down due to a sudden zombie invasion.  It main
    utility is in providing us with authentication support for
    testing.
    cOs&||_tjjj|||�dS)N)�digest_auth_handlerrrr[r)rrlr\r]rrr
r�s	zFakeProxyHandler.__init__cGsdS)Nr)rr^r\rrr
r_szFakeProxyHandler.log_messagecCs�tjj|jd�\}}}}}}||_|jj|�r�|jdd�|jdd�|j	�|j
jtd|jd��|j
jd�dS)	Nrr`ZOKzContent-Typez	text/htmlzYou've reached %s!<BR>r,sFOur apologies, but our server is down due to a sudden zombie invasion.)
�urllib�parseZurlparserUrVrlrrKrLrMrNrO�bytes)rZscmZnetlocrUZparamsZqueryZfragmentrrr
rfs	!	

zFakeProxyHandler.do_GETN)rrrrrr_rfrrrr
rk�srkz!Threading required for this test.csdeZdZdZdZdZdZ�fdd�Z�fdd�Zd	d
�Z	dd�Z
�S)
�BasicAuthTestsrXrYZ	IncorrectrZcsutt|�j�dd�}t|�|_|j|jj�d|jj|_|jj	�|jj
j�dS)Nc_s
t||�S)N)rW)r\r]rrr
�#http_server_with_basic_auth_handler!szABasicAuthTests.setUp.<locals>.http_server_with_basic_auth_handlerzhttp://127.0.0.1:%s)�superrp�setUprr�
addCleanuprr�
server_url�startr�wait)rrq)�	__class__rr
rss
zBasicAuthTests.setUpcstt|�j�dS)N)rrrp�tearDown)r)rxrr
ry)szBasicAuthTests.tearDowncCs�tjj�}|j|j|j|j|j�tjjtjj	|��y |j
tjj|j��Wn+tjj
k
r�|jd|j�YnXdS)Nz!Basic auth failed for the url: %s)rmr�HTTPBasicAuthHandler�add_passwordrbrurgrh�install_opener�build_opener�
assertTrue�urlopen�error�	HTTPError�fail)r�ahrrr
�test_basic_auth_success,s" z&BasicAuthTests.test_basic_auth_successcCsstjj�}|j|j|j|j|j�tjjtjj	|��|j
tjjtjj
|j�dS)N)rmrrzr{rbrurg�INCORRECT_PASSWDr|r}�assertRaisesr�r�r)rr�rrr
�test_basic_auth_httperror5s"z(BasicAuthTests.test_basic_auth_httperror)rrrrgrhr�rbrsryr�r�rr)rxr
rps	rpcs|eZdZdZdZdZdZ�fdd�Z�fdd�Zd	d
�Z	dd�Z
d
d�Zdd�Z�S)�ProxyAuthTestszhttp://localhostZtesterZtest123Z	TestRealmcs$tt��j�dd�}�j|tjj��dtjd<dtjd<t��_�jj	�j
�ji��jj�j
��fdd�}t|��_�jj��jjj�d�jj}tjjd	|i�}tjj��_tjj|�j��_dS)
NcSs!tjj�tjj|�dS)N)�os�environ�clearrI)�old_environrrr
�restore_environGs
z-ProxyAuthTests.setUp.<locals>.restore_environrd�NO_PROXY�no_proxycst�j||�S)N)rkrl)r\r])rrr
�create_fake_proxy_handlerRsz7ProxyAuthTests.setUp.<locals>.create_fake_proxy_handlerzhttp://127.0.0.1:%dr)rrr�rsrtr�r��copyr!rlr)rgrhr*rbrrrvrrwrrmrZProxyHandlerZProxyDigestAuthHandler�proxy_digest_handlerr}�opener)rr�r�Z	proxy_url�handler)rx)rr
rsDs"


	zProxyAuthTests.setUpcs$|jj�tt|�j�dS)N)rrrrr�ry)r)rxrr
ry^s
zProxyAuthTests.tearDowncCs_|jj|j|j|j|jd�|jjd�|jt	j
j|jj
|j�dS)NZbadr")r�r{rb�URLrgrhrlr(r�rmr�r�r��open)rrrr
�-test_proxy_with_bad_password_raises_httperrorbs	z<ProxyAuthTests.test_proxy_with_bad_password_raises_httperrorcCs6|jjd�|jtjj|jj|j�dS)Nr")	rlr(r�rmr�r�r�r�r�)rrrr
�,test_proxy_with_no_password_raises_httperrorjs	z;ProxyAuthTests.test_proxy_with_no_password_raises_httperrorcCsk|jj|j|j|j|j�|jjd�|jj	|j�}x|j
�r\qMW|j�dS)Nr")r�r{rbr�rgrhrlr(r�r��read�close)r�resultrrr
�test_proxy_qop_auth_workspsz(ProxyAuthTests.test_proxy_qop_auth_workscCs�|jj|j|j|j|j�|jjd�y|jj	|j�}Wnt
jjk
rnd}YnX|r�x|j
�r�qxW|j�dS)Nzauth-int)r�r{rbr�rgrhrlr(r�r�rmr��URLErrorr�r�)rr�rrr
�0test_proxy_qop_auth_int_works_or_throws_urlerrorysz?ProxyAuthTests.test_proxy_qop_auth_int_works_or_throws_urlerror)
rrrr�rgrhrbrsryr�r�r�r�rr)rxr
r�<s	r�cs)G��fdd�dtjj���S)NcsaeZdZdZgZgZdZdd�Zdd�Z��fdd�Z	d	d
�Z
dS)z1GetRequestHandler.<locals>.FakeHTTPRequestHandlerz	TestHTTP/�PcSs?|j�}x,|r:|jj|�}||d�}qWdS)N)�	send_headrNrO)r�body�donerrr
rf�s	z8GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_GETcSsC|jd}|jjt|��}|j�|jj|�dS)NzContent-Length)rRZrfiler��intrf�requestsr2)rZcontent_lengthZ	post_datarrr
�do_POST�s

z9GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_POSTcs�|j�_|jj|j��jd�\}}}|j|�x1|D])\}}|j||d|ji�qKW|r�|jdd�|j	�|S|j	�dS)NrrzContent-typez
text/plain)
rR�headers_receivedr�r2rU�poprKrLrrM)rZ
response_coderRr��headerrA)�FakeHTTPRequestHandler�	responsesrr
r��s
!
z;GetRequestHandler.<locals>.FakeHTTPRequestHandler.send_headcWsdS)Nr)rr\rrr
r_�sz=GetRequestHandler.<locals>.FakeHTTPRequestHandler.log_messageN)rrrZserver_versionr�r�rrfr�r�r_r)r�r�rr
r��sr�)rrr[)r�r)r�r�r
�GetRequestHandler�s%&r�cs	eZdZdZ�fdd�Zddd�Zddd�Zdd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Z�S))�TestUrlopenaNTests urllib.request.urlopen using the network.

    These tests are not exhaustive.  Assuming that testing using files does a
    good job overall of some of the basic interface features.  There are no
    tests exercising the optional 'data' and 'proxies' arguments.  No tests
    for transparent redirection have been written.
    csVtt|�j�dd�}|j|tjj��dtjd<dtjd<dS)NcSs!tjj�tjj|�dS)N)r�r�r�rI)r�rrr
r��s
z*TestUrlopen.setUp.<locals>.restore_environ�*r�r�)rrr�rsrtr�r�r�)rr�)rxrr
rs�s

zTestUrlopen.setUpNc
Ks�g}tjj|||�}zV|j|jd��|j|j��|j|jd��|j|j��Wd|j�Xdj	|�S)Nr`i�)
rmrr�extend�	readlinesr2�readliner�r�r)r�url�datar]�l�frrr
r�szTestUrlopen.urlopencCs�|dkrdgdfg}t|�}t|�|_|j|jj�|jj�|jjj�|jj}||_|S)Nr`s
we don't care)	r�rrrtrrvrrwr)rr�r�rrrr
�start_server�s
	zTestUrlopen.start_servercKs~ttjd�s|jd�ddlm}|dkrMdgdfg}t|�}||d||�}|j|_|S)NZHTTPSHandlerzssl support requiredr)�make_https_serverr`s
we care a bitZ
handler_class)�hasattrrmr�skipTestZtest.ssl_serversr�r�r)rr�r]r�r�rrrr
�start_https_server�s
zTestUrlopen.start_https_servercCsyd}dd
gdfdg|fg}|j|�}|jd|j�}|j||�|j|jdd	g�dS)NsWe got here...i.�Location�(http://localhost:%(port)s/somewhere_elserdr`zhttp://localhost:%s/�/z/somewhere_else)r�r�)r�rr�assertEqualr�)r�expected_responser�r�r�rrr
�test_redirection�s	zTestUrlopen.test_redirectioncCsZd}d}ddg|fg}|j|�}|jd|j�}|j||�dS)Nshello worldsa
hello worl
1
d
0
r`�Transfer-Encoding�chunkedzhttp://localhost:%s/)r�r�)r�rrr�)rr�Z
chunked_startrHr�r�rrr
�test_chunked�szTestUrlopen.test_chunkedcCs�d}|jdg|fg�}y|jd|j�WnAtjjk
r|}z|j�}|j�WYdd}~XnX|jd�|j	||�|j	|j
dg�dS)NsBad bad bad...i�zhttp://localhost:%s/weeblez404 should raise URLErrorz/weeble)r�rrrmr�r�r�r�r�r�r�)rr�r�r�r�rrr
�test_404
s
zTestUrlopen.test_404cCsad}|jdg|fg�}|jd|j�}|j||�|j|jdg�dS)Ns
pycon 2008...r`zhttp://localhost:%s/bizarrez/bizarre)r�rrr�r�)rr�r�r�rrr
�test_200s
zTestUrlopen.test_200cCsgd}|jdg|fg�}|jd|jd�}|j||�|j|jddg�dS)Ns
pycon 2008...r`zhttp://localhost:%s/bizarresget=with_feelingz/bizarre)r�rrr�r�)rr�r�r�rrr
�test_200_with_parameters s	z$TestUrlopen.test_200_with_parameterscCsN|j�}tjdt�}|jd|jd|�}|j|d�dS)N�cafilezhttps://localhost:%s/bizarre�contexts
we care a bit)r��ssl�create_default_context�CERT_localhostrrr�)rr�r�r�rrr
�
test_https(szTestUrlopen.test_httpscCs�|jdt�}|jd|jdt�}|j|d�|jtjj��!}|jd|jdt	�WdQRX|jdt	�}|jt
j��!}|jd|jdt	�WdQRXdS)N�certfilezhttps://localhost:%s/bizarrer�s
we care a bit)r�r�rrr�r�rmr�r��CERT_fakehostnamer�ZCertificateError)rr�r��cmrrr
�test_https_with_cafile.s	z"TestUrlopen.test_https_with_cafilecCsO|jdt�}|jtjj��!}|jd|jdd�WdQRXdS)Nr�zhttps://localhost:%s/bizarreZ	cadefaultT)r�r�r�rmr�r�rr)rr�r�rrr
�test_https_with_cadefault>sz%TestUrlopen.test_https_with_cadefaultcs�tdkr|jd�tjs/|jd�d��fdd�}tjtj�}|j|�|jd|dt�}tjdt�}|j	d|j
d|�|j�d	�dS)
Nzssl module requiredzSNI support required in OpenSSLcs
|�dS)Nr)Zssl_sockZserver_nameZinitial_context)�sni_namerr
�cb_sniKsz*TestUrlopen.test_https_sni.<locals>.cb_snir�r�r�zhttps://localhost:%sZ	localhost)r�r�ZHAS_SNIZ
SSLContextZPROTOCOL_TLSv1Zset_servername_callbackr�r�r�rrr�)rr�r�r�r)r�r
�test_https_sniEs
	

zTestUrlopen.test_https_snic
Csf|j�}tjjd|jdddi�}tjj|��WdQRX|j|jdd�dS)Nzhttp://localhost:%s/rRZRangezbytes=20-39)r�rmrZRequestrrr�r�)rr�Zreqrrr
�test_sending_headersUsz TestUrlopen.test_sending_headersc
Cs|j�}tjjd|j�}x+dD]#}|jt||�d|�q,Wz|j|j�d�Wd|j�XdS)	Nzhttp://localhost:%sr�r��info�geturlz3object returned from urlopen lacks the %s attributezcalling 'read' failed)r�r�r�r�)	r�rmrrrr~r�r�r�)rr��open_url�attrrrr
�
test_basic]s
zTestUrlopen.test_basiccCsr|j�}tjjd|j�}|�|j�}WdQRX|j|tjj	d�|j
|j�d�dS)Nzhttp://localhost:%szEobject returned by 'info' is not an instance of email.message.MessageZplain)r�rmrrrr�ZassertIsInstance�email�messageZMessager�Zget_content_subtype)rr�r�Zinfo_objrrr
�	test_infohs	zTestUrlopen.test_infocCsZ|j�}tjjd|j�}|�|j�}WdQRX|j|d|j�dS)Nzhttp://localhost:%s)r�rmrrrr�r�)rr�r�r�rrr
�test_geturlss
zTestUrlopen.test_geturlcCs_d}|jdg|fg�}tjjd|j�}x|D]}|j||�qAWdS)Ns
pycon 2008...r`zhttp://localhost:%s)r�rmrrrr�)rr�r�r��linerrr
�test_iteration{s

zTestUrlopen.test_iterationc	Cs�ddddddg}dj|�}|jdg|fg�}tjjd	|j�}xNt|�D]@\}}|j|||d
|t||�t|�f�qjW|j|dt|��dS)NsWe
sgot
shere
s	verylong i s
r�r`zhttp://localhost:%szQFetched line number %s doesn't match expected:
    Expected length was %s, got %sr+)	rr�rmrrr�	enumerater�rQ)r�linesr�r�r��indexr�rrr
�test_line_iteration�s%zTestUrlopen.test_line_iteration)rrrrrsrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr)rxr
r��s(


r�cCstj�adS)N)rZthreading_setup�threads_keyrrrr
�setUpModule�sr�cCstrtjt�dS)N)r�rZthreading_cleanuprrrr
�tearDownModule�sr��__main__)+rir�r�Zurllib.parsermZurllib.requestZhttp.serverrZunittestr-Ztestr�
import_modulerr��ImportErrorrU�dirname�__file__�hererr�r�rrrrrr!r[rWrkZ
skipUnlessZTestCaserpr�r�r�r�r�r�r�mainrrrr
�<module>sB
y,!$M+�