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.cpython-35.opt-2.pyc



���]P�@s�ddlZddlmZddlmZddlZddlZddlZddlZddlZddl	Z
ddl	mZmZm
Z
mZmZmZmZddlmZddlZ
ddlZGdd�dej�ZGdd	�d	ej�ZGd
d�d�ZGdd
�d
�ZGdd�de�ZGdd�dej�ZGdd�d�Z Gdd�d�Z!Gdd�dej"�Z#Gdd�d�Z$Gdd�d�Z%dd�Z&dd�Z'Gd d!�d!e
j(j)�Z*Gd"d#�d#e
j(j+�Z,Gd$d%�d%e
j(j)�Z-Gd&d'�d'�Z.Gd(d)�d)ej�Z/d*d+�Z0Gd,d-�d-ej�Z1Gd.d/�d/ej�Z2Gd0d1�d1ej�Z3e4d2kr�ej5�dS)3�N)�support)�test_urllib)�Request�OpenerDirector�HTTPBasicAuthHandler�HTTPPasswordMgrWithPriorAuth�_parse_proxy�_proxy_bypass_macosx_sysconf�AbstractDigestAuthHandler)�urlparsec@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TrivialTestsc	Cs�x�dD]�}i}td||�|d=|dkr�tjdkr�|jd	�|jd
�}}|j|jd�|j|jd�xD|j�D]6\}}|j|jd|d
|||jf�q�WqWdS)N�request�response�parse�error�robotparserzfrom urllib.%s import *�__builtins__�nt�url2pathname�pathname2urlZ
nturl2pathz	urllib.%sz.%r is exposed in 'urllib.%s' but defined in %r)r
rrrr)�exec�os�name�pop�assertEqual�
__module__�items)�self�module�context�u�p�k�v�r$�1/opt/alt/python35/lib64/python3.5/test_urllib2.py�test___all__s
zTrivialTests.test___all__cCs�|jttjjd�tjjtjj�j	tj
d�}tjdkr\d|}n
d|}tjj|�}|j�|j
�dS)Nz	bogus url�/rz
file:///%sz	file://%s)�assertRaises�
ValueError�urllibr
�urlopenr�path�abspath�__file__�replace�sepr�read�close)rZfnameZfile_url�fr$r$r%�test_trivial-s'


zTrivialTests.test_trivialc
Cs�ddddgfdddgfdddd	d
ddd
gfddddgfg}x0|D](\}}|jtjj|�|�q^WdS)Nza,b,c�a�b�czpath"o,l"og"i"cal, examplezpath"o,l"og"i"calZexampleza, b, "c", "d", "e,f", g, hz"c"z"d"z"e,f"�g�hza="b\"c", d="e\,f", g="h\\i"za="b"c"zd="e,f"zg="h\i")rr*r
Zparse_http_list)r�tests�string�listr$r$r%�test_parse_http_list?sz!TrivialTests.test_parse_http_listcCs/tjjd�}|j|jt|��dS)N�reason)r*r�URLError�assertInr>�str)r�errr$r$r%�test_URLError_reasonstrJsz$TrivialTests.test_URLError_reasonstrN)�__name__r�__qualname__r&r4r=rCr$r$r$r%rsrc@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�RequestHdrsTestscCs\d}|jt|dddi�jdd�|jt|dddi�jdd�dS)Nzhttp://example.com�headersz	Spam-eggs�blahz	spam-EggS)rrrG)r�urlr$r$r%�test_request_headers_dictQs
z*RequestHdrsTests.test_request_headers_dictcCs7d}t|dddi�}|j|jd��|j|j�dg�|jdd�|jt|j��d
dg�|j|jd��|j|j	d��|j|j	dd	�d	�|j
d�|j|jd��|jd
d�|j|jd
��|j
d
�|j|jd
��dS)Nzhttp://example.comrG�	Spam-eggsrHzFoo-Bar�baz�Foo-barz	Not-there�defaultzUnredirected-spamZEggs)rKrH)rMrL)rKrH)r�
assertTrue�
has_headerrZheader_items�
add_header�sorted�assertFalse�assertIsNone�
get_headerZ
remove_header�add_unredirected_header)rrI�reqr$r$r%�test_request_headers_methodsfs 


