B
    u9až	 ã               @   s@  d Z ddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZ ddlZyddlZW n ek
r€   dZY nX ddlZddlZddlZddlmZmZ ddlmZ ddlZdd„ Zdad4d	d
„Zdd„ Zdd„ ZG dd„ de ƒZ!G dd„ de ƒZ"G dd„ de
j#ƒZ$G dd„ de
j#ƒZ%G dd„ de
j#ƒZ&G dd„ de
j#e!e"ƒZ'G dd„ de
j#ƒZ(G dd„ de
j#ƒZ)G dd „ d e
j#e!ƒZ*G d!d"„ d"e
j#ƒZ+G d#d$„ d$e
j#ƒZ,G d%d&„ d&e
j#ƒZ-G d'd(„ d(e
j#ƒZ.G d)d*„ d*e
j#ƒZ/G d+d,„ d,e
j#ƒZ0G d-d.„ d.e
j#ƒZ1G d/d0„ d0e
j#ƒZ2G d1d2„ d2e
j#ƒZ3e4d3kr<e
 5¡  dS )5z;Regression tests for what was in Python 2's "urllib" moduleé    N)Úpatch)Úsupport)Úurl2pathnameÚpathname2url)Ú	b64encodec             C   s4   t t| ƒƒdd…  ¡ }t|ƒdkr,d| }d| S )z!Escape char as RFC 2396 specifiesé   Né   z0%sú%)ÚhexÚordÚupperÚlen)ÚcharZhex_repr© r   ú!/usr/lib/python3.7/test_urllib.pyÚ	hexescape   s    r   c             C   sN   |dk	rt jj|d}nts(tƒ }|ant}|dkr>| | ¡S | | |¡S dS )z.urlopen(url [, data]) -> open file-like objectN)Úproxies)ÚurllibÚrequestÚFancyURLopenerÚ
_urlopenerÚopen)ÚurlÚdatar   Úopenerr   r   r   Úurlopen%   s    
r   c            	   C   s$   t  dtf¡ tj ¡ S Q R X d S )Nz8FancyURLopener style of invoking requests is deprecated.)r   Úcheck_warningsÚDeprecationWarningr   r   r   r   r   r   r   r   5   s    
r   c                s8   G ‡ fdd„dt jƒ‰G ‡fdd„dtjjƒ‰ | ˆ _ˆ S )Nc                   s@   e Zd ZdZ‡ fdd„Zdd„ Zddd„Zdd	d
„Zdd„ ZdS )zfakehttp.<locals>.FakeSocketr   c                s
   |ˆ _ d S )N)Úbuf)Úselfr   )ÚFakeHTTPConnectionr   r   Úsendall@   s    z$fakehttp.<locals>.FakeSocket.sendallc             _   s   |  j d7  _ | S )Nr   )Úio_refs)r   ÚargsÚkwdsr   r   r   ÚmakefileC   s    z%fakehttp.<locals>.FakeSocket.makefileNc             S   s   | j r
dS tj | |¡S )Nó    )ÚclosedÚioÚBytesIOÚread)r   Zamtr   r   r   r*   G   s    z!fakehttp.<locals>.FakeSocket.readc             S   s   | j r
dS tj | |¡S )Nr&   )r'   r(   r)   Úreadline)r   Úlengthr   r   r   r+   L   s    z%fakehttp.<locals>.FakeSocket.readlinec             S   s(   |  j d8  _ | j dkr$tj | ¡ d S )Nr   r   )r"   r(   r)   Úclose)r   r   r   r   r-   Q   s    
z"fakehttp.<locals>.FakeSocket.close)N)N)	Ú__name__Ú
__module__Ú__qualname__r"   r!   r%   r*   r+   r-   r   )r    r   r   Ú
FakeSocket=   s   

r1   c                   s   e Zd ZdZ‡ fdd„ZdS )z$fakehttp.<locals>.FakeHTTPConnectionNc                s   ˆ | j ƒ| _| jt| ƒ_d S )N)ÚfakedataZsockÚtypeZfakesock)r   )r1   r   r   Úconnect[   s    z,fakehttp.<locals>.FakeHTTPConnection.connect)r.   r/   r0   r   r4   r   )r1   r   r   r    V   s   r    )r(   r)   ÚhttpÚclientÚHTTPConnectionr2   )r2   r   )r    r1   r   Úfakehttp<   s    r8   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚFakeHTTPMixinc             C   s   t jj| _t|ƒt j_d S )N)r5   r6   r7   Ú_connection_classr8   )r   r2   r   r   r   r8   d   s    
zFakeHTTPMixin.fakehttpc             C   s   | j tj_d S )N)r:   r5   r6   r7   )r   r   r   r   Ú
unfakehttph   s    zFakeHTTPMixin.unfakehttpN)r.   r/   r0   r8   r;   r   r   r   r   r9   c   s   r9   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚFakeFTPMixinc             C   s&   G dd„ dt ƒ}tjj| _|tj_d S )Nc               @   s&   e Zd Zd	dd„Zdd„ Zdd„ ZdS )
z,FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapperNTc             S   s   d S )Nr   )r   ÚuserZpasswdÚhostZportÚdirsZtimeoutZ
persistentr   r   r   Ú__init__o   s    z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.__init__c             S   s   t  ¡ dfS )Nr   )r(   r)   )r   Úfiler3   r   r   r   Úretrfiles   s    z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfilec             S   s   d S )Nr   )r   r   r   r   r-   v   s    z2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close)NT)r.   r/   r0   r@   rB   r-   r   r   r   r   ÚFakeFtpWrappern   s    
rC   )Úobjectr   r   Ú
ftpwrapperÚ_ftpwrapper_class)r   rC   r   r   r   Úfakeftpm   s    
zFakeFTPMixin.fakeftpc             C   s   | j tj_d S )N)rF   r   r   rE   )r   r   r   r   Ú	unfakeftp|   s    zFakeFTPMixin.unfakeftpN)r.   r/   r0   rG   rH   r   r   r   r   r<   l   s   r<   c               @   sx   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú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.

    c             C   sX   t d| jj dƒ| _ttjdƒ}z| | j¡ W d | ¡  X tj| _	t
d| j	 ƒ| _d S )Nztest_urllib: %s
ÚasciiÚwbzfile:%s)ÚbytesÚ	__class__r.   Útextr   r   ÚTESTFNÚwriter-   Úpathnamer   Úreturned_obj)r   Úfr   r   r   ÚsetUpˆ   s    
zurlopen_FileTests.setUpc             C   s   | j  ¡  t tj¡ dS )zShut down the open objectN)rR   r-   ÚosÚremover   rO   )r   r   r   r   ÚtearDown”   s    
zurlopen_FileTests.tearDownc             C   s*   x$dD ]}|   t| j|ƒd| ¡ qW d S )N)	r*   r+   Ú	readlinesÚfilenor-   ÚinfoÚgeturlÚgetcodeÚ__iter__z/object returned by urlopen() lacks %s attribute)Ú
assertTrueÚhasattrrR   )r   Úattrr   r   r   Útest_interface™   s    
z urlopen_FileTests.test_interfacec             C   s   |   | j| j ¡ ¡ d S )N)ÚassertEqualrN   rR   r*   )r   r   r   r   Ú	test_read¡   s    zurlopen_FileTests.test_readc             C   s,   |   | j| j ¡ ¡ |   d| j ¡ d¡ d S )Nr&   zKcalling readline() after exhausting the file did not return an empty string)rb   rN   rR   r+   )r   r   r   r   Útest_readline¤   s    zurlopen_FileTests.test_readlinec             C   s4   | j  ¡ }|  t|ƒdd¡ |  |d | jd¡ d S )Nr   z.readlines() returned the wrong number of linesr   z"readlines() returned improper text)rR   rX   rb   r   rN   )r   Z
lines_listr   r   r   Útest_readlinesª   s
    
