a
    äze¬ ã                   @   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zddlZW n ey~   dZY n0 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d5d	d
„Zdd„ Zd6d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 e
j"ƒZ/G d.d/„ d/e
j"ƒZ0G d0d1„ d1e
j"ƒZ1G d2d3„ d3e
j"ƒZ2e3d4kr6e
 4¡  dS )7z;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.9/test/test_urllib.pyÚ	hexescape   s    r   c                 C   sN   |durt jj|d}nts(tƒ }|ant}|du 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 ¡ W  d   ƒ S 1 s.0    Y  d S )Nz8FancyURLopener style of invoking requests is deprecated.)r   Úcheck_warningsÚDeprecationWarningr   r   r   r   r   r   r   r   4   s    ÿÿr   Fc                    s:   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   ÚmakefileB   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.   F   s    z!fakehttp.<locals>.FakeSocket.readc                 S   s   | j r
dS tj | |¡S r)   )r+   r,   r-   Úreadline)r!   Úlengthr   r   r   r/   K   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   r1   P   s    
z"fakehttp.<locals>.FakeSocket.close)N)N)	Ú__name__Ú
__module__Ú__qualname__r%   r$   r(   r.   r/   r1   r   r"   r   r   Ú
FakeSocket<   s   

r6   c                       s(   e Zd ZdZ‡ fdd„Z”r$dd„ ZdS )z$fakehttp.<locals>.FakeHTTPConnectionNc                    s   ˆ | j ƒ| _| jt| ƒ_d S r   )ÚfakedataÚsockÚtypeZfakesockr2   )r6   r   r   ÚconnectZ   s    z,fakehttp.<locals>.FakeHTTPConnection.connectc                 S   s   d S r   r   r2   r   r   r   r1   c   s    z*fakehttp.<locals>.FakeHTTPConnection.close)r3   r4   r5   r   r:   r1   r   )r6   Ú
mock_closer   r   r#   U   s   r#   )r,   r-   ÚhttpÚclientÚHTTPConnectionr7   )r7   r;   r   )r#   r6   r;   r   Úfakehttp;   s    r?   c                   @   s   e Zd Zddd„Zdd„ ZdS )ÚFakeHTTPMixinFc                 C   s"   t ||d}tjj| _|tj_d S )N©r;   )r?   r<   r=   r>   Ú_connection_class)r!   r7   r;   Zfake_http_classr   r   r   r?   k   s    
zFakeHTTPMixin.fakehttpc                 C   s   | j tj_d S r   )rB   r<   r=   r>   r2   r   r   r   Ú
unfakehttpp   s    zFakeHTTPMixin.unfakehttpN)F)r3   r4   r5   r?   rC   r   r   r   r   r@   j   s   
r@   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 r   r   )r!   ÚuserÚpasswdÚhostÚportÚdirsÚtimeoutZ
persistentr   r   r   Ú__init__w   s    z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.__init__c                 S   s   t  ¡ dfS )Nr   )r,   r-   )r!   Úfiler9   r   r   r   Úretrfile{   s    z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfilec                 S   s   d S r   r   r2   r   r   r   r1   ~   s    z2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close)NT)r3   r4   r5   rK   rM   r1   r   r   r   r   ÚFakeFtpWrapperv   s
     ÿ
rN   )Úobjectr   r   Ú
ftpwrapperÚ_ftpwrapper_class)r!   rN   r   r   r   Úfakeftpu   s    
zFakeFTPMixin.fakeftpc                 C   s   | j tj_d S r   )rQ   r   r   rP   r2   r   r   r   Ú	unfakeftp„   s    zFakeFTPMixin.unfakeftpN)r3   r4   r5   rR   rS   r   r   r   r   rD   t   s   rD   c                   @   s   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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   sp   t d| jj dƒ| _ttjdƒ}z| | j¡ W | ¡  n
| ¡  0 tj| _	t
j | j	¡| _td| j ƒ| _d S )Nztest_urllib: %s
ÚasciiÚwbúfile:%s)ÚbytesÚ	__class__r3   Útextr   r   ÚTESTFNÚwriter1   Úpathnamer   ÚparseÚquoteÚquoted_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)ra   r1   ÚosÚremover   r[   r2   r   r   r   ÚtearDown   s    
zurlopen_FileTests.tearDownc                 C   s&   dD ]}|   t| j|ƒd| ¡ qd S )N)	r.   r/   Ú	readlinesÚfilenor1   ÚinfoÚgeturlÚgetcodeÚ__iter__ú/object returned by urlopen() lacks %s attribute)Ú
assertTrueÚhasattrra   ©r!   Úattrr   r   r   Útest_interface¢   s    ÿÿz urlopen_FileTests.test_interfacec                 C   s   |   | j| j ¡ ¡ d S r   )ÚassertEqualrZ   ra   r.   r2   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)rs   rZ   ra   r/   r2   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)ra   rg   rs   r   rZ   )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)	ra   rh   ÚassertIsInstanceÚintrs   rd   r.   r   rZ   )r!   Zfile_numr   r   r   Útest_filenoº   s
    
