
    7bh~"                       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m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Zej6                  s e
j8                  d	      d
 Zdad/dZd Z d0dZ! G d de"      Z# G d de"      Z$ G d de
jJ                        Z& G d de
jJ                        Z' G d de
jJ                        Z( G d de
jJ                  e#e$      Z) G d de
jJ                        Z* G d de
jJ                        Z+ G d de
jJ                  e#      Z, G d  d!e
jJ                        Z- G d" d#e
jJ                        Z. G d$ d%e
jJ                        Z/ G d& d'e
jJ                        Z0 G d( d)e
jJ                        Z1 G d* d+e#e
jJ                        Z2 G d, d-e
jJ                        Z3e4d.k(  r e
jj                          yy# e$ r dZY w xY w)1z;Regression tests for what was in Python 2's "urllib" module    N)patch)support)	os_helper)socket_helper)warnings_helper)	b64encodeztest requires gethostname()c                 |    t        t        |             dd j                         }t        |      dk(  rd|z  }d|z   S )z!Escape char as RFC 2396 specifies   N   z0%s%)hexordupperlen)charhex_reprs     :/opt/python-3.12.12/usr/lib/python3.12/test/test_urllib.py	hexescaper      s?    3t9~ab!'')H
8}8#>    c                     |!t         j                  j                  |      }nt        st               }|ant        }||j	                  |       S |j	                  | |      S )z.urlopen(url [, data]) -> open file-like object)proxies)urllibrequestFancyURLopener
_urlopeneropen)urldatar   openers       r   urlopenr    *   s[     ..w.?!
|{{3{{3%%r   c                      t        j                  dt        f      5  t        j                  j                         cd d d        S # 1 sw Y   y xY w)Nz8FancyURLopener style of invoking requests is deprecated.)r   check_warningsDeprecationWarningr   r   r    r   r   r   r   :   s=    		'	'G 
! ~~,,.
! 
! 
!s   AAc                      G fddt         j                         G fddt        j                  j                        | _        S )Nc                   8    e Zd ZdZ fdZd ZddZddZd Zy)	fakehttp.<locals>.FakeSocketr   c                     |_         y N)buf)selfr   FakeHTTPConnections     r   sendallz$fakehttp.<locals>.FakeSocket.sendallE   s    %)"r   c                 0    | xj                   dz  c_         | S )Nr   )io_refs)r+   argskwdss      r   makefilez%fakehttp.<locals>.FakeSocket.makefileH   s    LLALKr   Nc                 \    | j                   ryt        j                  j                  | |      S Nr   )closedioBytesIOread)r+   amts     r   r8   z!fakehttp.<locals>.FakeSocket.readL   s!    {{::??4--r   c                 \    | j                   ryt        j                  j                  | |      S r4   )r5   r6   r7   readline)r+   lengths     r   r;   z%fakehttp.<locals>.FakeSocket.readlineQ   s#    {{::&&tV44r   c                     | xj                   dz  c_         | j                   dk(  r t        j                  j                  |        y y )Nr   r   )r/   r6   r7   closer+   s    r   r>   z"fakehttp.<locals>.FakeSocket.closeV   s3    LLAL||q 

  & !r   r)   )	__name__
__module____qualname__r/   r-   r2   r8   r;   r>   )r,   s   r   
FakeSocketr'   B   s!    	*		.
	5
	'r   rC   c                   *    e Zd ZdZ fdZW rd Zyy)$fakehttp.<locals>.FakeHTTPConnectionNc                 h     | j                         | _        | j                  t        |       _        y r)   )fakedatasocktypefakesock)r+   rC   s    r   connectz,fakehttp.<locals>.FakeHTTPConnection.connect`   s#    "4==1DI"&))DJr   c                      y r)   r$   r?   s    r   r>   z*fakehttp.<locals>.FakeHTTPConnection.closei       r   )r@   rA   rB   r*   rK   r>   )rC   
mock_closes   r   r,   rE   [   s     	, 
 r   r,   )r6   r7   httpclientHTTPConnectionrG   )rG   rN   r,   rC   s    `@@r   fakehttprR   A   s:    'RZZ '2T[[77   #+r   c                       e Zd ZddZd Zy)FakeHTTPMixinc                     t        ||      }t        j                  j                  | _        |t        j                  _        y )NrN   )rR   rO   rP   rQ   _connection_class)r+   rG   rN   fake_http_classs       r   rR   zFakeHTTPMixin.fakehttpq   s,    "8
C!%!;!;%4"r   c                 B    | j                   t        j                  _        y r)   )rW   rO   rP   rQ   r?   s    r   
unfakehttpzFakeHTTPMixin.unfakehttpv   s    %)%;%;"r   NF)r@   rA   rB   rR   rZ   r$   r   r   rT   rT   p   s    5
<r   rT   c                       e Zd Zd Zd Zy)FakeFTPMixinc                      G d dt               }t        j                  j                  | _        |t        j                  _        y )Nc                   $    e Zd Z	 	 ddZd Zd Zy),FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapperNc                      y r)   r$   )r+   userpasswdhostportdirstimeout
persistents           r   __init__z5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.__init__}   s    r   c                 .    t        j                         dfS )Nr   )r6   r7   )r+   filerI   s      r   retrfilez5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfile   s    zz|Q&r   c                      y r)   r$   r?   s    r   r>   z2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close   rM   r   )NT)r@   rA   rB   ri   rl   r>   r$   r   r   FakeFtpWrapperr`   |   s    HL $'r   rn   )objectr   r   
ftpwrapper_ftpwrapper_class)r+   rn   s     r   fakeftpzFakeFTPMixin.fakeftp{   s.    		V 		 "(!:!:$2!r   c                 B    | j                   t        j                  _        y r)   )rq   r   r   rp   r?   s    r   	unfakeftpzFakeFTPMixin.unfakeftp   s    $($:$:!r   N)r@   rA   rB   rr   rt   r$   r   r   r]   r]   z   s    3;r   r]   c                   p    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)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                    t        d| j                  j                  z  d      | _        t	        t
        j                  d      }	 |j                  | j                         |j                          t
        j                  | _	        t        j                  j                  | j                        | _        t        d| j                  z        | _        y # |j                          w xY w)Nztest_urllib: %s
asciiwbfile:%s)bytes	__class__r@   textr   r   TESTFNwriter>   pathnamer   parsequotequoted_pathnamer    returned_obj)r+   fs     r   setUpzurlopen_FileTests.setUp   s    -0G0GG!#	!!4(	GGDIIGGI!((%||11$--@#I0D0D$DE GGIs   C C"c                 ~    | j                   j                          t        j                  t        j
                         y)zShut down the open objectN)r   r>   osremover   r~   r?   s    r   tearDownzurlopen_FileTests.tearDown   s&    !
		)""#r   c                 d    dD ]+  }| j                  t        | j                  |      d|z         - y )N)	r8   r;   	readlinesfilenor>   infogeturlgetcode__iter__/object returned by urlopen() lacks %s attribute)
assertTruehasattrr   r+   attrs     r   test_interfacez urlopen_FileTests.test_interface   s6    GDOOGD$5$5t<JGr   c                 l    | j                  | j                  | j                  j                                y r)   )assertEqualr}   r   r8   r?   s    r   	test_readzurlopen_FileTests.test_read   s%    D$5$5$:$:$<=r   c                     | j                  | j                  | j                  j                                | j                  d| j                  j                         d       y )Nr   zKcalling readline() after exhausting the file did not return an empty string)r   r}   r   r;   r?   s    r   test_readlinezurlopen_FileTests.test_readline   sK    D$5$5$>$>$@Ad//88:3	4r   c                     | j                   j                         }| j                  t        |      dd       | j                  |d   | j                  d       y )Nr   z.readlines() returned the wrong number of linesr   z"readlines() returned improper text)r   r   r   r   r}   )r+   
lines_lists     r   test_readlinesz urlopen_FileTests.test_readlines   sM    &&002
Z!I	KA		=	?r   c                     | j                   j                         }| j                  |t        d       | j	                  t        j                  |t        | j                              | j                  d       y )Nzfileno() did not return an intzTReading on the file descriptor returned by fileno() did not return the expected text)	r   r   assertIsInstanceintr   r   r8   r   r}   )r+   file_nums     r   test_filenozurlopen_FileTests.test_fileno   sW    $$++-h-MN3tyy>:DII<	=r   c                 8    | j                   j                          y r)   )r   r>   r?   s    r   
test_closezurlopen_FileTests.test_close   s     	!r   c                     | j                  | j                  j                  t        j                  j
                         y r)   )r   r   headersemailmessageMessager?   s    r   test_headerszurlopen_FileTests.test_headers   s(    d//779N9NOr   c                 d    | j                  | j                  j                  | j                         y r)   )r   r   r   r   r?   s    r   test_urlzurlopen_FileTests.test_url   s$    **..0D0DEr   c                 N    | j                  | j                  j                         y r)   )assertIsNoner   statusr?   s    r   test_statuszurlopen_FileTests.test_status   s    $++223r   c                     | j                  | j                  j                         t        j                  j
                         y r)   )r   r   r   r   r   r   r?   s    r   	test_infozurlopen_FileTests.test_info   s+    d//4468M8MNr   c                 l    | j                  | j                  j                         | j                         y r)   )r   r   r   r   r?   s    r   test_geturlzurlopen_FileTests.test_geturl   s'    **113T5I5IJr   c                 V    | j                  | j                  j                                y r)   )r   r   r   r?   s    r   test_getcodezurlopen_FileTests.test_getcode   s    $++3356r   c                 ^    | j                   D ]  }| j                  || j                           y r)   )r   r   r}   )r+   lines     r   	test_iterzurlopen_FileTests.test_iter   s(     %%DT499- &r   c                 |    | j                  t        t        j                  j                  d| j
                  z          y )Nz./)assertRaises