z urlopen_FileTests.test_readlinesc             C   s:   | j  ¡ }|  |td¡ |  t |t| jƒ¡| jd¡ d S )Nzfileno() did not return an intzTReading on the file descriptor returned by fileno() did not return the expected text)	rR   rY   ÚassertIsInstanceÚintrb   rU   r*   r   rN   )r   Zfile_numr   r   r   Útest_fileno±   s    
zurlopen_FileTests.test_filenoc             C   s   | j  ¡  d S )N)rR   r-   )r   r   r   r   Ú
test_close¸   s    zurlopen_FileTests.test_closec             C   s   |   | j ¡ tjj¡ d S )N)rf   rR   rZ   ÚemailÚmessageÚMessage)r   r   r   r   Ú	test_info½   s    zurlopen_FileTests.test_infoc             C   s   |   | j ¡ | j¡ d S )N)rb   rR   r[   rQ   )r   r   r   r   Útest_geturlÀ   s    zurlopen_FileTests.test_geturlc             C   s   |   | j ¡ ¡ d S )N)ZassertIsNonerR   r\   )r   r   r   r   Útest_getcodeÃ   s    zurlopen_FileTests.test_getcodec             C   s"   x| j D ]}|  || j¡ qW d S )N)rR   rb   rN   )r   Úliner   r   r   Ú	test_iterÆ   s    zurlopen_FileTests.test_iterc             C   s   |   ttjjd| j ¡ d S )Nz./)ÚassertRaisesÚ
ValueErrorr   r   r   rQ   )r   r   r   r   Útest_relativelocalfileÏ   s    z(urlopen_FileTests.test_relativelocalfileN)r.   r/   r0   Ú__doc__rT   rW   ra   rc   rd   re   rh   ri   rm   rn   ro   rq   rt   r   r   r   r   rI   €   s   	rI   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú
ProxyTestsc             C   s:   t  ¡ | _x*ttjƒD ]}d| ¡ kr| j |¡ qW d S )NÚproxy)r   ZEnvironmentVarGuardÚenvÚlistrU   ÚenvironÚlowerÚunset)r   Úkr   r   r   rT   Õ   s    
zProxyTests.setUpc             C   s   | j  ¡  | ` d S )N)rx   Ú__exit__)r   r   r   r   rW   Ý   s    
zProxyTests.tearDownc             C   sp   | j  dd¡ tj ¡ }|  d|d ¡ | j  dd¡ |  tj d¡¡ |  tj d¡¡ |  tj d¡¡ d S )NÚNO_PROXYÚ	localhostÚnoz0localhost, anotherdomain.com, newdomain.com:1234zanotherdomain.comzanotherdomain.com:8888znewdomain.com:1234)rx   Úsetr   r   Úgetproxies_environmentrb   r^   Úproxy_bypass_environment)r   r   r   r   r   Ú+test_getproxies_environment_keep_no_proxiesâ   s    
z6ProxyTests.test_getproxies_environment_keep_no_proxiesc          	   C   sp   zP| j  dd¡ tj ¡ }|  d|d ¡ | j  dd¡ tj ¡ }|  d|¡ W d | j  d¡ | j  d¡ X d S )NÚ
HTTP_PROXYzhttp://somewhere:3128r5   ZREQUEST_METHODÚGET)rx   r‚   r   r   rƒ   rb   ÚassertNotInr|   )r   r   r   r   r   Útest_proxy_cgi_ignoreí   s    

z ProxyTests.test_proxy_cgi_ignorec             C   s¦   t jj}| j dd¡ |  |dƒ¡ |  |dƒ¡ |  |dƒ¡ |  |dƒ¡ |  |dƒ¡ |  |dƒ¡ |  |d	ƒ¡ |  |d
ƒ¡ |  |dƒ¡ |  |dƒ¡ d S )Nr   z8localhost, anotherdomain.com, newdomain.com:1234, .d.o.tr€   Z	LocalHostZ	LOCALHOSTznewdomain.com:1234z	foo.d.o.tzanotherdomain.com:8888zwww.newdomain.com:1234Zprelocalhostznewdomain.comznewdomain.com:1235)r   r   r„   rx   r‚   r^   ÚassertFalse)r   Zbypassr   r   r   Ú(test_proxy_bypass_environment_host_matchù   s    z3ProxyTests.test_proxy_bypass_environment_host_matchN)r.   r/   r0   rT   rW   r…   r‰   r‹   r   r   r   r   rv   Ó   s
   rv   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚProxyTests_withOrderedEnvc             C   s   t j| _t ¡ t _d S )N)rU   rz   Ú
_saved_envÚcollectionsÚOrderedDict)r   r   r   r   rT     s    zProxyTests_withOrderedEnv.setUpc             C   s   | j t_d S )N)r   rU   rz   )r   r   r   r   rW     s    z"ProxyTests_withOrderedEnv.tearDownc             C   s  dt jd< dt jd< |  tj d¡¡ |  tj d¡¡ dt jd< dt jd< tj ¡ }|  i |¡ d	t jd< d
t jd< |  tj d¡¡ |  tj d¡¡ |  tj d¡¡ |  tj d¡¡ |  tj d¡¡ dt jd< dt jd< tj ¡ }|  d|d ¡ d S )NÚ Zno_proxyr€   ZNo_ProxyZ	arbitraryZ
http_proxyzhttp://somewhere:3128r†   z%localhost, noproxy.com, my.proxy:1234zxyz.comznoproxy.com:5678zmy.proxy:1234zmy.proxyzhttp://somewhereelse:3128Z
Http_Proxyr5   )	rU   rz   rŠ   r   r   r„   rƒ   rb   r^   )r   r   r   r   r   Ú,test_getproxies_environment_prefer_lowercase  s&    









zFProxyTests_withOrderedEnv.test_getproxies_environment_prefer_lowercaseN)r.   r/   r0   rT   rW   r‘   r   r   r   r   rŒ   	  s   rŒ   c               @   sì   e Zd ZdZdd„ Zdd„ Zdd„ Ze e	d¡d	d
„ ƒZ
e e	d¡dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Ze ejd#d$¡d%d&„ ƒZd'd(„ Zd)d*„ Zd+d,„ Ze e	d¡d-d.„ ƒZd/S )0Úurlopen_HttpTestsz.Test urlopen() opening a fake http connection.c             C   sn   |   d| d ¡ zLtdƒ}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W d |  ¡  X d S )Ns   HTTP/s    200 OK

Hello!zhttp://python.org/s   Hello!r&   éÈ   )r8   r   rb   r+   r[   r\   r;   )r   ZverÚfpr   r   r   Ú
check_read0  s    zurlopen_HttpTests.check_readc             C   s>   d}|   d¡ z tj |¡}|  | ¡ |¡ W d |  ¡  X d S )Nz-http://docs.python.org/library/urllib.html#OKs   HTTP/1.1 200 OK

Hello!)r8   r   r   r   rb   r[   r;   )r   r   r”   r   r   r   Útest_url_fragment;  s    
z#urlopen_HttpTests.test_url_fragmentc             C   s4   |   d¡ ztdƒ}|  |jj¡ W d |  ¡  X d S )Ns   HTTP/1.1 200 OK

Hello!zhttp://www.python.org)r8   r   r^   r”   Z
will_closer;   )r   Úrespr   r   r   Útest_willcloseE  s
    
z urlopen_HttpTests.test_willclosezssl module requiredc          
   C   sæ   xàt tddƒƒdg D ]È}t|ƒ}d|› d}|  d¡ zšt|ƒ dd¡}tjj}|  	|d	|› ¡ t
j d
|› ¡ W d Q R X |  	|d	|› ¡ t
j d|› ¡ W d Q R X td
|› ƒ}|  || ¡ ¡ W d |  ¡  X qW d S )Nr   é!   é   z//localhost:7777/testú/s   HTTP/1.1 200 OK

Hello.ú\z\\zcontain control.*zhttp:zhttps:)ry   ÚrangeÚchrr8   ÚreprÚreplacer5   r6   Ú
InvalidURLÚassertRaisesRegexr   r   r   rˆ   r[   r;   )r   Zchar_nor   Úschemeless_urlZescaped_char_reprr¡   r—   r   r   r   Ú#test_url_with_control_char_rejectedM  s     
z5urlopen_HttpTests.test_url_with_control_char_rejectedc          	   C   sÈ   |   d¡ d}d| d }zžtjj}|  |d¡ tj d|› ¡ W d Q R X |  |d¡ tj d|› ¡ W d Q R X td|› ƒ}|  d	| 	¡ ¡ |  d
| 	¡ ¡ |  d| 	¡ ¡ W d |  
¡  X d S )Ns   HTTP/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 . .)zhttp:zcontain control.*\\nzhttps:ú úÚ
)r8   r5   r6   r¡   r¢   r   r   r   rˆ   r[   r;   )r   r>   r£   r¡   r—   r   r   r   Ú/test_url_with_newline_header_injection_rejectedh  s    

zAurlopen_HttpTests.test_url_with_newline_header_injection_rejectedc             C   s   |   d¡ d S )Ns   0.9)r•   )r   r   r   r   Útest_read_0_9‚  s    zurlopen_HttpTests.test_read_0_9c             C   s   |   d¡ d S )Ns   1.0)r•   )r   r   r   r   Útest_read_1_0‡  s    zurlopen_HttpTests.test_read_1_0c             C   s   |   d¡ d S )Ns   1.1)r•   )r   r   r   r   Útest_read_1_1Š  s    zurlopen_HttpTests.test_read_1_1c             C   s,   |   d¡ z|  ttd¡ W d |  ¡  X d S )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
zhttp://python.org/)r8   rr   ÚOSErrorr   r;   )r   r   r   r   Útest_read_bogus  s
    z!urlopen_HttpTests.test_read_bogusc          	   C   sF   |   d¡ z,d}|  tjj|¡ tdƒ W d Q R X W d |  ¡  X d S )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
zRedirection to url 'file:zhttp://python.org/)r8   r¢   r   ÚerrorÚ	HTTPErrorr   r;   )r   Úmsgr   r   r   Útest_invalid_redirectš  s    z'urlopen_HttpTests.test_invalid_redirectc          	   C   sF   x@t tƒ jƒD ]0}|  d¡ z|  tjjtd¡ W d |  	¡  X qW d S )Ns_   HTTP/1.1 302 Found
Location: file://guidocomputer.athome.com:/python/license
Connection: close
zhttp://something)
r   r   Zmaxtriesr8   rr   r   r®   r¯   r   r;   )r   Úir   r   r   Útest_redirect_limit_independentª  s    
z1urlopen_HttpTests.test_redirect_limit_independentc             C   s,   |   d¡ z|  ttd¡ W d |  ¡  X d S )Nr&   zhttp://something)r8   rr   r¬   r   r;   )r   r   r   r   Útest_empty_socket¸  s    
z#urlopen_HttpTests.test_empty_socketc          	   C   sB   |   tjj¡}tdƒ W d Q R X |  |jj¡ |  |jj¡ d S )Nz/file://localhost/a/file/which/doesnot/exists.py)	rr   r   r®   ÚURLErrorr   r^   Ú	exceptionÚfilenameÚreason)r   Úer   r   r   Útest_missing_localfileÁ  s    z(urlopen_HttpTests.test_missing_localfilec          	   C   s¤   t  ¡ \}}d| tjjd¡ }z4|  tj |¡¡ t|ƒ}|  |¡ W d Q R X W d t 	|¡ t 
|¡ X |  tj |¡¡ |  tjj¡ t|ƒ W d Q R X d S )Nzfile://localhost/r›   )ÚtempfileÚmkstempr    rU   ÚpathÚsepr^   Úexistsr   r-   ÚunlinkrŠ   rr   r   r®   rµ   )r   ÚfdZtmp_fileZtmp_fileurlZfobjr   r   r   Útest_file_notexistsÈ  s    

z%urlopen_HttpTests.test_file_notexistsc          	   C   sF   d}|   tjj¡}t|ƒ W d Q R X |  |jj¡ |  |jj	¡ d S )Nzftp:///path)
rr   r   r®   rµ   r   rŠ   r¶   r·   r^   r¸   )r   Ztest_ftp_urlr¹   r   r   r   Útest_ftp_nohostÖ  s
    z!urlopen_HttpTests.test_ftp_nohostc          	   C   sB   |   tjj¡}tdƒ W d Q R X |  |jj¡ |  |jj	¡ d S )Nz.ftp://localhost/a/file/which/doesnot/exists.py)
rr   r   r®   rµ   r   rŠ   r¶   r·   r^   r¸   )r   r¹   r   r   r   Útest_ftp_nonexistingÝ  s    z&urlopen_HttpTests.test_ftp_nonexistingZMAXFTPCACHEr   c             C   s@   |   ¡  z(tj ddddg ¡tjjd< tdƒ W d |  ¡  X d S )Nr=   Úpassr€   é   Útestzftp://localhost)rG   r   r   rE   Zftpcacher   rH   )r   r   r   r   Útest_ftp_cache_pruningã  s
    z(urlopen_HttpTests.test_ftp_cache_pruningc             C   sf   |   d¡ zLtdƒ}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W d |  ¡  X d S )Ns   HTTP/1.0 200 OK

Hello!zhttp://user:pass@python.org/s   Hello!r&   r“   )r8   r   rb   r+   r[   r\   r;   )r   r”   r   r   r   Útest_userpass_inurlì  s    
z%urlopen_HttpTests.test_userpass_inurlc             C   s¨   |   d¡ zŽd}d |¡}tjj}dt| d¡ƒ d¡ }t|ƒ}|  	||j
 d¡¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ |¡ |  | ¡ d	¡ W d |  ¡  X d S )
Ns   HTTP/1.0 200 OK

Hello!za b:c dzhttp://{}@python.org/zAuthorization: Basic %s
ÚASCIIzUTF-8s   Hello!r&   r“   )r8   Úformatr5   r6   r7   r   ÚencodeÚdecoder   ÚassertInr   rb   r+   ZassertNotEqualr[   r\   r;   )r   Zuserpassr   Zfakehttp_wrapperZauthorizationr”   r   r   r   Útest_userpass_inurl_w_spaces÷  s    

z.urlopen_HttpTests.test_userpass_inurl_w_spacesc          	   C   s(   t  dtf¡ tj ¡  W d Q R X d S )Nr   )r   r   r   r   r   Ú	URLopener)r   r   r   r   Útest_URLopener_deprecation
  s    z,urlopen_HttpTests.test_URLopener_deprecationc          
   C   sN   t  ¡ }t dtf¡. |  t¡ tjj	dd|d W d Q R X W d Q R X d S )Nr   zhttps://localhostz/nonexistent/path)ZcafileÚcontext)
ÚsslZcreate_default_contextr   r   r   rr   rs   r   r   r   )r   rÒ   r   r   r   Útest_cafile_and_context  s
    z)urlopen_HttpTests.test_cafile_and_contextN) r.   r/   r0   ru   r•   r–   r˜   ÚunittestÚ
skipUnlessrÓ   r¤   r¨   r©   rª   r«   r­   r±   r³   r´   rº   rÂ   rÃ   rÄ   r   rD   r   r   rÈ   rÉ   rÏ   rÑ   rÔ   r   r   r   r   r’   -  s,   
		r’   c               @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )Úurlopen_DataTestsz"Test urlopen() opening a data URL.c             C   sR   d| _ d| _d| _d| _d| _tj | j¡| _tj | j¡| _	tj | j¡| _
d S )Nu   test 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%3Dzdata:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7
QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 vHgAAAABJRU5ErkJggg%3D%3D%0A%20)rN   ÚimageÚtext_urlÚtext_url_base64Ú	image_urlr   r   r   Útext_url_respÚtext_url_base64_respÚimage_url_resp)r   r   r   r   rT     s    
zurlopen_DataTests.setUpc             C   s*   x$dD ]}|   t| j|ƒd| ¡ qW d S )N)r*   r+   rX   r-   rZ   r[   r\   r]   z/object returned by urlopen() lacks %s attribute)r^   r_   rÜ   )r   r`   r   r   r   ra   7  s    
z urlopen_DataTests.test_interfacec             C   st   |   | j ¡ tjj¡ |  | j ¡  ¡ ddg¡ |  | j	 ¡ d t
t| jƒƒ¡ |  tj d¡ ¡  ¡ ddg¡ d S )N)z
text/plainr   )Úcharsetz
ISO-8859-1zcontent-lengthzdata:,)rß   zUS-ASCII)rf   rÜ   rZ   rj   rk   rl   rb   rÝ   Ú
get_paramsrÞ   Ústrr   rØ   r   r   r   )r   r   r   r   rm   ?  s    
zurlopen_DataTests.test_infoc             C   s@   |   | j ¡ | j¡ |   | j ¡ | j¡ |   | j ¡ | j¡ d S )N)rb   rÜ   r[   rÙ   rÝ   rÚ   rÞ   rÛ   )r   r   r   r   rn   H  s    zurlopen_DataTests.test_geturlc             C   s0   |   | j ¡  t| j ¡  ¡ ƒd ¡| j¡ d S )Nrß   )rb   rÜ   r*   rÍ   ÚdictrZ   rà   rN   )r   r   r   r   Útest_read_textN  s    z urlopen_DataTests.test_read_textc             C   s0   |   | j ¡  t| j ¡  ¡ ƒd ¡| j¡ d S )Nrß   )rb   rÝ   r*   rÍ   râ   rZ   rà   rN   )r   r   r   r   Útest_read_text_base64R  s    z'urlopen_DataTests.test_read_text_base64c             C   s   |   | j ¡ | j¡ d S )N)rb   rÞ   r*   rØ   )r   r   r   r   Útest_read_imageW  s    z!urlopen_DataTests.test_read_imagec             C   s   |   ttjjd¡ d S )Nzdata:text/plain)rr   rs   r   r   r   )r   r   r   r   Útest_missing_commaZ  s    z$urlopen_DataTests.test_missing_commac             C   s   |   ttjjd¡ d S )Nzdata:;base64,Cg=)rr   rs   r   r   r   )r   r   r   r   Útest_invalid_base64_data]  s    z*urlopen_DataTests.test_invalid_base64_dataN)r.   r/   r0   ru   rT   ra   rm   rn   rã   rä   rå   ræ   rç   r   r   r   r   r×     s   	r×   c               @   sj   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )Úurlretrieve_FileTestsz(Test urllib.urlretrieve() on local filesc             C   s^   g | _ |  tj¡ d| _z$ttjdƒ}| | j¡ | ¡  W d y| ¡  W n   Y nX X d S )Ns   testing urllib.urlretrieverK   )Ú	tempFilesÚregisterFileForCleanUpr   rO   rN   r   rP   r-   )r   ÚFILEr   r   r   rT   e  s      zurlretrieve_FileTests.setUpc             C   s0   x*| j D ] }yt |¡ W q   Y qX qW d S )N)ré   rU   rV   )r   Zeachr   r   r   rW   z  s
      zurlretrieve_FileTests.tearDownc             C   sJ   t j |¡}y| d¡ W n tk
