o
    IXhP                    @   sV  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mZ ddlmZ ddlZzddlZW n eyK   dZY nw 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d7ddZdd Z d8d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%Z0G d,d- d-e
j%Z1G d.d/ d/e#e
j%Z2G d0d1 d1e
j%Z3G d2d3 d3e
j%Z4G d4d5 d5e
j%Z5e6d6kr)e
7  dS dS )9z;Regression tests for what was in Python 2's "urllib" module    N)patch)support)	os_helper)warnings_helper)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   :/opt/python-3.10.19/usr/lib/python3.10/test/test_urllib.py	hexescape   s   r   c                 C   sJ   |durt jj|d}n
tst }|ant}|du r|| S || |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w   Y  d S )Nz8FancyURLopener style of invoking requests is deprecated.)r   check_warningsDeprecationWarningr   r   r   r   r   r   r   r   6   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   sendallA   s   
z$fakehttp.<locals>.FakeSocket.sendallc                 _   s   |  j d7  _ | S )Nr
   )io_refs)r#   argskwdsr   r   r   makefileD   s   z%fakehttp.<locals>.FakeSocket.makefileNc                 S      | j rdS tj| |S N    )closedioBytesIOread)r#   Zamtr   r   r   r1   H      z!fakehttp.<locals>.FakeSocket.readc                 S   r+   r,   )r.   r/   r0   readline)r#   lengthr   r   r   r3   M   r2   z%fakehttp.<locals>.FakeSocket.readlinec                 S   s,   |  j d8  _ | j dkrtj|  d S d S )Nr
   r   )r'   r/   r0   closer#   r   r   r   r5   R   s   
z"fakehttp.<locals>.FakeSocket.closer    )	__name__
__module____qualname__r'   r&   r*   r1   r3   r5   r   r$   r   r   
FakeSocket>   s    

r:   c                       s,   e Zd ZdZ fddZrdd ZdS dS )z$fakehttp.<locals>.FakeHTTPConnectionNc                    s    | j | _| jt| _d S r    )fakedatasocktypeZfakesockr6   )r:   r   r   connect\   s   z,fakehttp.<locals>.FakeHTTPConnection.connectc                 S      d S r    r   r6   r   r   r   r5   e      z*fakehttp.<locals>.FakeHTTPConnection.close)r7   r8   r9   r!   r>   r5   r   )r:   
mock_closer   r   r%   W   s    r%   )r/   r0   httpclientHTTPConnectionr;   )r;   rA   r   )r%   r:   rA   r   fakehttp=   s   rE   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rA   )rE   rB   rC   rD   _connection_class)r#   r;   rA   Zfake_http_classr   r   r   rE   m   s   
zFakeHTTPMixin.fakehttpc                 C      | j tj_d S r    )rH   rB   rC   rD   r6   r   r   r   
unfakehttpr      zFakeHTTPMixin.unfakehttpNF)r7   r8   r9   rE   rJ   r   r   r   r   rF   l   s    
rF   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   r?   r    r   )r#   userpasswdhostportdirstimeoutZ
persistentr   r   r   __init__y   s   z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.__init__c                 S   s   t  dfS )Nr   )r/   r0   )r#   filer=   r   r   r   retrfile}      z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfilec                 S   r?   r    r   r6   r   r   r   r5      r@   z2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close)NT)r7   r8   r9   rT   rV   r5   r   r   r   r   FakeFtpWrapperx   s    
rX   )objectr   r   
ftpwrapper_ftpwrapper_class)r#   rX   r   r   r   fakeftpw   s   
zFakeFTPMixin.fakeftpc                 C   rI   r    )r[   r   r   rZ   r6   r   r   r   	unfakeftp   rK   zFakeFTPMixin.unfakeftpN)r7   r8   r9   r\   r]   r   r   r   r   rM   v   s    rM   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_FileTestszTest 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|  w tj| _	t
j| j	| _td| j | _d S )Nztest_urllib: %s
asciiwbfile:%s)bytes	__class__r7   textr   r   TESTFNwriter5   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)rk   r5   osremover   re   r6   r   r   r   tearDown   s   
zurlopen_FileTests.tearDownc                 C   &   dD ]}|  t| j|d|  qd S )N)	r1   r3   	readlinesfilenor5   infogeturlgetcode__iter__/object returned by urlopen() lacks %s attribute)
assertTruehasattrrk   r#   attrr   r   r   test_interface      z urlopen_FileTests.test_interfacec                 C   s   |  | j| j  d S r    )assertEqualrd   rk   r1   r6   r   r   r   	test_read      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)r   rd   rk   r3   r6   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)rk   rr   r   r   rd   )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)	rk   rs   assertIsInstanceintr   rn   r1   r   rd   )r#   Zfile_numr   r   r   test_fileno   s
   
