Your IP : 216.73.217.32


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



���]�	�@sddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
ddlZyddlZWnek
r�dZYnXddlZddlZddlZddlmZmZddlmZddlZdd�Zdadddd	�Zd
d�Zdd
�ZGdd�de�Z Gdd�de�Z!Gdd�de	j"�Z#Gdd�de	j"�Z$Gdd�de	j"�Z%Gdd�de	j"e e!�Z&Gdd�de	j"�Z'Gdd�de	j"�Z(Gdd�de	j"e �Z)Gd d!�d!e	j"�Z*Gd"d#�d#e	j"�Z+Gd$d%�d%e	j"�Z,Gd&d'�d'e	j"�Z-Gd(d)�d)e	j"�Z.Gd*d+�d+e	j"�Z/Gd,d-�d-e	j"�Z0Gd.d/�d/e	j"�Z1Gd0d1�d1e	j"�Z2e3d2kre	j4�dS)3�N)�patch)�support)�url2pathname�pathname2url)�	b64encodecCsFtt|��dd�j�}t|�dkr>d|}d|S)N��z0%s�%)�hex�ord�upper�len)�charZhex_repr�r�0/opt/alt/python35/lib64/python3.5/test_urllib.py�	hexescapes"
rcCso|dk	r$tjjd|�}nts<t�}|ant}|dkr[|j|�S|j||�SdS)N�proxies)�urllib�request�FancyURLopener�
_urlopener�open)�url�datarZopenerrrr�urlopen%s		
rc	Cs.tjdtf��tjj�SWdQRXdS)Nz8FancyURLopener style of invoking requests is deprecated.)r�check_warnings�DeprecationWarningrrrrrrrr5s
rcsNG�fdd�dtj��G�fdd�dtjj��|�_�S)Ncs^eZdZdZ�fdd�Zdd�Zddd�Zdd	d
�Zdd�ZdS)
zfakehttp.<locals>.FakeSocketrcs
|�_dS)N)�buf)�selfr)�FakeHTTPConnectionrr�sendall@sz$fakehttp.<locals>.FakeSocket.sendallc_s|jd7_|S)Nr)�io_refs)r�args�kwdsrrr�makefileCsz%fakehttp.<locals>.FakeSocket.makefileNcSs |jr
dStjj||�S)N�)�closed�io�BytesIO�read)rZamtrrrr)Gs	z!fakehttp.<locals>.FakeSocket.readcSs |jr
dStjj||�S)Nr%)r&r'r(�readline)r�lengthrrrr*Ls	z%fakehttp.<locals>.FakeSocket.readlinecSs2|jd8_|jdkr.tjj|�dS)Nrr)r!r'r(�close)rrrrr,Qsz"fakehttp.<locals>.FakeSocket.close)	�__name__�
__module__�__qualname__r!r r$r)r*r,r)rrr�
FakeSocket=sr0cs(eZdZdZ�fdd�ZdS)z$fakehttp.<locals>.FakeHTTPConnectionNcs(�|j�|_|jt|�_dS)N)�fakedata�sock�typeZfakesock)r)r0rr�connect[sz,fakehttp.<locals>.FakeHTTPConnection.connect)r-r.r/rr4r)r0rrrVsr)r'r(�http�client�HTTPConnectionr1)r1r)rr0r�fakehttp<s"	r8c@s(eZdZdd�Zdd�ZdS)�
FakeHTTPMixincCs%tjj|_t|�tj_dS)N)r5r6r7�_connection_classr8)rr1rrrr8dszFakeHTTPMixin.fakehttpcCs|jtj_dS)N)r:r5r6r7)rrrr�
unfakehttphszFakeHTTPMixin.unfakehttpN)r-r.r/r8r;rrrrr9csr9c@s(eZdZdd�Zdd�ZdS)�FakeFTPMixincCs5Gdd�dt�}tjj|_|tj_dS)Nc@s:eZdZdddd�Zdd�Zdd�ZdS)	z,FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapperNTcSsdS)Nr)r�userZpasswd�host�port�dirs�timeoutZ
persistentrrr�__init__osz5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.__init__cSstj�dfS)Nr)r'r()r�filer3rrr�retrfilessz5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfilecSsdS)Nr)rrrrr,vsz2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close)r-r.r/rBrDr,rrrr�FakeFtpWrappernsrE)�objectrr�
ftpwrapper�_ftpwrapper_class)rrErrr�fakeftpmszFakeFTPMixin.fakeftpcCs|jtj_dS)N)rHrrrG)rrrr�	unfakeftp|szFakeFTPMixin.unfakeftpN)r-r.r/rIrJrrrrr<lsr<c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�urlopen_FileTestscCsvtd|jjd�|_ttjd�}z|j|j�Wd|j�Xtj|_	t
d|j	�|_dS)Nztest_urllib: %s
�ascii�wbzfile:%s)�bytes�	__class__r-�textrr�TESTFN�writer,�pathnamer�returned_obj)r�frrr�setUp�szurlopen_FileTests.setUpcCs!|jj�tjtj�dS)N)rTr,�os�removerrQ)rrrr�tearDown�s
zurlopen_FileTests.tearDownc	Cs5x.dD]&}|jt|j|�d
|�qWdS)Nr)r*�	readlines�filenor,�info�geturl�getcode�__iter__z/object returned by urlopen() lacks %s attribute)	r)r*rZr[r,r\r]r^r_)�
assertTrue�hasattrrT)r�attrrrr�test_interface�s