z-RequestHdrsTests.test_request_headers_methodscCsZtjj�}|j}|j}|dddd�|dddd�|ddd	d�|dd
dd�|d
ddd�|d
ddd�|dddd�|dddd�|dddd�|dddd�|j|dd�d#�|j|dd�d$�|j|dd�d%�|j|dd�d&�|j|dd�d'�|j|dd�d(�|j|dd
�d)�|j|d
d�d*�|j|dd�d+�|j|dd �d,�|j|dd!�d-�|j|dd"�d.�|j|dd�d/�|j|dd�d0�|j|dd�d1�dS)2Nz
Some Realmzhttp://example.com/�joe�passwordzhttp://example.com/ni�nir7zhttp://example.com/foo�foozhttp://example.com/bar�bar�ninir6�firstrH�second�spamr5zhttp://example.com�1zhttp://c.example.com:3128�3z
d.example.com�4�dze.example.com:3128�5�ezexample.comzhttp://example.com/spamzhttp://example.com/spam/spamzhttp://a.example.com/z
c.example.comzc.example.com:3128)rYrZ)rYrZ)rYrZ)rYrZ)rYrZ)r\r[)r]r^)r`ra)rbr5)NN)NN)rcr7)rcr7)rdre)rfrg)r*r
�HTTPPasswordMgr�add_password�find_user_passwordr)r�mgr�add�find_user_passr$r$r%�test_password_manager�s\		z&RequestHdrsTests.test_password_managercCstjj�}|j}|j}|dddd�|dddd�|d	d
dd�|d
ddd�|j|dd�d�|j|dd�d�|j|dd�d �|j|dd�d!�|j|dd�d"�|j|dd�d#�|j|dd�d$�|j|dd�d%�|j|dd�d&�|j|d	d�d'�|j|d	d
�d(�|j|d	d�d)�|j|d
d�d*�|j|d
d�d+�|j|d
d�d,�|j|d
d�d-�dS).Nr3zhttp://g.example.com:80�10�jr8zhttp://h.example.com�11r"r9zi.example.com:80�12�l�iz
j.example.com�13�mzg.example.com:100zg.example.com:80z
g.example.comzhttp://g.example.com:100zhttp://g.example.comz
h.example.comzh.example.com:80zhttp://h.example.com:80z
i.example.comzhttp://i.example.com:80zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80)NN)rorp)NN)NN)rorp)rorp)rqr")rqr")rqr")NN)rrrs)rrrs)rurv)NN)rurv)NN)r*r
rhrirjr)rrkrlrmr$r$r%�"test_password_manager_default_port�sD		z3RequestHdrsTests.test_password_manager_default_portN)rDrrErJrXrnrwr$r$r$r%rFOs$;rFc@s7eZdZgZdejdd�Zdd�ZdS)�
MockOpenerNcCs!||||_|_|_dS)N)rW�data�timeout)rrWryrzr$r$r%�open�szMockOpener.opencGs|||_|_dS)N)�proto�args)rr|r}r$r$r%r�szMockOpener.error)rDrrE�
addheaders�socket�_GLOBAL_DEFAULT_TIMEOUTr{rr$r$r$r%rx�srxc@s:eZdZddd�Zddd�Zdd�ZdS)�MockFileNcCsdS)Nr$)r�countr$r$r%r1�sz
MockFile.readcCsdS)Nr$)rr�r$r$r%�readline�szMockFile.readlinecCsdS)Nr$)rr$r$r%r2szMockFile.close)rDrrEr1r�r2r$r$r$r%r��sr�c@seZdZdd�ZdS)�MockHeaderscCst|j��S)N)r<�values)rrr$r$r%�
getheadersszMockHeaders.getheadersN)rDrrEr�r$r$r$r%r�sr�c@s7eZdZddd�Zdd�Zdd�ZdS)�MockResponseNcCsAtjj||�||||f\|_|_|_|_dS)N)�io�StringIO�__init__�code�msgrGrI)rr�r�rGryrIr$r$r%r�
szMockResponse.__init__cCs|jS)N)rG)rr$r$r%�infoszMockResponse.infocCs|jS)N)rI)rr$r$r%�geturlszMockResponse.geturl)rDrrEr�r�r�r$r$r$r%r�	sr�c@s(eZdZdd�Zdd�ZdS)�
MockCookieJarcCs
||_dS)N)�ach_req)rr
r$r$r%�add_cookie_headerszMockCookieJar.add_cookie_headercCs|||_|_dS)N)�ec_req�ec_r)rrr
r$r$r%�extract_cookiesszMockCookieJar.extract_cookiesN)rDrrEr�r�r$r$r$r%r�sr�c@s(eZdZdd�Zdd�ZdS)�
FakeMethodcCs||_||_||_dS)N)�	meth_name�handle�action)rr�r�r�r$r$r%r�s		zFakeMethod.__init__cGs|j|j|j|�S)N)r�r�r�)rr}r$r$r%�__call__#szFakeMethod.__call__N)rDrrEr�r�r$r$r$r%r�sr�c@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�MockHTTPResponsecCs1||_||_||_||_d|_dS)N��)�fpr��statusr>r�)rr�r�r�r>r$r$r%r�(s
				zMockHTTPResponse.__init__cCsdS)N�r$)rr$r$r%r1/szMockHTTPResponse.readcCsiS)Nr$)rr$r$r%r�2szMockHTTPResponse.infocCs|jS)N)rI)rr$r$r%r�5szMockHTTPResponse.geturlN)rDrrEr�r1r�r�r$r$r$r%r�'sr�c@sveZdZdd�Zejdd�Zdd�Zdddd	�Zddd
d�Z	dd
�Z
dd�ZdS)�
MockHTTPClasscCs:d|_g|_d|_d|_d|_i|_dS)NrF)�level�req_headersry�raise_on_endheaders�sock�_tunnel_headers)rr$r$r%r�:s					zMockHTTPClass.__init__cCs||_||_|S)N)�hostrz)rr�rzr$r$r%r�Bs		zMockHTTPClass.__call__cCs
||_dS)N)r�)rr�r$r$r%�set_debuglevelGszMockHTTPClass.set_debuglevelNcCs5||_||_|r$||_n
|jj�dS)N)�_tunnel_hostZ_tunnel_portr��clear)rr��portrGr$r$r%�
set_tunnelJs
		zMockHTTPClass.set_tunnelcCse||_||_|dk	r3|j|j�7_|jj�|rO||_|jrat��dS)N)�method�selectorr�r�sortryr��OSError)rr�rIZbodyrGr$r$r%r
Rs		
		zMockHTTPClass.requestcCstt�idd�S)Nr��OK)r�r�)rr$r$r%�getresponse]szMockHTTPClass.getresponsecCsdS)Nr$)rr$r$r%r2`szMockHTTPClass.close)rDrrEr�rr�r�r�r�r
r�r2r$r$r$r%r�9sr�c@s^eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�MockHandleri�cCs|j|�dS)N)�_define_methods)r�methodsr$r$r%r�iszMockHandler.__init__cCskxd|D]\}t|�dkr.|\}}n
|d}}t|||j�}t|j||�qWdS)N�)�lenr�r��setattr�	__class__)rr��specrr��methr$r$r%r�ls

zMockHandler._define_methodscOs%|jjj||||f�|dkr/dS|dkr?|S|dkrdtddid�}|S|dkrztd�S|jd�r||jd	�d
d�}yt|�}Wntk
r�YnXtddid�}|jj	d|d||di�S|d
kr!t
j	jd��dS)Nzreturn selfzreturn responser�r�r�zreturn requestzhttp://blah/r� ��httpr�raiserH)�parent�calls�appendr�r�
startswith�rfind�intr)rr*r?)rZfn_namer�r}�kwds�resr�r$r$r%r�us*

#zMockHandler.handlecCsdS)Nr$)rr$r$r%r2�szMockHandler.closecCs||_g|j_dS)N)r�r�)rr�r$r$r%�
add_parent�s	zMockHandler.add_parentcCs#t|d�sdS|j|jkS)N�
handler_orderT)�hasattrr�)r�otherr$r$r%�__lt__�szMockHandler.__lt__N)
rDrrEr�r�r�r�r2r�r�r$r$r$r%r�ds	r�cCs�g}d}xp|D]h}Gdd�dt�}||�}|j|7_|j|�|d}|j|�|j|�qW|S)Nrc@seZdZdS)z6add_ordered_mock_handlers.<locals>.MockHandlerSubclassN)rDrrEr$r$r$r%�MockHandlerSubclass�sr�r�)r�r�r�r��add_handler)�opener�	meth_spec�handlersr��methsr�r9r$r$r%�add_ordered_mock_handlers�s



r�cGs+t�}x|D]}|j|�qW|S)N)rr�)Zhandler_instancesr�r9r$r$r%�build_test_opener�s	
r�c@s4eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlercCs ||_||_|j�dS)N)r�rG�reset)rr�rGr$r$r%r��s		zMockHTTPHandler.__init__cCsd|_g|_dS)Nr)�_count�requests)rr$r$r%r��s	zMockHTTPHandler.resetcCs�ddl}ddl}|jj|j|��|jdkr�|jd|_tjj|j	}|j
|j�}|jj
d|t�|j	||�S||_|j
d�}tdd|d|j��SdS)Nrr�r�z