r8   t d¡‚Y nX dtj 	|¡ S )Nzutf-8z!filePath is not encodable to utf8z	file://%s)
rU   r½   ÚabspathrÌ   ÚUnicodeEncodeErrorrÕ   ZSkipTestr   r   r   )r   ZfilePathr   r   r   ÚconstructLocalFileUrl€  s    z+urlretrieve_FileTests.constructLocalFileUrlr&   c             C   sZ   t  ¡ \}}z,|  |¡ t |d¡}| |¡ | ¡  W dy| ¡  W n   Y nX X |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.rK   N)r»   r¼   rê   rU   ÚfdopenrP   r-   )r   r   ZnewFdZnewFilePathZnewFiler   r   r   ÚcreateNewTempFileˆ  s    

  z'urlretrieve_FileTests.createNewTempFilec             C   s   | j  |¡ d S )N)ré   Úappend)r   ZfileNamer   r   r   rê   ˜  s    z,urlretrieve_FileTests.registerFileForCleanUpc             C   s>   t j dtj ¡}|  |d tj¡ |  |d tjj	d¡ d S )Nzfile:%sr   r   zFdid not get an email.message.Message instance as second returned value)
r   r   Úurlretriever   rO   rb   rf   rj   rk   rl   )r   Úresultr   r   r   Ú
test_basic›  s    z urlretrieve_FileTests.test_basicc             C   sœ   dt j }|  |¡ tj |  t j¡|¡}|  ||d ¡ |  t	j
 |¡d¡ t|dƒ}z| ¡ }| ¡  W d y| ¡  W n   Y nX X |  | j|¡ d S )Nz%s.2r   zcopy of the file was not madeÚrb)r   rO   rê   r   r   rò   rî   rb   r^   rU   r½   r¿   r   r*   r-   rN   )r   Úsecond_tempró   rë   rN   r   r   r   Ú	test_copy¤  s    



  zurlretrieve_FileTests.test_copyc                sB   dgf‡ fdd„	}dt j }ˆ  |¡ tj ˆ  t j¡||¡ d S )Nr   c                sH   ˆ   | t¡ ˆ   |t¡ ˆ   |t¡ ˆ  | |d ¡ |d d |d< d S )Nr   r   )rf   rg   rb   )Úblock_countÚblock_read_sizeÚ	file_sizeZcount_holder)r   r   r   Ú
