Your IP : 216.73.216.213


Current Path : /opt/alt/python34/lib64/python3.4/test/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/test/__pycache__/test_urllib.cpython-34.pyo

�
g f��@s�dZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZddlZddlZddlZddlZddlmZmZddlmZddlZdd�Zdaddd	d
�Zdd�ZGd
d�de�ZGdd�de�ZGdd�de
j �Z!Gdd�de
j �Z"Gdd�de
j ee�Z#Gdd�de
j �Z$Gdd�de
j �Z%Gdd�de
j e�Z&Gdd�de
j �Z'Gdd �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 �Z.Gd-d.�d.e
j �Z/e0d/kr�e
j1�ndS)0zRegresssion tests for urllib�N)�patch)�support)�url2pathname�pathname2url)�	b64encodecCsItt|��dd�j�}t|�dkrAd|}nd|S)z!Escape char as RFC 2396 specifies�N�z0%s�%)�hex�ord�upper�len)�charZhex_repr�r�5/opt/alt/python34/lib64/python3.4/test/test_urllib.py�	hexescapes"
rc	Cs�|dk	r$tjjd|�}n@ts^tjdtf��tjj�}WdQX|ant}|dkr}|j|�S|j||�SdS)z.urlopen(url [, data]) -> open file-like objectN�proxiesz8FancyURLopener style of invoking requests is deprecated.)�urllib�requestZFancyURLopener�
_urlopenerr�check_warnings�DeprecationWarning�open)�url�datarZopenerrrr�urlopen!s
	
rcsHG�fdd�dtj��G��fdd�dtjj���S)Ncs^eZdZdZ�fdd�Zdd�Zddd�Zdd	d
�Zdd�ZdS)
zfakehttp.<locals>.FakeSocketrcs
|�_dS)N)�buf)�selfr)�FakeHTTPConnectionrr�sendall8sz$fakehttp.<locals>.FakeSocket.sendallc_s|jd7_|S)Nr)�io_refs)r�args�kwdsrrr�makefile;sz%fakehttp.<locals>.FakeSocket.makefileNcSs |jr
dStjj||�S)N�)�closed�io�BytesIO�read)rZamtrrrr(?s	z!fakehttp.<locals>.FakeSocket.readcSs |jr
dStjj||�S)Nr$)r%r&r'�readline)rZlengthrrrr)Ds	z%fakehttp.<locals>.FakeSocket.readlinecSs5|jd8_|jdkr1tjj|�ndS)Nrr)r r&r'�close)rrrrr*Isz"fakehttp.<locals>.FakeSocket.close)	�__name__�
__module__�__qualname__r rr#r(r)r*r)rrr�
FakeSocket5sr.cs.eZdZdZ���Zdd�ZdS)z$fakehttp.<locals>.FakeHTTPConnectionNcSs|j|_dS)N)�fakesockZsock)rrrr�connectTsz,fakehttp.<locals>.FakeHTTPConnection.connect)r+r,r-rr/r0r)r.�fakedatarrrNsr)r&r'�http�client�HTTPConnection)r1r)rr.r1r�fakehttp4s%	r5c@s(eZdZdd�Zdd�ZdS)�
FakeHTTPMixincCs%tjj|_t|�tj_dS)N)r2r3r4�_connection_classr5)rr1rrrr5[szFakeHTTPMixin.fakehttpcCs|jtj_dS)N)r7r2r3r4)rrrr�
unfakehttp_szFakeHTTPMixin.unfakehttpN)r+r,r-r5r8rrrrr6Zsr6c@s(eZdZdd�Zdd�ZdS)�FakeFTPMixincCs5Gdd�dt�}tjj|_|tj_dS)Nc@s:eZdZdddd�Zdd�Zdd�ZdS)	z,FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapperNTcSsdS)Nr)r�userZpasswd�hostZport�dirsZtimeoutZ
persistentrrr�__init__fsz5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.__init__cSstj�dfS)Nr)r&r')r�file�typerrr�retrfilejsz5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfilecSsdS)Nr)rrrrr*msz2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close)r+r,r-r=r@r*rrrr�FakeFtpWrapperesrA)�objectrr�
ftpwrapper�_ftpwrapper_class)rrArrr�fakeftpdszFakeFTPMixin.fakeftpcCs|jtj_dS)N)rDrrrC)rrrr�	unfakeftpsszFakeFTPMixin.unfakeftpN)r+r,r-rErFrrrrr9csr9c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�urlopen_FileTestsz�Test urlopen() opening a temporary file.

    Try to test as much functionality as possible so as to cut down on reliance
    on connecting to the Net for testing.

    cCsvtd|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�setUpszurlopen_FileTests.setUpcCs!|jj�tjtj�dS)zShut down the open objectN)rPr*�os�removerrM)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)	zreadzreadlinez	readlineszfilenozclosezinfozgeturlzgetcodez__iter__)�
assertTrue�hasattrrP)r�attrrrr�test_interface�s

z urlopen_FileTests.test_interfacecCs |j|j|jj��dS)N)�assertEqualrLrPr()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)r`rLrPr))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)rPrVr`r
rL)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)	rPrW�assertIsInstance�intr`rSr(r
rL)rZfile_numrrr�test_fileno�s$zurlopen_FileTests.test_filenocCs|jj�dS)N)rPr*)rrrr�
test_close�szurlopen_FileTests.test_closecCs#|j|jj�tjj�dS)N)rdrPrX�email�message�Message)rrrr�	test_info�szurlopen_FileTests.test_infocCs |j|jj�|j�dS)N)r`rPrYrO)rrrr�test_geturl�szurlopen_FileTests.test_geturlcCs|j|jj��dS)N)ZassertIsNonerPrZ)rrrr�test_getcode�szurlopen_FileTests.test_getcodecCs+x$|jD]}|j||j�q
WdS)N)rPr`rL)r�linerrr�	test_iter�szurlopen_FileTests.test_itercCs$|jttjjd|j�dS)Nz./)�assertRaises�
ValueErrorrrrrO)rrrr�test_relativelocalfile�sz(urlopen_FileTests.test_relativelocalfileN)r+r,r-�__doc__rRrUr_rarbrcrfrgrkrlrmrorrrrrrrGws	rGc@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�
ProxyTestscCsRtj�|_x<ttj�D]+}d|j�kr|jj|�qqWdS)N�proxy)rZEnvironmentVarGuard�env�listrS�environ�lower�unset)r�krrrrR�szProxyTests.setUpcCs|jj�|`dS)N)rv�__exit__)rrrrrU�s
zProxyTests.tearDowncCsf|jjdd�tjj�}|jd|d�|jjdd�|jtjjd��dS)NZNO_PROXY�	localhost�noz+localhost, anotherdomain.com, newdomain.comzanotherdomain.com)rv�setrr�getproxies_environmentr`r\Zproxy_bypass_environment)rrrrr�+test_getproxies_environment_keep_no_proxies�s
z6ProxyTests.test_getproxies_environment_keep_no_proxiescCs�zl|jjdd�tjj�}|jd|d�|jjdd�tjj�}|jd|�Wd|jjd�|jjd�XdS)NZ
HTTP_PROXYzhttp://somewhere:3128r2ZREQUEST_METHOD�GET)rvrrrr�r`ZassertNotInrz)rrrrr�test_proxy_cgi_ignore�sz ProxyTests.test_proxy_cgi_ignoreN)r+r,r-rRrUr�r�rrrrrt�s	rtc@seZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zejejdd�dd��Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(S))�urlopen_HttpTestsz.Test urlopen() opening a fake http connection.c
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$��)r5rr`r)rYrZr8)rZver�fprrr�
check_read�s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!)r5rrrr`rYr8)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)r5rr\r�Z
will_closer8)rZresprrr�test_willcloses