ValueErrorr   r   r    r   r?   s    r   test_relativelocalfilez(urlopen_FileTests.test_relativelocalfile   s'    *V^^%;%;D4==<PQr   N)r@   rA   rB   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   rv   rv      s]    F$
>4?="
PF4OK7.Rr   rv   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)
ProxyTestsc                     | j                  t        j                               | _        t	        t
        j                        D ]0  }d|j                         v s| j                  j                  |       2 y )Nproxy)	enterContextr   EnvironmentVarGuardenvlistr   environlowerunset)r+   ks     r   r   zProxyTests.setUp   sN    $$Y%B%B%DEbjj!A!'')#q! "r   c                    | j                   j                  dd       t        j                  j	                         }| j                  d|d          | j                   j                  dd       | j                  t        j                  j                  d             | j                  t        j                  j                  d             | j                  t        j                  j                  d             y )NNO_PROXY	localhostno0localhost, anotherdomain.com, newdomain.com:1234anotherdomain.comanotherdomain.com:8888newdomain.com:1234)r   setr   r   getproxies_environmentr   r   proxy_bypass_environmentr+   r   s     r   +test_getproxies_environment_keep_no_proxiesz6ProxyTests.test_getproxies_environment_keep_no_proxies   s    Z-..779gdm4Z!ST??@STU??@XYZ??@TUVr   c                    	 | j                   j                  dd       t        j                  j	                         }| j                  d|d          | j                   j                  dd       t        j                  j	                         }| j                  d|       | j                   j                  d       | j                   j                  d       y # | j                   j                  d       | j                   j                  d       w xY w)N
HTTP_PROXYhttp://somewhere:3128rO   REQUEST_METHODGET)r   r   r   r   r   r   assertNotInr   r   s     r   test_proxy_cgi_ignorez ProxyTests.test_proxy_cgi_ignore   s    		)HHLL'>?nn;;=G4gfoFHHLL)51nn;;=GVW-HHNN+,HHNN<( HHNN+,HHNN<(s   BC 8Dc                    t         j                  j                  }| j                  j	                  dd       | j                   |d             | j                   |d             | j                   |d             | j                   |d             | j                   |d             | j                   |d             | j                   |d	             | j                   |d
             | j                   |d             | j                   |d             | j                   |d             | j                   |d             | j                   |d             | j                   |d             y )Nr   z8localhost, anotherdomain.com, newdomain.com:1234, .d.o.tr   	LocalHost	LOCALHOSTz
.localhostr   z.newdomain.com:1234z	foo.d.o.tzd.o.tr   z.anotherdomain.com:8888zwww.newdomain.com:1234prelocalhostnewdomain.comznewdomain.com:1235r   r   r   r   r   r   assertFalser+   bypasss     r   (test_proxy_bypass_environment_host_matchz3ProxyTests.test_proxy_bypass_environment_host_match  s    88ZO	Q{+,{+,{+,|,-345456{+,w(78989:789/001 456r   c                    t         j                  j                  }| j                  j	                  dd       | j                   |d             | j                   |d             | j                  j	                  dd       | j                   |d             | j                   |d             | j                   |d             y )Nr   *r   r   z*, anotherdomain.comr   r   r   s     r   *test_proxy_bypass_environment_always_matchz5ProxyTests.test_proxy_bypass_environment_always_match  s    88Z%/0345Z!7823401 456r   c                     t         j                  j                  }| j                  j	                  dd       | j                   |d             | j                   |d             | j                   |d             y )Nr   r   z
localhost
zanotherdomain.com:8888
znewdomain.com:1234
)r   r   r   r   r   r   r   s     r   %test_proxy_bypass_environment_newlinez0ProxyTests.test_proxy_bypass_environment_newline)  sb    88ZG	I./ :;< 678r   N)	r@   rA   rB   r   r   r   r   r   r   r$   r   r   r   r      s!    "	W
)7&79r   r   c                       e Zd Zd Zd Zd Zy)ProxyTests_withOrderedEnvc                 h    t         j                  | _        t        j                         t         _        y r)   )r   r   
_saved_envcollectionsOrderedDictr?   s    r   r   zProxyTests_withOrderedEnv.setUp4  s    ** ,,.
r   c                 .    | j                   t        _        y r)   )r   r   r   r?   s    r   r   z"ProxyTests_withOrderedEnv.tearDown:  s    __
r   c                 ~   dt         j                  d<   dt         j                  d<   | j                  t        j                  j                  d             | j                  t        j                  j                  d             dt         j                  d<   dt         j                  d<   t        j                  j                         }| j                  i |       d	t         j                  d<   d
t         j                  d<   | j                  t        j                  j                  d             | j                  t        j                  j                  d             | j                  t        j                  j                  d             | j                  t        j                  j                  d             | j                  t        j                  j                  d             dt         j                  d<   dt         j                  d<   t        j                  j                         }| j                  d|d          y )N no_proxyr   No_Proxy	arbitrary
http_proxyr   r   z%localhost, noproxy.com, my.proxy:1234zxyz.comznoproxy.com:5678zmy.proxy:1234zmy.proxyzhttp://somewhereelse:3128
Http_ProxyrO   )	r   r   r   r   r   r   r   r   r   r   s     r   ,test_getproxies_environment_prefer_lowercasezFProxyTests_withOrderedEnv.test_getproxies_environment_prefer_lowercase=  s   !#

:!,

:@@MN@@MN#%

< #:

< ..779W%!H

:!*

:??LM??@RST??PQ@@LM@@MN#:

< #>

< ..7790'&/Br   N)r@   rA   rB   r   r   r   r$   r   r   r   r   2  s    /%Cr   r   c                      e Zd ZdZd Zd Zd Z ej                  e	d      d        Z
 ej                  e	d      d        Z ej                  e	d      d        Z ej                  e	d      d	        Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z ej4                  ej8                  dd      d        Zd Zd Zd Z  ej                  e	d      d        Z!y)urlopen_HttpTestsz.Test urlopen() opening a fake http connection.c                    | j                  d|z   dz          	 t        d      }| j                  |j                         d       | j                  |j                         d       | j                  |j	                         d       | j                  |j                         d       | j                          y # | j                          w xY w)Ns   HTTP/s    200 OK

Hello!http://python.org/   Hello!r      rR   r    r   r;   r   r   rZ   )r+   verfps      r   
check_readzurlopen_HttpTests.check_readY  s    hn'??@	-.BR[[]I6R[[]C0RYY[*>?RZZ\3/OODOOs   BB5 5Cc                     d}| j                  d       	 t        j                  j                  |      }| j	                  |j                         |       | j                          y # | j                          w xY w)Nz-http://docs.python.org/library/urllib.html#OK   HTTP/1.1 200 OK

Hello!)rR   r   r   r    r   r   rZ   )r+   r   r  s      r   test_url_fragmentz#urlopen_HttpTests.test_url_fragmentd  sX    =67	'',BRYY[#.OODOOs   ?A% %A7c                     | j                  d       	 t        d      }| j                  |j                  j                         | j                          y # | j                          w xY w)Nr
  http://www.python.org)rR   r    r   r  
will_closerZ   )r+   resps     r   test_willclosez urlopen_HttpTests.test_willclosen  sJ    67	23DOODGG../OODOOs   0A A&zssl module requiredc                    t        t        dd            dgz   D ]  }t        |      }d| d}| j                  d       	 t	        |      j                  dd      }t        j                  j                  }| j                  |d	|       5  t        j                  j                  d
|        d d d        | j                  |d	|       5  t        j                  j                  d|        d d d        t        d
|       }| j                  ||j                                | j                           y # 1 sw Y   xY w# 1 sw Y   WxY w# | j                          w xY w)Nr   !      z//localhost:7777/test/   HTTP/1.1 200 OK