zurlopen_FileTests.test_filenoc                 C   s   | j   d S r    )rk   r5   r6   r   r   r   
test_close      zurlopen_FileTests.test_closec                 C   s   |  | jjtjj d S r    )r   rk   ZheadersemailmessageMessager6   r   r   r   test_headers   r   zurlopen_FileTests.test_headersc                 C   s   |  | jj| j d S r    )r   rk   r   rj   r6   r   r   r   test_url      zurlopen_FileTests.test_urlc                 C   s   |  | jj d S r    )assertIsNonerk   statusr6   r   r   r   test_status      zurlopen_FileTests.test_statusc                 C   s   |  | j tjj d S r    )r   rk   rt   r   r   r   r6   r   r   r   	test_info   s   zurlopen_FileTests.test_infoc                 C      |  | j | j d S r    )r   rk   ru   rj   r6   r   r   r   test_geturl   r   zurlopen_FileTests.test_geturlc                 C   s   |  | j  d S r    )r   rk   rv   r6   r   r   r   test_getcode      zurlopen_FileTests.test_getcodec                 C   s   | j D ]	}| || j qd S r    )rk   r   rd   )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   rg   r6   r   r   r   test_relativelocalfile   s   z(urlopen_FileTests.test_relativelocalfileN)r7   r8   r9   __doc__rm   rp   r}   r   r   r   r   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 )
ProxyTestsc                 C   s6   t  | _ttjD ]}d| v r| j| q
d S )Nproxy)r   ZEnvironmentVarGuardenvlistrn   environlowerunset)r#   kr   r   r   rm      s   
zProxyTests.setUpc                 C   s   | j   | ` d S r    )r   __exit__r6   r   r   r   rp      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no0localhost, anotherdomain.com, newdomain.com:1234anotherdomain.comanotherdomain.com:8888newdomain.com:1234)r   setr   r   getproxies_environmentr   ry   proxy_bypass_environmentr#   r   r   r   r   +test_getproxies_environment_keep_no_proxies   s   
z6ProxyTests.test_getproxies_environment_keep_no_proxiesc              	   C   s   z5| j dd tj }| d|d  | j dd tj }| d| W | j d | j d d S | j d | j d w )N
HTTP_PROXYhttp://somewhere:3128rB   ZREQUEST_METHODGET)r   r   r   r   r   r   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   ry   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)
r7   r8   r9   rm   rp   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    )rn   r   
_saved_envcollectionsOrderedDictr6   r   r   r   rm   5  s   zProxyTests_withOrderedEnv.setUpc                 C   s   | j t_d S r    )r   rn   r   r6   r   r   r   rp   ;  rW   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_ProxyrB   )	rn   r   r   r   r   r   r   r   ry   r   r   r   r   ,test_getproxies_environment_prefer_lowercase>  s&   









zFProxyTests_withOrderedEnv.test_getproxies_environment_prefer_lowercaseN)r7   r8   r9   rm   rp   r   r   r   r   r   r   3  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   st   |  d| d  z+td}| | d | | d | | d | | d W |   d S |   w )Ns   HTTP/s    200 OK

Hello!http://python.org/   Hello!r-      rE   r   r   r3   ru   rv   rJ   )r#   verfpr   r   r   
check_readZ  s   zurlopen_HttpTests.check_readc                 C   sD   d}|  d ztj|}| | | W |   d S |   w )Nz-http://docs.python.org/library/urllib.html#OK   HTTP/1.1 200 OK

Hello!)rE   r   r   r   r   ru   rJ   )r#   r   r   r   r   r   test_url_fragmente  s   
z#urlopen_HttpTests.test_url_fragmentc                 C   s:   |  d ztd}| |jj W |   d S |   w )Nr   http://www.python.org)rE   r   ry   r   Z
will_closerJ   )r#   respr   r   r   test_willcloseo  s
   
z urlopen_HttpTests.test_willclosezssl module requiredc              	   C   s  t tdddg D ]{}t|}d| d}| d zet|dd}tjj}| 	|d	|  t
jd
|  W d    n1 sEw   Y  | 	|d	|  t
jd|  W d    n1 sgw   Y  td
| }| ||  W |   q
|   w d S )Nr   !      z//localhost:7777/test/   HTTP/1.1 200 OK

Hello.\\\contain control.*http:https:)r   rangechrrE   reprreplacerB   rC   
InvalidURLassertRaisesRegexr   r   r   r   ru   rJ   )r#   char_nor   schemeless_urlescaped_char_reprr   r   r   r   r   (test_url_path_with_control_char_rejectedw  s*   


z:urlopen_HttpTests.test_url_path_with_control_char_rejectedc                 C   s   |  d d}d| d }zhtjj}| |d tjd|  W d    n1 s,w   Y  | |d tjd|  W d    n1 sKw   Y  td| }| d	|	  | d
|	  | d|	  W | 
  d S | 
  w )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    
)rE   rB   rC   r   r   r   r   r   r   ru   rJ   )r#   rP   r   r   r   r   r   r   4test_url_path_with_newline_header_injection_rejected  s$   
zFurlopen_HttpTests.test_url_path_with_newline_header_injection_rejectedc              	   C   s   t tdddg D ]h}t|}d| d}| d zRt|dd}tjj}| 	|d	|  t
d
|  W d    n1 sCw   Y  | 	|d	|  t
d|  W d    n1 scw   Y  W |   q
|   w d S )Nr   r   r   z//localhostz/test/r   r   r   r   r   r   )r   r   r   rE   r   r   rB   rC   r   r   r   rJ   )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 }zLtjj}| |d td|  W d    n1 s*w   Y  | |d td|  W d    n1 sGw   Y  W |   d S W |   d S |   w )	Nr   zlocalhost
X-injected: header
r   r   zcontain control.*\\rr   r   r   )rE   rB   rC   r   r   r   rJ   )r#   rP   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      |  d d S )Ns   0.9r   r6   r   r   r   test_read_0_9  r   zurlopen_HttpTests.test_read_0_9c                 C   r   )Ns   1.0r   r6   r   r   r   test_read_1_0  rK   zurlopen_HttpTests.test_read_1_0c                 C   r   )Ns   1.1r   r6   r   r   r   test_read_1_1  rK   zurlopen_HttpTests.test_read_1_1c                 C   s6   | j ddd z| ttd W |   d S |   w )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
TrG   r   rE   r   OSErrorr   rJ   r6   r   r   r   test_read_bogus  s   z!urlopen_HttpTests.test_read_bogusc                 C   sr   | j ddd z,d}| tjj| td W d    n1 s!w   Y  W |   d S W |   d S |   w )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
TrG   zRedirection to url 'file:r   )rE   r   r   error	HTTPErrorr   rJ   )r#   msgr   r   r   test_invalid_redirect  s   
z'urlopen_HttpTests.test_invalid_redirectc              	   C   sL   t t jD ]}| jddd z| tjjtd W | 	  q| 	  w d S )Ns_   HTTP/1.1 302 Found
Location: file://guidocomputer.athome.com:/python/license
Connection: close
TrG   http://something)
r   r   ZmaxtriesrE   r   r   r   r   r   rJ   )r#   ir   r   r   test_redirect_limit_independent  s   z1urlopen_HttpTests.test_redirect_limit_independentc                 C   s2   |  d z| ttd W |   d S |   w )Nr-   r   r   r6   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w   Y  | |jj | |jj d S )Nz/file://localhost/a/file/which/doesnot/exists.py)	r   r   r   URLErrorr   ry   	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.w   Y  W t	| t
| nt	| t
| w | tj| | tjj t| W d    d S 1 sjw   Y  d S )Nzfile://localhost/r   )tempfilemkstempr   rn   pathsepry   existsr   r5   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 sw   Y  | |jj | |jj	 d S )Nzftp:///path
r   r   r   r   r   r   r   r  ry   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w   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   sF   |    ztjddddg tjjd< td W |   d S |   w )NrN   passr      testzftp://localhost)r\   r   r   rZ   Zftpcacher   r]   r6   r   r   r   test_ftp_cache_pruning-  s
   
z(urlopen_HttpTests.test_ftp_cache_pruningc                 C   sl   |  d z+td}| | d | | d | | d | | d W |   d S |   w )N   HTTP/1.0 200 OK

Hello!zhttp://user:pass@python.org/r   r-   r   r   )r#   r   r   r   r   test_userpass_inurl6  s   
z%urlopen_HttpTests.test_userpass_inurlc                 C   s   |  d zLd}d|}tjj}dt|dd }t|}| 	||j
d | | d | | d | | | | | d	 W |   d S |   w )
Nr  za b:c dzhttp://{}@python.org/zAuthorization: Basic %s
ASCIIzUTF-8r   r-   r   )rE   formatrB   rC   rD   r   encodedecoder   assertInr!   r   r3   ZassertNotEqualru   rv   rJ   )r#   Zuserpassr   Zfakehttp_wrapperZauthorizationr   r   r   r   test_userpass_inurl_w_spacesA  s   

z.urlopen_HttpTests.test_userpass_inurl_w_spacesc                 C   s>   t dtf tj  W d    d S 1 sw   Y  d S Nr   )r   r   r   r   r   	URLopenerr6   r   r   r   test_URLopener_deprecationT  s   "z,urlopen_HttpTests.test_URLopener_deprecationc              	   C   s   t  }tdtf/ | t tjj	dd|d W d    n1 s%w   Y  W d    d S W d    d S 1 s=w   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_contextX  s   "z)urlopen_HttpTests.test_cafile_and_contextN)"r7   r8   r9   r   r   r   r   unittest
skipUnlessr!  r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   rY   r   r   r  r  r  r  r"  r   r   r   r   r   W  s>    








	

r   c                   @   X   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/    IENDB`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   
urlcleanuprd   imagetext_urltext_url_base64	image_urlr   text_url_resptext_url_base64_respimage_url_respr6   r   r   r   rm   e  s   zurlopen_DataTests.setUpc                 C   rq   )N)r1   r3   rr   r5   rt   ru   rv   rw   rx   )ry   rz   r-  r{   r   r   r   r}     r~   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:,)r0  zUS-ASCII)r   r-  rt   r   r   r   r   r.  
get_paramsr/  strr   r)  r   r   r   r6   r   r   r   r     s   zurlopen_DataTests.test_infoc                 C   s@   |  | j | j |  | j | j |  | j | j d S r    )r   r-  ru   r*  r.  r+  r/  r,  r6   r   r   r   r     s
   zurlopen_DataTests.test_geturlc                 C   0   |  | j t| j  d | j d S Nr0  )r   r-  r1   r  dictrt   r1  rd   r6   r   r   r   test_read_text  s
   z urlopen_DataTests.test_read_textc                 C   r3  r4  )r   r.  r1   r  r5  rt   r1  rd   r6   r   r   r   test_read_text_base64  s
   z'urlopen_DataTests.test_read_text_base64c                 C   r   r    )r   r/  r1   r)  r6   r   r   r   test_read_image  r   z!urlopen_DataTests.test_read_imagec                 C      |  ttjjd d S )Nzdata:text/plainr   r   r   r   r   r6   r   r   r   test_missing_comma  r   z$urlopen_DataTests.test_missing_commac                 C   r9  )Nzdata:;base64,Cg=r:  r6   r   r   r   test_invalid_base64_data     z*urlopen_DataTests.test_invalid_base64_dataN)r7   r8   r9   r   rm   r}   r   r   r6  r7  r8  r;  r<  r   r   r   r   r&  b  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 d S    Y d S z|  W w    Y w )Ns   testing urllib.urlretriever`   )r'  r   r   r(  	tempFilesregisterFileForCleanUpr   re   rd   r   rf   r5   )r#   FILEr   r   r   rm     s   	