z urlopen_HttpTests.test_willclosecCs|jd�dS)Ns0.9)r�)rrrr�
test_read_0_9szurlopen_HttpTests.test_read_0_9cCs|jd�dS)Ns1.0)r�)rrrr�
test_read_1_0szurlopen_HttpTests.test_read_1_0cCs|jd�dS)Ns1.1)r�)rrrr�
test_read_1_1szurlopen_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/)r5rp�OSErrorrr8)rrrr�test_read_boguss
z!urlopen_HttpTests.test_read_bogusc
Cs<|jd�z|jtjjtd�Wd|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
zhttp://python.org/)r5rpr�errorZ	HTTPErrorrr8)rrrr�test_invalid_redirect&sz'urlopen_HttpTests.test_invalid_redirectc
Cs6|jd�z|jttd�Wd|j�XdS)Nr$zhttp://something)r5rpr�rr8)rrrr�test_empty_socket5s
z#urlopen_HttpTests.test_empty_socketc	CsR|jtjj��}td�WdQX|j|jj�|j|jj�dS)Nz/file://localhost/a/file/which/doesnot/exists.py)	rprr��URLErrorrr\�	exception�filename�reason)r�errr�test_missing_localfile>sz(urlopen_HttpTests.test_missing_localfilecCs�tj�\}}d|jtjjd�}z?|jtjj|��t|��}|j|�WdQXWdtj	|�tj
|�X|jtjj|��|jt
jj��t|�WdQXdS)Nzfile://localhost/�/)�tempfile�mkstemp�replacerS�path�sepr\�existsrr*�unlink�assertFalserprr�r�)r�fdZtmp_fileZtmp_fileurlZfobjrrr�test_file_notexistsEs
z%urlopen_HttpTests.test_file_notexistsc	CsXd}|jtjj��}t|�WdQX|j|jj�|j|jj	�dS)Nzftp:///path)
rprr�r�rr�r�r�r\r�)rZtest_ftp_urlr�rrr�test_ftp_nohostSs
z!urlopen_HttpTests.test_ftp_nohostc	CsR|jtjj��}td�WdQX|j|jj�|j|jj	�dS)Nz.ftp://localhost/a/file/which/doesnot/exists.py)
rprr�r�rr�r�r�r\r�)rr�rrr�test_ftp_nonexistingZsz&urlopen_HttpTests.test_ftp_nonexistingZMAXFTPCACHErcCsR|j�z6tjjddddg�tjjd<td�Wd|j�XdS)Nr:�passr}��testzftp://localhost)rErrrCZftpcacherrF)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�)r5rr`r)rYrZr8)rr�rrr�test_userpass_inurljs
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�)r5�formatr2r3r4r�encode�decoder�assertInrr`r)ZassertNotEqualrYrZr8)rZuserpassrZfakehttp_wrapperZ
authorizationr�rrr�test_userpass_inurl_w_spacesus
z.urlopen_HttpTests.test_userpass_inurl_w_spacescCs-tjdtf��tjj�WdQXdS)N�)rrrrr�	URLopener)rrrr�test_URLopener_deprecation�sz,urlopen_HttpTests.test_URLopener_deprecationc
CsBtj�}|jt��!tjjdddd|�WdQXdS)Nzhttps://localhostZcafilez/nonexistent/path�context)�sslZcreate_default_contextrprqrrr)rr�rrr�test_cafile_and_context�s	z)urlopen_HttpTests.test_cafile_and_contextN)r+r,r-rsr�r�r�r�r�r�r�r�r�r�r�r�r�rrBrrr�r�r�r�r�rrrrr��s&

	$
r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)�urlopen_DataTestsz"Test urlopen() opening a data URL.cCsyd|_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)rL�image�text_url�text_url_base64�	image_urlrrr�
text_url_resp�text_url_base64_resp�image_url_resp)rrrrrR�s						zurlopen_DataTests.setUpcCs5x.d
D]&}|jt|j|�d	|�qWdS)Nr(r)rVr*rXrYrZr[z/object returned by urlopen() lacks %s attribute)zreadzreadlinez	readlineszclosezinfozgeturlzgetcodez__iter__)r\r]r�)rr^rrrr_�s

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�)�charsetr�)r�r�)r�r�)rdr�rXrhrirjr`r��
get_paramsr��strr
r�rrr)rrrrrk�s
!zurlopen_DataTests.test_infocCsX|j|jj�|j�|j|jj�|j�|j|jj�|j�dS)N)r`r�rYr�r�r�r�r�)rrrrrl�s
zurlopen_DataTests.test_geturlcCsB|j|jj�jt|jj�j��d�|j�dS)Nr�)r`r�r(r��dictrXr�rL)rrrr�test_read_text�sz urlopen_DataTests.test_read_textcCsB|j|jj�jt|jj�j��d�|j�dS)Nr�)r`r�r(r�r�rXr�rL)rrrr�test_read_text_base64�sz'urlopen_DataTests.test_read_text_base64cCs |j|jj�|j�dS)N)r`r�r(r�)rrrr�test_read_image�sz!urlopen_DataTests.test_read_imagecCs|jttjjd�dS)Nzdata:text/plain)rprqrrr)rrrr�test_missing_comma�sz$urlopen_DataTests.test_missing_commacCs|jttjjd�dS)Nzdata:;base64,Cg=)rprqrrr)rrrr�test_invalid_base64_data�sz*urlopen_DataTests.test_invalid_base64_dataN)
r+r,r-rsrRr_rkrlr�r�r�r�r�rrrrr��s	r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�urlretrieve_FileTestsz(Test urllib.urlretrieve() on local filescCssg|_|jtj�d|_z0ttjd�}|j|j�|j�Wdy|j�WnYnXXdS)Nstesting urllib.urlretrieverI)�	tempFiles�registerFileForCleanUprrMrLrrNr*)r�FILErrrrR�s		zurlretrieve_FileTests.setUpc
Cs4x-|jD]"}ytj|�Wq
Yq
Xq
WdS)N)r�rSrT)rZeachrrrrU�s
zurlretrieve_FileTests.tearDowncCs[tjj|�}y|jd�Wn!tk
rFtjd��YnXdtjj	|�S)Nzutf-8z!filePath is not encodable to utf8z	file://%s)
rSr��abspathr��UnicodeEncodeError�unittestZSkipTestrrr)rZfilePathrrr�constructLocalFileUrl�s
z+urlretrieve_FileTests.constructLocalFileUrlr$cCsmtj�\}}z:|j|�tj|d�}|j|�|j�Wdy|j�WnYnXX|S)z�Creates a new temporary file containing the specified data,
        registers the file for deletion during the test fixture tear down, and
        returns the absolute path of the file.rIN)r�r�r�rS�fdopenrNr*)rrZnewFdZnewFilePathZnewFilerrr�createNewTempFiles

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�urlretrieverrMr`rdrhrirj)r�resultrrr�
test_basicsz 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)rrMr�rrr�r�r`r\rSr�r�rr(r*rL)r�second_tempr�r�rLrrr�	test_copys

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

	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�AszAurlretrieve_FileTests.test_reporthook_0_bytes.<locals>.hooktesterrrr)	r�rrr�r�rrMr`r
)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�r�rrrr�NszAurlretrieve_FileTests.test_reporthook_5_bytes.<locals>.hooktester�x�rrrsxxxxx)	r�rrr�r�rrMr`r
)rr�r�r�rrr�test_reporthook_5_bytesIs
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�r�rrrr�\szDurlretrieve_FileTests.test_reporthook_8193_bytes.<locals>.hooktesterr�i �rrri )	r�rrr�r�rrMr`r
)rr�r�r�rrr�test_reporthook_8193_bytesWs
z0urlretrieve_FileTests.test_reporthook_8193_bytesN)r+r,r-rsrRrUr�r�r�r�r�r�r�r�r�rrrrr��s	r�c@s.eZdZdZdd�Zdd�ZdS)�urlretrieve_HttpTestsz5Test urllib.urlretrieve() using fake http connectionscCsa|jd�dd�}|jtjj��-ztjjdd|�Wd|j�XWdQXdS)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�_reporthookvszYurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError.<locals>._reporthookzhttp://example.com/Z
reporthook)r5rprr��ContentTooShortErrorrr�r8)rr�rrr�.test_short_content_raises_ContentTooShortErrorkszDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorcCsO|jd�|jtjj��'ztjjd�Wd|j�XWdQXdS)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/)r5rprr�r�rr�r8)rrrr�Atest_short_content_raises_ContentTooShortError_without_reporthook�szWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookN)r+r,r-rsr�r�rrrrr�hsr�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)�QuotingTestsauTests for urllib.quote() and urllib.quote_plus()

    According to RFC 2396 (Uniform Resource Identifiers), to escape a
    character you write it as '%' + <2 character US-ASCII hex value>.
    The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
    character properly. Case does not matter on the hex letters.

    The various character sets specified are:

    Reserved characters : ";/?:@&=+$,"
        Have special meaning in URIs and must be escaped if not being used for
        their special meaning
    Data characters : letters, digits, and "-_.!~*'()"
        Unreserved and do not need to be escaped; can be, though, if desired
    Control characters : 0x00 - 0x1F, 0x7F
        Have no use in URIs so must be escaped
    space : 0x20
        Must be escaped
    Delimiters : '<>#%"'
        Must be escaped
    Unwise : "{}|\^[]`"
        Must be escaped

    cCs}djddddg�}tjj|�}|j||d||f�tjj|�}|j||d||f�dS)Nr�ZABCDEFGHIJKLMNOPQRSTUVWXYZZabcdefghijklmnopqrstuvwxyz�
0123456789z_.-zusing quote(): %r != %rzusing quote_plus(): %r != %r)�joinr�parse�quoter`�
quote_plus)rZdo_not_quoter�rrr�test_never_quote�s	zQuotingTests.test_never_quotecCs!|jtjjjdd�dS)Nrr�)r`rr�r��__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�)rr�r�r`r�)rZquote_by_defaultr��expectrrr�	test_safe�s,	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)�chr)�.0�numrrr�
<listcomp>�s	z5QuotingTests.test_default_quoting.<locals>.<listcomp>� z
<>#%"{}|\^[]`�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)
�ranger�r�r�rr�r�r`rr�)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)N� zusing quote(): %r != %r�+zusing quote_plus(): %r != +z
a b cd e fzusing quote_plus(): %r != %r)rr�r�r`rr�r�)rr��givenr�rrr�test_quoting_space�szQuotingTests.test_quoting_spacecCsz|jtjjd�d�|jtjjdd�d�|jtjjd�d�|jtjjdd�d�dS)Nzalpha+beta gammazalpha%2Bbeta+gammarzalpha+beta+gammasalpha+beta gamma�+)r`rr�r�)rrrr�test_quoting_plusszQuotingTests.test_quoting_pluscCs�d}d}tjj|�}|j||d||f�|jttjj|dd�tjj|�}|j||d||f�dS)Ns��ab�z%A2%D8ab%FFzusing quote(): %r != %rr�zlatin-1z"using quote_from_bytes(): %r != %r)rr�r�r`rp�	TypeErrorZquote_from_bytes)rrr�r�rrr�test_quote_bytess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)rr�r�r`rpr�)rrr�r�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+%FFr�zlatin-1zusing quote_plus(): %r != %ruab漢字 cdzab%3F%3F+cdrr�)rr�r�r`)rrr�r�rrr�test_quote_plus_with_unicodeNs	z)QuotingTests.test_quote_plus_with_unicodeN)
r+r,r-rsr�r�r�rr	rr
rrrrrrr��s
*r�c@sjeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�UnquotingTestszwTests for unquote() and unquote_plus()

    See the doc string for quoting_Tests for details on quoting and such.

    cCsmg}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