r�r�r�)�email�copyr�r��deepcopyr�r��client�	responsesr�Zmessage_from_stringrGr�rr�rWr��get_full_url)rrWr�r�rr�r$r$r%�	http_open�s		zMockHTTPHandler.http_openN)rDrrEr�r�r�r$r$r$r%r��sr�c@s+eZdZddd�Zdd�ZdS)�MockHTTPSHandlerrcCs)tjjj|d|�t�|_dS)N�
debuglevel)r*r
�AbstractHTTPHandlerr�r��httpconn)rr�r$r$r%r��szMockHTTPSHandler.__init__cCs|j|j|�S)N)�do_openr�)rrWr$r$r%�
https_open�szMockHTTPSHandler.https_openN)rDrrEr�r�r$r$r$r%r��sr�c@s4eZdZdd�Zdd�Zdd�ZdS)�MockHTTPHandlerCheckAuthcCs||_d|_dS)NF)r��has_auth_header)rr�r$r$r%r��s	z!MockHTTPHandlerCheckAuth.__init__cCs
d|_dS)NF)r�)rr$r$r%r��szMockHTTPHandlerCheckAuth.resetcCsM|jd�rd|_tjj|j}t|j|t�d|j��S)N�
AuthorizationTr�)	rPr�r�r�r�r�r�r�r�)rrWrr$r$r%r��s	z"MockHTTPHandlerCheckAuth.http_openN)rDrrEr�r�r�r$r$r$r%r��sr�c@s(eZdZdd�Zdd�ZdS)�MockPasswordManagercCs(||_||_||_||_dS)N)�realmrI�userrZ)rr�Zurir�rZr$r$r%ri�s			z MockPasswordManager.add_passwordcCs"||_||_|j|jfS)N)�target_realm�
target_urlr�rZ)rr�Zauthurir$r$r%rjs		z&MockPasswordManager.find_user_passwordN)rDrrErirjr$r$r$r%r��sr�c@sdeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�OpenerDirectorTestscCs6Gdd�dt�}|jtt�j|��dS)Nc@seZdZdS)z<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerN)rDrrEr$r$r$r%�
NonHandlersr�)�objectr(�	TypeErrorrr�)rr�r$r$r%�test_add_non_handler
s	z(OpenerDirectorTests.test_add_non_handlercCs�ddlm}t�}ddgd
gg}t||�|jtjj��x(dD] }|j||j	|d
�qXWdS)Nr)r?r��return self�
proxy_open�redirect_request�do�proxy�redirectz://example.com/)r�r�)r�r�)r�r�)r�r�r�)
�urllib.errorr?rr�r�r*r
ZUnknownHandlerr(r{)rr?�or��schemer$r$r%�test_badly_named_methodss			

z,OpenerDirectorTests.test_badly_named_methodsc
Cs�t�}dddgdgdgd	gg}t||�}td�}|j|�}|j||d�|ddf|ddfg}x[t||j�D]G\}}|\}	}
}}|j|	|
f|�|j||f�q�WdS)
Nr��ftp_open�http_error_302�return selfzhttp://example.com/r�r)r�r�)r�r�)rr�rr{r�zipr�)
rr�r�r�rW�rr��expected�got�handlerrr}r�r$r$r%�test_handled(s	 z OpenerDirectorTests.test_handledcCs�t�}g}xqd	gdfdgdfgD]Q\}}Gdd�dt�}||�}||_|j|�|j|�q.W|jd�|j|jdd|d�|j|jdd|d�dS)
Nr��return selfi�rc@seZdZdS)zCOpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassN)rDrrEr$r$r$r%r�Gsr�zhttp://example.com/r�)r�r)rr�r�r�r�r{rr�)rr�r�r�r�r�r9r$r$r%�test_handler_orderBs		

z&OpenerDirectorTests.test_handler_ordercCst�}dgdgg}t||�}td�}|jtjj|j|�|j|j	|dd|fifg�dS)Nr�r��return selfzhttp://example.com/r)r�r�)r�r)
rr�rr(r*rr?r{rr�)rr�r�r�rWr$r$r%�
test_raiseTs	zOpenerDirectorTests.test_raisecCst�}dgddgdddgdgg}t||�}Gd	d
�d
�}td�}|j|�|dd|ff|d
d||�ddiffg}xct||j�D]O\}}|\}	}
}|j|	|
f|dd
��|j||d
�q�WdS)Nr��	error 302�http_error_400r�r��return responseZhttp_error_303Z
http_errorc@seZdZdd�ZdS)z4OpenerDirectorTests.test_http_error.<locals>.UnknowncSsdS)NTr$)rr�r$r$r%�__eq__osz;OpenerDirectorTests.test_http_error.<locals>.Unknown.__eq__N)rDrrErr$r$r$r%�Unknownnsrzhttp://example.com/rr�i.r�)r�r)r	r�)r�r
)rr�rr{r�r�r)rr�r�r�rrWr�rrrZmethod_namer}r$r$r%�test_http_erroras"		

 z#OpenerDirectorTests.test_http_errorcCsnt�}d	d
gddgg}t||�}td�}|j|�|ddf|ddf|ddf|ddfg}x�t|j�D]�\}\}}}	}
|dkr�|j||f||�|jt|	�d�|j|	dt�q�|j||f||�|jt|	�d�|j|	dt�|	ddk	r�|j|	dt	�q�WdS)
N�http_request�return request�
http_response�return responsezhttp://example.com/rr�r�)rr)rr)rr)rr)
rr�rr{�	enumerater�rr�ZassertIsInstancer�)rr�r�r�rWr�rtrrr}r�r$r$r%�test_processors}s(	
 (z#OpenerDirectorTests.test_processorsN)
rDrrEr�r�rrrr
rr$r$r$r%r�s
r�cCsyy|jd�Wn!tk
r4tjd��YnXtjj|�}tjdkru|j	d�ru|dd�}|S)Nzutf-8zpath is not encodable to utf8rz///r�)
�encode�UnicodeEncodeError�unittestZSkipTestr*r
rrrr�)r,�urlpathr$r$r%�sanepathname2url�s
rc@s�eZdZdd�Zdd�Zdd�Zdd�Zd	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(�Zd)d*�Zd+d,�Zejejd-kd.�d/d0��Zd1d2d3�Zd4d5�Zd6d7�Z d8d9�Z!d:d;�Z"d<d=�Z#d>d?�Z$d@dA�Z%dBdC�Z&dDdE�Z'dFdG�Z(dHdI�Z)dJS)K�HandlerTestscsmGdd�d��G�fdd�dtjj�}ddl}d}||�}t�|_xdd|jd	d	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dd|jdd	d
ddgd
df	dddd	d	dddgd	df	dd|jd	d	dgddf	gD]\	}}}}}	}
}}}
t|�}d|_|j	|�}|j
|j|�|j
|j|	�|j
|j
tj|��|j
|j|�|j
|j|�|j
|jj|�|j
|jj|
�|j�}|j
|jd�|
�|j
t|d�t|��qGWdS)Nc@s4eZdZdd�Zdd�Zdd�ZdS)z-HandlerTests.test_ftp.<locals>.MockFTPWrappercSs
||_dS)N)ry)rryr$r$r%r��sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__cSs2|||_|_tj|j�t|j�fS)N)�filename�filetyper�r�ryr�)rrrr$r$r%�retrfile�sz6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfilecSsdS)Nr$)rr$r$r%r2�sz3HandlerTests.test_ftp.<locals>.MockFTPWrapper.closeN)rDrrEr�rr2r$r$r$r%�MockFTPWrapper�srcs4eZdZdd�Zej�fdd�ZdS)z-HandlerTests.test_ftp.<locals>.NullFTPHandlercSs
||_dS)N)ry)rryr$r$r%r��sz6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__csH|||_|_|||_|_||_�|j�|_|jS)N)r��passwdr�r��dirsry�
ftpwrapper)rr�rr�r�rrz)rr$r%�connect_ftp�s
	z9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftpN)rDrrEr�rr�r!r$)rr$r%�NullFTPHandler�sr"rzrheum rhaponicumz ftp://localhost/foo/bar/baz.html�	localhostr��Ir\r]zbaz.htmlz	text/htmlz'ftp://parrot@localhost/foo/bar/baz.htmlZparrotz*ftp://%25parrot@localhost/foo/bar/baz.htmlz%parrotz,ftp://%2542parrot@localhost/foo/bar/baz.htmlz	%42parrotzftp://localhost:80/foo/bar/�P�Dzftp://localhost/baz.gif;type=a�Azbaz.gifzContent-typezContent-length)r*r