zurlretrieve_FileTests.setUpc                 C   s(   | j D ]}zt| W q   Y qd S r    )r?  rn   ro   )r#   Zeachr   r   r   rp     s   

zurlretrieve_FileTests.tearDownc                 C   sD   t j|}z|d W n ty   tdw dtj	| S )Nzutf-8z!filePath is not encodable to utf8z	file://%s)
rn   r  abspathr  UnicodeEncodeErrorr#  ZSkipTestr   r   r   )r#   ZfilePathr   r   r   constructLocalFileUrl  s   
z+urlretrieve_FileTests.constructLocalFileUrlr-   c                 C   sl   t  \}}z#| | t|d}|| |  W z|  W |S    Y |S z|  W w    Y w )zCreates 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.r`   )r  r  r@  rn   fdopenrf   r5   )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 )Nra   r   r
   zFdid not get an email.message.Message instance as second returned value)
r   r   urlretriever   re   r   r   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 n   Y nz|  W w    Y w | | j| d S )N%s.2r   zcopy of the file was not maderb)r   re   r@  r   r   rH  rD  r   ry   rn   r  r
  r   r1   r5   rd   )r#   second_temprI  rA  rd   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
   )r   r   r   )block_countblock_read_size	file_sizeZcount_holderr6   r   r   