hooktester¸  s
    z9urlretrieve_FileTests.test_reporthook.<locals>.hooktesterz%s.2)r   rO   rê   r   r   rò   rî   )r   rû   rö   r   )r   r   Útest_reporthook¶  s    


z%urlretrieve_FileTests.test_reporthookc             C   sX   g }|fdd„}|   ¡ }tj |  |¡tj|¡ |  t|ƒd¡ |  |d d d¡ d S )Nc             S   s   |  | ||f¡ d S )N)rñ   )rø   rù   rú   Ú_reportr   r   r   rû   Ç  s    zAurlretrieve_FileTests.test_reporthook_0_bytes.<locals>.hooktesterr   r   r   )	rð   r   r   rò   rî   r   rO   rb   r   )r   Úreportrû   ÚsrcFileNamer   r   r   Útest_reporthook_0_bytesÄ  s    
z-urlretrieve_FileTests.test_reporthook_0_bytesc             C   sn   g }|fdd„}|   d¡}tj |  |¡tj|¡ |  t|ƒd¡ |  |d d d¡ |  |d d d¡ d S )Nc             S   s   |  | ||f¡ d S )N)rñ   )rø   rù   rú   rý   r   r   r   rû   Ô  s    zAurlretrieve_FileTests.test_reporthook_5_bytes.<locals>.hooktesters   xxxxxr   r   é   r   )	rð   r   r   rò   rî   r   rO   rb   r   )r   rþ   rû   rÿ   r   r   r   Útest_reporthook_5_bytesÏ  s    