z urlopen_FileTests.test_interfacecCs |j|j|jj��dS)N)�assertEqualrPrTr))rrrr�	test_read�szurlopen_FileTests.test_readcCs<|j|j|jj��|jd|jj�d�dS)Nr%zKcalling readline() after exhausting the file did not return an empty string)rdrPrTr*)rrrr�
test_readline�szurlopen_FileTests.test_readlinecCsF|jj�}|jt|�dd�|j|d|jd�dS)Nrz.readlines() returned the wrong number of linesrz"readlines() returned improper text)rTrZrdr
rP)rZ
lines_listrrr�test_readlines�s
z urlopen_FileTests.test_readlinescCsQ|jj�}|j|td�|jtj|t|j��|jd�dS)Nzfileno() did not return an intzTReading on the file descriptor returned by fileno() did not return the expected text)	rTr[�assertIsInstance�intrdrWr)r
rP)rZfile_numrrr�test_fileno�s$zurlopen_FileTests.test_filenocCs|jj�dS)N)rTr,)rrrr�
test_close�szurlopen_FileTests.test_closecCs#|j|jj�tjj�dS)N)rhrTr\�email�message�Message)rrrr�	test_info�szurlopen_FileTests.test_infocCs |j|jj�|j�dS)N)rdrTr]rS)rrrr�test_geturl�szurlopen_FileTests.test_geturlcCs|j|jj��dS)N)ZassertIsNonerTr^)rrrr�test_getcode�szurlopen_FileTests.test_getcodecCs+x$|jD]}|j||j�q
WdS)N)rTrdrP)r�linerrr�	test_iter�szurlopen_FileTests.test_itercCs$|jttjjd|j�dS)Nz./)�assertRaises�
ValueErrorrrrrS)rrrr�test_relativelocalfile�sz(urlopen_FileTests.test_relativelocalfileN)r-r.r/rVrYrcrerfrgrjrkrorprqrsrvrrrrrK�s	rKc@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�
ProxyTestscCsOtj�|_x9ttj�D](}d|j�kr|jj|�qWdS)N�proxy)rZEnvironmentVarGuard�env�listrW�environ�lower�unset)r�krrrrV�szProxyTests.setUpcCs|jj�|`dS)N)ry�__exit__)rrrrrY�s
zProxyTests.tearDowncCs�|jjdd�tjj�}|jd|d�|jjdd�|jtjjd��|jtjjd��|jtjjd��dS)N�NO_PROXY�	localhost�noz0localhost, anotherdomain.com, newdomain.com:1234zanotherdomain.comzanotherdomain.com:8888znewdomain.com:1234)ry�setrr�getproxies_environmentrdr`�proxy_bypass_environment)rrrrr�+test_getproxies_environment_keep_no_proxies�sz6ProxyTests.test_getproxies_environment_keep_no_proxiescCs�zl|jjdd�tjj�}|jd|d�|jjdd�tjj�}|jd|�Wd|jjd�|jjd�XdS)N�
HTTP_PROXYzhttp://somewhere:3128r5ZREQUEST_METHOD�GET)ryr�rrr�rd�assertNotInr})rrrrr�test_proxy_cgi_ignore�sz ProxyTests.test_proxy_cgi_ignorecCs�tjj}|jjdd�|j|d��|j|d��|j|d��|j|d��|j|d��|j|d��|j|d	��|j|d
��|j|d��|j|d��dS)
Nr�z8localhost, anotherdomain.com, newdomain.com:1234, .d.o.tr�Z	LocalHostZ	LOCALHOSTznewdomain.com:1234z	foo.d.o.tzanotherdomain.com:8888zwww.newdomain.com:1234Zprelocalhostz
newdomain.comznewdomain.com:1235)rrr�ryr�r`�assertFalse)rZbypassrrr�(test_proxy_bypass_environment_host_match�sz3ProxyTests.test_proxy_bypass_environment_host_matchN)r-r.r/rVrYr�r�r�rrrrrw�s
rwc@s4eZdZdd�Zdd�Zdd�ZdS)�ProxyTests_withOrderedEnvcCstj|_tj�t_dS)N)rWr{�
_saved_env�collections�OrderedDict)rrrrrVszProxyTests_withOrderedEnv.setUpcCs|jt_dS)N)r�rWr{)rrrrrYsz"ProxyTests_withOrderedEnv.tearDowncCs]dtjd<dtjd<|jtjjd��|jtjjd��dtjd<dtjd<tjj�}|ji|�d	tjd<d
tjd<|jtjjd��|jtjjd��|jtjjd��|jtjjd
��|jtjjd��dtjd<dtjd<tjj�}|jd|d�dS)N�Zno_proxyr�ZNo_ProxyZ	arbitraryZ
http_proxyzhttp://somewhere:3128r�z%localhost, noproxy.com, my.proxy:1234zxyz.comznoproxy.com:5678z
my.proxy:1234zmy.proxyzhttp://somewhereelse:3128Z
Http_Proxyr5)	rWr{r�rrr�r�rdr`)rrrrr�,test_getproxies_environment_prefer_lowercases&







zFProxyTests_withOrderedEnv.test_getproxies_environment_prefer_lowercaseN)r-r.r/rVrYr�rrrrr�	sr�c@sZeZdZdd�Zdd�Zdd�Zejed�dd	��Z	ejed�d
d��Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zejejd"d#�d$d%��Zd&d'�Zd(d)�Zd*d+�Zejed�d,d-��Zd.S)/�urlopen_HttpTestsc
Cs�|jd|d�zhtd�}|j|j�d�|j|j�d�|j|j�d�|j|j�d�Wd|j�XdS)NsHTTP/s 200 OK

Hello!zhttp://python.org/sHello!r%��)r8rrdr*r]r^r;)rZver�fprrr�
check_read0szurlopen_HttpTests.check_readc
CsQd}|jd�z,tjj|�}|j|j�|�Wd|j�XdS)Nz-http://docs.python.org/library/urllib.html#OKsHTTP/1.1 200 OK

Hello!)r8rrrrdr]r;)rrr�rrr�test_url_fragment;s
z#urlopen_HttpTests.test_url_fragmentc
CsB|jd�z#td�}|j|jj�Wd|j�XdS)NsHTTP/1.1 200 OK

Hello!zhttp://www.python.org)r8rr`r�Z
will_closer;)r�resprrr�test_willcloseEs