�WdQXdS)N�zusing unquote(): %r != %rzusing unquote_plus(): %r != %rr�r	rz/using unquote(): not all characters escaped: %s�quietTr$)rrr�rr��unquoter`�unquote_plusr�r��countrpr�AttributeErrorrr�BytesWarning)rZescape_listr�rr�r�Z
escape_stringrrr�test_unquotinges,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	rHz"using unquote_to_bytes(): %r != %r)	rr�rr`rJ�unquote_to_bytesrprr)rrr�r�rrr�test_unquoting_badpercent~s@z(UnquotingTests.test_unquoting_badpercentcCs?d}d}tjj|�}|j||d||f�dS)Nz%Ab%eAs��z"using unquote_to_bytes(): %r != %r)rr�rr`)rrr�r�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)rrr�rr`r)rrr�r�rrr�test_unquoting_parts�sz#UnquotingTests.test_unquoting_partscCs�d}|}tjj|�}|j||d||f�|jdd�}tjj|�}|j||d||f�dS)Nzare+there+spaces...zusing unquote(): %r != %rrrzusing unquote_plus(): %r != %r)rr�rr`r�r)rrr�r�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)rr�rr`)rrr�r�rrr�test_unquote_to_bytes�s.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 != %rr�rzbr%FCckner_sapporo_20050930.doczlatin-1z%E6%BC%A2%E5%AD%97u漢字z%F3%B1u�r�r��ignoreu	漢%C3%BCu漢üu漢%FC)rr�rr`)rrr�r�rrr�test_unquote_with_unicode�sN	z(UnquotingTests.test_unquote_with_unicodeN)r+r,r-rsrrrrr r!r#rrrrr^s$	rc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�urlencode_TestszTests for urlencode()c	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�d
S)aHelper method for testing different input types.

        'given' must lead to only the pairs:
            * 1st, 1
            * 2nd, 2
            * 3rd, 3

        Test cannot assume anything about order.  Docs make no guarantee and
        have possible dictionary input.

        z1st=1z2nd=2z3rd=3ztesting %s: %s not found in %s�&rz#testing %s: expected 2 '&'s; got %srz1testing %s: '&' not located in proper place in %sr�r�z5testing %s: unexpected number of characters: %s != %sN��r&r')