Z
FTPHandler�ftplibrxr�ZFTP_PORTrrzr�rr�rr�r�
gethostbynamer�rr rrr��getr�r�)rr"r(ryr9rIr�r�r�rZtype_rrZmimetyperWr�rGr$)rr%�test_ftp�sL"1	zHandlerTests.test_ftpc#Cswddl}tjj�}t�}|_tj}tt	j
j|��}d}d|d|dtj
d�|fg}ytj
tj��}Wntjk
r�d}YnX|r�|jd||f�x"|D]}	t|d�}
z�z|
j|�Wd|
j�X|jt|	��}z(|j�}|j�}
|j�}Wd|j�Xt	j|�}|jj|jd	d
�}Wdt	j|�X|j||�|j|
dd�|j|
d
d�|j|
d|�|j||	�q�Wx�d|dddtj
d�t	j�|fdt	j�|fgD]k}	zTt|d�}
z|
j|�Wd|
j�X|j tj!j"|jt|	��Wdt	j|�Xq;Wtjj�}t�}|_x�ddddd gD]�\}	}t|	�}y|j|�Wn%tj!j"k
r/|j#|�Yn'X|j$|j%|�|j|j&d�|j|j&dk|�q�WdS)!Nrs
hello, world
zfile://localhost%sz	file://%szfile://%s%sr#r��wbZusegmtTzContent-typez
text/plainzContent-lengthruz
Last-modifiedzfile://localhost:80%szfile:///file_does_not_exist.txtz)file://not-a-local-host.com//dir/file.txtzfile://%s:80%s/%sz,file://somerandomhost.ontheinternet.com%s/%s�file://ftp.example.com//foo.txtF� file://ftp.example.com///foo.txt�file://ftp.example.com/foo.txt�"file://somehost//foo/something.txt�#file://localhost//foo/something.txt�ftp)r-F)r.F)r/F)r0F)r1F)'Zemail.utilsr*r
�FileHandlerrxr�r�TESTFNrrr,r-rr)�gethostname�gaierrorr�r{�writer2Z	file_openrr1r�r��statZutilsZ
formatdate�st_mtime�remover�getcwdr(rr?rS�assertIsrW�type)rr�r9r�r4rZtowrite�urlsZ	localaddrrIr3r�ryrGZrespurlZstatsZmodifiedr2rWr$r$r%�	test_file�s�	
zHandlerTests.test_filecCs�tjj�}t�}|_d}xFd'd(gD]8\}}t||ddi�}d|_|jdd�t�}|j	||�}|j
|j|j|j
|j|jd	kd
f|j�}	|	j|	j|j|j
�|�|j|jd�|j|jd�|j|j|�|j|jd
�|j|jd)d*d+g�|j|j|�q2Wd|_|jtjj|j	||�tdd�}|jt|j|�d,g|_x}d-D]u}td|�}t d	d
id�}|j|�}
|dkr<|j!d|j"�|j!d|j"�n.|j|j"dd�|j|j"dd�|j|j"dd�|j|j"dd�|jdd�|jdd�|jdd�|jdd�|j|�}
|j|j"dd�|j|j"dd�|j|j"dd�|j|j"dd�q�Wdd�}x]iddifD]I}td|�|�}|s�|jt#|j|�qf|j|�}
qfWt$j%�}
|
j&d�xyidd ifD]e}td|
|�}|s|jt#|j|�q�|j|�}
|jt'|
j(d��d �q�W|
j)�t*j*d!d"d#d$d%g�}xZidd&ifD]F}td||�}|j|�}
|jt'|
j(d��d&�q�WdS).Nzhttp://example.com/�GET�POST�blah�Foor]�Spam�eggsr�r�zexample.comrr'�
Connectionr2TZbadpost�r�zContent-lengthzContent-type�0z!application/x-www-form-urlencoded�Hostr\rLcssdVdVdVdS)Nsonestwosthreer$r$r$r$r%�
iterable_body�sz-HandlerTests.test_http.<locals>.iterable_bodyzContent-Length�sSomething
Something
Something
�r$r�r����)r@N)rArB)rFr2)rCr])rDrE)rDrE)rGN)+r*r
r�rxr�rrzrVr�r�r1r�r�r�r�r�r*�__contains__rr�r�r�r�r�ryr�r(rr?r��do_request_r~r��assertNotIn�unredirected_hdrsr)r��BytesIOr7r�rUr2�array)rr9r�rIr�ryrWr�r��hdrs�newreqrJrGZfile_objZiterable_arrayr$r$r%�	test_httpKs�		
	

#
zHandlerTests.test_httpcCsIt�}tdd�}|j|�|jd�|j|jd�dS)Nr�r�zhttps://www.example.com)rr�r�r{rZ_debuglevel)rr�r9r$r$r%�test_http_handler_debuglevel�s
	