z urlopen_HttpTests.test_willclosezssl module requiredcCs,x%ttdd��dgD]}t|�}dj|�}|jd�z�t|�jdd�}tjj	}|j
|dj|���tjj
d	j|��WdQRX|j
|dj|���tjj
d
j|��WdQRXt
d	j|��}|j||j��Wd|j�XqWdS)Nr�!�z//localhost:7777/test{}/sHTTP/1.1 200 OK

Hello.�\z\\zcontain control.*{}zhttp:{}zhttps:{})rz�range�chr�formatr8�repr�replacer5r6�
InvalidURL�assertRaisesRegexrrrr�r]r;)rZchar_nor�schemeless_urlZescaped_char_reprr�r�rrr�#test_url_with_control_char_rejectedMs$#
  z5urlopen_HttpTests.test_url_with_control_char_rejectedcCs|jd�d}d|d}z�tjj}|j|d��tjjdj|��WdQRX|j|d��tjjdj|��WdQRXtdj|��}|j	d	|j
��|j	d
|j
��|j	d|j
��Wd|j�XdS)NsHTTP/1.1 200 OK

Hello.z:localhost:7777?a=1 HTTP/1.1
X-injected: header
TEST: 123z//z:8080/test/?test=az*contain control.*\\r.*(found at least . .)zhttp:{}zcontain control.*\\nzhttps:{}� �
�
)r8r5r6r�r�rrrr�r�r]r;)rr>r�r�r�rrr�/test_url_with_newline_header_injection_rejectedjs

  zAurlopen_HttpTests.test_url_with_newline_header_injection_rejectedcCs|jd�dS)Ns0.9)r�)rrrr�
test_read_0_9�szurlopen_HttpTests.test_read_0_9cCs|jd�dS)Ns1.0)r�)rrrr�
test_read_1_0�szurlopen_HttpTests.test_read_1_0cCs|jd�dS)Ns1.1)r�)rrrr�
test_read_1_1�szurlopen_HttpTests.test_read_1_1c
Cs6|jd�z|jttd�Wd|j�XdS)Ns�HTTP/1.1 401 Authentication Required
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Connection: close
Content-Type: text/html; charset=iso-8859-1
zhttp://python.org/)r8rt�OSErrorrr;)rrrr�test_read_bogus�s
z!urlopen_HttpTests.test_read_boguscCsS|jd�z4d}|jtjj|��td�WdQRXWd|j�XdS)Ns�HTTP/1.1 302 Found
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Location: file://guidocomputer.athome.com:/python/license
Connection: close
Content-Type: text/html; charset=iso-8859-1
zRedirection to url 'file:zhttp://python.org/)r8r�r�error�	HTTPErrorrr;)r�msgrrr�test_invalid_redirect�sz'urlopen_HttpTests.test_invalid_redirectcCsYxRtt�j�D]>}|jd�z|jtjjtd�Wd|j	�XqWdS)Ns_HTTP/1.1 302 Found
Location: file://guidocomputer.athome.com:/python/license
Connection: close
zhttp://something)
r�rZmaxtriesr8rtrr�r�rr;)r�irrr�test_redirect_limit_independent�sz1urlopen_HttpTests.test_redirect_limit_independentc
Cs6|jd�z|jttd�Wd|j�XdS)Nr%zhttp://something)r8rtr�rr;)rrrr�test_empty_socket�s
z#urlopen_HttpTests.test_empty_socketc	CsS|jtjj��}td�WdQRX|j|jj�|j|jj�dS)Nz/file://localhost/a/file/which/doesnot/exists.py)	rtrr��URLErrorrr`�	exception�filename�reason)r�errr�test_missing_localfile�sz(urlopen_HttpTests.test_missing_localfilecCs�tj�\}}d|jtjjd�}z@|jtjj|��t|��}|j|�WdQRXWdtj	|�tj
|�X|jtjj|��|jt
jj��t|�WdQRXdS)Nzfile://localhost/�/)�tempfile�mkstempr�rW�path�sepr`�existsrr,�unlinkr�rtrr�r�)r�fdZtmp_fileZtmp_fileurlZfobjrrr�test_file_notexists�s
z%urlopen_HttpTests.test_file_notexistsc	CsYd}|jtjj��}t|�WdQRX|j|jj�|j|jj	�dS)Nzftp:///path)
rtrr�r�rr�r�r�r`r�)rZtest_ftp_urlr�rrr�test_ftp_nohost�s
z!urlopen_HttpTests.test_ftp_nohostc	CsS|jtjj��}td�WdQRX|j|jj�|j|jj	�dS)Nz.ftp://localhost/a/file/which/doesnot/exists.py)
rtrr�r�rr�r�r�r`r�)rr�rrr�test_ftp_nonexisting�sz&urlopen_HttpTests.test_ftp_nonexistingZMAXFTPCACHErcCsR|j�z6tjjddddg�tjjd<td�Wd|j�XdS)Nr=�passr���testzftp://localhost)rIrrrGZftpcacherrJ)rrrr�test_ftp_cache_pruning�s

(z(urlopen_HttpTests.test_ftp_cache_pruningc
Cs�|jd�zhtd�}|j|j�d�|j|j�d�|j|j�d�|j|j�d�Wd|j�XdS)NsHTTP/1.0 200 OK

Hello!zhttp://user:pass@python.org/sHello!r%r�)r8rrdr*r]r^r;)rr�rrr�test_userpass_inurl�s
z%urlopen_HttpTests.test_userpass_inurlc
Cs�|jd�z�d}dj|�}tjj}dt|jd��jd�}t|�}|j	||j
jd��|j|j�d�|j|j�d�|j
|j�|�|j|j�d	�Wd|j�XdS)
NsHTTP/1.0 200 OK

