B
    2*™\Æ  ã               @   s4  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mZmZ d dlmZ d dlZdd„ Zdad3d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!ƒZ.G d,d-„ d-e	j!ƒZ/G d.d/„ d/e	j!ƒZ0G d0d1„ d1e	j!ƒZ1e2d2kr0e	 3¡  dS )4é    N)Úpatch)Úsupport)Úurl2pathnameÚpathname2url)Ú	b64encodec             C   s4   t t| ƒƒdd …  ¡ }t|ƒdkr,d| }d| S )Né   é   z0%sú%)ÚhexÚordÚupperÚlen)ÚcharZhex_repr© r   ú;C:\ALexclude\prg\programme\Python37\Lib\test\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 )N)Ú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   4   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   Ú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 )Nr&   )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   r-   P   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   ÚconnectZ   s    z,fakehttp.<locals>.FakeHTTPConnection.connect)r.   r/   r0   r   r4   r   )r1   r   r   r    U   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   c   s    
zFakeHTTPMixin.fakehttpc             C   s   | j tj_d S )N)r:   r5   r6   r7   )r   r   r   r   Ú
unfakehttpg   s    zFakeHTTPMixin.unfakehttpN)r.   r/   r0   r8   r;   r   r   r   r   r9   b   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passwdZhostZportÚdirsZtimeoutZ
persistentr   r   r   Ú__init__n   s    z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.__init__c             S   s   t  ¡ dfS )Nr   )r(   r)   )r   Úfiler3   r   r   r   Úretrfiler   s    z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfilec             S   s   d S )Nr   )r   r   r   r   r-   u   s    z2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close)NT)r.   r/   r0   r?   rA   r-   r   r   r   r   ÚFakeFtpWrapperm   s    
rB   )Úobjectr   r   Ú
ftpwrapperÚ_ftpwrapper_class)r   rB   r   r   r   Úfakeftpl   s    
zFakeFTPMixin.fakeftpc             C   s   | j tj_d S )N)rE   r   r   rD   )r   r   r   r   Ú	unfakeftp{   s    zFakeFTPMixin.unfakeftpN)r.   r/   r0   rF   rG   r   r   r   r   r<   k   s   r<   c               @   st   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d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Úurlopen_FileTestsc             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 )N)rQ   r-   ÚosÚremover   rN   )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ÚhasattrrQ   )r   Úattrr   r   r   Útest_interface˜   s    
z urlopen_FileTests.test_interfacec             C   s   |   | j| j ¡ ¡ d S )N)ÚassertEqualrM   rQ   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)ra   rM   rQ   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)rQ   rW   ra   r   rM   )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)	rQ   rX   ÚassertIsInstanceÚintra   rT   r*   r   rM   )r   Zfile_numr   r   r   Útest_fileno°   s    
zurlopen_FileTests.test_filenoc             C   s   | j  ¡  d S )N)rQ   r-   )r   r   r   r   Ú
test_close·   s    zurlopen_FileTests.test_closec             C   s   |   | j ¡ tjj¡ d S )N)re   rQ   rY   ÚemailÚmessageÚMessage)r   r   r   r   Ú	test_info¼   s    zurlopen_FileTests.test_infoc             C   s   |   | j ¡ | j¡ d S )N)ra   rQ   rZ   rP   )r   r   r   r   Útest_geturl¿   s    zurlopen_FileTests.test_geturlc             C   s   |   | j ¡ ¡ d S )N)ZassertIsNonerQ   r[   )r   r   r   r   Útest_getcodeÂ   s    zurlopen_FileTests.test_getcodec             C   s"   x| j D ]}|  || j¡ qW d S )N)rQ   ra   rM   )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   rP   )r   r   r   r   Útest_relativelocalfileÎ   s    z(urlopen_FileTests.test_relativelocalfileN)r.   r/   r0   rS   rV   r`   rb   rc   rd   rg   rh   rl   rm   rn   rp   rs   r   r   r   r   rH      s   	rH   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ÚlistrT   ÚenvironÚlowerÚunset)r   Úkr   r   r   rS   Ô   s    
zProxyTests.setUpc             C   s   | j  ¡  | ` d S )N)rv   Ú__exit__)r   r   r   r   rV   Ü   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)rv   Úsetr   r   Úgetproxies_environmentra   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)rv   r€   r   r   r   ra   ZassertNotInrz   )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‚   rv   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   rS   rV   rƒ   r†   rˆ   r   r   r   r   rt   Ò   s
   rt   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)rT   rx   Ú
_saved_envÚcollectionsÚOrderedDict)r   r   r   r   rS   
  s    zProxyTests_withOrderedEnv.setUpc             C   s   | j t_d S )N)rŠ   rT   rx   )r   r   r   r   rV     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   )	rT   rx   r‡   r   r   r‚   r   ra   r]   )r   r   r   r   r   Ú,test_getproxies_environment_prefer_lowercase  s&    









zFProxyTests_withOrderedEnv.test_getproxies_environment_prefer_lowercaseN)r.   r/   r0   rS   rV   rŽ   r   r   r   r   r‰     s   r‰   c               @   sÀ   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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 )+Úurlopen_HttpTestsc             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   ra   r+   rZ   r[   r;   )r   ZverÚfpr   r   r   Ú
check_read/  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   ra   rZ   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   Zrespr   r   r   Útest_willcloseD  s
    
z urlopen_HttpTests.test_willclosec             C   s   |   d¡ d S )Ns   0.9)r’   )r   r   r   r   Útest_read_0_9L  s    zurlopen_HttpTests.test_read_0_9c             C   s   |   d¡ d S )Ns   1.0)r’   )r   r   r   r   Útest_read_1_0Q  s    zurlopen_HttpTests.test_read_1_0c             C   s   |   d¡ d S )Ns   1.1)r’   )r   r   r   r   Útest_read_1_1T  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   rq   ÚOSErrorr   r;   )r   r   r   r   Útest_read_bogusW  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   ZassertRaisesRegexr   ÚerrorÚ	HTTPErrorr   r;   )r   Úmsgr   r   r   Útest_invalid_redirectd  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)
Úranger   Zmaxtriesr8   rq   r   rš   r›   r   r;   )r   Úir   r   r   Útest_redirect_limit_independentt  s    
z1urlopen_HttpTests.test_redirect_limit_independentc             C   s,   |   d¡ z|  ttd¡ W d |  ¡  X d S )Nr&   zhttp://something)r8   rq   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)	rq   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/ú/)ÚtempfileÚmkstempÚreplacerT   ÚpathÚsepr]   Úexistsr   r-   Úunlinkr‡   rq   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)
rq   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)
rq   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)rF   r   r   rD   Zftpcacher   rG   )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   ra   r+   rZ   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   ra   r+   ZassertNotEqualrZ   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_deprecationzssl module requiredc          
   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   rq   rr   r   r   r   )r   rÁ   r   r   r   Útest_cafile_and_contextØ  s
    z)urlopen_HttpTests.test_cafile_and_contextN)r.   r/   r0   r’   r“   r”   r•   r–   r—   r™   r   r    r¡   r§   r±   r²   r³   r   rC   r   r   r·   r¸   r¾   rÀ   ÚunittestÚ
skipUnlessrÂ   rÃ   r   r   r   r   r   ,  s&   
		r   c               @   sT   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d„ Z
dd„ ZdS )Úurlopen_DataTestsc             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)rM   ÚimageÚtext_urlÚtext_url_base64Ú	image_urlr   r   r   Útext_url_respÚtext_url_base64_respÚimage_url_resp)r   r   r   r   rS   å  s    
zurlopen_DataTests.setUpc             C   s*   x$dD ]}|   t| j|ƒd| ¡ qW d S )N)r*   r+   rW   r-   rY   rZ   r[   r\   z/object returned by urlopen() lacks %s attribute)r]   r^   rË   )r   r_   r   r   r   r`     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)re   rË   rY   ri   rj   rk   ra   rÌ   Ú
get_paramsrÍ   Ústrr   rÇ   r   r   r   )r   r   r   r   rl   	  s    
zurlopen_DataTests.test_infoc             C   s@   |   | j ¡ | j¡ |   | j ¡ | j¡ |   | j ¡ | j¡ d S )N)ra   rË   rZ   rÈ   rÌ   rÉ   rÍ   rÊ   )r   r   r   r   rm     s    zurlopen_DataTests.test_geturlc             C   s0   |   | j ¡  t| j ¡  ¡ ƒd ¡| j¡ d S )NrÎ   )ra   rË   r*   r¼   ÚdictrY   rÏ   rM   )r   r   r   r   Útest_read_text  s    z urlopen_DataTests.test_read_textc             C   s0   |   | j ¡  t| j ¡  ¡ ƒd ¡| j¡ d S )NrÎ   )ra   rÌ   r*   r¼   rÑ   rY   rÏ   rM   )r   r   r   r   Útest_read_text_base64  s    z'urlopen_DataTests.test_read_text_base64c             C   s   |   | j ¡ | j¡ d S )N)ra   rÍ   r*   rÇ   )r   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)rq   rr   r   r   r   )r   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=)rq   rr   r   r   r   )r   r   r   r   Útest_invalid_base64_data'  s    z*urlopen_DataTests.test_invalid_base64_dataN)r.   r/   r0   rS   r`   rl   rm   rÒ   rÓ   rÔ   rÕ   rÖ   r   r   r   r   rÆ   â  s   	rÆ   c               @   sf   e 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_FileTestsc             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.urlretrieverJ   )Ú	tempFilesÚregisterFileForCleanUpr   rN   rM   r   rO   r-   )r   ÚFILEr   r   r   rS   /  s      zurlretrieve_FileTests.setUpc             C   s0   x*| j D ] }yt |¡ W q   Y qX qW d S )N)rØ   rT   rU   )r   Zeachr   r   r   rV   D  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)
rT   r¬   Úabspathr»   ÚUnicodeEncodeErrorrÄ   ZSkipTestr   r   r   )r   ZfilePathr   r   r   ÚconstructLocalFileUrlJ  s    z+urlretrieve_FileTests.constructLocalFileUrlr&   c             C   sZ   t  ¡ \}}z,|  |¡ t |d¡}| |¡ | ¡  W d y| ¡  W n   Y nX X |S )NrJ   )r©   rª   rÙ   rT   ÚfdopenrO   r-   )r   r   ZnewFdZnewFilePathZnewFiler   r   r   ÚcreateNewTempFileR  s    

  z'urlretrieve_FileTests.createNewTempFilec             C   s   | j  |¡ d S )N)rØ   Úappend)r   ZfileNamer   r   r   rÙ   b  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   rN   ra   re   ri   rj   rk   )r   Úresultr   r   r   Ú
test_basice  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   rN   rÙ   r   r   rá   rÝ   ra   r]   rT   r¬   r®   r   r*   r-   rM   )r   Úsecond_temprâ   rÚ   rM   r   r   r   Ú	test_copyn  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   )re   rf   ra   )Úblock_countÚblock_read_sizeÚ	file_sizeZcount_holder)r   r   r   Ú
hooktester‚  s
    z9urlretrieve_FileTests.test_reporthook.<locals>.hooktesterz%s.2)r   rN   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   rN   ra   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   rN   ra   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   rN   ra   r   )r   rí   rê   rî   r   r   r   Útest_reporthook_8193_bytes§  s    
z0urlretrieve_FileTests.test_reporthook_8193_bytesN)r&   )r.   r/   r0   rS   rV   rÝ   rß   rÙ   rã   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S )Úurlretrieve_HttpTestsc          	   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   rq   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   rq   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   rù   rú   r   r   r   r   rõ   ¸  s   rõ   c               @   sT   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d„ Z
dd„ ZdS )ÚQuotingTestsc             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Úquotera   Ú
quote_plus)r   Zdo_not_quoterâ   r   r   r   Útest_never_quoteû  s    zQuotingTests.test_never_quotec             C   s   |   tjjjd d¡ d S )Nr   r¨   )ra   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ÿ   ra   r   )r   Zquote_by_defaultrâ   Úexpectr   r   r   Ú	test_safe  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   )Úchr)Ú.0Únumr   r   r   ú
<listcomp>,  s    z5QuotingTests.test_default_quoting.<locals>.<listcomp>é    z<>#%"{}|\^[]`é   r   z1using quote(): %s should be escaped to %s, not %sz6using quote_plus(): %s should be escapes to %s, not %szab[]cdz
ab%5B%5Dcdzusing quote(): %r != %rzusing quote_plus(): %r != %r)
rž   rà   r
  rý   r   rþ   rÿ   ra   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 )Nú zusing quote(): %r != %rú+zusing quote_plus(): %r != +z