rr��	urlencoder�r`r�indexr\�isdigitr
)	rrZ	test_typeZexpect_somewherer�rZamp_locationZon_amp_leftZon_amp_rightrrr�help_inputtypes$
zurlencode_Tests.help_inputtypecCs)|jidd6dd6dd6d�dS)N�1�1st�2�2nd�3�3rdzusing dict as input type)r+)rrrr�test_using_mapping?sz"urlencode_Tests.test_using_mappingcCs|jdd	d
gd�dS)Nr-r,r/r.r1r0z*using sequence of two-item tuples as input)z1str,)z2ndr.)z3rdr0)r+)rrrr�test_using_sequenceDsz#urlencode_Tests.test_using_sequencecCs�idd6}dtd�td�f}tjj|�}|j||�idd6}d}tjj|�}|j||�dS)N�=r%z%s=%szA bunch of pluseszkey namezkey+name=A+bunch+of+pluses)rrr�r(r`)rrr�r�rrr�test_quotingIs

zurlencode_Tests.test_quotingcCs�idddgd6}dtjjtdddg��}tjj|�}|j||�tjj|d�}x,|dD] }d|}|j||�q}W|j|jd�dd	|jd��dS)
Nr,r.r0Zsequencezsequence=%sTr%rzExpected 2 '&'s, got %s)rr�r�r�r(r`r�r)rrr�r��valuerrr�
test_doseqTs%
zurlencode_Tests.test_doseqcCs<|jdtjji��|jdtjjg��dS)Nr�)r`rr�r()rrrr�test_empty_sequenceasz#urlencode_Tests.test_empty_sequencecCsJ|jdtjjidd6��|jdtjjidd6��dS)Nza=1r�aza=None)r`rr�r()rrrr�test_nonstring_valueses#z%urlencode_Tests.test_nonstring_valuescCs�|jdtjjiddgd6d��|jdtjjiddgd6d��tjd	d
g�}|jdtjji|d6d��dS)Nza=1&a=2rrr9Tz
a=None&a=a�bza=a&a=b)r9r)r;r)r`rr�r(�collections�OrderedDict)rrrrr�test_nonstring_seq_valuesis,	#	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=%3Fr�r�rr�z
%C2%A0=%C3%81z%A0=%C1zlatin-1�r?r@)rA�r?r@)rB�r?r@)rC)rr�r(r`)rrr�r�rrr�test_urlencode_encodingqsz'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�doseqTr�r�rr�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@)rG�rr@�r?rH)rI�r?r@)rJ�rFr@�r?rK)rL�r?r@)rM�rFr@�r?rN)rO)rr�r(r`)rrr�r�rrr�test_urlencode_encoding_doseq�s4z-urlencode_Tests.test_urlencode_encoding_doseqcCs�d}d}tjj|�}|j||�tjj|d�}|j||�d}d}tjj|d�}|j||�dS)N��$��$z
%A0%24=%C1%24TrFz%A0%24=42&%A0%24=%C1%24�rQrR)rS�rFrR�rQrT)rU)rr�r(r`)rrr�r�rrr�test_urlencode_bytes�sz$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��$��$r�z:$z	%A0$=%C1$rET�
rFz%A0$=%C1$&%A0$=13&%A0$=42r�zlatin-1�rWrX)rZ�rWrX)r[�rXrYrF�rWr\)r]�rWrX)r^�rWrX)r_�rXrYrF�rWr`)ra)rr�r(r`)rrr�r�rrr�&test_urlencode_encoding_safe_parameter�s4			z6urlencode_Tests.test_urlencode_encoding_safe_parameterN)r+r,r-rsr+r2r3r5r7r8r:r>rDrPrVrbrrrrr$s 
%r$c@sUeZdZdZdd�Zdd�Zejej	dkd�dd	��Z
d
S)�Pathname_Testsz&Test pathname2url() and url2pathname()cCs�tjjdddd�}d}tjj|�}|j||d||f�tjj|�}|j||d||f�dS)N�partsZofr9r�zparts/of/a/pathzpathname2url() failed; %s != %szurl2pathame() failed; %s != %s)rSr�r�rrrr`r)rZ
expected_pathZexpected_urlr�rrrr��s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)
rSr�r�rr�r�rrr`r)rrr�r�rrrr5�s.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)rgrhri)rrrr`)rrr�rr�rrr�test_ntpath
s
zPathname_Tests.test_ntpathN)r+r,r-rsr�r5r�Z
skipUnless�sys�platformrjrrrrrc�s