Hello!za b:c dzhttp://{}@python.org/zAuthorization: Basic %s
�ASCIIzUTF-8sHello!r%r�)r8r�r5r6r7r�encode�decoder�assertInrrdr*ZassertNotEqualr]r^r;)rZuserpassrZfakehttp_wrapperZ
authorizationr�rrr�test_userpass_inurl_w_spaces�s
z.urlopen_HttpTests.test_userpass_inurl_w_spacesc	Cs.tjdtf��tjj�WdQRXdS)Nr�)rrrrr�	URLopener)rrrr�test_URLopener_deprecationsz,urlopen_HttpTests.test_URLopener_deprecationc
CsCtj�}|jt��!tjjdddd|�WdQRXdS)Nzhttps://localhostZcafilez/nonexistent/path�context)�sslZcreate_default_contextrtrurrr)rr�rrr�test_cafile_and_contexts	z)urlopen_HttpTests.test_cafile_and_contextN)r-r.r/r�r�r��unittest�
skipUnlessr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrFrrr�r�r�r�r�rrrrr�-s*

	$	r�c@s|eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�urlopen_DataTestscCsyd|_d|_d|_d|_d|_tjj|j�|_tjj|j�|_	tjj|j�|_
dS)Nutest data URLs :;,%=& ö Ä sU�PNG


IHDR{@��sRGB���IDAT�c```���?�
o/IEND�B`�zZdata:text/plain;charset=UTF-8,test%20data%20URLs%20%3A%3B%2C%25%3D%26%20%C3%B6%20%C3%84%20zPdata:text/plain;charset=ISO-8859-1;base64,dGVzdCBkYXRhIFVSTHMgOjssJT0mIPYgxCA%3Dz�data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7
QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 vHgAAAABJRU5ErkJggg%3D%3D%0A%20)rP�image�text_url�text_url_base64�	image_urlrrr�
text_url_resp�text_url_base64_resp�image_url_resp)rrrrrVs						zurlopen_DataTests.setUpcCs5x.d
D]&}|jt|j|�d	|�qWdS)Nr)r*rZr,r\r]r^r_z/object returned by urlopen() lacks %s attribute)r)r*rZr,r\r]r^r_)r`rar�)rrbrrrrc8s

z urlopen_DataTests.test_interfacecCs�|j|jj�tjj�|j|jj�j�dd	g�|j|j	j�dt
t|j���|jt
jjd�j�j�d
dg�dS)N�
text/plainr��charset�
ISO-8859-1zcontent-lengthzdata:,�US-ASCII)r�r�)r�r�)r�r�)r�r�)rhr�r\rlrmrnrdr��
get_paramsr��strr
r�rrr)rrrrro@s
!zurlopen_DataTests.test_infocCsX|j|jj�|j�|j|jj�|j�|j|jj�|j�dS)N)rdr�r]r�r�r�r�r�)rrrrrpIs
zurlopen_DataTests.test_geturlcCsB|j|jj�jt|jj�j��d�|j�dS)Nr�)rdr�r)r��dictr\r�rP)rrrr�test_read_textOsz urlopen_DataTests.test_read_textcCsB|j|jj�jt|jj�j��d�|j�dS)Nr�)rdr�r)r�r�r\r�rP)rrrr�test_read_text_base64Ssz'urlopen_DataTests.test_read_text_base64cCs |j|jj�|j�dS)N)rdr�r)r�)rrrr�test_read_imageXsz!urlopen_DataTests.test_read_imagecCs|jttjjd�dS)Nzdata:text/plain)rtrurrr)rrrr�test_missing_comma[sz$urlopen_DataTests.test_missing_commacCs|jttjjd�dS)Nzdata:;base64,Cg=)rtrurrr)rrrr�test_invalid_base64_data^sz*urlopen_DataTests.test_invalid_base64_dataN)r-r.r/rVrcrorpr�r�r�r�r�rrrrr�s	r�c@s�eZdZdd�Zdd�Zdd�Zddd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�urlretrieve_FileTestscCssg|_|jtj�d|_z0ttjd�}|j|j�|j�Wdy|j�WnYnXXdS)Nstesting urllib.urlretrieverM)�	tempFiles�registerFileForCleanUprrQrPrrRr,)r�FILErrrrVfs		zurlretrieve_FileTests.setUpc
Cs4x-|jD]"}ytj|�Wq
Yq
Xq
WdS)N)r�rWrX)rZeachrrrrY{s
zurlretrieve_FileTests.tearDowncCs[tjj|�}y|jd�Wn!tk
rFtjd��YnXdtjj	|�S)Nzutf-8z!filePath is not encodable to utf8z	file://%s)
rWr��abspathr��UnicodeEncodeErrorr�ZSkipTestrrr)rZfilePathrrr�constructLocalFileUrl�s
z+urlretrieve_FileTests.constructLocalFileUrlr%cCsmtj�\}}z:|j|�tj|d�}|j|�|j�Wdy|j�WnYnXX|S)NrM)r�r�r�rW�fdopenrRr,)rrZnewFdZnewFilePathZnewFilerrr�createNewTempFile�s

z'urlretrieve_FileTests.createNewTempFilecCs|jj|�dS)N)r��append)rZfileNamerrrr��sz,urlretrieve_FileTests.registerFileForCleanUpcCsQtjjdtj�}|j|dtj�|j|dtjj	d�dS)Nzfile:%srrzFdid not get an email.message.Message instance as second returned value)
rr�urlretrieverrQrdrhrlrmrn)r�resultrrr�
test_basic�sz urlretrieve_FileTests.test_basiccCs�dtj}|j|�tjj|jtj�|�}|j||d�|jt	j
j|�d�t|d�}z|j
�}|j�Wdy|j�WnYnXX|j|j|�dS)Nz%s.2rzcopy of the file was not made�rb)rrQr�rrr�r�rdr`rWr�r�rr)r,rP)r�second_tempr�r�rPrrr�	test_copy�s