z)HandlerTests.test_http_handler_debuglevelcCs�tjj�}t�|_d}ddddg}xy|D]q}t||�}|j|�}|j|jdd�|j	dd�|j|�}|j|jdd�q:WdS)	NrGz#http://example.com/foo/bar/baz.htmlz$http://example.com//foo/bar/baz.htmlz$http://example.com/foo//bar/baz.htmlz$http://example.com/foo/bar//baz.htmlrIzexample.comzsomeproxy:3128)
r*r
r�rxr�rrQrrS�	set_proxy)rr9ryZds_urlsZds_urlZds_reqZ	np_ds_reqZp_ds_reqr$r$r%�test_http_doubleslash�s	
z"HandlerTests.test_http_doubleslashcCs�dddg}td�}xw|D]o}||_t|�}|j|j�|�|j|jped|j�|jt|j��j|j�q"WdS)Nzhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashzhttp://example.comr�)r�full_urlrrr��fragmentZquery)rr>r�rIZparsedr$r$r%�test_full_url_setter�s	
	z!HandlerTests.test_full_url_settercCsItd�}|`|j|j�|j|j�|j|jd�dS)Nzhttp://www.example.comr�)rr\rTr]rr�)rr�r$r$r%�test_full_url_deleter�s
z"HandlerTests.test_full_url_deletercCs�tjj�}t�|_d}t|�}|j|�}|j|jd�|j|j	d�d}t|�}|j|�}|j|jd�|j|j	d�dS)Nzhttp://www.python.org?getspamzwww.python.orgz	/?getspamzhttp://www.python.orgr�)
r*r
r�rxr�rrQrr�r�)rr9Z	weird_urlrWrWZurl_without_pathr$r$r%�test_fixpath_in_weirdurls�sz&HandlerTests.test_fixpath_in_weirdurlscCs�tjj�}t�}|_d}t|�}tddid|�}|j||�}|j||�|j	t
|d��tddid|�}|j||�}|j||�|j	t
|d��tdd	id|�}|j||�}|j||�|j	t
|d��td
did|�}|j|j||��|j|j
d�|j|j||d
dif�dS)
Nzhttp://example.com/r�r�r�r|��ZAccepted��zPartial contenti�zBad gatewayr�)r*r
ZHTTPErrorProcessorrxr�rr�rr<rSr�rTrr|r})rr9r�rIrWr��newrr$r$r%�test_errors
s(zHandlerTests.test_errorscCs�t�}tjj|�}t�|_td�}tddid�}|j|�}|j	|j
|�|j	|j
|�|j|jd�|j
|j�|j||�}|j	|j|�|j	|j|�|j	||�dS)Nzhttp://example.com/r�r�r�zexample.com)r�r*r
�HTTPCookieProcessorrxr�rr�rr<r�r�origin_req_hostrSZunverifiablerr�r�)r�cjr9rWr�rWrcr$r$r%�test_cookies#s	zHandlerTests.test_cookiescCs*d}d}tjj�}t�}|_x�dD]�}x�dD]�}t|d|�}t||�}tj|_	|j
d	d
�|dk	r�|j
dtt|���|j
dd
�y)||t�|dtd|i��Wn5tjjk
r|j|d�|j|�YnX|j|jj�|�y|j|jj�d�Wn%tk
ry|j|jj�YnXdd�|jjD�}	|jd|	�|jd|	�|j|jjd	d
�|jd|jj�|jd|jj�q?Wq2Wt|�}tj|_	|dd�}
t|dd�}d}tj|_	y%x|
||d�|d}qRWWn1tjjk
r�|j|tjjj�YnXt|dd�}d}tj|_	y)x"|
||d|�|d}q�WWn1tjjk
r%|j|tjjj�YnXdS)Nzhttp://example.com/a.htmlzhttp://example.com/b.html�-�.�/�3�
blah
blah
z
http_error_%sZNonsensezviking=withholdzContent-LengthrDra�Blah�locationr@cSsg|]}|j��qSr$)�lower)�.0�xr$r$r%�
<listcomp>Ss	z.HandlerTests.test_redirect.<locals>.<listcomp>zcontent-lengthzcontent-typecSs,|j|t�ddtd|i��dS)Ni.rnro)r�r�r�)r9rWrIr$r$r%r�`sz,HandlerTests.test_redirect.<locals>.redirectrfzexample.comrzhttp://example.com/r�zhttp://example.com/%d)rirjrkrl)Nrm)r*r
�HTTPRedirectHandlerrxr��getattrrrr�rzrQrAr�rVr�r�r�	HTTPErrorr�assertIsNotNonerWr��
get_method�AttributeErrorrSryrGrRrSZmax_repeatsZmax_redirections)r�from_urlZto_urlr9r�r�ryr�rWrGr�r�r$r$r%�
test_redirect4sj


	zHandlerTests.test_redirectcCsd}dddg}dddg}d}tjj�}t�}|_t|�}tj|_xS|D]K}|d	|}	|j	tj
j|j|t
�d
dtd|	i��qhWx]|D]U}|d	|}
|j|t
�d
d
td|
i��|j|jj�|
�q�WdS)Nzhttp://example.com/a.htmlr��httpsr2�file�imapZldapzexample.com/b.htmlz://i.zSecurity LoopholerozThat's fine)r*r
rtrxr�rrr�rzr(rrvr�r�r�rrWr�)rrzZ
valid_schemesZinvalid_schemesZschemeless_urlr9r�rWr�Zinvalid_url�	valid_urlr$r$r%�test_invalid_redirect~s$

z"HandlerTests.test_invalid_redirectcCs�d}d}tjj�}t�}|_t|�}tj|_tj	j
||�}|j|t�ddt
d|i��|j|jj�|�dS)Nzhttp://example.com/a.htmlz/b.htmli.zThat's finero)r*r
rtrxr�rrr�rzrZurljoinr�r�r�rrWr�)rrzZrelative_urlr9r�rWrr$r$r%�test_relative_redirect�sz#HandlerTests.test_relative_redirectc	Cs�ddlm}ddlm}|�}||dd�tdd�}tjj�}tjj�}tjj	|�}t
||||�}|jd�|j|j
jd��dS)	Nr)�	CookieJar)�interact_netscapezhttp://www.example.com/z	spam=eggsi.z%Location: http://www.cracker.com/

�Cookie)Zhttp.cookiejarr�Ztest.test_http_cookiejarr�r�r*r
�HTTPDefaultErrorHandlerrtrer�r{rSrWrP)	rr�r�rg�hh�hdeh�hrh�cpr�r$r$r%�test_cookie_redirect�s	
z!HandlerTests.test_cookie_redirectcCsxd}tdd|�}tjj�}tjj�}t|||�}|jd�}|j|j�|j	��dS)Nz(http://www.example.com/index.html#OK

i.z
Location: zhttp://www.example.com)
r�r*r
r�rtr�r{rr��strip)rZredirected_urlr�r�r�r�r�r$r$r%�test_redirect_fragment�sz#HandlerTests.test_redirect_fragmentcs�tjj�d}tj|�tj_�jttjd��td	�����fdd�}|tjj_t	jj
d�}�j|j�d�dS)
Ns(HTTP/1.1 302 Found
Location: ?query

�HTTPConnection�/path�/path?querycs?�j|t����j|||||�d|j_dS)NsHTTP/1.1 200 OK