ÿzurlopen_FileTests.test_filenoc                 C   s   | j  ¡  d S r   )ra   r1   r2   r   r   r   Ú
test_closeÁ   s    zurlopen_FileTests.test_closec                 C   s   |   | jjtjj¡ d S r   )rw   ra   ZheadersÚemailÚmessageÚMessager2   r   r   r   Útest_headersÆ   s    zurlopen_FileTests.test_headersc                 C   s   |   | jj| j¡ d S r   )rs   ra   r   r`   r2   r   r   r   Útest_urlÉ   s    zurlopen_FileTests.test_urlc                 C   s   |   | jj¡ d S r   )ÚassertIsNonera   Ústatusr2   r   r   r   Útest_statusÌ   s    zurlopen_FileTests.test_statusc                 C   s   |   | j ¡ tjj¡ d S r   )rw   ra   ri   r{   r|   r}   r2   r   r   r   Ú	test_infoÏ   s    zurlopen_FileTests.test_infoc                 C   s   |   | j ¡ | j¡ d S r   )rs   ra   rj   r`   r2   r   r   r   Útest_geturlÒ   s    zurlopen_FileTests.test_geturlc                 C   s   |   | j ¡ ¡ d S r   )r€   ra   rk   r2   r   r   r   Útest_getcodeÕ   s    zurlopen_FileTests.test_getcodec                 C   s   | j D ]}|  || j¡ qd S r   )ra   rs   rZ   )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   r]   r2   r   r   r   Útest_relativelocalfileá   s    z(urlopen_FileTests.test_relativelocalfileN)r3   r4   r5   Ú__doc__rc   rf   rr   rt   ru   rv   ry   rz   r~   r   r‚   rƒ   r„   r…   r‡   rŠ   r   r   r   r   rT   ˆ   s"   	rT   c                   @   sD   e 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 )Ú
ProxyTestsc                 C   s6   t  ¡ | _ttjƒD ]}d| ¡ v r| j |¡ qd S )NÚproxy)r   ZEnvironmentVarGuardÚenvÚlistrd   ÚenvironÚlowerÚunset)r!   Úkr   r   r   rc   ç   s    
zProxyTests.setUpc                 C   s   | j  ¡  | ` d S r   )rŽ   Ú__exit__r2   r   r   r   rf   ï   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Únoú0localhost, anotherdomain.com, newdomain.com:1234úanotherdomain.comúanotherdomain.com:8888únewdomain.com:1234)rŽ   Úsetr   r   Úgetproxies_environmentrs   rn   Úproxy_bypass_environment©r!   r   r   r   r   Ú+test_getproxies_environment_keep_no_proxiesô   s    
z6ProxyTests.test_getproxies_environment_keep_no_proxiesc              	   C   sˆ   zh| j  dd¡ tj ¡ }|  d|d ¡ | j  dd¡ tj ¡ }|  d|¡ W | j  d¡ | j  d¡ n| j  d¡ | j  d¡ 0 d S )NÚ
HTTP_PROXYúhttp://somewhere:3128r<   ZREQUEST_METHODÚGET)rŽ   rœ   r   r   r   rs   ÚassertNotInr’   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ƒ¡ |  |dƒ¡ |  |dƒ¡ |  |dƒ¡ d S )Nr•   z8localhost, anotherdomain.com, newdomain.com:1234, .d.o.tr–   Z	LocalHostZ	LOCALHOSTz
.localhostr›   z.newdomain.com:1234z	foo.d.o.tzd.o.trš   z.anotherdomain.com:8888zwww.newdomain.com:1234Zprelocalhostúnewdomain.comznewdomain.com:1235©r   r   rž   rŽ   rœ   rn   ÚassertFalse©r!   Zbypassr   r   r   Ú(test_proxy_bypass_environment_host_match  s$    ÿz3ProxyTests.test_proxy_bypass_environment_host_matchc                 C   sn   t jj}| j dd¡ |  |dƒ¡ |  |dƒ¡ | j dd¡ |  |dƒ¡ |  |dƒ¡ |  |dƒ¡ d S )Nr•   Ú*r¦   r›   z*, anotherdomain.comr™   r§   r©   r   r   r   Ú*test_proxy_bypass_environment_always_match  s    z5ProxyTests.test_proxy_bypass_environment_always_matchc                 C   sD   t jj}| j dd¡ |  |dƒ¡ |  |dƒ¡ |  |dƒ¡ d S )Nr•   r˜   z
localhost
zanotherdomain.com:8888
znewdomain.com:1234
)r   r   rž   rŽ   rœ   r¨   r©   r   r   r   Ú%test_proxy_bypass_environment_newline(  s    ÿz0ProxyTests.test_proxy_bypass_environment_newlineN)
r3   r4   r5   rc   rf   r    r¥   rª   r¬   r­   r   r   r   r   rŒ   å   s   
rŒ   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 r   )rd   r   Ú
_saved_envÚcollectionsÚOrderedDictr2   r   r   r   rc   3  s    zProxyTests_withOrderedEnv.setUpc                 C   s   | j t_d S r   )r¯   rd   r   r2   r   r   r   rf   9  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_proxyr¢   r¡   z%localhost, noproxy.com, my.proxy:1234zxyz.comznoproxy.com:5678zmy.proxy:1234zmy.proxyzhttp://somewhereelse:3128Z
Http_Proxyr<   )	rd   r   r¨   r   r   rž   r   rs   rn   rŸ   r   r   r   Ú,test_getproxies_environment_prefer_lowercase<  s&    









zFProxyTests_withOrderedEnv.test_getproxies_environment_prefer_lowercaseN)r3   r4   r5   rc   rf   r³   r   r   r   r   r®   1  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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/d0„ Z e e	d¡d1d2„ ƒZ!d3S )4Úurlopen_HttpTestsz.Test urlopen() opening a fake http connection.c                 C   sv   |   d| d ¡ zTtdƒ}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W |  ¡  n
|  ¡  0 d S )Ns   HTTP/s    200 OK

Hello!úhttp://python.org/ó   Hello!r*   éÈ   ©r?   r   rs   r/   rj   rk   rC   )r!   ÚverÚfpr   r   r   Ú
check_readX  s    zurlopen_HttpTests.check_readc                 C   sF   d}|   d¡ z(tj |¡}|  | ¡ |¡ W |  ¡  n
|  ¡  0 d S )Nz-http://docs.python.org/library/urllib.html#OKó   HTTP/1.1 200 OK

Hello!)r?   r   r   r   rs   rj   rC   )r!   r   rº   r   r   r   Útest_url_fragmentc  s    
z#urlopen_HttpTests.test_url_fragmentc                 C   s<   |   d¡ z"tdƒ}|  |jj¡ W |  ¡  n
|  ¡  0 d S )Nr¼   úhttp://www.python.org)r?   r   rn   rº   Z
will_closerC   )r!   Úrespr   r   r   Útest_willclosem  s
    
z urlopen_HttpTests.test_willclosezssl module requiredc              	   C   s  t tddƒƒdg D ]ø}t|ƒ}d|› d}|  d¡ zÊt|ƒ dd¡}tjj}|  	|d	|› ¡" t
j d
|› ¡ W d   ƒ n1 sŠ0    Y  |  	|d	|› ¡" t
j d|› ¡ W d   ƒ n1 sÎ0    Y  td
|› ƒ}|  || ¡ ¡ W |  ¡  q|  ¡  0 qd S )Nr   é!   é   z//localhost:7777/testú/ó   HTTP/1.1 200 OK

Hello.ú\ú\\úcontain control.*úhttp:úhttps:)r   ÚrangeÚchrr?   ÚreprÚreplacer<   r=   Ú
InvalidURLÚassertRaisesRegexr   r   r   r¤   rj   rC   )r!   Úchar_nor   Úschemeless_urlÚescaped_char_reprrÎ   r¿   r   r   r   Ú(test_url_path_with_control_char_rejectedu  s$    

ÿ0
ÿ0z:urlopen_HttpTests.test_url_path_with_control_char_rejectedc                 C   sø   |   d¡ d}d| d }zÎtjj}|  |d¡" tj d|› ¡ W d   ƒ n1 sX0    Y  |  |d¡" tj d|› ¡ W d   ƒ n1 s–0    Y  td|› ƒ}|  d	| 	¡ ¡ |  d
| 	¡ ¡ |  d| 	¡ ¡ W |  
¡  n
|  
¡  0 d S )NrÄ   z:localhost:7777?a=1 HTTP/1.1
X-injected: header
TEST: 123ú//ú:8080/test/?test=az*contain control.*\\r.*(found at least . .)rÈ   úcontain control.*\\nrÉ   ú úÚ
)r?   r<   r=   rÎ   rÏ   r   r   r   r¤   rj   rC   )r!   rG   rÑ   rÎ   r¿   r   r   r   Ú4test_url_path_with_newline_header_injection_rejected  s     
ÿ00zFurlopen_HttpTests.test_url_path_with_newline_header_injection_rejectedc              	   C   sì   t tddƒƒdg D ]Ò}t|ƒ}d|› d}|  d¡ z¤t|ƒ dd¡}tjj}|  	|d	|› ¡ t
d
|› ƒ W d   ƒ n1 s†0    Y  |  	|d	|› ¡ t
d|› ƒ W d   ƒ n1 sÆ0    Y  W |  ¡  q|  ¡  0 qd S )Nr   rÁ   rÂ   z//localhostz/test/rÄ   rÅ   rÆ   rÇ   rÈ   rÉ   )r   rÊ   rË   r?   rÌ   rÍ   r<   r=   rÎ   rÏ   r   rC   )r!   rÐ   r   rÑ   rÒ   rÎ   r   r   r   Ú(test_url_host_with_control_char_rejectedª  s    

ÿ,.z:urlopen_HttpTests.test_url_host_with_control_char_rejectedc                 C   s²   |   d¡ d}d| d }zˆtjj}|  |d¡ td|› ƒ W d   ƒ n1 sT0    Y  |  |d¡ td|› ƒ W d   ƒ n1 sŽ0    Y  W |  ¡  n
|  ¡  0 d S )	NrÄ   zlocalhost
X-injected: header
rÔ   rÕ   zcontain control.*\\rrÈ   rÖ   rÉ   )r?   r<   r=   rÎ   rÏ   r   rC   )r!   rG   rÑ   rÎ   r   r   r   Ú4test_url_host_with_newline_header_injection_rejected»  s    
ÿ,.zFurlopen_HttpTests.test_url_host_with_newline_header_injection_rejectedc                 C   s   |   d¡ d S )Ns   0.9©r»   r2   r   r   r   Útest_read_0_9Ê  s    zurlopen_HttpTests.test_read_0_9c                 C   s   |   d¡ d S )Ns   1.0rÝ   r2   r   r   r   Útest_read_1_0Ï  s    zurlopen_HttpTests.test_read_1_0c                 C   s   |   d¡ d S )Ns   1.1rÝ   r2   r   r   r   Útest_read_1_1Ò  s    zurlopen_HttpTests.test_read_1_1c                 C   s8   | j ddd z|  ttd¡ W |  ¡  n
|  ¡  0 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
TrA   rµ   ©r?   rˆ   ÚOSErrorr   rC   r2   r   r   r   Útest_read_bogusÕ  s    ûz!urlopen_HttpTests.test_read_bogusc                 C   sf   | j ddd zHd}|  tjj|¡ tdƒ W d   ƒ n1 sB0    Y  W |  ¡  n
|  ¡  0 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
TrA   zRedirection to url 'file:rµ   )r?   rÏ   r   ÚerrorÚ	HTTPErrorr   rC   )r!   Úmsgr   r   r   Útest_invalid_redirectâ  s    ú(z'urlopen_HttpTests.test_invalid_redirectc              	   C   sN   t tƒ jƒD ]<}| jddd z|  tjjtd¡ W |  	¡  q|  	¡  0 qd S )Ns_   HTTP/1.1 302 Found
Location: file://guidocomputer.athome.com:/python/license
Connection: close
TrA   úhttp://something)
rÊ   r   Zmaxtriesr?   rˆ   r   rä   rå   r   rC   )r!   Úir   r   r   Útest_redirect_limit_independentò  s    ýÿz1urlopen_HttpTests.test_redirect_limit_independentc                 C   s4   |   d¡ z|  ttd¡ W |  ¡  n
|  ¡  0 d S )Nr*   rè   rá   r2   r   r   r   Útest_empty_socket   s    
z#urlopen_HttpTests.test_empty_socketc                 C   sV   |   tjj¡}tdƒ W d   ƒ n1 s,0    Y  |  |jj¡ |  |jj¡ d S )Nz/file://localhost/a/file/which/doesnot/exists.py)	rˆ   r   rä   ÚURLErrorr   rn   Ú	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¡ }z\|  tj |¡¡ t|ƒ}|  |¡ W d   ƒ n1 s\0    Y  W t 	|¡ t 
|¡ nt 	|¡ t 
|¡ 0 |  tj |¡¡ |  tjj¡ t|ƒ W d   ƒ n1 sÒ0    Y  d S )Nzfile://localhost/rÃ   )ÚtempfileÚmkstemprÍ   rd   ÚpathÚseprn   Úexistsr   r1   Úunlinkr¨   rˆ   r   rä   rì   )r!   ÚfdZtmp_fileZtmp_fileurlZfobjr   r   r   Útest_file_notexists  s    
*
ÿ
z%urlopen_HttpTests.test_file_notexistsc                 C   sZ   d}|   tjj¡}t|ƒ W d   ƒ n1 s00    Y  |  |jj¡ |  |jj	¡ d S )Nzftp:///path©
rˆ   r   rä   rì   r   r¨   rí   rî   rn   rï   )r!   Ztest_ftp_urlrñ   r   r   r   Útest_ftp_nohost  s
    &z!urlopen_HttpTests.test_ftp_nohostc                 C   sV   |   tjj¡}tdƒ W d   ƒ n1 s,0    Y  |  |jj¡ |  |jj	¡ d S )Nz.ftp://localhost/a/file/which/doesnot/exists.pyrû   rð   r   r   r   Útest_ftp_nonexisting%  s    &z&urlopen_HttpTests.test_ftp_nonexistingZMAXFTPCACHEr   c                 C   sH   |   ¡  z0tj ddddg ¡tjjd< tdƒ W |  ¡  n
|  ¡  0 d S )NrE   Úpassr–   é   Útestzftp://localhost)rR   r   r   rP   Zftpcacher   rS   r2   r   r   r   Útest_ftp_cache_pruning+  s
    
z(urlopen_HttpTests.test_ftp_cache_pruningc                 C   sn   |   d¡ zTtdƒ}|  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ |  | ¡ d¡ W |  ¡  n
|  ¡  0 d S )Nó   HTTP/1.0 200 OK

Hello!zhttp://user:pass@python.org/r¶   r*   r·   r¸   )r!   rº   r   r   r   Útest_userpass_inurl4  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 |  ¡  n
|  ¡  0 d S )
Nr  za b:c dzhttp://{}@python.org/zAuthorization: Basic %s
ÚASCIIzUTF-8r¶   r*   r·   )r?   Úformatr<   r=   r>   r   ÚencodeÚdecoder   ÚassertInr   rs   r/   ZassertNotEqualrj   rk   rC   )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   ƒ n1 s.0    Y  d S ©Nr²   )r   r   r   r   r   Ú	URLopenerr2   r   r   r   Útest_URLopener_deprecationR  s    z,urlopen_HttpTests.test_URLopener_deprecationc              	   C   sv   t  ¡ }t dtf¡L |  t¡" tjj	dd|d W d   ƒ n1 sJ0    Y  W d   ƒ n1 sh0    Y  d S )Nr²   zhttps://localhostz/nonexistent/path)ZcafileÚcontext)
ÚsslZcreate_default_contextr   r   r   rˆ   r‰   r   r   r   )r!   r  r   r   r   Útest_cafile_and_contextV  s    ÿz)urlopen_HttpTests.test_cafile_and_contextN)"r3   r4   r5   r‹   r»   r½   rÀ   ÚunittestÚ
skipUnlessr  rÓ   rÚ   rÛ   rÜ   rÞ   rß   rà   rã   rç   rê   rë   rò   rú   rü   rý   r   rO   r   r   r  r  r	  r  r  r   r   r   r   r´   U  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   s`   |   tjj¡ 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)Ú
addCleanupr   r   Ú
urlcleanuprZ   ÚimageÚtext_urlÚtext_url_base64Ú	image_urlr   Útext_url_respÚtext_url_base64_respÚimage_url_respr2   r   r   r   rc   c  s    ÿÿÿÿÿzurlopen_DataTests.setUpc                 C   s&   dD ]}|   t| j|ƒd| ¡ qd S )N)r.   r/   rg   r1   ri   rj   rk   rl   rm   )rn   ro   r  rp   r   r   r   rr   ‚  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)rw   r  ri   r{   r|   r}   rs   r  Ú
get_paramsr  Ústrr   r  r   r   r   r2   r   r   r   rƒ   Š  s    ÿÿÿzurlopen_DataTests.test_infoc                 C   s@   |   | j ¡ | j¡ |   | j ¡ | j¡ |   | j ¡ | j¡ d S r   )rs   r  rj   r  r  r  r  r  r2   r   r   r   r„   “  s
    ÿzurlopen_DataTests.test_geturlc                 C   s0   |   | j ¡  t| j ¡  ¡ ƒd ¡| j¡ d S ©Nr  )rs   r  r.   r  Údictri   r  rZ   r2   r   r   r   Útest_read_text™  s
    ÿÿz urlopen_DataTests.test_read_textc                 C   s0   |   | j ¡  t| j ¡  ¡ ƒd ¡| j¡ d S r  )rs   r  r.   r  r   ri   r  rZ   r2   r   r   r   Útest_read_text_base64  s
    ÿþz'urlopen_DataTests.test_read_text_base64c                 C   s   |   | j ¡ | j¡ d S r   )rs   r  r.   r  r2   r   r   r   Útest_read_image¢  s    z!urlopen_DataTests.test_read_imagec                 C   s   |   ttjjd¡ d S )Nzdata:text/plain©rˆ   r‰   r   r   r   r2   r   r   r   Útest_missing_comma¥  s    z$urlopen_DataTests.test_missing_commac                 C   s   |   ttjjd¡ d S )Nzdata:;base64,Cg=r$  r2   r   r   r   Útest_invalid_base64_data¨  s    z*urlopen_DataTests.test_invalid_base64_dataN)r3   r4   r5   r‹   rc   rr   rƒ   r„   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†   |   tjj¡ g | _|  tj¡ d| _z>t	tjdƒ}| 