zurlretrieve_FileTests.test_copycsXdg�fdd�}dtj}�j|�tjj�jtj�||�dS)NrcsZ�j|t��j|t��j|t��j||d�|dd|d<dS)Nrr)rhrird)�block_count�block_read_size�	file_sizeZcount_holder)rrr�
hooktester�s
z9urlretrieve_FileTests.test_reporthook.<locals>.hooktesterz%s.2)rrQr�rrr�r�)rr�r�r)rr�test_reporthook�s

	z%urlretrieve_FileTests.test_reporthookcCsug}|dd�}|j�}tjj|j|�tj|�|jt|�d�|j|ddd�dS)NcSs|j|||f�dS)N)r�)r�r�r��_reportrrrr��szAurlretrieve_FileTests.test_reporthook_0_bytes.<locals>.hooktesterrrr)	r�rrr�r�rrQrdr
)r�reportr��srcFileNamerrr�test_reporthook_0_bytes�s
z-urlretrieve_FileTests.test_reporthook_0_bytescCs�g}|dd�}|jd�}tjj|j|�tj|�|jt|�d�|j|ddd�|j|ddd�dS)	NcSs|j|||f�dS)N)r�)r�r�r�rrrrr��szAurlretrieve_FileTests.test_reporthook_5_bytes.<locals>.hooktester�x�rrrsxxxxx)	r�rrr�r�rrQrdr
)rrr�rrrr�test_reporthook_5_bytes�s
z-urlretrieve_FileTests.test_reporthook_5_bytescCs�g}|dd�}|jdd�}tjj|j|�tj|�|jt|�d�|j|ddd�|j|ddd	�|j|ddd	�|j|ddd	�dS)
NcSs|j|||f�dS)N)r�)r�r�r�rrrrr��szDurlretrieve_FileTests.test_reporthook_8193_bytes.<locals>.hooktesterri �rrri )	r�rrr�r�rrQrdr
)rrr�rrrr�test_reporthook_8193_bytes�s
z0urlretrieve_FileTests.test_reporthook_8193_bytesN)r-r.r/rVrYr�r�r�r�r�rrrr	rrrrr�cs	r�c@s(eZdZdd�Zdd�ZdS)�urlretrieve_HttpTestscCsb|jd�dd�}|jtjj��-ztjjdd|�Wd|j�XWdQRXdS)Ns�HTTP/1.1 200 OK
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Connection: close
Content-Length: 100
Content-Type: text/html; charset=iso-8859-1

FF
cSsdS)Nr)Zpar1Zpar2Zpar3rrr�_reporthook�szYurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError.<locals>._reporthookzhttp://example.com/Z
reporthook)r8rtrr��ContentTooShortErrorrr�r;)rrrrr�.test_short_content_raises_ContentTooShortError�szDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorcCsP|jd�|jtjj��'ztjjd�Wd|j�XWdQRXdS)Ns�HTTP/1.1 200 OK
Date: Wed, 02 Jan 2008 03:03:54 GMT
Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
Connection: close
Content-Length: 100
Content-Type: text/html; charset=iso-8859-1