Hello!)r�nextr
r�Zfakedata)�connr�rI�pos�kw)�
real_classrr>r$r%r
�sz3HandlerTests.test_redirect_no_path.<locals>.requestzhttp://python.org/pathzhttp://python.org/path?query)r�r�)
r�r�r�r�fakehttpZ
addCleanupr��iterr
r*r+rr�)rZ	response1r
r�r$)r�rr>r%�test_redirect_no_path�sz"HandlerTests.test_redirect_no_pathc
Cs�Gdd�dtjj�}|�}tjj|�}d}x�|D]�\}}|jt|���dtjd	|d
�|_|j	d�}d|d
}|j
}	|j|	j|�t|	��WdQRXqDWdS)Nc@seZdZdd�ZdS)z4HandlerTests.test_redirect_encoding.<locals>.HandlercSs:|j|j|�}|jj|_tjd�|_|S)Ns)HTTP/1.1 200 OK
Content-Length: 3

123)r��
connection�buf�last_bufrr�)rrW�resultr$r$r%r��s
z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_openN)rDrrEr�r$r$r$r%�Handler�sr��/på-dansk/�/p%C3%A5-dansk/�/spaced%20path/�
/spaced path/�/?på-dansk�/?p%C3%A5-dansks!HTTP/1.1 302 Redirect
Location: s

zhttp://example.com/sGET � �r�r��r�r��r�r��r�r�)r�r�r�r�)
r*r
�HTTPHandler�build_openerZsubTest�reprrr�r�r{r�rOr�)
rr�rr�r:ror�rrr
r$r$r%�test_redirect_encoding�s		z#HandlerTests.test_redirect_encodingcCs�t�}tjjtdd��}|j|�d
gg}t||�}td�}|j|j	d�|j
|�|j|j	d�|j|ddfgdd	�|jD��dS)Nr�zproxy.example.com:3128r��return responsezhttp://acme.example.com/zacme.example.comrcSs g|]}|dd��qS)rr�r$)rq�tupr$r$r%rs�s	z+HandlerTests.test_proxy.<locals>.<listcomp>)r�r�)rr*r
�ProxyHandler�dictr�r�rrr�r{r�)rr��phr�r�rWr$r$r%�
test_proxy�s	

zHandlerTests.test_proxycCs�dtjd<t�}tjjtdd��}|j|�td�}|j	|j
d�|j|�|j	|j
d�td�}|j	|j
d�|j|�|j	|j
d�tjd=dS)	Nz
python.org�no_proxyr�zproxy.example.comzhttp://www.perl.org/zwww.perl.orgzhttp://www.python.orgzwww.python.org)r�environrr*r
r�r�r�rrr�r{)rr�r�rWr$r$r%�test_proxy_no_proxy�s
	


z HandlerTests.test_proxy_no_proxycCs�dtjd<t�}tjjtdd��}|j|�td�}|j	|j
d�|j|�|j	|j
d�tjd=dS)N�*r�r�zproxy.example.comzhttp://www.python.orgzwww.python.org)rr�rr*r
r�r�r�rrr�r{)rr�r�rWr$r$r%�test_proxy_no_proxy_alls
	

z$HandlerTests.test_proxy_no_proxy_allcCs�t�}tjjtdd��}|j|�d
gg}t||�}td�}|j|j	d�|j
|�|j|j	d�|j|ddfgdd	�|jD��dS)Nr|zproxy.example.com:3128r��return responsezhttps://www.example.com/zwww.example.comrcSs g|]}|dd��qS)rr�r$)rqr�r$r$r%rs%s	z1HandlerTests.test_proxy_https.<locals>.<listcomp>)r�r�)rr*r
r�r�r�r�rrr�r{r�)rr�r�r�r�rWr$r$r%�test_proxy_httpss	

zHandlerTests.test_proxy_httpscCst�}tjjtdd��}|j|�t�}|j|�td�}|jdd�|jdd�|j	|j
d�|j|j�|j
|�|jd
|jj�|jd|jj�|j|j�|j	|j
d�|j	|jd	�d�dS)Nr|zproxy.example.com:3128zhttps://www.example.com/�Proxy-Authorization�FooBar�
User-Agent�Grailzwww.example.comzProxy-authorization)r�r�)r�r�)rr*r
r�r�r�r�rrQrr�rTr�r{rRr�r�r@rwrU)rr�r�Z
https_handlerrWr$r$r%�$test_proxy_https_proxy_authorization's$	
	

	
	
z1HandlerTests.test_proxy_https_proxy_authorization�darwinzonly relevant for OSXcCs�ddddddddgi}x+dD]#}|jt||�d|�q(Wx+dD]#}|jt||�d|�qVWdddgi}|jtd|��dS)NZexclude_simpleF�
exceptions�foo.barz	*.bar.com�	127.0.0.1z10.10z10.0/16�www.bar.com�	10.10.0.1�10.0.0.1z expected bypass of %s to be True�abc.foo.bar�bar.com�	127.0.0.2�	10.11.0.1�notinbypassz!expected bypass of %s to be FalseT�test)r�r�r�r�r�)r�r�r�r�r�)rOr	rS)rZbypassr�r$r$r%�test_osx_proxy_bypass>s

z"HandlerTests.test_osx_proxy_bypass�"c	Cs�t�}t�}tjj|�}d}tdd|||f�}|j|�|j|�|j||d|||dd�dS)NzACME Widget Storei�z(WWW-Authenticate: Basic realm=%s%s%s

r�z!http://acme.example.com/protected)rr�r*r
rr�r��_test_basic_auth)r�
quote_charr��password_manager�auth_handlerr��http_handlerr$r$r%�test_basic_authTs		

	zHandlerTests.test_basic_authcCs|jdd�dS)Nr��')r�)rr$r$r%�(test_basic_auth_with_single_quoted_realmdsz5HandlerTests.test_basic_auth_with_single_quoted_realmcCs�t�}t�}tjj|�}d}tdd|�}|j|�|j|�|jt��'|j	||d|||dd�WdQRXdS)NzACME Widget Storei�z$WWW-Authenticate: Basic realm=%s

r�z!http://acme.example.com/protected)
rr�r*r
rr�r�ZassertWarns�UserWarningr�)rr�r�r�r�r�r$r$r%�#test_basic_auth_with_unquoted_realmgs		

	z0HandlerTests.test_basic_auth_with_unquoted_realmc	Cs�t�}tjjtdd��}|j|�t�}tjj|�}d}tdd|�}|j|�|j|�|j	||d|||dd�dS)Nr�zproxy.example.com:3128z
ACME Networksi�z(Proxy-Authenticate: Basic realm="%s"

zProxy-authorizationz&http://acme.example.com:3128/protected)
rr*r
r�r�r�r�ZProxyBasicAuthHandlerr�r�)rr�r�r�r�r�r�r$r$r%�test_proxy_basic_authws	
	

	z"HandlerTests.test_proxy_basic_authc
	Cs�Gdd�dt�}Gdd�dtjj�}Gdd�dtjj�}|�}t�}||�}||�}d}tdd	|�}	|j|�|j|�|j|	�|j||d