hooktester  s
   z9urlretrieve_FileTests.test_reporthook.<locals>.hooktesterrK  )r   re   r@  r   r   rH  rD  )r#   rR  rM  r   r6   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      | | ||f d S r    rG  rO  rP  rQ  Z_reportr   r   r   rR    r   zAurlretrieve_FileTests.test_reporthook_0_bytes.<locals>.hooktesterr
   r   r	   	rF  r   r   rH  rD  r   re   r   r   r#   reportrR  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   rT  r    rU  rV  r   r   r   rR  $  r   zAurlretrieve_FileTests.test_reporthook_5_bytes.<locals>.hooktesters   xxxxxr	   r      r
   rW  rX  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   rT  r    rU  rV  r   r   r   rR  2  r   zDurlretrieve_FileTests.test_reporthook_8193_bytes.<locals>.hooktester   xi      r   r	   r
   i    rW  rX  r   r   r   test_reporthook_8193_bytes-  s   z0urlretrieve_FileTests.test_reporthook_8193_bytesN)r-   )r7   r8   r9   r   rm   rp   rD  rF  r@  rJ  rN  rS  rZ  r\  r_  r   r   r   r   r>    s    
	r>  c                   @       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|   w W d    d S 1 s8w   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   r?   r    r   )Zpar1Zpar2Zpar3r   r   r   _reporthookN  r@   zYurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError.<locals>._reporthook)Z