z-urlretrieve_FileTests.test_reporthook_5_bytesc             C   sš   g }|fdd„}|   dd ¡}tj |  |¡tj|¡ |  t|ƒd¡ |  |d d d¡ |  |d d d	¡ |  |d d d	¡ |  |d d d	¡ d S )
Nc             S   s   |  | ||f¡ d S )N)rñ   )rø   rù   rú   rý   r   r   r   rû   â  s    zDurlretrieve_FileTests.test_reporthook_8193_bytes.<locals>.hooktesteró   xi   é   r   r   r   i    )	rð   r   r   rò   rî   r   rO   rb   r   )r   rþ   rû   rÿ   r   r   r   Útest_reporthook_8193_bytesÝ  s    
z0urlretrieve_FileTests.test_reporthook_8193_bytesN)r&   )r.   r/   r0   ru   rT   rW   rî   rð   rê   rô   r÷   rü   r   r  r  r   r   r   r   rè   b  s   
	rè   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )Úurlretrieve_HttpTestsz5Test urllib.urlretrieve() using fake http connectionsc          	   C   sR   |   d¡ dd„ }|  tjj¡( ztjjtj|d W d |  	¡  X W d Q R X d S )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
c             S   s   d S )Nr   )Zpar1Zpar2Zpar3r   r   r   Ú_reporthookü  s    zYurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError.<locals>._reporthook)Z
reporthook)
r8   rr   r   r®   ÚContentTooShortErrorr   rò   r   ÚTEST_HTTP_URLr;   )r   r  r   r   r   Ú.test_short_content_raises_ContentTooShortErrorñ  s    
zDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorc          	   C   sF   |   d¡ |  tjj¡$ ztj tj¡ W d |  	¡  X W d Q R X d S )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
)
r8   rr   r   r®   r  r   rò   r   r	  r;   )r   r   r   r   ÚAtest_short_content_raises_ContentTooShortError_without_reporthook  s    zWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookN)r.   r/   r0   ru   r
  r  r   r   r   r   r  î  s   r  c               @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚQuotingTestsau  Tests for urllib.quote() and urllib.quote_plus()

    According to RFC 3986 (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

    c             C   sZ   d  ddddg¡}tj |¡}|  ||d||f ¡ tj |¡}|  ||d||f ¡ d S )Nr   ZABCDEFGHIJKLMNOPQRSTUVWXYZZabcdefghijklmnopqrstuvwxyzÚ
0123456789z_.-~zusing quote(): %r != %rzusing quote_plus(): %r != %r)Újoinr   ÚparseÚquoterb   Ú
quote_plus)r   Zdo_not_quoteró   r   r   r   Útest_never_quote1  s    zQuotingTests.test_never_quotec             C   s   |   tjjjd d¡ d S )Nr   r›   )rb   r   r  r  Ú__defaults__)r   r   r   r   Útest_default_safe>  s    zQuotingTests.test_default_safec             C   sî   d}t jj||d}|  ||d||f ¡ t jj||d}|  ||d||f ¡ t jj|dd}|  ||d||f ¡ t jjdddd	}t jjddd
d	}|  ||d||f ¡ t jjdddd	}t jjddd
d	}|  ||d||f ¡ d S )Nz<>)Úsafezusing quote(): %r != %rzusing quote_plus(): %r != %rs   <>u   aÃ¼bzlatin-1õ   Ã¼)Úencodingr  r   ó   ü)r   r  r  rb   r  )r   Zquote_by_defaultró   Úexpectr   r   r   Ú	test_safeB  s,    zQuotingTests.test_safec          	   C   sì   dd„ t dƒD ƒ}| d¡ | tdƒ¡ d |¡}xd|D ]\}tj |¡}|  t|ƒ|d|t|ƒ|f ¡ tj 	|¡}|  t|ƒ|d|t|ƒ|f ¡ q:W ~d	}d
}tj |¡}|  ||d||f ¡ tj 	|¡}|  ||d||f ¡ d S )Nc             S   s   g | ]}t |ƒ‘qS r   )rž   )Ú.0Únumr   r   r   ú
<listcomp>b  s    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 %szab[]cdz
ab%5B%5Dcdzusing quote(): %r != %rzusing quote_plus(): %r != %r)
r   rñ   rž   r  r   r  r  rb   r   r  )r   Zshould_quoter   ró   Zpartial_quoteÚexpectedr   r   r   Útest_default_quoting_  s,    


