Your IP : 216.73.216.147


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-2.pyc



���]s`�
@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(eZdZdd�Zdd�ZdS)�LoopbackHttpServercCs-tjjj|||�|jjd�dS)Ng�������?)�http�server�
HTTPServer�__init__�socket�
settimeout)�selfZserver_addressZRequestHandlerClass�r�:/opt/alt/python35/lib64/python3.5/test_urllib2_localnet.pyr!szLoopbackHttpServer.__init__cCs,|jj�\}}|jd�||fS)Ng$@)r	�acceptr
)r�requestZclient_addressrrr
�get_request*s
zLoopbackHttpServer.get_requestN)�__name__�
__module__�__qualname__rrrrrr
rs	rc@s4eZdZdd�Zdd�Zdd�ZdS)�LoopbackHttpServerThreadcCsVtjj|�d|_tj�|_d|_td|�|_|jj	|_
dS)NFzHTTP/1.0�	127.0.0.1r)rr)r�Threadr�_stop_server�Event�readyZprotocol_versionr�httpdZserver_port�port)r�request_handlerrrr
r9s		z!LoopbackHttpServerThread.__init__cCs$d|_|j�|jj�dS)NT)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)rrrrrr!rrrr
r6s	rc@s|eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�DigestAuthHandlercCs1d|_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�"���r:)�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/r1r2)rr?rDrErFZ
final_dictZHA1_strrGZHA2_strrHZresponse_strrIr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(r5�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)NrTzProxy-AuthorizationZusernamer4F)�lenr&�headersrQrC�getr%�remove�path�
short_pathrKZcommand)rrr?rDZauth_validatedrVrrr
r �s,	


		

z DigestAuthHandler.handle_requestN)rrrrr)r*r+r5rCrKrQr rrrr
r"Qsr"c@s�eZdZdZdZdZdeefZejej	d��j
d�Zdd�Zdd	�Z
d
d�Zdd
�Zdd�ZdS)�BasicAuthHandler�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)rLrMrN)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)rLrM�REALMrN)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 rasIt works)rSrTrdrOrP�ENCODED_AUTHrLrN)rrrr
�do_GET�s
	

zBasicAuthHandler.do_GETN)rrr�USER�PASSWDrcZUSER_PASSWD�base64Z	b64encoder1�decoderfrr`rbrdrgrrrr
rX�s!rXc@s4eZdZdd�Zdd�Zdd�ZdS)�FakeProxyHandlercOs&||_tjjj|||�dS)N)�digest_auth_handlerrrr\r)rrmr]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)	NrraZOKzContent-Typez	text/htmlzYou've reached %s!<BR>r-sFOur apologies, but our server is down due to a sudden zombie invasion.)
�urllib�parse�urlparserVrWrmr rLrMrNrOrP�bytes)rZscmZnetlocrV�paramsZqueryZfragmentrrr
rgs	!	