reporthookr'  r   r   r(  rE   r   r   ContentTooShortErrorrH  r   ZTEST_HTTP_URLrJ   )r#   rc  r   r   r   .test_short_content_raises_ContentTooShortErrorA  s   


"zDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorc              	   C   sr   |  tjj | d | tjj ztjt	j
 W |   n|   w W d    d S 1 s2w   Y  d S )Nrb  rd  r6   r   r   r   Atest_short_content_raises_ContentTooShortError_without_reporthookX  s   
	"zWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookN)r7   r8   r9   r   rf  rg  r   r   r   r   ra  >  s    ra  c                   @   r%  )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   rh   ri   r   
quote_plus)r#   Zdo_not_quoterI  r   r   r   test_never_quote  s   

zQuotingTests.test_never_quotec                 C   s   |  tjjjd d d S )Nr   r   )r   r   rh   ri   __defaults__r6   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<>saferl  rm  s   <>u   aüblatin-1   ü)encodingrt  r      )r   rh   ri   r   ro  )r#   Zquote_by_defaultrI  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  q~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>  s    z5QuotingTests.test_default_quoting.<locals>.<listcomp>    z<>#%"{}|\^[]`r   r   z1using quote(): %s should be escaped to %s, not %sz6using quote_plus(): %s should be escapes to %s, not %szab[]cdz
ab%5B%5Dcdrl  rm  )
r   rG  r   rn  r   rh   ri   r   r   ro  )r#   Zshould_quoter   rI  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   rl  +zusing quote_plus(): %r != +z
a b cd e frm  )r   rh   ri   r   r   ro  r   )r#   rI  givenry  r   r   r   test_quoting_space  s&   

zQuotingTests.test_quoting_spacec                 C   sX   |  tjdd |  tjddd |  tjdd |  tjddd d S )Nzalpha+beta gammazalpha%2Bbeta+gammar  zalpha+beta+gammas   alpha+beta gamma   +)r   r   rh   ro  r6   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%FFrl  ru  rw  z"using quote_from_bytes(): %r != %r)r   rh   ri   r   r   	TypeErrorquote_from_bytesr#   r  ry  rI  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%BFrl  rw  errorsr  ru  r     漢字%E6%BC%A2%E5%AD%97z%3F%3Fr   z%26%2328450%3B%26%2323383%3Bxmlcharrefreplace)r   rh   ri   r   r   rC  r  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+%FFru  r  rm  u   ab漢字 cdzab%3F%3F+cdr   r  )r   rh   ro  r   r  r   r   r   test_quote_plus_with_unicode(  s   