rcc@s"eZdZdZdd�ZdS)�
Utility_Testsz=Testcase to test the various utility functions in the urllib.cCs|jtjj�t�dS)zATest the urllib.request.thishost utility function returns a tupleN)rdrrZthishost�tuple)rrrr�
test_thishostszUtility_Tests.test_thishostN)r+r,r-rsrorrrrrmsrmc@s"eZdZdZdd�ZdS)�URLopener_Testsz4Testcase to test the open method of URLopener class.c
CstGdd�dtjj�}tjdtf��=|j|�jd�d�|j|�jd�d�WdQXdS)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-rqrrrr�DummyURLopener'srrz8DummyURLopener style of invoking requests is deprecated.zspam://example/ /z//example/%20/z,spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/z'//c:|windows%/:=&?~#+!$,;'@()*[]|/path/)rrr�rrrr`r)rrrrrr�test_quoted_open&s

z URLopener_Tests.test_quoted_openN)r+r,r-rsrsrrrrrp#srpc@s.eZdZdZdd�Zdd�ZdS)�RequestTestsz&Unit tests for urllib.request.Request.cCsWtjj}|d�}|j|j�d�|di�}|j|j�d�dS)Nzhttp://www.python.orgr�ZPOST)rr�Requestr`�
get_method)rrurrrr�test_default_values�s
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�)rrrur`rxrv)rrurrrr�test_with_method_arg�s	z!RequestTests.test_with_method_argN)r+r,r-rsrwryrrrrrt�srtc@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�URL2PathNameTestscCsF|jtd�d�|jtd�d�|jtd�d�dS)Nz///C|zC:z///C:z///C|/zC:\)r`r)rrrr�test_converting_drive_letter�sz.URL2PathNameTests.test_converting_drive_lettercCs0|jtd�d�|jtd�d�dS)Nz
///C/test/z
\\\C\test\z////C/test/z	\\C\test\)r`r)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)r`r)rrrr�test_simple_compare�sz%URL2PathNameTests.test_simple_comparecCs|jttd�dS)Nu///è|/)rp�IOErrorr)rrrr�test_non_ascii_drive_letter�sz-URL2PathNameTests.test_non_ascii_drive_lettercCs@dddg}x*|D]"}|jtt|��|�qWdS)NzC:z\\\C\test\\zC:\foo\bar\spam.foo)r`rr)r�
list_of_pathsr�rrr�test_roundtrip_url2pathname�s
	
z-URL2PathNameTests.test_roundtrip_url2pathnameN)r+r,r-r{r|r}rr�rrrrrz�s
rzc@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�PathName2URLTestscCs0|jtd�d�|jtd�d�dS)NzC:z///C:zC:\)r`r)rrrrr{�sz.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/)r`r)rrrrr|�sz6PathName2URLTests.test_converting_when_no_drive_lettercCs|jtd�d�dS)NzC:\foo\bar\spam.fooz///C:/foo/bar/spam.foo)r`r)rrrrr}�sz%PathName2URLTests.test_simple_comparecCs|jttd�dS)NzXX:\)rpr~r)rrrr�test_long_drive_letter�sz(PathName2URLTests.test_long_drive_lettercCs@dddg}x*|D]"}|jtt|��|�qWdS)Nz///C:z/////folder/test/z///C:/foo/bar/spam.foo)r`rr)rr�r�rrr�test_roundtrip_pathname2url�s
	
z-PathName2URLTests.test_roundtrip_pathname2urlN)r+r,r-r{r|r}r�r�rrrrr��s
r��__main__)2rsZurllib.parserZurllib.requestZurllib.errorZhttp.clientr2Z
email.messagerhr&r�Z
unittest.mockrr�rrSr�rkr�Z
nturl2pathrr�base64rr<rrrr5rBr6r9ZTestCaserGrtr�r�r�r�r�rr$rcrmrprtrzr�r+�mainrrrr�<module>sN&	R%�I�)���<a