| j¡ | ¡  W z| ¡  W q‚   Y q‚0 nz| ¡  W n   Y n0 0 d S )Ns   testing urllib.urlretrieverV   )r  r   r   r  Ú	tempFilesÚregisterFileForCleanUpr   r[   rZ   r   r\   r1   )r!   ÚFILEr   r   r   rc   °  s    	
ÿzurlretrieve_FileTests.setUpc                 C   s,   | j D ] }zt |¡ W q   Y q0 qd S r   )r(  rd   re   )r!   Zeachr   r   r   rf   È  s    
zurlretrieve_FileTests.tearDownc                 C   sH   t j |¡}z| d¡ W n ty6   t d¡‚Y n0 dtj 	|¡ S )Nzutf-8z!filePath is not encodable to utf8z	file://%s)
rd   rõ   Úabspathr  ÚUnicodeEncodeErrorr  ZSkipTestr   r   r   )r!   ZfilePathr   r   r   ÚconstructLocalFileUrlÎ  s    z+urlretrieve_FileTests.constructLocalFileUrlr*   c                 C   st   t  ¡ \}}zF|  |¡ t |d¡}| |¡ | ¡  W z| ¡  W qp   Y qp0 nz| ¡  W n   Y n0 0 |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.rV   )ró   rô   r)  rd   Úfdopenr\   r1   )r!   r   ZnewFdZnewFilePathZnewFiler   r   r   ÚcreateNewTempFileÖ  s    