||	|dd�|j	|j
dd
gd�dS)Nc@s(eZdZdd�Zdd�ZdS)zQHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorcSstj|�g|_dS)N)rr��recorded)rr$r$r%r��s
zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__cSs|jj|�dS)N)r�r�)rr�r$r$r%�record�szXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordN)rDrrEr�r�r$r$r$r%�RecordingOpenerDirector�sr�c@seZdZdd�ZdS)zOHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc_s-|jjd�tjjj|||�dS)N�digest)r�r�r*r
�HTTPDigestAuthHandler�http_error_401)rr}r�r$r$r%r��sz^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401N)rDrrEr�r$r$r$r%�TestDigestAuthHandler�sr�c@seZdZdd�ZdS)zNHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc_s-|jjd�tjjj|||�dS)N�basic)r�r�r*r
rr�)rr}r�r$r$r%r��sz]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401N)rDrrEr�r$r$r$r%�TestBasicAuthHandler�sr�z
ACME Networksi�z&WWW-Authenticate: Basic realm="%s"

r�z!http://acme.example.com/protectedr�r�r�)rr*r
r�rr�r�r�r�rr�)
rr�r�r�r�r�Zdigest_handlerZ
basic_handlerr�r�r$r$r%�#test_basic_and_digest_auth_handlers�s$			


	z0HandlerTests.test_basic_and_digest_auth_handlerscCs^t�}tjjd�}tdd�}|j|�|j|�|jt|jd�dS)Ni�zWWW-Authenticate: Kerberos

zhttp://www.example.com)	rr*r
r�r�r�r(r)r{)rr�Zdigest_auth_handlerr�r$r$r%�$test_unsupported_auth_digest_handler�s	

z1HandlerTests.test_unsupported_auth_digest_handlercCs^t�}tjjd�}tdd�}|j|�|j|�|jt|jd�dS)Ni�zWWW-Authenticate: NTLM

zhttp://www.example.com)	rr*r
rr�r�r(r)r{)rr�Zbasic_auth_handlerr�r$r$r%�#test_unsupported_auth_basic_handler�s	

z0HandlerTests.test_unsupported_auth_basic_handlerc	Cs�ddl}	d	\}
}|j|||
|�|j||j�|j||j�|j|
|j�|j||j�|j|�|j|j|�|j|j	|�|jt
|j�d�|j|jdj
|��td|
|fd�}d|	j|�j�j�}
|j|jdj|�|
�|j|jdj||
�d|_|_|j�|j|�|jt
|j�d�|j|jdj
|��dS)
Nr�wile�coyoter�z%s:%s�asciizBasic r�)r�r�)�base64rirr�rIr�rZr{r�r�r�r�rSrP�bytesZencodebytesr��decoderUrSr�)rr�r�Zauth_headerr�r�r��request_urlZ
protected_urlr�r�rZZuserpassZauth_hdr_valuer$r$r%r��s0


zHandlerTests._test_basic_authc	Cs�d\}}d}td�}t�}t|�}|jd|||dd�|j|�}|j|�t�}|j|�|j|�|j|�|j|j	�dS)Nr�r�z!http://acme.example.com/protectedr��is_authenticatedT)r�r�)
r�rrrir�rOrr�r{r�)	rr�rZr�r��pwd_manager�auth_prior_handler�is_authr�r$r$r%�test_basic_prior_auth_auto_send�s	
	


z,HandlerTests.test_basic_prior_auth_auto_sendc
Csd\}}d}d}t�}t|�}|j||||�|j|�}|j|�t�}|j|�tddd�}	|j|	�|j|�|j|�}|j	|�t
d�}	|j|	j�t�}|j|�|j|	�|j|�|j	|	j�dS)	Nr�r�z!http://acme.example.com/protectedZACMEi�z&WWW-Authenticate: Basic realm="%s"

r�)r�r�)rrrir�rSrr�r�r{rOr�r�)
rr�rZr�r�r�r�r�r�r�r$r$r%�.test_basic_prior_auth_send_after_first_success
s.	
	



	


z;HandlerTests.test_basic_prior_auth_send_after_first_successcCs�x�dD]�\}}dj|�}tj|j�|�}tjj�}td�}d|_|j	||��}|j
�WdQRX|j|jj
dj|��qWdS)
N�Connection: close�data�Transfer-Encoding: chunked�4
data
0

�Content-Length: 4zHTTP/1.1 200 OK
{}