a b cd e fzusing quote_plus(): %r != %r)r   rþ   rÿ   ra   r   r   r«   )r   râ   Úgivenr  r   r   r   Útest_quoting_spaceE  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ó   +)ra   r   rþ   r   )r   r   r   r   Útest_quoting_plusX  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ÿ   ra   rq   Ú	TypeErrorZquote_from_bytes)r   r  r  râ   r   r   r   Útest_quote_bytesd  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ÿ   ra   rq   rÜ   )r   r  r  râ   r   r   r   Útest_quote_with_unicodet  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   ra   )r   r  r  râ   r   r   r   Útest_quote_plus_with_unicodež  s    
z)QuotingTests.test_quote_plus_with_unicodeN)r.   r/   r0   r  r  r	  r  r  r  r  r  r  r   r   r   r   rû   á  s   *rû   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 )ÚUnquotingTestsc          	   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þ   Úunquotera   Úunquote_plusrà   rý   Úcountrq   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	   rI   z"using unquote_to_bytes(): %r != %rr   )	r   rþ   r!  ra   rK   Úunquote_to_bytesrq   r  r$  )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þ   r'  ra   )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!  ra   r"  )r   r  r  râ   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...zusing unquote(): %r != %rr  r  zusing unquote_plus(): %r != %r)r   rþ   r!  ra   r«   r"  )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þ   r'  ra   )r   r  r  râ   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 )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!  ra   )r   r  r  râ   r   r   r   Útest_unquote_with_unicode2  sN    z(UnquotingTests.test_unquote_with_unicodeN)
r.   r/   r0   r&  r(  r)  r+  r,  r-  r/  r   r   r   r   r  ®  s   $	r  c               @   sl   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d„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )Úurlencode_Testsc       	   	   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 )Nz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 != %s)
r   rþ   Ú	urlencoder½   ra   r#  Úindexr]   Úisdigitr   )	r   r  Z	test_typeZexpect_somewhererâ   r  Zamp_locationZon_amp_leftZon_amp_rightr   r   r   Úhelp_inputtypeo  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)r6  )r   r   r   r   Útest_using_mapping  s    z"urlencode_Tests.test_using_mappingc             C   s   |   dddgd¡ d S )N)r:  r7  )r;  r8  )r<  r9  z*using sequence of two-item tuples as input)r6  )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 )Nr1  ú=z%s=%szkey namezA bunch of pluseszkey+name=A+bunch+of+pluses)r   r   rþ   r3  ra   )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sequencer7  r8  r9  zsequence=%sTr1  r   zExpected 2 '&'s, got %s)r   rþ   r   rÐ   r3  ra   r½   r#  )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   )ra   r   rþ   r3  )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)ra   r   rþ   r3  )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=2rD  r   r   Tz
a=None&a=a)rD  r   )Úbr   za=a&a=b)ra   r   rþ   r3  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þ   r3  ra   )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))rH  rI  z%3F=%3FTr¹   r«   )Údoseqr  r  ))rH  )r   rI  z%3F=1&%3F=%3F)r  r  z%C2%A0=%C3%81))rH  )é*   rI  z%C2%A0=42&%C2%A0=%C3%81z%A0=%C1zlatin-1)r  z%A0=42&%A0=%C1)r   rþ   r3  ra   )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    $)rL  s   Á$z%A0%24=42&%A0%24=%C1%24)r   rþ   r3  ra   )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)rK  r  ))s    $)s   Á$é   rL  z%A0$=%C1$&%A0$=13&%A0$=42zlatin-1)r  r  )rK  r  r  )r   rþ   r3  ra   )r   r  râ   r  r   r   r   Ú&test_urlencode_encoding_safe_parameter  s4    
z6urlencode_Tests.test_urlencode_encoding_safe_parameterN)r.   r/   r0   r6  r=  r>  r@  rB  rC  rE  rG  rJ  rM  rN  rP  r   r   r   r   r0  l  s    %r0  c               @   s6   e Zd Zdd„ Zdd„ Ze ejdkd¡dd„ ƒZ	d	S )
ÚPathname_Testsc             C   s^   t j dddd¡}d}tj |¡}|  ||d||f ¡ tj |¡}|  ||d||f ¡ d S )NÚpartsZofrD  r¬   zparts/of/a/pathzpathname2url() failed; %s != %szurl2pathame() failed; %s != %s)rT   r¬   rý   r   r   r   ra   r   )r   Zexpected_pathZexpected_urlrâ   r   r   r   rã   2  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)
rT   r¬   rý   r   rþ   rÿ   r   r   ra   r   )r   r  r  râ   r   r   r   r@  ?  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   ra   )r   r  r  r   râ   r   r   r   Útest_ntpathZ  s    
zPathname_Tests.test_ntpathN)
r.   r/   r0   rã   r@  rÄ   rÅ   ÚsysÚplatformrU  r   r   r   r   rQ  /  s   rQ  c               @   s   e Zd Zdd„ ZdS )ÚUtility_Testsc             C   s   |   tj ¡ t¡ d S )N)re   r   r   ZthishostÚtuple)r   r   r   r   Útest_thishostn  s    zUtility_Tests.test_thishostN)r.   r/   r0   rZ  r   r   r   r   rX  k  s   rX  c               @   s   e Zd Zdd„ ZdS )ÚURLopener_Testsc          	   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_spamx  s    zBURLopener_Tests.test_quoted_open.<locals>.DummyURLopener.open_spamN)r.   r/   r0   r\  r   r   r   r   ÚDummyURLopenerw  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   ra   r   )r   r]  r   r   r   Útest_quoted_openv  s    