ÿz'urlretrieve_FileTests.createNewTempFilec                 C   s   | j  |¡ d S r   )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 )NrW   r   r   zFdid not get an email.message.Message instance as second returned value)
r   r   Úurlretriever   r[   rs   rw   r{   r|   r}   )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 z| ¡  W q¤   Y q¤0 nz| ¡  W n   Y n0 0 |  | j|¡ d S )Nú%s.2r   zcopy of the file was not madeÚrb)r   r[   r)  r   r   r1  r-  rs   rn   rd   rõ   r÷   r   r.   r1   rZ   )r!   Úsecond_tempr2  r*  rZ   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   )rw   rx   rs   )Úblock_countÚblock_read_sizeÚ	file_sizeZcount_holderr2   r   r   Ú
hooktester  s
    z9urlretrieve_FileTests.test_reporthook.<locals>.hooktesterr4  )r   r[   r)  r   r   r1  r-  )r!   r;  r6  r   r2   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 r   ©r0  ©r8  r9  r:  Z_reportr   r   r   r;    s    zAurlretrieve_FileTests.test_reporthook_0_bytes.<locals>.hooktesterr   r   r   ©	r/  r   r   r1  r-  r   r[   rs   r   ©r!   Úreportr;  Z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 r   r=  r>  r   r   r   r;  "  s    zAurlretrieve_FileTests.test_reporthook_5_bytes.<locals>.hooktesters   xxxxxr   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 r   r=  r>  r   r   r   r;  0  s    zDurlretrieve_FileTests.test_reporthook_8193_bytes.<locals>.hooktesteró   xi   é   r   r   r   i    r?  r@  r   r   r   Útest_reporthook_8193_bytes+  s    ÿz0urlretrieve_FileTests.test_reporthook_8193_bytesN)r*   )r3   r4   r5   r‹   rc   rf   r-  r/  r)  r3  r7  r<  rB  rD  rG  r   r   r   r   r'  ­  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   s|   |   tjj¡ |  d¡ dd„ }|  tjj¡: ztjjt	j
|d W |  ¡  n
|  ¡  0 W d   ƒ n1 sn0    Y  d S )NóÑ   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 r   r   )Zpar1Zpar2Zpar3r   r   r   Ú_reporthookL  s    zYurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError.<locals>._reporthook)Z
reporthook©r  r   r   r  r?   rˆ   rä   ÚContentTooShortErrorr1  r   ZTEST_HTTP_URLrC   )r!   rJ  r   r   r   Ú.test_short_content_raises_ContentTooShortError?  s    


ÿzDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorc              	   C   sp   |   tjj¡ |  d¡ |  tjj¡6 ztj t	j
¡ W |  ¡  n
|  ¡  0 W d   ƒ n1 sb0    Y  d S )NrI  rK  r2   r   r   r   ÚAtest_short_content_raises_ContentTooShortError_without_reporthookV  s    
	zWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookN)r3   r4   r5   r‹   rM  rN  r   r   r   r   rH  <  s   rH  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   sV   d  g d¢¡}tj |¡}|  ||d||f ¡ tj |¡}|  ||d||f ¡ d S )Nr²   )ÚABCDEFGHIJKLMNOPQRSTUVWXYZÚabcdefghijklmnopqrstuvwxyzÚ
0123456789z_.-~úusing quote(): %r != %rúusing quote_plus(): %r != %r)Újoinr   r^   r_   rs   Ú
quote_plus)r!   Zdo_not_quoter2  r   r   r   Útest_never_quoteƒ  s    
ÿ
ÿzQuotingTests.test_never_quotec                 C   s   |   tjjjd d¡ d S )Nr   rÃ   )rs   r   r^   r_   Ú__defaults__r2   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<>©ÚsaferS  rT  s   <>u   aÃ¼búlatin-1õ   Ã¼)Úencodingr[  r²   ó   ü)r   r^   r_   rs   rV  )r!   Zquote_by_defaultr2  Úexpectr   r   r   Ú	test_safe”  s>    
ÿÿÿÿ
ÿÿÿÿÿzQuotingTests.test_safec              	   C   sè   dd„ t dƒD ƒ}| d¡ | tdƒ¡ d |¡}|D ]\}tj |¡}|  t|ƒ|d|t|ƒ|f ¡ tj 	|¡}|  t|ƒ|d|t|ƒ|f ¡ q8~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>´  r*   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%5DcdrS  rT  )
rÊ   r0  rË   rU  r   r^   r_   rs   r   rV  )r!   Zshould_quoter   r2  Zpartial_quoteÚexpectedr   r   r   Útest_default_quoting±  s8    