Hello.\\\contain control.*http:https:)r   rangechrrR   reprreplacerO   rP   
InvalidURLassertRaisesRegexr   r   r    r   r   rZ   )r+   char_nor   schemeless_urlescaped_char_reprr  r  s          r   (test_url_path_with_control_char_rejectedz:urlopen_HttpTests.test_url_path_with_control_char_rejectedv  sH   E!TN+tf4Gw<D4TF!<NMM:;" %)J$6$6tU$C!![[33
++"34E3F GINN**U>2B+CDI ++"34E3F GINN**VN3C+DEI ~&678  t{{}5!/ 5I II I !s=   A
E#D8.E#E.6E8E	=EE		EE"c                    | j                  d       d}d|z   dz   }	 t        j                  j                  }| j	                  |d      5  t
        j                  j                  d|        d d d        | j	                  |d      5  t
        j                  j                  d|        d d d        t        d|       }| j                  d	|j                                | j                  d
|j                                | j                  d|j                                | j                          y # 1 sw Y   xY w# 1 sw Y   xY w# | j                          w xY 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   
)rR   rO   rP   r  r   r   r   r    r   r   rZ   )r+   rd   r"  r  r  s        r   4test_url_path_with_newline_header_injection_rejectedzFurlopen_HttpTests.test_url_path_with_newline_header_injection_rejected  s%   67O';;	 //J''IK&&~.>'?@K ''
4KL&&/?'@A M U>"234DS$++-0T4;;=1T4;;=1OOK K ML OOs<   ,E 	#D0,E #D<)A6E 0D95E <EE Ec                    t        t        dd            dgz   D ]  }t        |      }d| d}| j                  d       	 t	        |      j                  dd      }t        j                  j                  }| j                  |d	|       5  t        d
|        d d d        | j                  |d	|       5  t        d|        d d d        | j                           y # 1 sw Y   HxY w# 1 sw Y   (xY w# | j                          w xY w)Nr   r  r  z//localhostz/test/r  r  r  r  r  r  )r   r  r  rR   r  r  rO   rP   r  r   r    rZ   )r+   r!  r   r"  r#  r  s         r   (test_url_host_with_control_char_rejectedz:urlopen_HttpTests.test_url_host_with_control_char_rejected  s   E!TN+tf4Gw<D*4&7NMM:;	"$(J$6$6tU$C!![[33
++"34E3F GIeN#345I ++J:KL]K^8_`f^$456 a ! 5I I a` !s=    A
C8
C C86C,C8 C)	%C8,C5	1C88D
c                    | j                  d       d}d|z   dz   }	 t        j                  j                  }| j	                  |d      5  t        d|        d d d        | j	                  |d      5  t        d|        d d d        | j                          y # 1 sw Y   CxY w# 1 sw Y   &xY w# | j                          w xY w)	Nr  zlocalhost
X-injected: header
r&  r'  zcontain control.*\\rr  r(  r  )rR   rO   rP   r  r   r    rZ   )r+   rd   r"  r  s       r   4test_url_host_with_newline_header_injection_rejectedzFurlopen_HttpTests.test_url_host_with_newline_header_injection_rejected  s    674';;	//J''35%/015 ''
4KL& 012 M OO5 5 ML OOs;   ,B2 	BB2 2B&B2 B#B2 &B/+B2 2Cc                 &    | j                  d       y )Ns   0.9r  r?   s    r   test_read_0_9zurlopen_HttpTests.test_read_0_9  s     	r   c                 &    | j                  d       y )Ns   1.0r2  r?   s    r   test_read_1_0zurlopen_HttpTests.test_read_1_0      r   c                 &    | j                  d       y )Ns   1.1r2  r?   s    r   test_read_1_1zurlopen_HttpTests.test_read_1_1  r6  r   c                 d   | j                  dd       	 | j                  t        j                  j                        5 }t        j
                  j                  d       d d d        j                  j                          | j                          y # 1 sw Y   4xY w# | j                          w xY 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
TrV   r  )
rR   r   r   error	HTTPErrorr   r    	exceptionr>   rZ   )r+   cms     r   test_read_bogusz!urlopen_HttpTests.test_read_bogus  s     
  	 		""6<<#9#9:b&&';< ;LL OO	 ;: OOs"   )B  B"B BB B/c                 j   | j                  dd       	 d}| j                  t        j                  j                  |      5 }t        j
                  j                  d       d d d        j                  j                          | j                          y # 1 sw Y   4xY w# | j                          w xY 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
TrV   zRedirection to url 'file:r  )
rR   r   r   r:  r;  r   r    r<  r>   rZ   )r+   msgr=  s      r   test_invalid_redirectz'urlopen_HttpTests.test_invalid_redirect  s       	 		-C''(>(>D&&';< ELL OO	 ED OOs#   ,B   B!"B  BB   B2c                    t        t               j                        D ]U  }| j                  dd       	 | j	                  t
        j                  j                  t        d       | j                          W y # | j                          w xY w)Ns_   HTTP/1.1 302 Found
Location: file://guidocomputer.athome.com:/python/license
Connection: close
TrV   http://something)
r  r   maxtriesrR   r   r   r:  r;  r    rZ   )r+   is     r   test_redirect_limit_independentz1urlopen_HttpTests.test_redirect_limit_independent  su     ~'001AMM    "!!&,,"8"8'&( ! 2 !s   /A77B	c                     | j                  d       	 | j                  t        t        d       | j	                          y # | j	                          w xY w)Nr   rC  )rR   r   OSErrorr    rZ   r?   s    r   test_empty_socketz#urlopen_HttpTests.test_empty_socket  s=     	c	gw0BCOODOOs	   ? Ac                 *   | j                  t        j                  j                        5 }t	        d       d d d        | j                  j                  j                         | j                  |j                  j                         y # 1 sw Y   TxY w)Nz/file://localhost/a/file/which/doesnot/exists.py)	r   r   r:  URLErrorr    r   r<  filenamereasonr+   es     r   test_missing_localfilez(urlopen_HttpTests.test_missing_localfile  s_    v||445EF 6,,-**+ 65   B		Bc                    t        j                         \  }}d|j                  t        j                  j
                  d      z   }	 | j                  t        j                  j                  |             t        |      5 }| j                  |       d d d        t        j                  |       t        j                  |       | j                  t        j                  j                  |             | j                  t        j                  j                        5  t        |       d d d        y # 1 sw Y   xY w# t        j                  |       t        j                  |       w xY w# 1 sw Y   y xY w)Nzfile://localhost/r  )tempfilemkstempr  r   pathsepr   existsr    r>   unlinkr   r   r   r:  rK  )r+   fdtmp_filetmp_fileurlfobjs        r   test_file_notexistsz%urlopen_HttpTests.test_file_notexists  s    '')H)H,<,<RWW[[#,NN	 OOBGGNN845%% & HHRLIIh12v||445K  65 &% HHRLIIh55s0   9D; ?D/D; E*/D84D; ;,E'*E3c                 .   d}| j                  t        j                  j                        5 }t	        |       d d d        | j                  j                  j                         | j                  |j                  j                         y # 1 sw Y   TxY w)Nzftp:///path
r   r   r:  rK  r    r   r<  rL  r   rM  )r+   test_ftp_urlrO  s      r   test_ftp_nohostz!urlopen_HttpTests.test_ftp_nohost"  se    $v||445L! 6--.**+ 65s   BBc                 *   | j                  t        j                  j                        5 }t	        d       d d d        | j                  j                  j                         | j                  |j                  j                         y # 1 sw Y   TxY w)Nz.ftp://localhost/a/file/which/doesnot/exists.pyr_  rN  s     r   test_ftp_nonexistingz&urlopen_HttpTests.test_ftp_nonexisting)  sa    v||445DE 6--.**+ 65rQ  MAXFTPCACHEr   c                    | j                          	 t        j                  j                  ddddg       t        j                  j                  d<   t        d       | j                          y # | j                          w xY w)Nrb   passr      testzftp://localhost)rr   r   r   rp   ftpcacher    rt   r?   s    r   test_ftp_cache_pruningz(urlopen_HttpTests.test_ftp_cache_pruning/  s]    	.4nn.G.GPVXcegik.lFNN##F+%&NNDNNs   A	A, ,A>c                    | j                  d       	 t        d      }| j                  |j                         d       | j                  |j                         d       | j                  |j	                         d       | j                  |j                         d       | j                          y # | j                          w xY w)N   HTTP/1.0 200 OK