z URLopener_Tests.test_quoted_openN)r.   r/   r0   r^  r   r   r   r   r[  s  s   r[  c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚRequestTestsc             C   s>   t jj}|dƒ}|  | ¡ d¡ |di ƒ}|  | ¡ d¡ d S )Nzhttp://www.python.orgr…   ZPOST)r   r   ÚRequestra   Ú
get_method)r   r`  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   r`  ra   rc  ra  )r   r`  r   r   r   r   Útest_with_method_argÞ  s    z!RequestTests.test_with_method_argN)r.   r/   r0   rb  rd  r   r   r   r   r_  Ô  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|zC:z///C:z///C|/zC:\)ra   r   )r   r   r   r   Útest_converting_drive_letterî  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\)ra   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)ra   r   )r   r   r   r   Útest_simple_compareø  s    
z%URL2PathNameTests.test_simple_comparec             C   s   |   ttd¡ d S )Nu   ///Ã¨|/)rq   ÚIOErrorr   )r   r   r   r   Útest_non_ascii_drive_letterü  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)ra   r   r   )r   Úlist_of_pathsr¬   r   r   r   Útest_roundtrip_url2pathnameÿ  s
    
z-URL2PathNameTests.test_roundtrip_url2pathnameN)r.   r/   r0   rf  rg  rh  rj  rl  r   r   r   r   re  ì  s
   re  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:\)ra   r   )r   r   r   r   rf  	  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/)ra   r   )r   r   r   r   rg    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)ra   r   )r   r   r   r   rh    s    
z%PathName2URLTests.test_simple_comparec             C   s   |   ttd¡ d S )NzXX:\)rq   ri  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)ra   r   r   )r   rk  r¬   r   r   r   Útest_roundtrip_pathname2url  s
    
z-PathName2URLTests.test_roundtrip_pathname2urlN)r.   r/   r0   rf  rg  rh  rn  ro  r   r   r   r   rm    s
   rm  Ú__main__)NN)4Zurllib.parser   Zurllib.requestZurllib.errorZhttp.clientr5   Zemail.messageri   r(   rÄ   Zunittest.mockr   r¶   r   rT   rÂ   ÚImportErrorrV  r©   Z
nturl2pathr   r   Úbase64r   r‹   r   r   r   r   r8   rC   r9   r<   ZTestCaserH   rt   r‰   r   rÆ   r×   rõ   rû   r  r0  rQ  rX  r[  r_  re  rm  r.   Úmainr   r   r   r   Ú<module>   s`   

'	S6$ 7J ) N ? D<a