z)QuotingTests.test_quote_plus_with_unicodeN)r7   r8   r9   r   rp  rr  rz  r  r  r  r  r  r  r   r   r   r   rh  k  s    *rh  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 ]3}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   rh   unquoter   unquote_plusrG  rn  countr   r  AttributeError)r#   Zescape_listr|  r  ry  rI  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   r_   "using unquote_to_bytes(): %r != %rr   )	r   rh   r  r   rb   unquote_to_bytesr   r  r  r  r   r   r   test_unquoting_badpercentV  sL   








z(UnquotingTests.test_unquoting_badpercentc                 C   s.   d}d}t j|}| ||d||f  d S )Nz%Ab%eAs   r  r   rh   r  r   r  r   r   r   test_unquoting_mixed_casez  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abcdrl  r  )r   r   rh   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...r  r  r   r  )r   rh   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 )	N"br%C3%BCckner_sapporo_20050930.docs   brückner_sapporo_20050930.docr  	   漢%C3%BCs   漢üs   %A2%D8ab%FFr  s	   %A2ab%FFr  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 )Nr  u   brückner_sapporo_20050930.docr  r  zbr%FCckner_sapporo_20050930.docru  r  r  r  z%F3%B1u   �r   )r  r   ignorer  u   漢üu   漢%FCr   rh   r  r   r  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  r  r   r   r   test_unquoting_with_bytes_input  s$   


z.UnquotingTests.test_unquoting_with_bytes_inputN)r7   r8   r9   r   r  r  r  r  r  r  r  r  r   r   r   r   r  8  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  }| | oF| 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   rh   	urlencoder  r   r  indexry   isdigitr   )	r#   r  Z	test_typeZexpect_somewhererI  r  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  r6   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  r6   r   r   r   test_using_sequence3  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   rh   r  r   r  r   r   r   test_quoting8  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| }| || q)| |ddd|d  d S )NZsequence)r  r  r  zsequence=%sTr  r	   zExpected 2 '&'s, got %s)r   rh   ro  r2  r  r   r  r  )r#   r  ry  rI  valuer   r   r   
test_doseqC  s   zurlencode_Tests.test_doseqc                 C   s,   |  dtji  |  dtjg  d S r  r   r   rh   r  r6   r   r   r   test_empty_sequenceP  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  r6   r   r   r   test_nonstring_valuesT  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)r   r   rh   r  r   r   r"   r   r   r   test_nonstring_seq_valuesX  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   r  %C2%A0=%C3%81%A0=%C1ru  r  r   rh   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 )Nr  r  Tr  r   )doseqrw  r  ))r  )r
   r  z%3F=1&%3F=%3Fr  r  ))r  )*   r  z%C2%A0=42&%C2%A0=%C3%81r  ru  r  z%A0=42&%A0=%C1r  r  r   r   r   test_urlencode_encoding_doseqs  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  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 )Nr  z:$rs  z	%A0$=%C1$T)r  rt  ))r  )r     r  z%A0$=%C1$&%A0$=13&%A0$=42ru  )rt  rw  )r  rt  rw  r  )r#   r  rI  ry  r   r   r   &test_urlencode_encoding_safe_parameter  s:   
z6urlencode_Tests.test_urlencode_encoding_safe_parameterN)r7   r8   r9   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)rn   r  rn  r   r   r   r   r   )r#   Zexpected_pathZexpected_urlrI  r   r   r   rJ    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)
rn   r  rn  r   rh   ri   r   r   r   r   r  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   r   r  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   r   )r#   r  ry  r   rI  r   r   r   test_ntpath  s"   zPathname_Tests.test_ntpathN)r7   r8   r9   r   rJ  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)r   r   r   Zthishosttupler6   r   r   r   test_thishost  r=  zUtility_Tests.test_thishostN)r7   r8   r9   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   sp   G dd dt jj}tdtf | | dd | | dd W d    d S 1 s1w   Y  d S )Nc                   @      e Zd Zdd ZdS )z8URLopener_Tests.test_quoted_open.<locals>.DummyURLopenerc                 S      |S r    r   r#   r   r   r   r   	open_spam)  r@   zBURLopener_Tests.test_quoted_open.<locals>.DummyURLopener.open_spamN)r7   r8   r9   r  r   r   r   r   DummyURLopener(      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   r   r   )r#   r  r   r   r   test_quoted_open'  s"   