z!QuotingTests.test_default_quotingc             C   s°   t j d¡}|  |tdƒd|tdƒf ¡ t j d¡}|  |dd| ¡ d}| dtdƒ¡}t j |¡}|  ||d||f ¡ | dd¡}t j |¡}|  ||d||f ¡ d S )Nr¥   zusing quote(): %r != %rú+zusing quote_plus(): %r != +z
a b cd e fzusing quote_plus(): %r != %r)r   r  r  rb   r   r  r    )r   ró   Úgivenr  r   r   r   Útest_quoting_space{  s    
zQuotingTests.test_quoting_spacec             C   sX   |   tj d¡d¡ |   tj dd¡d¡ |   tj d¡d¡ |   tj dd¡d¡ d S )Nzalpha+beta gammazalpha%2Bbeta+gammar!  zalpha+beta+gammas   alpha+beta gammaó   +)rb   r   r  r  )r   r   r   r   Útest_quoting_plusŽ  s    zQuotingTests.test_quoting_plusc             C   sf   d}d}t j |¡}|  ||d||f ¡ | jtt jj|dd t j |¡}|  ||d||f ¡ d S )Ns   ¢Øabÿz%A2%D8ab%FFzusing quote(): %r != %rzlatin-1)r  z"using quote_from_bytes(): %r != %r)r   r  r  rb   rr   Ú	TypeErrorZquote_from_bytes)r   r"  r  ró   r   r   r   Útest_quote_bytesš  s    zQuotingTests.test_quote_bytesc             C   s(  d}d}t j |¡}|  ||d||f ¡ t jj|d d d}|  ||d||f ¡ d}d}t jj|dd}|  ||d||f ¡ d}d	}t j |¡}|  ||d||f ¡ d}| jtt jj|dd d}d
}t jj|ddd}|  ||d||f ¡ d}d}t jj|ddd}|  ||d||f ¡ d S )Nu   Â¢Ã˜abÃ¿z%C2%A2%C3%98ab%C3%BFzusing quote(): %r != %r)r  Úerrorsz%A2%D8ab%FFzlatin-1)r  u   æ¼¢å­—z%E6%BC%A2%E5%AD%97z%3F%3Fr    z%26%2328450%3B%26%2323383%3BÚxmlcharrefreplace)r   r  r  rb   rr   rí   )r   r"  r  ró   r   r   r   Útest_quote_with_unicodeª  sB    

z$QuotingTests.test_quote_with_unicodec             C   sb   d}d}t jj|dd}|  ||d||f ¡ d}d}t jj|ddd	}|  ||d||f ¡ d S )
Nu   Â¢Ã˜ Ã¿z
%A2%D8+%FFzlatin-1)r  zusing quote_plus(): %r != %ru   abæ¼¢å­— cdzab%3F%3F+cdr    )r  r(  )r   r  r  rb   )r   r"  r  ró   r   r   r   Útest_quote_plus_with_unicodeÔ  s    
z)QuotingTests.test_quote_plus_with_unicodeN)r.   r/   r0   ru   r  r  r  r   r#  r%  r'  r*  r+  r   r   r   r   r    s   *r  c               @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚUnquotingTestszwTests for unquote() and unquote_plus()

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

    c          	   C   s  g }xrt dƒD ]f}tt|ƒƒ}t|ƒ}tj |¡}|  ||d||f ¡ tj |¡}|  ||d||f ¡ | |¡ qW d 	|¡}~tj |¡}|  | 
d¡dd| ¡ |  ttftjjd ¡ |  ttftjjd¡ tjdtfd	d
 |  ttftjjd¡ W d Q R X d S )Né€   zusing unquote(): %r != %rzusing unquote_plus(): %r != %rr   r	   r   z/using unquote(): not all characters escaped: %sr   T)Úquietr&   )r   r   rž   r   r  Úunquoterb   Úunquote_plusrñ   r  Úcountrr   r&  ÚAttributeErrorr   r   ÚBytesWarning)r   Zescape_listr  r"  r  ró   Zescape_stringr   r   r   Útest_unquotingë  s,    
zUnquotingTests.test_unquotingc             C   s>  d}|}t j |¡}|  ||d||f ¡ d}|}t j |¡}|  ||d||f ¡ d}|}t j |¡}|  ||d||f ¡ d}t|dƒ}t j |¡}|  ||d||f ¡ d}t|dƒ}t j |¡}|  ||d||f ¡ d}t|dƒ}t j |¡}|  ||d||f ¡ |  ttft jjd ¡ |  ttft jjd¡ d S )Nz%xabzusing unquote(): %r != %rz%xr	   rJ   z"using unquote_to_bytes(): %r != %rr   )	r   r  r/  rb   rL   Úunquote_to_bytesrr   r&  r2  )r   r"  r  ró   r   r   r   Útest_unquoting_badpercent  s@    








z(UnquotingTests.test_unquoting_badpercentc             C   s.   d}d}t j |¡}|  ||d||f ¡ d S )Nz%Ab%eAs   «êz"using unquote_to_bytes(): %r != %r)r   r  r5  rb   )r   r"  r  ró   r   r   r   Útest_unquoting_mixed_case(  s    z(UnquotingTests.test_unquoting_mixed_casec             C   sX   dt dƒ }d}tj |¡}|  ||d||f ¡ tj |¡}|  ||d||f ¡ d S )Nzab%sdÚcZabcdzusing quote(): %r != %rzusing unquote_plus(): %r != %r)r   r   r  r/  rb   r0  )r   r"  r  ró   r   r   r   Útest_unquoting_parts1  s    z#UnquotingTests.test_unquoting_partsc             C   s\   d}|}t j |¡}|  ||d||f ¡ | dd¡}t j |¡}|  ||d||f ¡ d S )Nzare+there+spaces...zusing unquote(): %r != %rr!  r¥   zusing unquote_plus(): %r != %r)r   r  r/  rb   r    r0  )r   r"  r  ró   r   r   r   Útest_unquoting_plus=  s    z"UnquotingTests.test_unquoting_plusc             C   s¨   d}d}t j |¡}|  ||d||f ¡ t j d¡}d}|  ||d||f ¡ d}d}t j |¡}|  ||d||f ¡ d}d}t j |¡}|  ||d||f ¡ d S )	Nz"br%C3%BCckner_sapporo_20050930.docs   brÃ¼ckner_sapporo_20050930.docz"using unquote_to_bytes(): %r != %ru	   æ¼¢%C3%BCs   æ¼¢Ã¼s   %A2%D8ab%FFs   ¢Øabÿs	   %A2Øab%FF)r   r  r5  rb   )r   r"  r  ró   r   r   r   Útest_unquote_to_bytesI  s.    z$UnquotingTests.test_unquote_to_bytesc             C   sx  d}d}t j |¡}|  ||d||f ¡ t jj|d d d}|  ||d||f ¡ t jjddd}d}|  ||d||f ¡ d}d	}t j |¡}|  ||d||f ¡ d
}d}t j |¡}|  ||d||f ¡ t jj|dd}|  ||d||f ¡ d
}d}t jj|dd}|  ||d||f ¡ t j d¡}d}|  ||d||f ¡ t jjddd}d}|  ||d||f ¡ d S )Nz"br%C3%BCckner_sapporo_20050930.docu   brÃ¼ckner_sapporo_20050930.doczusing unquote(): %r != %r)r  r(  zbr%FCckner_sapporo_20050930.doczlatin-1)r  z%E6%BC%A2%E5%AD%97u   æ¼¢å­—z%F3%B1u   ï¿½r    )r(  r   Úignoreu	   æ¼¢%C3%BCu   æ¼¢Ã¼u   æ¼¢%FC)r   r  r/  rb   )r   r"  r  ró   r   r   r   Útest_unquote_with_unicodeh  sN    z(UnquotingTests.test_unquote_with_unicodeN)r.   r/   r0   ru   r4  r6  r7  r9  r:  r;  r=  r   r   r   r   r,  ä  s   $	r,  c               @   sp   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Úurlencode_TestszTests for urlencode()c       	   	   C   sÄ   dddg}t j |¡}x$|D ]}|  ||d|||f ¡ qW |  | d¡dd|| d¡f ¡ | d¡}||d  }||d  }|  | ¡ o’| ¡ d	||f ¡ |  t	|ƒd
d|t	|ƒd
f ¡ dS )a  Helper 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.

        z1st=1z2nd=2z3rd=3ztesting %s: %s not found in %sú&r   z#testing %s: expected 2 '&'s; got %sr   z1testing %s: '&' not located in proper place in %sé   z5testing %s: unexpected number of characters: %s != %sN)