þÿþÿ
ÿ
ÿ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×   rS  ú+zusing quote_plus(): %r != +z
a b cd e frT  )r   r^   r_   rs   r   rV  rÍ   )r!   r2  Ú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+gammarh  zalpha+beta+gammas   alpha+beta gammaó   +)rs   r   r^   rV  r2   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 )Nó   ¢Øabÿú%A2%D8ab%FFrS  r\  ©r^  z"using quote_from_bytes(): %r != %r)r   r^   r_   rs   rˆ   Ú	TypeErrorÚquote_from_bytes©r!   ri  r`  r2  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%BFrS  ©r^  Úerrorsrn  r\  ro  õ   æ¼¢å­—ú%E6%BC%A2%E5%AD%97z%3F%3FrÍ   z%26%2328450%3B%26%2323383%3BÚxmlcharrefreplace)r   r^   r_   rs   rˆ   r,  rr  r   r   r   Útest_quote_with_unicodeü  sT    
ÿ
ÿ
ÿ
ÿÿ
ÿ
ÿ
ÿ
ÿ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+%FFr\  ro  rT  u   abæ¼¢å­— cdzab%3F%3F+cdrÍ   rt  )r   r^   rV  rs   rr  r   r   r   Útest_quote_plus_with_unicode&  s    
ÿ
ÿ
ÿz)QuotingTests.test_quote_plus_with_unicodeN)r3   r4   r5   r‹   rW  rY  ra  rg  rj  rl  rs  ry  rz  r   r   r   r   rO  i  s   *rO  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S )ÚUnquotingTestszwTests for unquote() and unquote_plus()

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

    c                 C   sÔ   g }t dƒD ]f}tt|ƒƒ}t|ƒ}tj |¡}|  ||d||f ¡ tj |¡}|  ||d||f ¡ | |¡ qd 	|¡}~tj |¡}|  | 
d¡dd| ¡ |  ttftjjd ¡ |  ttftjjd¡ d S )	Né€   úusing unquote(): %r != %rúusing unquote_plus(): %r != %rr²   r	   r   z/using unquote(): not all characters escaped: %sr   )rÊ   r   rË   r   r^   Úunquoters   Úunquote_plusr0  rU  Úcountrˆ   rp  ÚAttributeError)r!   Zescape_listrc  ri  r`  r2  Zescape_stringr   r   r   Útest_unquoting=  s2    
ÿÿÿ
ÿÿ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%xabr}  z%xr	   rU   ú"using unquote_to_bytes(): %r != %rr   )	r   r^   r  rs   rX   Úunquote_to_bytesrˆ   rp  r‚  rr  r   r   r   Útest_unquoting_badpercentT  sL    
ÿ
ÿ
ÿ

ÿ

ÿ

ÿz(UnquotingTests.test_unquoting_badpercentc                 C   s.   d}d}t j |¡}|  ||d||f ¡ d S )Nz%Ab%eAs   «êr„  ©r   r^   r…  rs   rr  r   r   r   Útest_unquoting_mixed_casex  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abcdrS  r~  )r   r   r^   r  rs   r€  rr  r   r   r   Útest_unquoting_parts  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...r}  rh  r×   r~  )r   r^   r  rs   rÍ   r€  rr  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 )	Nú"br%C3%BCckner_sapporo_20050930.docs   brÃ¼ckner_sapporo_20050930.docr„  õ	   æ¼¢%C3%BCs   æ¼¢Ã¼s   %A2%D8ab%FFrm  s	   %A2Øab%FFr‡  rr  r   r   r   Útest_unquote_to_bytes™  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 )NrŒ  u   brÃ¼ckner_sapporo_20050930.docr}  rt  zbr%FCckner_sapporo_20050930.docr\  ro  rw  rv  z%F3%B1u   ï¿½rÍ   )ru  r²   Úignorer  u   æ¼¢Ã¼u   æ¼¢%FC©r   r^   r  rs   rr  r   r   r   Útest_unquote_with_unicode¸  sb    
ÿ
ÿÿ
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ
ÿz(UnquotingTests.test_unquote_with_unicodec                 C   s‚   d}d}t j |¡}|  ||d||f ¡ d}d}t j |¡}|  ||d||f ¡ d}d}t j |¡}|  ||d||f ¡ d S )Ns   blueberryjamZblueberryjamr}  s   blÃ¥bÃ¦rsyltetÃ¸yu   blÃ¥bÃ¦rsyltetÃ¸ys   bl%c3%a5b%c3%a6rsyltet%c3%b8ju   blÃ¥bÃ¦rsyltetÃ¸jr  rr  r   r   r   Útest_unquoting_with_bytes_inputò  s$    
ÿ
ÿ
ÿz.UnquotingTests.test_unquoting_with_bytes_inputN)r3   r4   r5   r‹   rƒ  r†  rˆ  rŠ  r‹  rŽ  r‘  r’  r   r   r   r   r{  6  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¾   g d¢}t j |¡}|D ]}|  ||d|||f ¡ q|  | 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  rs   r  Úindexrn   Úisdigitr   )	r!   ri  Z	test_typeZexpect_somewherer2  rf  Zamp_locationZon_amp_leftZon_amp_rightr   r   r   Úhelp_inputtype  s4    ÿÿÿÿ
ÿÿþÿ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©r™  r2   r   r   r   Útest_using_mapping,  s    ÿz"urlencode_Tests.test_using_mappingc                 C   s   |   g d¢d¡ d S )N))r  rš  )rž  r›  )rŸ  rœ  z*using sequence of two-item tuples as inputr   r2   r   r   r   Útest_using_sequence1  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^   r–  rs   rr  r   r   r   Útest_quoting6  s    zurlencode_Tests.test_quotingc                 C   sŽ   dg d¢i}dt j tg d¢ƒ¡ }t j |¡}|  ||¡ t j |d¡}|d D ]}d| }|  ||¡ qR|  | d¡dd| d¡ ¡ d S )NZsequence)rš  r›  rœ  zsequence=%sTr”  r   zExpected 2 '&'s, got %s)r   r^   rV  r  r–  rs   r  r  )r!   ri  r`  r2  Úvaluer   r   r   Ú
test_doseqA  s    ÿzurlencode_Tests.test_doseqc                 C   s,   |   dtj i ¡¡ |   dtj g ¡¡ d S r
  ©rs   r   r^   r–  r2   r   r   r   Útest_empty_sequenceN  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=Noner§  r2   r   r   r   Útest_nonstring_valuesR  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=2r©  r   r   Tz