Hello!zhttp://user:pass@python.org/r  r   r  r  )r+   r  s     r   test_userpass_inurlz%urlopen_HttpTests.test_userpass_inurl8  s    67	78BR[[]I6R[[]C0RYY[*HIRZZ\3/OODOOs   BB/ /Cc                    | j                  d       	 d}dj                  |      }t        j                  j                  }dt        |j                  d            j                  d      z  }t        |      }| j                  ||j                  j                  d             | j                  |j                         d       | j                  |j                         d       | j                  |j                         |       | j                  |j                         d	       | j!                          y # | j!                          w xY w)
Nrl  za b:c dzhttp://{}@python.org/zAuthorization: Basic %s
ASCIIzUTF-8r  r   r  )rR   formatrO   rP   rQ   r   encodedecoder    assertInr*   r   r;   assertNotEqualr   r   rZ   )r+   userpassr   fakehttp_wrapperauthorizationr  s         r   test_userpass_inurl_w_spacesz.urlopen_HttpTests.test_userpass_inurl_w_spacesC  s    67	 H)00:C#{{99:&xw'?@GGPQMBMM-)9)=)=)D)DW)MNR[[]I6R[[]C0		S1RZZ\3/OODOOs   DD3 3Ec                     t        j                  dt        f      5  t        j                  j                          d d d        y # 1 sw Y   y xY wNr   )r   r"   r#   r   r   	URLopenerr?   s    r   test_URLopener_deprecationz,urlopen_HttpTests.test_URLopener_deprecationV  s3    ++R0B,CDNN$$& EDDs   AAc                 $   t        j                         }t        j                  dt        f      5  | j                  t              5  t        j                  j                  dd|       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   zhttps://localhostz/nonexistent/path)cafilecontext)
sslcreate_default_contextr   r"   r#   r   r   r   r   r    )r+   r  s     r   test_cafile_and_contextz)urlopen_HttpTests.test_cafile_and_contextZ  sp    ,,.++R1C,DE"":.&&'0CW '  / FE.. FEs#   B#A:)B:B	?BBN)"r@   rA   rB   r   r  r  r  unittest
skipUnlessr  r$  r,  r.  r0  r3  r5  r8  r>  rA  rF  rI  rP  r]  ra  rc  r   ro   r   r   rj  rm  rx  r|  r  r$   r   r   r   r   V  s5   8	 X34" 5"4 X34 52 X34" 5"  X34 5 
  "",!,, U\\&..-3 4	&' X34 5r   r   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)urlopen_DataTestsz"Test urlopen() opening a data URL.c                 
   | j                  t        j                  j                         d| _        d| _        d| _        d| _        d| _        | j                  t        j                  j                  | j                              | _        | j                  t        j                  j                  | j                              | _        | j                  t        j                  j                  | j                              | _        y )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   
urlcleanupr}   imagetext_urltext_url_base64	image_urlr   r    text_url_resptext_url_base64_respimage_url_respr?   s    r   r   zurlopen_DataTests.setUpg  s    112 ;	F 	
( 	 	. 	
 "..NN""4==13$($5$5NN""4#7#78%:!"//0F0Ft~~0VWr   c                 d    dD ]+  }| j                  t        | j                  |      d|z         - y )N)r8   r;   r   r>   r   r   r   r   r   )r   r   r  r   s     r   r   z urlopen_DataTests.test_interface  s6    GDOOGD$6$6=JGr   c                 L   | j                  | j                  j                         t        j                  j
                         | j                  | j                  j                         j                         ddg       | j                  | j                  j                         d   t        t        | j                                     t        j                  j                  d      }| j                  |j                         j                         ddg       |j!                          y )N)z
text/plainr   )charsetz
ISO-8859-1zcontent-lengthzdata:,)r  zUS-ASCII)r   r  r   r   r   r   r   r  
get_paramsr  strr   r  r   r   r    r>   )r+   rs     r   r   zurlopen_DataTests.test_info  s    d005579N9NO22779DDF!:;	=,,1134DEDJJ 	"NN""8,,,.!89	;		r   c                 <   | j                  | j                  j                         | j                         | j                  | j                  j                         | j
                         | j                  | j                  j                         | j                         y r)   )r   r  r   r  r  r  r  r  r?   s    r   r   zurlopen_DataTests.test_geturl  sp    ++224dmmD2299;  	",,335t~~Fr   c                     | j                  | j                  j                         j                  t	        | j                  j                         j                               d         | j                         y Nr  )r   r  r8   rr  dictr   r  r}   r?   s    r   test_read_textz urlopen_DataTests.test_read_text  sZ    ++00299##((*5578CEFJii	Qr   c                     | j                  | j                  j                         j                  t	        | j                  j                         j                               d         | j                         y r  )r   r  r8   rr  r  r   r  r}   r?   s    r   test_read_text_base64z'urlopen_DataTests.test_read_text_base64  sY    22779@@**//1<<>?	JLII	r   c                 l    | j                  | j                  j                         | j                         y r)   )r   r  r8   r  r?   s    r   test_read_imagez!urlopen_DataTests.test_read_image  s%    ,,113TZZ@r   c                 b    | j                  t        t        j                  j                  d       y )Nzdata:text/plainr   r   r   r   r    r?   s    r   test_missing_commaz$urlopen_DataTests.test_missing_comma  s    *V^^%;%;<MNr   c                 b    | j                  t        t        j                  j                  d       y )Nzdata:;base64,Cg=r  r?   s    r   test_invalid_base64_dataz*urlopen_DataTests.test_invalid_base64_data  s    *V^^%;%;<NOr   N)r@   rA   rB   r   r   r   r   r   r  r  r  r  r  r$   r   r   r  r  d  s9    ,X@	GQ
AOPr   r  c                   T    e Zd ZdZd Zd Zd ZddZd Zd Z	d Z
d	 Zd
 Zd Zd Zy)urlretrieve_FileTestsz(Test urllib.urlretrieve() on local filesc                    | j                  t        j                  j                         g | _        | j                  t        j                         d| _        	 t        t        j                  d      }|j                  | j                         |j                          |j                          y #  Y y xY w# j                          w #  Y w xY wxY w)Ns   testing urllib.urlretrievery   )r  r   r   r  	tempFilesregisterFileForCleanUpr   r~   r}   r   r   r>   )r+   FILEs     r   r   zurlretrieve_FileTests.setUp  s    112  	##I$4$451			(($/DJJtyy!JJLD Ds1   AB5 B. .B25C6CCC	Cc                 ^    | j                   D ]  }t        j                  |        y #  Y xY wr)   )r  r   r   )r+   eachs     r   r   zurlretrieve_FileTests.tearDown  s"    NND4 #Ds   (,c                     t         j                  j                  |      }dt        j                  j                  |      z  S )Nz	file://%s)r   rU  abspathr   r   pathname2url)r+   filePaths     r   constructLocalFileUrlz+urlretrieve_FileTests.constructLocalFileUrl  s.    77??8,V^^88BBBr   c                 .   t        j                         \  }}	 | j                  |       t        j                  |d      }|j                  |       |j                          |j                          |S #  Y |S xY w# j                          w #  Y w xY wx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.ry   )rS  rT  r  r   fdopenr   r>   )r+   r   newFdnewFilePathnewFiles        r   createNewTempFilez'urlretrieve_FileTests.createNewTempFile  s    
 &--/{	''4iit,GMM$MMO D Ds0   AA; !A3 3A8;B<BBBBc                 :    | j                   j                  |       y r)   )r  append)r+   fileNames     r   r  z,urlretrieve_FileTests.registerFileForCleanUp  s    h'r   c                    t         j                  j                  dt        j                  z        }| j                  |d   t        j                         | j                  |d   t        j                  j                  d       y )Nrz   r   r   zFdid not get an email.message.Message instance as second returned value)