z
http://dummy/zConnection not closed with {!r}�r�r��r�r��r�r�)r�r�r�)�formatrr�rr*r
r�rrzr�r1rO�fakesock�closed)rZtransferry�headerr�rrWZrespr$r$r%�test_http_closed5s	zHandlerTests.test_http_closedc
Csztjd�}tjj�}td�}d|_|jtj	j
��|j||�WdQRX|j|j
jd�dS)NrGz
http://dummy/zConnection not closed)rr�r*r
r�rrzr(r�r�Z
BadStatusLiner�rOr�r�)rr�rrWr$r$r%�test_invalid_closedFs	z HandlerTests.test_invalid_closedN)*rDrrEr+r?rXrYr[r^r_r`rdrhr{r�r�r�r�r�r�r�r�r�r�r�r�
skipUnless�sys�platformr�r�r�r�r�r�r�r�r�r�r�r�r�r$r$r$r%r�sF@\lJ
	#'2

%(rc@sseZdZdd�Zdd�Zejejd�d�dd��Z	d	d
�Z
dd�Zd
d�ZdS)�	MiscTestscs-|jt�fdd�|jD���dS)Nc3s|]}|j�kVqdS)N)r�)rqr9)�
handler_classr$r%�	<genexpr>Tsz/MiscTests.opener_has_handler.<locals>.<genexpr>)rO�anyr�)rr�rr$)rr%�opener_has_handlerSszMiscTests.opener_has_handlercCs�Gdd�dtjj�}Gdd�dtjj�}Gdd�dtjj�}tjj}|||�}|j||�|j||�|||��}|j||�|j||�||�}|j||�|�}|j|tjj�|tjj�}|j|tjj�|tjj��}|j|tjj�Gdd�dtjj�}|||�}|j||�|j||�dS)	Nc@seZdZdS)z2MiscTests.test_build_opener.<locals>.MyHTTPHandlerN)rDrrEr$r$r$r%�
MyHTTPHandlerXsrc@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.FooHandlercSsdS)Nr$)rr$r$r%�foo_open\sz8MiscTests.test_build_opener.<locals>.FooHandler.foo_openN)rDrrErr$r$r$r%�
FooHandler[sr	c@seZdZdd�ZdS)z/MiscTests.test_build_opener.<locals>.BarHandlercSsdS)Nr$)rr$r$r%�bar_open`sz8MiscTests.test_build_opener.<locals>.BarHandler.bar_openN)rDrrEr
r$r$r$r%�
BarHandler_src@seZdZdS)z7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerN)rDrrEr$r$r$r%�MyOtherHTTPHandler|sr)r*r
r��BaseHandlerr�r)rrr	rr�r�rr$r$r%�test_build_openerWs,	zMiscTests.test_build_openerZnetworkztest requires network accesscCs�tjd���tjj�}tjjd�}|jd|j�|j|dj	d��|jd|j�|jd|j
d��|j|dj	d��|jd|j�|jd|j
d��WdQRXdS)	Nzhttp://www.example.com/rbzus-ascii�1zContent-lengthZ
1234567890s
1234567890ro)rZtransient_internetr*r
r�rrryr{rrU)rr�r
r$r$r%�test_issue16464�szMiscTests.test_issue16464cCs�d}d}}}d}tjj|||||�}|jt|d��|j|jd�|jt|d��|j|jd�d|j|j	f}|jt
|�|�d|j|j	f}|jt|�|�dS)Nzsomething bad happenedzContent-Length: 42r>rGzHTTP Error %s: %sz<HTTPError %s: %r>)r*rrvrOr�rr>rGr�r�rAr�)rr�rIr�r�rVrBZexpected_errmsgr$r$r%�test_HTTPError_interface�sz"MiscTests.test_HTTPError_interfacecCsqddddddddd!d#d%d'g}x*|D]"\}}|jt|�|�q1W|jttd�fdS)(N�proxy.example.com�proxy.example.com:3128�joe:password@proxy.example.comrYrZ�#joe:password@proxy.example.com:3128�http://proxy.example.com/r��http://proxy.example.com:3128/�&http://joe:password@proxy.example.com/�*http://joe:password@proxy.example.com:3128�1ftp://joe:password@proxy.example.com/rubbish:3128r2�%http://joe:password@proxy.example.comzfile:/ftp.example.com�NNNr)rr�NNNr)rr�NNNr)rr�NNNr)rr�NrYrZr)rr �NrYrZr)rr!�r�NNr)rr"�r�NNr)rr#�r�rYrZr)rr$�r�rYrZr)rr%�r2rYrZr)rr&�r�rYrZr)rr')rrr(r))rZparse_proxy_test_casesZtcrr$r$r%�test_parse_proxy�s4	zMiscTests.test_parse_proxyc
CsLt�}|jt��}|jd�WdQRX|jt|j�d�dS)N�invalidz5Unsupported digest authentication algorithm 'invalid')r
r(r)Zget_algorithm_implsrrA�	exception)rr�excr$r$r%�test_unsupported_algorithm�s	z$MiscTests.test_unsupported_algorithmN)
rDrrErrrr�rZis_resource_enabledrrr(r,r$r$r$r%rQs,%rc@s�eZdZGdd�de�Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdS) �RequestTestsc@seZdZdZdS)zRequestTests.PutRequest�PUTN)rDrrEr�r$r$r$r%�
PutRequest�sr/cCsptd�|_tdddddi�|_tddd�|_|jd�|_|jddd�|_dS)	Nzhttp://www.python.org/~jeremy/ryrGzX-Testr�r��HEADrA)rr*�post�headr/�put�
force_post)rr$r$r%�setUp�szRequestTests.setUpcCs�|jd|jj��|jd|jj��|jd|jj��|jd|jj��|jd|jj��dS)NrAr@r0r.)rr1rxr*r2r3r4)rr$r$r%�test_method�s
zRequestTests.test_methodcCsh|j|jj�|jd|jj��d|j_|j|jj�|jd|jj��dS)Nr@rarA)rSr*ryrrxrO)rr$r$r%�	test_data�s
zRequestTests.test_datacCsi|jd|jj�|jjdd�|jd|jjd�d|j_|jd|jj�dS)NzContent-length�*ra)rRr*rSrVrry)rr$r$r%�.test_setting_data_should_remove_content_length�s
z;RequestTests.test_setting_data_should_remove_content_lengthcCsr|jd|jj�d|j_|jjdd�|jd|jjd�|j`|jd|jj�dS)NzContent-lengthr\rM)rRr*rSryrVr)rr$r$r%�/test_deleting_data_should_remove_content_length�s	z<RequestTests.test_deleting_data_should_remove_content_lengthcCs|jd|jj��dS)Nzhttp://www.python.org/~jeremy/)rr*r�)rr$r$r%�test_get_full_urls	zRequestTests.test_get_full_urlcCs9|jd|jj�td�}|jd|j�dS)Nz	/~jeremy/zhttp://www.python.org/r')rr*r�r)rrWr$r$r%�
test_selectorszRequestTests.test_selectorcCs|jd|jj�dS)Nr�)rr*r=)rr$r$r%�
test_get_typeszRequestTests.test_get_typecCs|jd|jj�dS)Nzwww.python.org)rr*r�)rr$r$r%�
test_get_hostszRequestTests.test_get_hostcCs#td�}|jd|j�dS)Nzhttp://www.%70ython.org/zwww.python.org)rrr�)rrWr$r$r%�test_get_host_unquotesz"RequestTests.test_get_host_unquotecCso|j|jj��|jjdd�|j|jj��|jd|jj�|jd|jj�dS)Nzwww.perl.orgr�zwww.python.org)rSr*Z	has_proxyrZrOrrfr�)rr$r$r%r�s
zRequestTests.test_proxycCs#td�}|jd|j�dS)Nz<URL:http://www.python.org>zwww.python.org)rrr�)rrWr$r$r%�test_wrapped_urlszRequestTests.test_wrapped_urlcCsjtd�}|jd|j�td�}|jd|j�d}t|�}|j|j�|�dS)Nz-http://www.python.org/?qs=query#fragment=truez
/?qs=queryzhttp://www.python.org/#fun=truer'z.http://docs.python.org/library/urllib2.html#OK)rrr�r�)rrWrIr$r$r%�test_url_fragment!szRequestTests.test_url_fragmentcCsIdddg}x3|D]+}t|�}|j|j�|j�qWdS)Nzhttp://docs.python.orgz.http://docs.python.org/library/urllib2.html#OKz-http://www.python.org/?qs=query#fragment=true)rrr�r\)rr>rIrWr$r$r%�test_url_fullurl_get_full_url,s	
z*RequestTests.test_url_fullurl_get_full_urlN)rDrrErr/r5r6r7r9r:r;r<r=r>r?r�r@rArBr$r$r$r%r-�s

r-�__main__)6rr�rrrr�rrUrZurllib.requestr*rrrrrr	r
Zurllib.parserr�Zhttp.clientr�ZTestCaserrFrxr�r�r�r�r�r�r��IOBaser�r�r�r�r�r
r
r�r�r�r�r�r�rrrr-rD�mainr$r$r$r%�<module>sN43�

+6"
������a