zFakeProxyHandler.do_GETN)rrrrr`rgrrrr
rl�srlz!Threading required for this test.csdeZdZdZdZdZdZ�fdd�Z�fdd�Zd	d
�Z	dd�Z
�S)
�BasicAuthTestsrYrZZ	Incorrectr[csutt|�j�dd�}t|�|_|j|jj�d|jj|_|jj	�|jj
j�dS)Nc_s
t||�S)N)rX)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)�superrs�setUprr�
addCleanuprr�
server_url�startr�wait)rrt)�	__class__rr
rvs
zBasicAuthTests.setUpcstt|�j�dS)N)rurs�tearDown)r)r{rr
r|)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)rnr�HTTPBasicAuthHandler�add_passwordrcrxrhri�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)rnrr}r~rcrxrh�INCORRECT_PASSWDrr��assertRaisesr�r�r�)rr�rrr
�test_basic_auth_httperror5s"z(BasicAuthTests.test_basic_auth_httperror)rrrrhrir�rcrvr|r�r�rr)r{r
rss	rscs|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�clearrJ)�old_environrrr
�restore_environGs
z-ProxyAuthTests.setUp.<locals>.restore_environre�NO_PROXY�no_proxycst�j||�S)N)rlrm)r]r^)rrr
�create_fake_proxy_handlerRsz7ProxyAuthTests.setUp.<locals>.create_fake_proxy_handlerzhttp://127.0.0.1:%dr)rur�rvrwr�r��copyr"rmr*rhrir+rcrrryrrzrrnrZProxyHandlerZProxyDigestAuthHandler�proxy_digest_handlerr��opener)rr�r�Z	proxy_url�handler)r{)rr
rvDs"


	zProxyAuthTests.setUpcs$|jj�tt|�j�dS)N)rrrur�r|)r)r{rr
r|^s
zProxyAuthTests.tearDowncCs_|jj|j|j|j|jd�|jjd�|jt	j
j|jj
|j�dS)NZbadr#)r�r~rc�URLrhrirmr)r�rnr�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#)	rmr)r�rnr�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~rcr�rhrirmr)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~rcr�rhrirmr)r�r�rnr��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�rhrircrvr|r�r�r�r�rr)r{r
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_headrOrP)r�body�donerrr
rg�s	z8GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_GETcSsC|jd}|jjt|��}|j�|jj|�dS)NzContent-Length)rSZrfiler��intrg�requestsr3)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)
rS�headers_receivedr�r3rV�poprLrMrrN)rZ
response_coderSr��headerrB)�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�rrgr�r�r`r)r�r�rr
r��sr�)rrr\)r�r)r�r�r
�GetRequestHandler�s%&r�cseZdZ�fdd�Zddd�Zddd�Zddd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Z�S)(�TestUrlopencsVtt|�j�dd�}|j|tjj��dtjd<dtjd<dS)NcSs!tjj�tjj|�dS)N)r�r�r�rJ)r�rrr
r��s
z*TestUrlopen.setUp.<locals>.restore_environ�*r�r�)rur�rvrwr�r�r�)rr�)r{rr
rv�s

zTestUrlopen.setUpNc
Ks�g}tjj|||�}zV|j|jd��|j|j��|j|jd��|j|j��Wd|j�Xdj	|�S)Nrai�)
rnrr��extend�	readlinesr3�readliner�r�r)r�url�datar^�l�frrr
r��szTestUrlopen.urlopencCs�|dkrdgdfg}t|�}t|�|_|j|jj�|jj�|jjj�|jj}||_|S)Nras
we don't care)	r�rrrwrryrrzr)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_serverras
we care a bitZ
handler_class)�hasattrrnr�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_elsererazhttp://localhost:%s/�/z/somewhere_else)r�r�)r�r�r�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
ra�Transfer-Encoding�chunkedzhttp://localhost:%s/)r�r�)r�r�rr�)rr�Z
chunked_startrIr�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�r�rrnr�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...razhttp://localhost:%s/bizarrez/bizarre)r�r�rr�r�)rr�r�r�rrr
�test_200s
zTestUrlopen.test_200cCsgd}|jdg|fg�}|jd|jd�}|j||�|j|jddg�dS)Ns
pycon 2008...razhttp://localhost:%s/bizarresget=with_feelingz/bizarre)r�r�rr�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_localhostr�rr�)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�r�rr�r�rnr�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�rnr�r�r�r)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:%s�	localhost)r�r�ZHAS_SNIZ
SSLContextZPROTOCOL_TLSv1Zset_servername_callbackr�r�r�r�rr�)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/rSZRangezbytes=20-39)r�rnrZRequestrr�r�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�rnrr�rr�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�rnrr�rr�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�rnrr�rr�r�)rr�r�r�rrr
�test_geturlss
zTestUrlopen.test_geturlcCs_d}|jdg|fg�}tjjd|j�}x|D]}|j||�qAWdS)Ns
pycon 2008...razhttp://localhost:%s)r�rnrr�rr�)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�razhttp://localhost:%szQFetched line number %s doesn't match expected:
    Expected length was %s, got %sr,)	rr�rnrr�r�	enumerater�rR)r�linesr�r�r��indexr�rrr
�test_line_iteration�s%zTestUrlopen.test_line_iteration)rrrrvr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr)r{r
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__)+rjr�r�Zurllib.parsernZurllib.requestZhttp.serverrZunittestr.Ztestr�
import_modulerr��ImportErrorrV�dirname�__file__�hererr�r�rrrrrr"r\rXrlZ
skipUnlessZTestCasersr�r�r�r�r�r�r�mainrrrr
�<module>sB
y,!$M+