r   r   urlretriever   r~   r   r   r   r   r   )r+   results     r   
test_basicz urlretrieve_FileTests.test_basic  sb     ++I	8H8H,HII$4$45fQi)>)>9	:r   c                 F   dt         j                  z  }| j                  |       t        j                  j                  | j                  t         j                        |      }| j                  ||d          | j                  t        j                  j                  |      d       t        |d      }	 |j                         }|j                          |j                          | j                  | j                  |       y #  Y !xY w# |j                          w #  Y w xY wxY w)N%s.2r   zcopy of the file was not maderb)r   r~   r  r   r   r  r  r   r   r   rU  rW  r   r8   r>   r}   )r+   second_tempr  r  r}   s        r   	test_copyzurlretrieve_FileTests.test_copy  s    y///##K0++D,F,F-*,fQi0{3 69 	:K&	99;DJJLD) D Ds0   3 D D   DD DD DD c                      dgf fd	}dt         j                  z  } j                  |       t        j                  j                   j                  t         j                        ||       y )Nr   c                     j                  | t               j                  |t               j                  |t               j                  | |d          |d   dz   |d<   y )Nr   r   )r   r   r   )block_countblock_read_size	file_sizecount_holderr+   s       r   
hooktesterz9urlretrieve_FileTests.test_reporthook.<locals>.hooktester	  sY    !!+s3!!/37!!)S1[,q/:*1o1LOr   r  )r   r~   r  r   r   r  r  )r+   r  r  s   `  r   test_reporthookz%urlretrieve_FileTests.test_reporthook  sX    NOS 	2 y///##K0""&&y'7'78	%r   c                    g }|fd}| j                         }t        j                  j                  | j	                  |      t
        j                  |       | j                  t        |      d       | j                  |d   d   d       y )Nc                 ,    |j                  | ||f       y r)   r  r  r  r  _reports       r   r  zAurlretrieve_FileTests.test_reporthook_0_bytes.<locals>.hooktester      NNK)DEr   r   r   r
   	r  r   r   r  r  r   r~   r   r   r+   reportr  srcFileNames       r   test_reporthook_0_bytesz-urlretrieve_FileTests.test_reporthook_0_bytes  st    HN 	F,,.""4#=#=k#Jj	*Va(1q)r   c                 F   g }|fd}| j                  d      }t        j                  j                  | j	                  |      t
        j                  |       | j                  t        |      d       | j                  |d   d   d       | j                  |d   d   d       y )Nc                 ,    |j                  | ||f       y r)   r  r  s       r   r  zAurlretrieve_FileTests.test_reporthook_5_bytes.<locals>.hooktester%  r  r   s   xxxxxr
   r      r   r  r  s       r   test_reporthook_5_bytesz-urlretrieve_FileTests.test_reporthook_5_bytes   s     HN 	F,,X6""4#=#=k#Jj	*Va(1q)1q)r   c                    g }|fd}| j                  ddz        }t        j                  j                  | j	                  |      t
        j                  |       | j                  t        |      d       | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d   d       y )	Nc                 ,    |j                  | ||f       y r)   r  r  s       r   r  zDurlretrieve_FileTests.test_reporthook_8193_bytes.<locals>.hooktester3  r  r      xi      r   r
   r   i    r  r  s       r   test_reporthook_8193_bytesz0urlretrieve_FileTests.test_reporthook_8193_bytes.  s     HN 	F,,TD[9""4#=#=k#Jj	*Va(1t,1t,1t,1t,r   N)r   )r@   rA   rB   r   r   r   r  r  r  r  r  r  r  r  r  r$   r   r   r  r    s=    20C (:*$%	**-r   r  c                       e Zd ZdZd Zd Zy)urlretrieve_HttpTestsz5Test urllib.urlretrieve() using fake http connectionsc                    | j                  t        j                  j                         | j	                  d       d }| j                  t        j                  j                        5  	 t        j                  j                  t        j                  |       | j                          	 d d d        y # | j                          w xY w# 1 sw Y   y xY w)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                      y r)   r$   )par1par2par3s      r   _reporthookzYurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError.<locals>._reporthookO  s    r   )
reporthookr  r   r   r  rR   r   r:  ContentTooShortErrorr  r   TEST_HTTP_URLrZ   )r+   r  s     r   .test_short_content_raises_ContentTooShortErrorzDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorB  s    112  		 v||@@A"**7+@+@6A + C ! BA
 ! BAs$   'C)/B2C2CCCc                    | j                  t        j                  j                         | j	                  d       | j                  t        j                  j                        5  	 t        j                  j                  t        j                         | j                          	 d d d        y # | j                          w xY w# 1 sw Y   y xY w)Nr  r  r?   s    r   Atest_short_content_raises_ContentTooShortError_without_reporthookzWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookY  s    112  	 v||@@A"**7+@+@A!	 BA !	 BAs$   $C&-B-C-B??CCN)r@   rA   rB   r   r  r  r$   r   r   r  r  ?  s    ?"."r   r  c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)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                 
   dj                  g d      }t        j                  j                  |      }| j	                  ||d|d|       t        j                  j                  |      }| j	                  ||d|d|       y )Nr   )ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
0123456789z_.-~using quote():  != using quote_plus(): )joinr   r   r   r   
quote_plus)r+   do_not_quoter  s      r   test_never_quotezQuotingTests.test_never_quote  su    xx !) * ##L1v6BFK	M((6v:FO	Qr   c                 r    | j                  t        j                  j                  j                  d   d       y )Nr   r  )r   r   r   r   __defaults__r?   s    r   test_default_safezQuotingTests.test_default_safe  s'    ++88;SAr   c                    d}t         j                  j                  ||      }| j                  ||d|d|       t         j                  j	                  ||      }| j                  ||d|d|       t         j                  j                  |d      }| j                  ||d|d|       t         j                  j                  ddd	
      }t         j                  j                  ddd
      }| j                  ||d|d|       t         j                  j                  ddd
      }t         j                  j                  ddd
      }| j                  ||d|d|       y )Nz<>safer  r  r  s   <>u   aüblatin-1   ü)encodingr  r      )r   r   r   r   r  )r+   quote_by_defaultr  expects       r   	test_safezQuotingTests.test_safe  s\   ##$4;K#L)66FO	Q(()9.> ) @)6*F4	5 ##$45#A)66FO	Q ##Hyv#N##Hyr#J &*	+ ##Hyw#O##Hyr#J &*	+r   c                    t        d      D cg c]  }t        |       }}|j                  d       |j                  t        d             dj                  |      }|D ]  }t        j
                  j                  |      }| j                  t        |      |d|dt        |      d|       t        j
                  j                  |      }| j                  t        |      |d|d	t        |      d|        ~d
}d}t        j
                  j                  |      }| j                  ||d|d|       t        j
                  j                  |      }| j                  ||d|d|       y c c}w )N    z<>#%"{}|\^[]`r  r   r  z should be escaped to z, not r  z should be escapes to zab[]cdz
ab%5B%5Dcdr  )
r  r  r  r  r   r   r   r   r   r  )r+   numshould_quoter   r  partial_quoteexpecteds          r   test_default_quotingz!QuotingTests.test_default_quoting  sE    -2"I6ISCI6,-CH%ww|, D\\''-FYt_f #IdOV=> \\,,T2FYt_f #IdOV=> !  ##M266>G	I((76;CVL	N- 7s   E5c           
      @   t         j                  j                  d      }| j                  |t	        d      d|dt	        d             t         j                  j                  d      }| j                  |dd|z         d}|j                  dt	        d            }t         j                  j                  |      }| j                  ||d|d|       |j                  dd      }t         j                  j                  |      }| j                  ||d|d|       y )Nr)  r  r  +zusing quote_plus(): %r != +z
a b cd e fr  )r   r   r   r   r   r  r  )r+   r  givenr  s       r   test_quoting_spacezQuotingTests.test_quoting_space  s     ##C(36<inM	O((-6?	AsIcN3##E*6<fE	GsC(((/;A6J	Lr   c                    | j                  t        j                  j                  d      d       | j                  t        j                  j                  dd      d       | j                  t        j                  j                  d      d       | j                  t        j                  j                  dd      d       y )Nzalpha+beta gammazalpha%2Bbeta+gammar  zalpha+beta+gammas   alpha+beta gamma   +)r   r   r   r  r?   s    r   test_quoting_pluszQuotingTests.test_quoting_plus  s    001CD-	/001CSI+	- 	001DE-	/ 	001CTJ+	-r   c                 N   d}d}t         j                  j                  |      }| j                  ||d|d|       | j	                  t
        t         j                  j                  |d       t         j                  j                  |      }| j                  ||d|d|       y )N   ab%A2%D8ab%FFr  r  r  r  zusing quote_from_bytes(): )r   r   r   r   r   	TypeErrorquote_from_bytesr+   r  r  r  s       r   test_quote_byteszQuotingTests.test_quote_bytes  s    !##E*6<fE	G 	)V\\%7%7%. 	 	0 ..u5"F,	-r   c                 H   d}d}t         j                  j                  |      }| j                  ||d|d|       t         j                  j                  |d d       }| j                  ||d|d|       d}d}t         j                  j                  |d      }| j                  ||d|d|       d	}d
}t         j                  j                  |      }| j                  ||d|d|       d	}| j	                  t
        t         j                  j                  |d       d	}d}t         j                  j                  |dd      }| j                  ||d|d|       d	}d}t         j                  j                  |dd      }| j                  ||d|d|       y )Nu   ¢Øabÿz%C2%A2%C3%98ab%C3%BFr  r  r  errorsr  r  r     漢字%E6%BC%A2%E5%AD%97z%3F%3Fr  z%26%2328450%3B%26%2323383%3Bxmlcharrefreplace)r   r   r   r   r   UnicodeEncodeErrorr  s       r   test_quote_with_unicodez$QuotingTests.test_quote_with_unicode  s    '##E*6<fE	G ##ED#F6<fE	G !##EI#>6<fE	G %##E*6<fE	G ,fll.@.@%-6 	 	8 ##EI+4 $ 66<fE	G /##EI+> $ @6<fE	Gr   c                     d}d}t         j                  j                  |d      }| j                  ||d|d|       d}d}t         j                  j                  |dd	
      }| j                  ||d|d|       y )Nu   ¢Ø ÿz