a=None&a=a)r©  r   )Úbr   za=a&a=b)rs   r   r^   r–  r°   r±   r    r   r   r   Útest_nonstring_seq_valuesV  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©)õ   Â õ   Ãú%3F=%3Fr  rÍ   rt  ú%C2%A0=%C3%81ú%A0=%C1r\  ro  ©r   r^   r–  rs   rr  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 )Nr­  r°  Tr  rÍ   )Údoseqr^  ru  ))r®  )r   r¯  z%3F=1&%3F=%3Frt  r±  ))r®  )é*   r¯  z%C2%A0=42&%C2%A0=%C3%81r²  r\  ro  z%A0=42&%A0=%C1r³  rr  r   r   r   Útest_urlencode_encoding_doseqq  s8    
ÿ
ÿz-urlencode_Tests.test_urlencode_encoding_doseqc                 C   s`   d}d}t j |¡}|  ||¡ t j |d¡}|  ||¡ d}d}t j |d¡}|  ||¡ d S )N©)ó    $ó   Á$z%A0%24=%C1%24T))r¹  )r¶  rº  z%A0%24=42&%A0%24=%C1%24r³  rr  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 )Nr¸  z:$rZ  z	%A0$=%C1$T)rµ  r[  ))r¹  )rº  é   r¶  z%A0$=%C1$&%A0$=13&%A0$=42r\  )r[  r^  )rµ  r[  r^  r³  )r!   ri  r2  r`  r   r   r   Ú&test_urlencode_encoding_safe_parameter¤  s:    
ÿÿÿz6urlencode_Tests.test_urlencode_encoding_safe_parameterN)r3   r4   r5   r‹   r™  r¡  r¢  r¤  r¦  r¨  rª  r¬  r´  r·  r»  r½  r   r   r   r   r“  	  s    %r“  c                   @   sT   e Zd ZdZdd„ Zdd„ Ze ej	dkd¡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ofr©  rõ   zparts/of/a/pathúpathname2url() failed; %s != %szurl2pathame() failed; %s != %s)rd   rõ   rU  r   r   r   rs   r   )r!   Zexpected_pathZexpected_urlr2  r   r   r   r3  Ï  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/hererÀ  zurl2pathname() failed; %s != %sz	make sureZusing_quotez%s/using_quotezmake+sure/using_unquotez	make+sureZusing_unquote)
rd   rõ   rU  r   r^   r_   r   r   rs   r   rr  r   r   r   r¤  Ü  s>    ÿÿÿÿÿÿÿÿzPathname_Tests.test_quotingÚwin32z*test specific to the nturl2path functions.c                 C   sX   d}d}t j |¡}|  ||d||f ¡ d}d}t j |¡}|  ||d||f ¡ d S )Nz
\\?\C:\dirz	///C:/dirrÀ  z\\?\unc\server\share\dirz/server/share/dir)r   r   r   rs   rr  r   r   r   Útest_prefixes÷  s     ÿÿÿÿzPathname_Tests.test_prefixesz.test specific to the urllib.url2path function.c                 C   sb   d}d}|D ]&}t j |¡}|  ||d||f ¡ qd}d}t j |¡}|  ||d||f ¡ d S )N)z/C:/z///C:/z/C|//úC:\z/urllib.request..url2pathname() failed; %s != %sz
///C|/pathzC:\pathz.urllib.request.url2pathname() failed; %s != %s)r   r   r   rs   )r!   ri  r`  r   r2  r   r   r   Útest_ntpath	  s"    ÿÿÿÿzPathname_Tests.test_ntpathN)r3   r4   r5   r‹   r3  r¤  r  r  ÚsysÚplatformrÃ  rÅ  r   r   r   r   r¾  Ì  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)rw   r   r   ZthishostÚtupler2   r   r   r   Útest_thishost  s    zUtility_Tests.test_thishostN)r3   r4   r5   r‹   rÊ  r   r   r   r   rÈ    s   rÈ  c                   @   sT   e Zd ZdZdd„ Zejeddd„ ƒZejeddd„ ƒZ	ejedd	d
„ ƒZ
dS )ÚURLopener_Testsz4Testcase to test the open method of URLopener class.c                 C   sn   G dd„ dt jjƒ}t dtf¡8 |  |ƒ  d¡d¡ |  |ƒ  d¡d¡ W d   ƒ n1 s`0    Y  d S )Nc                   @   s   e Zd Zdd„ ZdS )z8URLopener_Tests.test_quoted_open.<locals>.DummyURLopenerc                 S   s   |S r   r   ©r!   r   r   r   r   Ú	open_spam'  s    zBURLopener_Tests.test_quoted_open.<locals>.DummyURLopener.open_spamN)r3   r4   r5   rÍ  r   r   r   r   ÚDummyURLopener&  s   rÎ  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   rs   r   )r!   rÎ  r   r   r   Útest_quoted_open%  s     ÿÿ
ÿÿ
ÿþz URLopener_Tests.test_quoted_open)Úcategoryc                 C   s†   t  ¡ j}tj|d\}}t |¡ dtj |¡ }tj 	¡  
|¡\}}|  tj |¡tj |¡¡ W d   ƒ n1 sx0    Y  d S )N)Údirzfile:)r   Ztemp_dirró   rô   rd   r1   r   r   r   r  Úretrievers   rõ   Únormcase)r!   Ztmpdirrù   ZtmpfileZfileurlrî   Ú_r   r   r   Útest_urlopener_retrieve_file4  s    

z,URLopener_Tests.test_urlopener_retrieve_filec                 C   sJ   d}|   d¡ |  | j¡ tj ¡  |¡\}}|  tj	 
|¡d d¡ d S )Nzhttp://www.python.org/file.txtr¼   r   z.txt)r?   r  rC   r   r   r  rÒ  rs   rd   rõ   Úsplitext)r!   r   rî   rÔ  r   r   r   Útest_urlopener_retrieve_remote>  s
    
z.URLopener_Tests.test_urlopener_retrieve_remotec                 C   s„   G dd„ dt jjƒ}dD ]f}|  tt jj|¡ |  tt j ¡ j|¡ |  tt j ¡ j|¡ |  t|ƒ j|¡ |  t|ƒ j|¡ q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 r   r   rÌ  r   r   r   Úopen_local_fileJ  s    zLURLopener_Tests.test_local_file_open.<locals>.DummyURLopener.open_local_fileN)r3   r4   r5   rØ  r   r   r   r   rÎ  I  s   rÎ  )zlocal_file://examplezlocal-file://example)r   r   r  rˆ   râ   r   r   rÒ  )r!   rÎ  r   r   r   r   Útest_local_file_openF  s    z$URLopener_Tests.test_local_file_openN)r3   r4   r5   r‹   rÏ  r   Zignore_warningsr   rÕ  r×  rÙ  r   r   r   r   rË  "  s   

	


rË  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 )Nr¾   r£   ZPOST)r   r   ÚRequestrs   Ú
get_method©r!   rÛ  r   r   r   r   Útest_default_valuesW  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 )Nr¾   ZHEAD)Úmethodr£   )r   r   rÛ  rs   rß  rÜ  rÝ  r   r   r   Útest_with_method_arg^  s    z!RequestTests.test_with_method_argN)r3   r4   r5   r‹   rÞ  rà  r   r   r   r   rÚ  T  s   rÚ  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|úC:ú///C:z///C|/rÄ  ©rs   r   r2   r   r   r   Útest_converting_drive_lettern  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\rä  r2   r   r   r   Ú$test_converting_when_no_drive_letters  s    z6URL2PathNameTests.test_converting_when_no_drive_letterc                 C   s   |   tdƒd¡ d S )Nz///C|/foo/bar/spam.fooúC:\foo\bar\spam.foorä  r2   r   r   r   Útest_simple_comparex  s    
ÿz%URL2PathNameTests.test_simple_comparec                 C   s   |   ttd¡ d S )Nu   ///Ã¨|/)rˆ   ÚIOErrorr   r2   r   r   r   Útest_non_ascii_drive_letter|  s    z-URL2PathNameTests.test_non_ascii_drive_letterc                 C   s*   g d¢}|D ]}|   tt|ƒƒ|¡ qd S )N)râ  z\\\C\test\\rç  )rs   r   r   ©r!   Zlist_of_pathsrõ   r   r   r   Útest_roundtrip_url2pathname  s    z-URL2PathNameTests.test_roundtrip_url2pathnameN)r3   r4   r5   rå  ræ  rè  rê  rì  r   r   r   r   rá  l  s
   rá  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 )Nrâ  rã  rÄ  ©rs   r   r2   r   r   r   rå  ‰  s    z.PathName2URLTests.test_converting_drive_letterc                 C   s4   |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ d S )Nz\\\folder\test\ú/////folder/test/z\\folder\test\z////folder/test/z\folder\test\z/folder/test/rî  r2   r   r   r   ræ    s    
ÿ
ÿ
ÿz6PathName2URLTests.test_converting_when_no_drive_letterc                 C   s   |   tdƒd¡ d S )Nrç  ú///C:/foo/bar/spam.foorî  r2   r   r   r   rè  •  s    
ÿz%PathName2URLTests.test_simple_comparec                 C   s   |   ttd¡ d S )NzXX:\)rˆ   ré  r   r2   r   r   r   Útest_long_drive_letter™  s    z(PathName2URLTests.test_long_drive_letterc                 C   s*   g d¢}|D ]}|   tt|ƒƒ|¡ qd S )N)rã  rï  rð  )rs   r   r   rë  r   r   r   Útest_roundtrip_pathname2urlœ  s    z-PathName2URLTests.test_roundtrip_pathname2urlN)r3   r4   r5   rå  ræ  rè  rñ  rò  r   r   r   r   rí  ‡  s
   rí  Ú__main__)NN)F)5r‹   Úurllib.parser   Zurllib.requestZurllib.errorZhttp.clientr<   Zemail.messager{   r,   r  Zunittest.mockr   r   r   rd   r  ÚImportErrorrÆ  ró   Z
nturl2pathr   r   Úbase64r   r°   r   r   r   r   r?   rO   r@   rD   ZTestCaserT   rŒ   r®   r´   r  r'  rH  rO  r{  r“  r¾  rÈ  rË  rÚ  rá  rí  r3   Úmainr   r   r   r   Ú<module>   sd   


/
]L$  M - N T DN2