r   r  Ú	urlencoderÎ   rb   r1  Úindexr^   Úisdigitr   )	r   r"  Z	test_typeZexpect_somewhereró   r  Zamp_locationZon_amp_leftZon_amp_rightr   r   r   Úhelp_inputtype¥  s$    


zurlencode_Tests.help_inputtypec             C   s   |   ddddœd¡ d S )NÚ1Ú2Ú3)Ú1stÚ2ndÚ3rdzusing dict as input type)rD  )r   r   r   r   Útest_using_mappingÅ  s    z"urlencode_Tests.test_using_mappingc             C   s   |   dddgd¡ d S )N)rH  rE  )rI  rF  )rJ  rG  z*using sequence of two-item tuples as input)rD  )r   r   r   r   Útest_using_sequenceÊ  s    z#urlencode_Tests.test_using_sequencec             C   s\   ddi}dt dƒt dƒf }tj |¡}|  ||¡ ddi}d}tj |¡}|  ||¡ d S )Nr?  ú=z%s=%szkey namezA bunch of pluseszkey+name=A+bunch+of+pluses)r   r   r  rA  rb   )r   r"  r  ró   r   r   r   Útest_quotingÏ  s    zurlencode_Tests.test_quotingc             C   s–   ddddgi}dt j tdddgƒ¡ }t j |¡}|  ||¡ t j |d¡}x$|d D ]}d| }|  ||¡ qXW |  | d¡dd	| d¡ ¡ d S )
NZsequencerE  rF  rG  zsequence=%sTr?  r   zExpected 2 '&'s, got %s)r   r  r  rá   rA  rb   rÎ   r1  )r   r"  r  ró   Úvaluer   r   r   Ú
test_doseqÚ  s    zurlencode_Tests.test_doseqc             C   s,   |   dtj i ¡¡ |   dtj g ¡¡ d S )Nr   )rb   r   r  rA  )r   r   r   r   Útest_empty_sequenceç  s    z#urlencode_Tests.test_empty_sequencec             C   s4   |   dtj ddi¡¡ |   dtj dd i¡¡ d S )Nza=1Úar   za=None)rb   r   r  rA  )r   r   r   r   Útest_nonstring_valuesë  s    z%urlencode_Tests.test_nonstring_valuesc             C   sh   |   dtj dddgid¡¡ |   dtj dd dgid¡¡ t ddg¡}|   d	tj d|id¡¡ d S )
Nza=1&a=2rR  r   r   Tz
a=None&a=a)rR  r   )Úbr   za=a&a=b)rb   r   r  rA  rŽ   r   )r   r   r   r   r   Útest_nonstring_seq_valuesï  s    z)urlencode_Tests.test_nonstring_seq_valuesc             C   sn   d}d}t jj|ddd}|  ||¡ d}d}t j |¡}|  ||¡ d}d}t jj|dd	}|  ||¡ d S )
N))õ   Â õ   Ãz%3F=%3FrÊ   r    )r  r(  z%C2%A0=%C3%81z%A0=%C1zlatin-1)r  )r   r  rA  rb   )r   r"  r  ró   r   r   r   Útest_urlencode_encoding÷  s    z'urlencode_Tests.test_urlencode_encodingc             C   sä   d}d}t jj|dddd}|  ||¡ d}d}t jj|dddd	}|  ||¡ d}d
}t j |d¡}|  ||¡ d}d}t j |d¡}|  ||¡ d}d}t jj|ddd}|  ||¡ d}d}t jj|ddd}|  ||¡ d S )N))rV  rW  z%3F=%3FTrÊ   r    )Údoseqr  r(  ))rV  )r   rW  z%3F=1&%3F=%3F)r  r(  z%C2%A0=%C3%81))rV  )é*   rW  z%C2%A0=42&%C2%A0=%C3%81z%A0=%C1zlatin-1)r  z%A0=42&%A0=%C1)r   r  rA  rb   )r   r"  r  ró   r   r   r   Útest_urlencode_encoding_doseq
  s4    