%A2%D8+%FFr  r  r  r  u   ab漢字 cdzab%3F%3F+cdr  r!  )r   r   r  r   r  s       r   test_quote_plus_with_unicodez)QuotingTests.test_quote_plus_with_unicode)  s    (((C;A6J	L $((09 ) ;;A6J	Lr   N)r@   rA   rB   r   r  r  r	  r  r  r  r  r'  r)  r$   r   r   r  r  l  s;    2QB+:N8L&
-- (GTLr   r  c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)UnquotingTestszwTests for unquote() and unquote_plus()

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

    c           	         g }t        d      D ]  }t        t        |            }t        |      }t        j                  j                  |      }| j                  ||d|d|       t        j                  j                  |      }| j                  ||d|d|       |j                  |        dj                  |      }~t        j                  j                  |      }| j                  |j                  d      dd|z         y )	N   using unquote(): r  using unquote_plus(): r   r   r   z/using unquote(): not all characters escaped: %s)r  r   r  r   r   unquoter   unquote_plusr  r  count)r+   escape_listr  r  r  r  escape_strings          r   test_unquotingzUnquotingTests.test_unquoting@  s    :Cc#h'EXF\\))%0FVV<BFKM\\..u5FVV$f./ u%  ,%%m4c*A &'	(r   c                     | j                  t        t        ft        j                  j
                  d        | j                  t        t        ft        j                  j
                  d       y )Nr$   )r   r  AttributeErrorr   r   r0  r?   s    r   #test_unquote_rejects_none_and_tuplez2UnquotingTests.test_unquote_rejects_none_and_tupleU  sD    9n5v||7K7KTR9n5v||7K7KRPr   c                    d}|}t         j                  j                  |      }| j                  ||d|d|       d}|}t         j                  j                  |      }| j                  ||d|d|       d}|}t         j                  j                  |      }| j                  ||d|d|       d}t	        |d      }t         j                  j                  |      }| j                  ||d|d|       d}t	        |d      }t         j                  j                  |      }| j                  ||d|d|       d}t	        |d      }t         j                  j                  |      }| j                  ||d|d|       | j                  t        t        ft         j                  j
                  d        | j                  t        t        ft         j                  j
                  d       y )	Nz%xabr.  r  z%xr   rx   using unquote_to_bytes(): r$   )	r   r   r0  r   r{   unquote_to_bytesr   r  r7  r  s       r   test_unquoting_badpercentz(UnquotingTests.test_unquoting_badpercentY  s   %%e,"F*, 	-%%e,"F*, 	-%%e,"F*, 	- ug&..u5"F*, 	-ug&..u5"F*, 	-ug&..u5"F*, 	-9n5v||7T7TVZ[9n5v||7T7TVXYr   c                 |    d}d}t         j                  j                  |      }| j                  ||d|d|       y )Nz%Ab%eAs   r:  r  r   r   r;  r   r  s       r   test_unquoting_mixed_casez(UnquotingTests.test_unquoting_mixed_case}  s<    ..u5"F,	-r   c                    dt        d      z  }d}t        j                  j                  |      }| j	                  ||d|d|       t        j                  j                  |      }| j	                  ||d|d|       y )Nzab%sdcabcdr  r  r/  )r   r   r   r0  r   r1  r  s       r   test_unquoting_partsz#UnquotingTests.test_unquoting_parts  su     )C.(%%e,6<fE	G**51=CVL	Nr   c                    d}|}t         j                  j                  |      }| j                  ||d|d|       |j	                  dd      }t         j                  j                  |      }| j                  ||d|d|       y )Nzare+there+spaces...r.  r  r  r)  r/  )r   r   r0  r   r  r1  r  s       r   test_unquoting_plusz"UnquotingTests.test_unquoting_plus  sz    %%%e,8>G	IsC(**51=CVL	Nr   c                    d}d}t         j                  j                  |      }| j                  ||d|d|       t         j                  j                  d      }d}| j                  ||d|d|       d}d}t         j                  j                  |      }| j                  ||d|d|       d	}d}t         j                  j                  |      }| j                  ||d|d|       y )
N"br%C3%BCckner_sapporo_20050930.docs   brückner_sapporo_20050930.docr:  r  	   漢%C3%BCs   漢üs   %A2%D8ab%FFr  s	   %A2ab%FFr>  r  s       r   test_unquote_to_bytesz$UnquotingTests.test_unquote_to_bytes  s    48..u5"F,	- ..~>("F,	- "..u5"F,	-
  "..u5"F,	-r   c                 6   d}d}t         j                  j                  |      }| j                  ||d|d|       t         j                  j                  |d d       }| j                  ||d|d|       t         j                  j                  dd      }d}| j                  ||d|d|       d	}d
}t         j                  j                  |      }| j                  ||d|d|       d}d}t         j                  j                  |      }| j                  ||d|d|       t         j                  j                  |d      }| j                  ||d|d|       d}d}t         j                  j                  |d      }| j                  ||d|d|       t         j                  j                  d      }d}| j                  ||d|d|       t         j                  j                  dd      }d}| j                  ||d|d|       y )NrG  u   brückner_sapporo_20050930.docr.  r  r!  zbr%FCckner_sapporo_20050930.docr  r  r$  r#  z%F3%B1u   �r  )r"  r   ignorerH  u   漢üu   漢%FCr   r   r0  r   r  s       r   test_unquote_with_unicodez(UnquotingTests.test_unquote_with_unicode  s   45%%e,8>G	I %%ed4%H8>G	I %%&G/8 & :58>G	I %%%e,8>G	I %%e,8>G	I %%eI%>8>G	I %%eH%=8>G	I %%n58>G	I
 %%kI%F8>G	Ir   c                 l   d}d}t         j                  j                  |      }| j                  ||d|d|       d}d}t         j                  j                  |      }| j                  ||d|d|       d}d}t         j                  j                  |      }| j                  ||d|d|       y )	Ns   blueberryjamblueberryjamr.  r  s   blåbærsyltetøyu   blåbærsyltetøys   bl%c3%a5b%c3%a6rsyltet%c3%b8ju   blåbærsyltetøjrL  r  s       r   test_unquoting_with_bytes_inputz.UnquotingTests.test_unquoting_with_bytes_input  s    %%e,8>G	I 70%%e,8>G	I 10%%e,8>G	Ir   N)r@   rA   rB   r   r5  r8  r<  r?  rC  rE  rI  rM  rP  r$   r   r   r+  r+  9  s<    (*Q"ZH-
N
N->8ItIr   r+  c                   X    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)urlencode_TestszTests for urlencode()c                    g d}t         j                  j                  |      }|D ]  }| j                  ||d|d|d|         | j	                  |j                  d      dd|d|j                  d             |j                  d      }||dz
     }||dz      }| j                  |j                         xr |j                         d|d	|       | j	                  t        |      d
d|dt        |      dd
       y)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 z: z not found in &r
   z: expected 2 '&'s; got r   z%: '&' not located in proper place in    z#: unexpected number of characters: r  N)
r   r   	urlencoders  r   r2  indexr   isdigitr   )	r+   r  	test_typeexpect_somewherer  r  amp_locationon_amp_lefton_amp_rights	            r   help_inputtypezurlencode_Tests.help_inputtype  s     7''.(HMM(F#Xv78 ) 	c*A#V\\#%68	9 ||C(\A-.lQ./++-H,2F2F2H)	* 	Vk $S[+?	@r   c                 0    | j                  ddddd       y )N123)1st2nd3rdzusing dict as input typer^  r?   s    r   test_using_mappingz"urlencode_Tests.test_using_mapping1  s    3c=6	8r   c                 ,    | j                  g dd       y )N))rc  r`  )rd  ra  )re  rb  z*using sequence of two-item tuples as inputrf  r?   s    r   test_using_sequencez#urlencode_Tests.test_using_sequence6  s    FH	Jr   c                    ddi}t        d      dt        d      }t        j                  j                  |      }| j	                  ||       ddi}d}t        j                  j                  |      }| j	                  ||       y )NrT  =zkey namezA bunch of pluseszkey+name=A+bunch+of+pluses)r   r   r   rV  r   r  s       r   test_quotingzurlencode_Tests.test_quoting;  su    S	%cNIcN;''.(/0-''.(r   c                    dg di}dt         j                  j                  t        g d            z  }t         j                  j	                  |      }| j                  ||       t         j                  j	                  |d      }|d   D ]  }d|z  }| j                  ||        | j                  |j                  d      dd|j                  d      z         y )Nsequence)r`  ra  rb  zsequence=%sTrT  r
   zExpected 2 '&'s, got %s)r   r   r  r  rV  r   rs  r2  )r+   r  r  r  values        r   