FF
zhttp://example.com/)r8rtrr�rrr�r;)rrrr�Atest_short_content_raises_ContentTooShortError_without_reporthookszWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookN)r-r.r/r
rrrrrr
�sr
c@s|eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�QuotingTestscCs}djddddg�}tjj|�}|j||d||f�tjj|�}|j||d||f�dS)Nr��ABCDEFGHIJKLMNOPQRSTUVWXYZ�abcdefghijklmnopqrstuvwxyz�
0123456789z_.-zusing quote(): %r != %rzusing quote_plus(): %r != %r)�joinr�parse�quoterd�
quote_plus)rZdo_not_quoter�rrr�test_never_quote2s	zQuotingTests.test_never_quotecCs!|jtjjjdd�dS)Nrr�)rdrrr�__defaults__)rrrr�test_default_safe?szQuotingTests.test_default_safecCs[d}tjj|d|�}|j||d||f�tjj|d|�}|j||d||f�tjj|dd�}|j||d||f�tjjddddd	�}tjjddddd
�}|j||d||f�tjjddddd�}tjjddddd
�}|j||d||f�dS)Nz<>�safezusing quote(): %r != %rzusing quote_plus(): %r != %rs<>uaüb�encodingzlatin-1�ür�s�)rrrrdr)rZquote_by_defaultr��expectrrr�	test_safeCs,	zQuotingTests.test_safecCsFdd�td�D�}|jd�|jtd��dj|�}x�|D]�}tjj|�}|jt|�|d|t|�|f�tjj	|�}|jt|�|d|t|�|f�qOW~d	}d
}tjj|�}|j||d||f�tjj	|�}|j||d||f�dS)
NcSsg|]}t|��qSr)r�)�.0�numrrr�
<listcomp>cs	z5QuotingTests.test_default_quoting.<locals>.<listcomp>� z
<>#%"{}|\^[]`r�r�z1using quote(): %s should be escaped to %s, not %sz6using quote_plus(): %s should be escapes to %s, not %szab[]cdz
ab%5B%5Dcdzusing quote(): %r != %rzusing quote_plus(): %r != %r)
r�r�r�rrrrrdrr)rZshould_quoterr�Z
partial_quote�expectedrrr�test_default_quoting`s,

z!QuotingTests.test_default_quotingcCs�tjjd�}|j|td�d|td�f�tjjd�}|j|dd|�d}|jdtd��}tjj|�}|j||d||f�|jdd�}tjj|�}|j||d||f�dS)Nr�zusing quote(): %r != %r�+zusing quote_plus(): %r != +z
a b cd e fzusing quote_plus(): %r != %r)rrrrdrrr�)rr��givenrrrr�test_quoting_space|szQuotingTests.test_quoting_spacecCsz|jtjjd�d�|jtjjdd�d�|jtjjd�d�|jtjjdd�d�dS)Nzalpha+beta gammazalpha%2Bbeta+gammar%zalpha+beta+gammasalpha+beta gamma�+)rdrrr)rrrr�test_quoting_plus�szQuotingTests.test_quoting_pluscCs�d}d}tjj|�}|j||d||f�|jttjj|dd�tjj|�}|j||d||f�dS)Ns��ab�z%A2%D8ab%FFzusing quote(): %r != %rrzlatin-1z"using quote_from_bytes(): %r != %r)rrrrdrt�	TypeErrorZquote_from_bytes)rr&rr�rrr�test_quote_bytes�szQuotingTests.test_quote_bytescCs�d}d}tjj|�}|j||d||f�tjj|dddd�}|j||d||f�d}d}tjj|dd�}|j||d||f�d}d	}tjj|�}|j||d||f�d}|jttjj|dd�d}d
}tjj|dddd�}|j||d||f�d}d}tjj|dddd
�}|j||d||f�dS)Nu¢Øabÿz%C2%A2%C3%98ab%C3%BFzusing quote(): %r != %rr�errorsz%A2%D8ab%FFzlatin-1u漢字z%E6%BC%A2%E5%AD%97z%3F%3Fr�z%26%2328450%3B%26%2323383%3B�xmlcharrefreplace)rrrrdrtr�)rr&rr�rrr�test_quote_with_unicode�sB		z$QuotingTests.test_quote_with_unicodecCs�d}d}tjj|dd�}|j||d||f�d}d}tjj|dddd	�}|j||d||f�dS)
Nu¢Ø ÿz
%A2%D8+%FFrzlatin-1zusing quote_plus(): %r != %ruab漢字 cdzab%3F%3F+cdr,r�)rrrrd)rr&rr�rrr�test_quote_plus_with_unicode�s	z)QuotingTests.test_quote_plus_with_unicodeN)r-r.r/rrrr$r'r)r+r.r/rrrrrs
*rc@sdeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�UnquotingTestscCsng}x�td�D]�}tt|��}t|�}tjj|�}|j||d||f�tjj|�}|j||d||f�|j|�qWdj	|�}~tjj|�}|j|j
d�dd|�|jtt
ftjjd�|jtt
ftjjf�tjdtfdd	��$|jtt
ftjjd
�WdQRXdS)N�zusing unquote(): %r != %rzusing unquote_plus(): %r != %rr�r	rz/using unquote(): not all characters escaped: %s�quietTr%)r�rr�rr�unquoterd�unquote_plusr�r�countrtr*�AttributeErrorrr�BytesWarning)rZescape_listr r&rr�Z
escape_stringrrr�test_unquoting�s,zUnquotingTests.test_unquotingcCs�d}|}tjj|�}|j||d||f�d}|}tjj|�}|j||d||f�d}|}tjj|�}|j||d||f�d}t|d�}tjj|�}|j||d||f�d}t|d�}tjj|�}|j||d||f�d}t|d�}tjj|�}|j||d||f�|jttftjjd�|jttftjjf�dS)Nz%xabzusing unquote(): %r != %rz%xr	rLz"using unquote_to_bytes(): %r != %r)	rrr3rdrN�unquote_to_bytesrtr*r6)rr&rr�rrr�test_unquoting_badpercents@z(UnquotingTests.test_unquoting_badpercentcCs?d}d}tjj|�}|j||d||f�dS)Nz%Ab%eAs��z"using unquote_to_bytes(): %r != %r)rrr9rd)rr&rr�rrr�test_unquoting_mixed_case)sz(UnquotingTests.test_unquoting_mixed_casecCsxdtd�}d}tjj|�}|j||d||f�tjj|�}|j||d||f�dS)Nzab%sd�cZabcdzusing quote(): %r != %rzusing unquote_plus(): %r != %r)rrrr3rdr4)rr&rr�rrr�test_unquoting_parts2sz#UnquotingTests.test_unquoting_partscCs�d}|}tjj|�}|j||d||f�|jdd�}tjj|�}|j||d||f�dS)Nzare+there+spaces...zusing unquote(): %r != %rr%r�zusing unquote_plus(): %r != %r)rrr3rdr�r4)rr&rr�rrr�test_unquoting_plus>sz"UnquotingTests.test_unquoting_pluscCs�d}d}tjj|�}|j||d||f�tjjd�}d}|j||d||f�d}d}tjj|�}|j||d||f�d}d}tjj|�}|j||d||f�dS)	Nz"br%C3%BCckner_sapporo_20050930.docsbrückner_sapporo_20050930.docz"using unquote_to_bytes(): %r != %ru	漢%C3%BCs漢üs%A2%D8ab%FFs��ab�s	%A2�ab%FF)rrr9rd)rr&rr�rrr�test_unquote_to_bytesJs.z$UnquotingTests.test_unquote_to_bytescCsd}d}tjj|�}|j||d||f�tjj|dddd�}|j||d||f�tjjddd�}d}|j||d||f�d}d	}tjj|�}|j||d||f�d
}d}tjj|�}|j||d||f�tjj|dd�}|j||d||f�d
}d
}tjj|dd�}|j||d||f�tjjd�}d}|j||d||f�tjjddd�}d}|j||d||f�dS)Nz"br%C3%BCckner_sapporo_20050930.docubrückner_sapporo_20050930.doczusing unquote(): %r != %rrr,zbr%FCckner_sapporo_20050930.doczlatin-1z%E6%BC%A2%E5%AD%97u漢字z%F3%B1u�r�r��ignoreu	漢%C3%BCu漢üu漢%FC)rrr3rd)rr&rr�rrr�test_unquote_with_unicodeisN	z(UnquotingTests.test_unquote_with_unicodeN)
r-r.r/r8r:r;r=r>r?rArrrrr0�s$	r0c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�urlencode_Testsc	Csdddg}tjj|�}x.|D]&}|j||d|||f�q(W|j|jd�dd||jd�f�|jd�}||d}||d}|j|j�o�|j�d	||f�|jt	|�dd|t	|�df�dS)Nz1st=1z2nd=2z3rd=3ztesting %s: %s not found in %s�&rz#testing %s: expected 2 '&'s; got %srz1testing %s: '&' not located in proper place in %srrz5testing %s: unexpected number of characters: %s != %s��rDrE)
rr�	urlencoder�rdr5�indexr`�isdigitr
)	rr&Z	test_typeZexpect_somewherer�r#Zamp_locationZon_amp_leftZon_amp_rightrrr�help_inputtype�s$
zurlencode_Tests.help_inputtypecCs&|jddddddid�dS)N�1st�1�2nd�2�3rd�3zusing dict as input type)rI)rrrr�test_using_mapping�sz"urlencode_Tests.test_using_mappingcCs|jdd	d
gd�dS)NrJrKrLrMrNrOz*using sequence of two-item tuples as input)rJrK)rLrM)rNrO)rI)rrrr�test_using_sequence�sz#urlencode_Tests.test_using_sequencecCs�ddi}dtd�td�f}tjj|�}|j||�ddi}d}tjj|�}|j||�dS)NrC�=z%s=%szkey namezA bunch of pluseszkey+name=A+bunch+of+pluses)rrrrFrd)rr&rr�rrr�test_quoting�szurlencode_Tests.test_quotingcCs�ddddgi}dtjjtdddg��}tjj|�}|j||�tjj|d�}x,|dD] }d|}|j||�q|W|j|jd�dd	|jd��dS)
NZsequencerKrMrOzsequence=%sTrCrzExpected 2 '&'s, got %s)rrrr�rFrdr�r5)rr&rr��valuerrr�
test_doseq�s%
zurlencode_Tests.test_doseqcCs<|jdtjji��|jdtjjg��dS)Nr�)rdrrrF)rrrr�test_empty_sequence�sz#urlencode_Tests.test_empty_sequencecCsH|jdtjjddi��|jdtjjddi��dS)Nza=1�arza=None)rdrrrF)rrrr�test_nonstring_values�s"z%urlencode_Tests.test_nonstring_valuescCs�|jdtjjdddgid��|jdtjjdddgid��tjd	d
g�}|jdtjjd|id��dS)Nza=1&a=2rWrrTz
a=None&a=a�bza=a&a=b)rWr)rYr)rdrrrFr�r�)rrrrr�test_nonstring_seq_values�s+	"	z)urlencode_Tests.test_nonstring_seq_valuescCs�d}d}tjj|dddd�}|j||�d}d}tjj|�}|j||�d}d	}tjj|dd
�}|j||�dS)N� �Áz%3F=%3Frr�r,r�z
%C2%A0=%C3%81z%A0=%C1zlatin-1�r[r\)r]�r[r\)r^�r[r\)r_)rrrFrd)rr&rr�rrr�test_urlencode_encoding�sz'urlencode_Tests.test_urlencode_encodingcCsQd}d}tjj|dddddd	�}|j||�d}d}tjj|ddddd	�}|j||�d}d}tjj|d�}|j||�d}d}tjj|d�}|j||�d}d}tjj|ddd�}|j||�d }d}tjj|ddd�}|j||�dS)!Nr[r\z%3F=%3F�doseqTrr�r,r�rz
%3F=1&%3F=%3Fz
%C2%A0=%C3%81�*z%C2%A0=42&%C2%A0=%C3%81z%A0=%C1zlatin-1z%A0=42&%A0=%C1�r[r\)rc�rr\�r[rd)re�r[r\)rf�rbr\�r[rg)rh�r[r\)ri�rbr\�r[rj)rk)rrrFrd)rr&rr�rrr�test_urlencode_encoding_doseqs4z-urlencode_Tests.test_urlencode_encoding_doseqcCs�d}d}tjj|�}|j||�tjj|d�}|j||�d}d}tjj|d�}|j||�dS)N��$��$z
%A0%24=%C1%24Trbz%A0%24=42&%A0%24=%C1%24�rmrn)ro�rbrn�rmrp)rq)rrrFrd)rr&rr�rrr�test_urlencode_bytes0sz$urlencode_Tests.test_urlencode_bytescCsPd}tjj|dd�}d}|j||�d}tjj|dddd�}d}|j||�d}d
}tjj|ddd�}|j||�d}tjj|dddd�}d}|j||�d}d}tjj|dddddd�}d}d
}tjj|ddddd�}|j||�dS)N��$��$rz:$z	%A0$=%C1$raT�
rbz%A0$=%C1$&%A0$=13&%A0$=42rzlatin-1�rsrt)rv�rsrt)rw�rtrurb�rsrx)ry�rsrt)rz�rsrt)r{�rtrurb�rsr|)r})rrrFrd)rr&r�rrrr�&test_urlencode_encoding_safe_parameter>s4			z6urlencode_Tests.test_urlencode_encoding_safe_parameterN)r-r.r/rIrPrQrSrUrVrXrZr`rlrrr~rrrrrB�s 
%rBc@sOeZdZdd�Zdd�Zejejdkd�dd��Z	d	S)
�Pathname_TestscCs�tjjdddd�}d}tjj|�}|j||d||f�tjj|�}|j||d||f�dS)N�partsZofrWr�zparts/of/a/pathzpathname2url() failed; %s != %szurl2pathame() failed; %s != %s)rWr�rrrrrdr)rZ
expected_pathZexpected_urlr�rrrr�iszPathname_Tests.test_basiccCs:tjjddd�}dtjjd�}tjj|�}|j||d||f�|}tjj	|�}|j||d||f�tjjdd�}d	tjjd�}tjj|�}|j||d||f�d
}tjjdd�}tjj	|�}|j||d||f�dS)
NZneedszquot=ing�herez
needs/%s/herezpathname2url() failed; %s != %szurl2pathname() failed; %s != %sz	make sureZusing_quotez%s/using_quotezmake+sure/using_unquotez	make+sureZ
using_unquote)
rWr�rrrrrrrdr)rr&rr�rrrrSvs.zPathname_Tests.test_quoting�win32z.test specific to the urllib.url2path function.cCs�d	}d}x=|D]5}tjj|�}|j||d||f�qWd}d}tjj|�}|j||d||f�dS)
N�/C:/�///C:/�/C|//zC:\z/urllib.request..url2pathname() failed; %s != %sz
///C|/pathzC:\pathz.urllib.request.url2pathname() failed; %s != %s)r�r�r�)rrrrd)rr&rrr�rrr�test_ntpath�s
zPathname_Tests.test_ntpathN)
r-r.r/r�rSr�r��sys�platformr�rrrrrfs
rc@seZdZdd�ZdS)�
Utility_TestscCs|jtjj�t�dS)N)rhrrZthishost�tuple)rrrr�
test_thishost�szUtility_Tests.test_thishostN)r-r.r/r�rrrrr��sr�c@s(eZdZdd�Zdd�ZdS)�URLopener_Testsc
CsuGdd�dtjj�}tjdtf��=|j|�jd�d�|j|�jd�d�WdQRXdS)Nc@seZdZdd�ZdS)z8URLopener_Tests.test_quoted_open.<locals>.DummyURLopenercSs|S)Nr)rrrrr�	open_spam�szBURLopener_Tests.test_quoted_open.<locals>.DummyURLopener.open_spamN)r-r.r/r�rrrr�DummyURLopener�sr�z8DummyURLopener style of invoking requests is deprecated.zspam://example/ /z//example/%20/z,spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/z'//c:|windows%/:=&?~#+!$,;'@()*[]|/path/)rrr�rrrrdr)rr�rrr�test_quoted_open�s

z URLopener_Tests.test_quoted_opencCs�Gdd�dtjj�}tjdd���tjdt�x�dD]�}|jttjj	|�|jttjj�j
|�|jttjj�j|�|jt|�j
|�|jt|�j|�qFWWdQRXdS)	Nc@seZdZdd�ZdS)z<URLopener_Tests.test_local_file_open.<locals>.DummyURLopenercSs|S)Nr)rrrrr�open_local_file�szLURLopener_Tests.test_local_file_open.<locals>.DummyURLopener.open_local_fileN)r-r.r/r�rrrrr��sr��recordTr@�local_file://example�local-file://example)r�r�)rrr��warnings�catch_warnings�simplefilterrrtr�rrZretrieve)rr�rrrr�test_local_file_open�s
z$URLopener_Tests.test_local_file_openN)r-r.r/r�r�rrrrr��sr�c@s(eZdZdd�Zdd�ZdS)�RequestTestscCsWtjj}|d�}|j|j�d�|di�}|j|j�d�dS)Nzhttp://www.python.orgr�ZPOST)rr�Requestrd�
get_method)rr�rrrr�test_default_valuess
z RequestTests.test_default_valuescCs�tjj}|ddd�}|j|jd�|j|j�d�|didd�}|j|jd�|j|j�d�|ddd�}|j|j�d�d|_|j|j�d�dS)Nzhttp://www.python.org�methodZHEADr�)rrr�rdr�r�)rr�rrrr�test_with_method_arg&s	z!RequestTests.test_with_method_argN)r-r.r/r�r�rrrrr�sr�c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�URL2PathNameTestscCsF|jtd�d�|jtd�d�|jtd�d�dS)Nz///C|zC:z///C:z///C|/zC:\)rdr)rrrr�test_converting_drive_letter6sz.URL2PathNameTests.test_converting_drive_lettercCs0|jtd�d�|jtd�d�dS)Nz
///C/test/z
\\\C\test\z////C/test/z	\\C\test\)rdr)rrrr�$test_converting_when_no_drive_letter;sz6URL2PathNameTests.test_converting_when_no_drive_lettercCs|jtd�d�dS)Nz///C|/foo/bar/spam.foozC:\foo\bar\spam.foo)rdr)rrrr�test_simple_compare@sz%URL2PathNameTests.test_simple_comparecCs|jttd�dS)Nu///è|/)rt�IOErrorr)rrrr�test_non_ascii_drive_letterDsz-URL2PathNameTests.test_non_ascii_drive_lettercCs@dddg}x*|D]"}|jtt|��|�qWdS)NzC:z\\\C\test\\zC:\foo\bar\spam.foo)rdrr)r�
list_of_pathsr�rrr�test_roundtrip_url2pathnameGs
	
z-URL2PathNameTests.test_roundtrip_url2pathnameN)r-r.r/r�r�r�r�r�rrrrr�4s
r�c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�PathName2URLTestscCs0|jtd�d�|jtd�d�dS)NzC:z///C:zC:\)rdr)rrrrr�Qsz.PathName2URLTests.test_converting_drive_lettercCsF|jtd�d�|jtd�d�|jtd�d�dS)Nz\\\folder\test\z/////folder/test/z\\folder\test\z////folder/test/z
\folder\test\z
/folder/test/)rdr)rrrrr�Usz6PathName2URLTests.test_converting_when_no_drive_lettercCs|jtd�d�dS)NzC:\foo\bar\spam.fooz///C:/foo/bar/spam.foo)rdr)rrrrr�]sz%PathName2URLTests.test_simple_comparecCs|jttd�dS)NzXX:\)rtr�r)rrrr�test_long_drive_letterasz(PathName2URLTests.test_long_drive_lettercCs@dddg}x*|D]"}|jtt|��|�qWdS)Nz///C:z/////folder/test/z///C:/foo/bar/spam.foo)rdrr)rr�r�rrr�test_roundtrip_pathname2urlds
	
z-PathName2URLTests.test_roundtrip_pathname2urlN)r-r.r/r�r�r�r�r�rrrrr�Os
r��__main__)5Zurllib.parserZurllib.requestZurllib.errorZhttp.clientr5Z
email.messagerlr'r�Z
unittest.mockrr�rrWr��ImportErrorr�r�r�Z
nturl2pathrr�base64rr�rrrrr8rFr9r<ZTestCaserKrwr�r�r�r�r
rr0rBrr�r�r�r�r�r-�mainrrrr�<module>sX
'	S6$�J�)���<r