"z URLopener_Tests.test_quoted_open)categoryc                 C   s   t  6}tj|d\}}t| dtj| }tj	 
|\}}| tj|tj| W d    d S 1 s=w   Y  d S )N)dirzfile:)r   Ztemp_dirr  r  rn   r5   r   r   r   r  retriever   r  normcase)r#   Ztmpdirr  ZtmpfileZfileurlr  _r   r   r   test_urlopener_retrieve_file6  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)rE   r'  rJ   r   r   r  r  r   rn   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 ]3}| tt jj| | tt j j| | tt j j| | t| j| | t| j| qd S )Nc                   @   r  )z<URLopener_Tests.test_local_file_open.<locals>.DummyURLopenerc                 S   r  r    r   r  r   r   r   open_local_fileL  r@   zLURLopener_Tests.test_local_file_open.<locals>.DummyURLopener.open_local_fileN)r7   r8   r9   r  r   r   r   r   r  K  r  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_openH  s   z$URLopener_Tests.test_local_file_openN)r7   r8   r9   r   r  r   Zignore_warningsr   r  r  r  r   r   r   r   r  $  s    


	

r  c                   @   r`  )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   Requestr   
get_methodr#   r  r   r   r   r   test_default_valuesY  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  r   r  r  r  r   r   r   test_with_method_arg`  s   z!RequestTests.test_with_method_argN)r7   r8   r9   r   r  r  r   r   r   r   r  V  s    r  c                   @   4   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  r   r   r6   r   r   r   test_converting_drive_letterp  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  r6   r   r   r   $test_converting_when_no_drive_letteru  s   z6URL2PathNameTests.test_converting_when_no_drive_letterc                 C      |  tdd d S )Nz///C|/foo/bar/spam.fooC:\foo\bar\spam.foor  r6   r   r   r   test_simple_comparez     
z%URL2PathNameTests.test_simple_comparec                 C      |  ttd d S )Nu   ///è|/)r   IOErrorr   r6   r   r   r   test_non_ascii_drive_letter~  r   z-URL2PathNameTests.test_non_ascii_drive_letterc                 C   *   g d}|D ]}|  tt|| qd S )N)r  z\\\C\test\\r  )r   r   r   r#   Zlist_of_pathsr  r   r   r   test_roundtrip_url2pathname  s   z-URL2PathNameTests.test_roundtrip_url2pathnameN)r7   r8   r9   r  r  r  r
  r  r   r   r   r   r  n  s    r  c                   @   r  )PathName2URLTestsc                 C   s$   |  tdd |  tdd d S )Nr  r   r  r   r   r6   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  r6   r   r   r   r    s   


z6PathName2URLTests.test_converting_when_no_drive_letterc                 C   r  )Nr  ///C:/foo/bar/spam.foor  r6   r   r   r   r    r  z%PathName2URLTests.test_simple_comparec                 C   r  )NzXX:\)r   r	  r   r6   r   r   r   test_long_drive_letter  r   z(PathName2URLTests.test_long_drive_letterc                 C   r  )N)r   r  r  )r   r   r   r  r   r   r   test_roundtrip_pathname2url  s   z-PathName2URLTests.test_roundtrip_pathname2urlN)r7   r8   r9   r  r  r  r  r  r   r   r   r   r    s    r  __main__)NNrL   )8r   urllib.parser   Zurllib.requestZurllib.errorZhttp.clientrB   Zemail.messager   r/   r#  Zunittest.mockr   r  r   Ztest.supportr   r   rn   r!  ImportErrorr  r  Z
nturl2pathr   r   base64r   r   r   r   r   r   rE   rY   rF   rM   ZTestCaser^   r   r   r   r&  r>  ra  rh  r  r  r  r  r  r  r  r  r7   mainr   r   r   r   <module>   sn    

/
]L$  M - N T DN2