test_doseqzurlencode_Tests.test_doseqF  s    O,!8!8_9M!NN''.(''t4:&E"U*FMM&&) ' 	c*A2V\\#5FF	Hr   c                     | j                  dt        j                  j                  i              | j                  dt        j                  j                  g              y rz  r   r   r   rV  r?   s    r   test_empty_sequencez#urlencode_Tests.test_empty_sequenceS  s@    V\\33B78V\\33B78r   c                     | j                  dt        j                  j                  ddi             | j                  dt        j                  j                  dd i             y )Nza=1ar   za=Nonerr  r?   s    r   test_nonstring_valuesz%urlencode_Tests.test_nonstring_valuesW  sH     6 6Qx @A6<<#9#93+#FGr   c                 f   | j                  dt        j                  j                  dddgid             | j                  dt        j                  j                  dd dgid             t	        j
                  ddg      }| j                  d	t        j                  j                  d|id             y )
Nza=1&a=2ru  r   r
   Tz
a=None&a=a)ru  r   )br   za=a&a=b)r   r   r   rV  r   r   )r+   r   s     r   test_nonstring_seq_valuesz)urlencode_Tests.test_nonstring_seq_values[  s    FLL$:$:C!Q=$$OP//tSk0BDI	K&&(';<//dTB	Dr   c                 L   d}d}t         j                  j                  |dd      }| j                  ||       d}d}t         j                  j                  |      }| j                  ||       d}d}t         j                  j                  |d	      }| j                  ||       y )
N)       Á%3F=%3Fro  r  r!  %C2%A0=%C3%81%A0=%C1r  r  r   r   rV  r   r  s       r   test_urlencode_encodingz'urlencode_Tests.test_urlencode_encodingc  s    '''	'R( ( ''.( (''	'B(r   c                    d}d}t         j                  j                  |ddd      }| j                  ||       d}d}t         j                  j                  |ddd	      }| j                  ||       d}d
}t         j                  j                  |d      }| j                  ||       d}d}t         j                  j                  |d      }| j                  ||       d}d}t         j                  j                  |dd      }| j                  ||       d}d}t         j                  j                  |dd      }| j                  ||       y )Nr{  r~  Tro  r  )doseqr  r"  ))r|  )r   r}  z%3F=1&%3F=%3Fr!  r  ))r|  )*   r}  z%C2%A0=42&%C2%A0=%C3%81r  r  r  z%A0=42&%A0=%C1r  r  s       r   test_urlencode_encoding_doseqz-urlencode_Tests.test_urlencode_encoding_doseqv  sL   '''T18 ( L( - ''t18 ( L( ( ''t4(-*''t4( (''ti'H(-!''ti'H(r   c                 >   d}d}t         j                  j                  |      }| j                  ||       t         j                  j                  |d      }| j                  ||       d}d}t         j                  j                  |d      }| j                  ||       y )N)   $   $z%A0%24=%C1%24T))r  )r  r  z%A0%24=42&%A0%24=%C1%24r  r  s       r   test_urlencode_bytesz$urlencode_Tests.test_urlencode_bytes  s    - ''.(''t4( 4*''t4(r   c                    d}t         j                  j                  |d      }d}| j                  ||       d}t         j                  j                  |dd      }d}| j                  ||       d}d}t         j                  j                  |dd      }| j                  ||       d}t         j                  j                  |dd	
      }d}| j                  ||       d}d}t         j                  j                  |ddd	      }d}d}t         j                  j                  |ddd	
      }| j                  ||       y )Nr  z:$r  z	%A0$=%C1$T)r  r  ))r  )r     r  z%A0$=%C1$&%A0$=13&%A0$=42r  )r  r  )r  r  r  r  )r+   r  r  r  s       r   &test_urlencode_encoding_safe_parameterz6urlencode_Tests.test_urlencode_encoding_safe_parameter  sI   
 .''D'9(-''T'E( 9,''t$'?( .''D1: ( <(-''T1: ( < 9,''t$1: ( <(r   N)r@   rA   rB   r   r^  rg  ri  rl  rp  rs  rv  ry  r  r  r  r  r$   r   r   rR  rR    sH    @@8
J
	)H9HD)&#)J)&)r   rR  c                      e Zd ZdZd Zd Z ej                  ej                  dk(  d      d        Z
 ej                  ej                  dk(  d      d        Z ej                  ej                  d	      d
        Z ej                  ej                  dk(  d      d        Z ej                  ej                  dk(  d      d        Z ej                  ej                  d	      d        Zy)Pathname_Testsz&Test pathname2url() and url2pathname()c                 ,   t         j                  j                  dddd      }d}t        j                  j                  |      }| j                  ||d|d|       t        j                  j                  |      }| j                  ||d|d|       y )	Npartsofru  rU  zparts/of/a/pathpathname2url() failed; r  zurl2pathame() failed; )r   rU  r  r   r   r  r   url2pathname)r+   expected_pathexpected_urlr  s       r   r  zPathname_Tests.test_basic  s    WdC@(,,];v ,0	1 ,,\: -1	2r   c                    t         j                  j                  ddd      }dt        j                  j                  d      z  }t        j                  j                  |      }| j                  ||d|d|       |}t        j                  j                  |      }| j                  ||d|d|       t         j                  j                  dd	      }d
t        j                  j                  d      z  }t        j                  j                  |      }| j                  ||d|d|       d}t         j                  j                  dd      }t        j                  j                  |      }| j                  ||d|d|       y )Nneedszquot=ingherezneeds/%s/herer  r  zurl2pathname() failed; z	make sureusing_quotez%s/using_quotezmake+sure/using_unquotez	make+sureusing_unquote)
r   rU  r  r   r   r   r   r  r   r  r  s       r   rl  zPathname_Tests.test_quoting  sD    Wj&9 6<<#5#5j#AA,,U3 &*	+ ,,V4 &*	+ [-8!FLL$6$6{$CC,,U3 &*	+ *k?;,,U3 &*	+r   win32z#test specific to Windows pathnames.c                    t         j                  j                  }| j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d	      d
       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j	                  t
        |d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d       d!       | j                   |d"      d
       | j                   |d      d       | j                   |d#      d       | j                   |d$      d       g d%}|D ]7  }| j                   |t         j                  j                  |            |       9 y )&Nz
\\?\C:\dirz	///C:/dirz\\?\unc\server\share\dirz//server/share/dirC:///C:C:\///C:/zC:\a\b.cz///C:/a/b.cz	C:\a\b.c\z///C:/a/b.c/z	C:\a\\b.cz///C:/a//b.cz	C:\a\b%#cz///C:/a/b%25%23cu   C:\a\béz///C:/a/b%C3%A9C:\foo\bar\spam.foo///C:/foo/bar/spam.foozXX:\z\folder\test\/folder/test/z\\folder\test\z//folder/test/z\\\folder\test\z///folder/test/z\\some\share\z//some/share/z\\some\share\a\b.cz//some/share/a/b.cu   \\some\share\a\b%#céz//some/share/a/b%25%23c%C3%A9zC:/a/b.cz
//?/C:/dirz//?/unc/server/share/dir)r  r  r  )r   r   r  r   r   IOErrorr  )r+   fnurlsr   s       r   test_pathname2url_winz$Pathname_Tests.test_pathname2url_win  s    ^^((,-{;<=?STD7+FX.L)=9N+^<N+^<M*,>?N+->?457OP'2w/./A013CD235FG/0/B568LM:;=\]J7013GHL);7679MN* CR ; ;C @A3G r   z test specific to POSIX pathnamesc                 X   t         j                  j                  }| j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d	       y )
Nr  z/a/b.cz//a/b.cz	////a/b.cz///a/b.cz
/////a/b.cz//////a/b.cz/a/b%#cz/a/b%25%23c)r   r   r  r   r+   r  s     r   test_pathname2url_posixz&Pathname_Tests.test_pathname2url_posix   s     ^^((C#&Hx0I4J6K-8I6r   zneed os_helper.FS_NONASCIIc                 .   t        j                         }t        j                         }t        j                  j                  t        j                  ||      }| j                  t        j                  j                  t        j                        |       y Nr!  )sysgetfilesystemencodinggetfilesystemencodeerrorsr   r   r   r   FS_NONASCIIr   r   r  r+   r  r"  r   s       r   test_pathname2url_nonasciiz)Pathname_Tests.test_pathname2url_nonascii+  se    ,,...0ll  !6!6RX Y44Y5J5JKSQr   c                 f   t         j                  j                  }| j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d	      d
       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j	                  t
        |d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d       d       | j                   |d!      d"       | j                   |d#      d       | j                   |d$      d%       | j                   |d&      d'       g d(}|D ]7  }| j                   |t         j                  j                  |            |       9 y ))Nz/C:/r  z///C|r  r  r  z/C|//zC:\\z