z-urlencode_Tests.test_urlencode_encoding_doseqc             C   s`   d}d}t j |¡}|  ||¡ t j |d¡}|  ||¡ d}d}t j |d¡}|  ||¡ d S )N))s    $s   Á$z%A0%24=%C1%24T))s    $)rZ  s   Á$z%A0%24=42&%A0%24=%C1%24)r   r  rA  rb   )r   r"  r  ró   r   r   r   Útest_urlencode_bytes/  s    z$urlencode_Tests.test_urlencode_bytesc             C   sÞ   d}t jj|dd}d}|  ||¡ d}t jj|ddd}d}|  ||¡ d}d}t jj|ddd}|  ||¡ d}t jj|dd	d
}d}|  ||¡ d}d}t jj|ddd	d}d}d}t jj|ddd	d
}|  ||¡ d S )N))s    $s   Á$z:$)r  z	%A0$=%C1$T)rY  r  ))s    $)s   Á$é   rZ  z%A0$=%C1$&%A0$=13&%A0$=42zlatin-1)r  r  )rY  r  r  )r   r  rA  rb   )r   r"  ró   r  r   r   r   Ú&test_urlencode_encoding_safe_parameter=  s4    
z6urlencode_Tests.test_urlencode_encoding_safe_parameterN)r.   r/   r0   ru   rD  rK  rL  rN  rP  rQ  rS  rU  rX  r[  r\  r^  r   r   r   r   r>  ¢  s    %r>  c               @   s:   e Zd ZdZdd„ Zdd„ Ze ej	dkd¡dd	„ ƒZ
d
S )ÚPathname_Testsz&Test pathname2url() and url2pathname()c             C   s^   t j dddd¡}d}tj |¡}|  ||d||f ¡ tj |¡}|  ||d||f ¡ d S )NÚpartsZofrR  r½   zparts/of/a/pathzpathname2url() failed; %s != %szurl2pathame() failed; %s != %s)rU   r½   r  r   r   r   rb   r   )r   Zexpected_pathZexpected_urlró   r   r   r   rô   h  s    zPathname_Tests.test_basicc             C   sà   t j ddd¡}dtj d¡ }tj |¡}|  ||d||f ¡ |}tj 	|¡}|  ||d||f ¡ t j dd¡}d	tj d¡ }tj |¡}|  ||d||f ¡ d
}t j dd¡}tj 	|¡}|  ||d||f ¡ d S )NZneedszquot=ingÚherezneeds/%s/herezpathname2url() failed; %s != %szurl2pathname() failed; %s != %sz	make sureZusing_quotez%s/using_quotezmake+sure/using_unquotez	make+sureZusing_unquote)
rU   r½   r  r   r  r  r   r   rb   r   )r   r"  r  ró   r   r   r   rN  u  s.    zPathname_Tests.test_quotingÚwin32z.test specific to the urllib.url2path function.c             C   sf   d}d}x.|D ]&}t j |¡}|  ||d||f ¡ qW d}d}t j |¡}|  ||d||f ¡ d S )N)z/C:/z///C:/z/C|//zC:\z/urllib.request..url2pathname() failed; %s != %sz
///C|/pathzC:\pathz.urllib.request.url2pathname() failed; %s != %s)r   r   r   rb   )r   r"  r  r   ró   r   r   r   Útest_ntpath  s    
zPathname_Tests.test_ntpathN)r.   r/   r0   ru   rô   rN  rÕ   rÖ   ÚsysÚplatformrc  r   r   r   r   r_  e  s
   r_  c               @   s   e Zd ZdZdd„ ZdS )ÚUtility_Testsz=Testcase to test the various utility functions in the urllib.c             C   s   |   tj ¡ t¡ dS )zATest the urllib.request.thishost utility function returns a tupleN)rf   r   r   ZthishostÚtuple)r   r   r   r   Útest_thishost¤  s    zUtility_Tests.test_thishostN)r.   r/   r0   ru   rh  r   r   r   r   rf  ¡  s   rf  c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚURLopener_Testsz4Testcase to test the open method of URLopener class.c          	   C   sZ   G dd„ dt jjƒ}t dtf¡. |  |ƒ  d¡d¡ |  |ƒ  d¡d¡ W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z8URLopener_Tests.test_quoted_open.<locals>.DummyURLopenerc             S   s   |S )Nr   )r   r   r   r   r   Ú	open_spam®  s    zBURLopener_Tests.test_quoted_open.<locals>.DummyURLopener.open_spamN)r.   r/   r0   rj  r   r   r   r   ÚDummyURLopener­  s   rk  z8DummyURLopener style of invoking requests is deprecated.zspam://example/ /z//example/%20/z,spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/z'//c:|windows%/:=&?~#+!$,;'@()*[]|/path/)r   r   rÐ   r   r   r   rb   r   )r   rk  r   r   r   Útest_quoted_open¬  s    



z URLopener_Tests.test_quoted_openc          	   C   s¬   G dd„ dt jjƒ}tjdd‚ t dt¡ xndD ]f}|  tt jj	|¡ |  tt j ¡ j
|¡ |  tt j ¡ j|¡ |  t|ƒ j
|¡ |  t|ƒ j|¡ q4W W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z<URLopener_Tests.test_local_file_open.<locals>.DummyURLopenerc             S   s   |S )Nr   )r   r   r   r   r   Úopen_local_file¾  s    zLURLopener_Tests.test_local_file_open.<locals>.DummyURLopener.open_local_fileN)r.   r/   r0   rm  r   r   r   r   rk  ½  s   rk  T)Úrecordr<  )zlocal_file://examplezlocal-file://example)r   r   rÐ   ÚwarningsÚcatch_warningsÚsimplefilterr   rr   r¬   r   r   Zretrieve)r   rk  r   r   r   r   Útest_local_file_open»  s    
z$URLopener_Tests.test_local_file_openN)r.   r/   r0   ru   rl  rr  r   r   r   r   ri  ©  s   ri  c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚRequestTestsz&Unit tests for urllib.request.Request.c             C   s>   t jj}|dƒ}|  | ¡ d¡ |di ƒ}|  | ¡ d¡ d S )Nzhttp://www.python.orgr‡   ZPOST)r   r   ÚRequestrb   Ú
get_method)r   rt  r   r   r   r   Útest_default_values  s
    
z RequestTests.test_default_valuesc             C   s”   t jj}|ddd}|  |jd¡ |  | ¡ d¡ |di dd}|  |jd¡ |  | ¡ d¡ |ddd}|  | ¡ d¡ d|_|  | ¡ d¡ d S )Nzhttp://www.python.orgZHEAD)Úmethodr‡   )r   r   rt  rb   rw  ru  )r   rt  r   r   r   r   Útest_with_method_arg%  s    z!RequestTests.test_with_method_argN)r.   r/   r0   ru   rv  rx  r   r   r   r   rs    s   rs  c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚURL2PathNameTestsc             C   s4   |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ d S )Nz///C|zC:z///C:z///C|/zC:\)rb   r   )r   r   r   r   Útest_converting_drive_letter5  s    z.URL2PathNameTests.test_converting_drive_letterc             C   s$   |   tdƒd¡ |   tdƒd¡ d S )Nz
///C/test/z
\\\C\test\z////C/test/z	\\C\test\)rb   r   )r   r   r   r   Ú$test_converting_when_no_drive_letter:  s    z6URL2PathNameTests.test_converting_when_no_drive_letterc             C   s   |   tdƒd¡ d S )Nz///C|/foo/bar/spam.foozC:\foo\bar\spam.foo)rb   r   )r   r   r   r   Útest_simple_compare?  s    
z%URL2PathNameTests.test_simple_comparec             C   s   |   ttd¡ d S )Nu   ///Ã¨|/)rr   ÚIOErrorr   )r   r   r   r   Útest_non_ascii_drive_letterC  s    z-URL2PathNameTests.test_non_ascii_drive_letterc             C   s0   dddg}x |D ]}|   tt|ƒƒ|¡ qW d S )NzC:z\\\C\test\\zC:\foo\bar\spam.foo)rb   r   r   )r   Úlist_of_pathsr½   r   r   r   Útest_roundtrip_url2pathnameF  s
    
z-URL2PathNameTests.test_roundtrip_url2pathnameN)r.   r/   r0   rz  r{  r|  r~  r€  r   r   r   r   ry  3  s
   ry  c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚPathName2URLTestsc             C   s$   |   tdƒd¡ |   tdƒd¡ d S )NzC:z///C:zC:\)rb   r   )r   r   r   r   rz  P  s    z.PathName2URLTests.test_converting_drive_letterc             C   s4   |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ d S )Nz\\\folder\test\z/////folder/test/z\\folder\test\z////folder/test/z\folder\test\z/folder/test/)rb   r   )r   r   r   r   r{  T  s    


z6PathName2URLTests.test_converting_when_no_drive_letterc             C   s   |   tdƒd¡ d S )NzC:\foo\bar\spam.fooz///C:/foo/bar/spam.foo)rb   r   )r   r   r   r   r|  \  s    
z%PathName2URLTests.test_simple_comparec             C   s   |   ttd¡ d S )NzXX:\)rr   r}  r   )r   r   r   r   Útest_long_drive_letter`  s    z(PathName2URLTests.test_long_drive_letterc             C   s0   dddg}x |D ]}|   tt|ƒƒ|¡ qW d S )Nz///C:z/////folder/test/z///C:/foo/bar/spam.foo)rb   r   r   )r   r  r½   r   r   r   Útest_roundtrip_pathname2urlc  s
    
z-PathName2URLTests.test_roundtrip_pathname2urlN)r.   r/   r0   rz  r{  r|  r‚  rƒ  r   r   r   r   r  N  s
   r  Ú__main__)NN)6ru   Zurllib.parser   Zurllib.requestZurllib.errorZhttp.clientr5   Zemail.messagerj   r(   rÕ   Zunittest.mockr   rÇ   r   rU   rÓ   ÚImportErrorrd  r»   ro  Z
nturl2pathr   r   Úbase64r   rŽ   r   r   r   r   r8   rD   r9   r<   ZTestCaserI   rv   rŒ   r’   r×   rè   r  r  r,  r>  r_  rf  ri  rs  ry  r  r.   Úmainr   r   r   r   Ú<module>   sd   

'	S6$ lJ ) N ? D<r