///C|/pathzC:\pathz
///C/test/z\C\test\z////C/test/z	\\C\test\zC:/path/to/filezC:\path\to\filezC:/path/to/file/zC:\path\to\file\zC:/path/to//filezC:\path\to\\filezC|/path/to/filez/C|/path/to/filez///C|/path/to/filez///C|/foo/bar/spam.foor  u   ///è|/z//server/path/to/filez\\server\path\to\filez////server/path/to/filez/////server/path/to/filez//localhost/C:/path/to/filez//localhost/C|/path/to/filez//localhost/path/to/filez\path\to\filez //localhost//server/path/to/filezC:/foo%2fbarz
C:\foo/barz//server/share/foo%2fbarz\\server\share\foo/bar)r  z	\C\test\\r  )r   r   r  r   r   r  r  )r+   r  pathsrU  s       r   test_url2pathname_winz$Pathname_Tests.test_url2pathname_win2  sm    ^^((FV,Gd+Gd+Hv.Gh/L):6L)=9M*O<-.0DE./1GH./1GH-.0DE./1EF013GH457OP'2}5346RS568TU679UV9:<PQ9:<PQ679KL>?A]^N+];679UV) DR ; ;D ABDI r   c                 (   t         j                  j                  }| j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d       y )Nz/foo/barz	//foo/barz
///foo/barz////foo/barz//localhost/foo/bar)r   r   r  r   r  s     r   test_url2pathname_posixz&Pathname_Tests.test_url2pathname_posix\  s{     ^^((J4K+6L):6M*K812J?r   c                    t        j                         }t        j                         }t        j                  }| j                  t        j                  j                  |      t        j                         t        j                  j                  |||      }| j                  t        j                  j                  |      t        j                         y r  )r  r  r  r   r  r   r   r   r  r   r   r  s       r   test_url2pathname_nonasciiz)Pathname_Tests.test_url2pathname_nonasciif  s    ,,...0##44S99;P;PQll  x G44S99;P;PQr   N)r@   rA   rB   r   r  rl  r  r  r  platformr  skipIfr  r   r  r  r  r  r  r$   r   r   r  r    s-   02+6 X0>@ H@ HD X__S\\W,79797 X..0LMR NR X0>@&J@&JP X__S\\W,79@9@ X..0LMR NRr   r  c                       e Zd ZdZd Zy)Utility_Testsz=Testcase to test the various utility functions in the urllib.c                 h    | j                  t        j                  j                         t               y)zATest the urllib.request.thishost utility function returns a tupleN)r   r   r   thishosttupler?   s    r   test_thishostzUtility_Tests.test_thishostr  s    fnn557?r   N)r@   rA   rB   r   r  r$   r   r   r  r  o  s    G@r   r  c                       e Zd ZdZd Z ej                  e      d        Z ej                  e      d        Z	 ej                  e      d        Z
y)URLopener_Testsz4Testcase to test the open method of URLopener class.c                 B    G d dt         j                  j                        }t        j                  dt
        f      5  | j                   |       j                  d      d       | j                   |       j                  d      d       d d d        y # 1 sw Y   y xY w)Nc                       e Zd Zd Zy)8URLopener_Tests.test_quoted_open.<locals>.DummyURLopenerc                     |S r)   r$   r+   r   s     r   	open_spamzBURLopener_Tests.test_quoted_open.<locals>.DummyURLopener.open_spam|      
r   N)r@   rA   rB   r  r$   r   r   DummyURLopenerr  {      r   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  s     r   test_quoted_openz URLopener_Tests.test_quoted_openz  s    	V^^55 	 ++K"$% ^-22#%%57 ^-22>@9;% % %s   ABB)categoryc                    t        j                         5 }t        j                  |      \  }}t	        j
                  |       dt        j                  j                  |      z   }t        j                  j                         j                  |      \  }}| j                  t        j                  j                  |      t        j                  j                  |             d d d        y # 1 sw Y   y xY w)N)dirzfile:)r   temp_dirrS  rT  r   r>   r   r   r  r{  retriever   rU  normcase)r+   tmpdirrY  tmpfilefileurlrL  _s          r   test_urlopener_retrieve_filez,URLopener_Tests.test_urlopener_retrieve_file  s    !V"**v6KBHHRL ; ;G DDG ..224==gFKHaRWW--h79I9I'9RS "!!s   CC++C4c                 $   d}| j                  d       | j                  | j                         t        j                  j                         j                  |      \  }}| j                  t        j                  j                  |      d   d       y )Nzhttp://www.python.org/file.txtr
  r   z.txt)rR   r  rZ   r   r   r{  r  r   r   rU  splitext)r+   r   rL  r  s       r   test_urlopener_retrieve_remotez.URLopener_Tests.test_urlopener_retrieve_remote  si    .67(nn..099#>!))(3A6?r   c                 B    G d dt         j                  j                        }dD ]  }| j                  t        t         j                  j
                  |       | j                  t        t         j                  j                         j                  |       | j                  t        t         j                  j                         j                  |       | j                  t         |       j                  |       | j                  t         |       j                  |        y )Nc                       e Zd Zd Zy)<URLopener_Tests.test_local_file_open.<locals>.DummyURLopenerc                     |S r)   r$   r  s     r   open_local_filezLURLopener_Tests.test_local_file_open.<locals>.DummyURLopener.open_local_file  r  r   N)r@   rA   rB   r  r$   r   r   r  r    r  r   r  )zlocal_file://examplezlocal-file://example)r   r   r{  r   rH  r    r   r  )r+   r  r   s      r   test_local_file_openz$URLopener_Tests.test_local_file_open  s    	V^^55 	 DCgv~~'='=sCgv~~'?'?'A'F'FLgv~~'?'?'A'J'JCPg~'7'<'<cBg~'7'@'@#F Dr   N)r@   rA   rB   r   r  r   ignore_warningsr#   r  r  r  r$   r   r   r  r  w  sy    >; %_$$.@AT BT %_$$.@A@ B@ %_$$.@A
G B
Gr   r  c                       e Zd ZdZd Zd Zy)RequestTestsz&Unit tests for urllib.request.Request.c                     t         j                  j                  } |d      }| j                  |j	                         d        |di       }| j                  |j	                         d       y )Nr  r   POST)r   r   Requestr   
get_methodr+   r  r   s      r   test_default_valuesz RequestTests.test_default_values  s[    ..((12++-u5126++-v6r   c                    t         j                  j                  } |dd      }| j                  |j                  d       | j                  |j                         d        |di d      }| j                  |j                  d       | j                  |j                         d        |dd      }| j                  |j                         d       d|_        | j                  |j                         d       y )Nr  HEAD)methodr   )r   r   r  r   r  r  r  s      r   test_with_method_argz!RequestTests.test_with_method_arg  s    ..((1&A0++-v612fE0++-v61%@++-u5++-v6r   N)r@   rA   rB   r   r  r  r$   r   r   r  r    s    077r   r  __main__)NNr[   )6r   urllib.parser   urllib.requesturllib.errorhttp.clientrO   email.messager   r6   r  unittest.mockr   rh  r   test.supportr   r   r   r   r  ImportErrorr  rS  base64r   r   has_gethostnameSkipTestr   r   r    r   rR   ro   rT   r]   TestCaserv   r   r   r   r  r  r  r  r+  rR  r  r  r  r  r@   mainr$   r   r   <module>r     s   A      	    " & ( 	     $$
(

9
:: 
& /,^<F <;6 ;(ZR)) ZRzD9"" D9N!C 1 1 !CHK))=, K\MP)) MP`H-H-- H-V*"H--} *"ZJL8$$ JLZRIX&& RIjA)h'' A)F\RX&& \R|@H%% @/GmX%6%6 /Gd78$$ 70 zHMMO _5  
Cs   G G)(G)