
    daf                    <   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mZ dd	lmZ ddlZej<                  s e
j>                  d
      d Z da!d4dZ"d Z#d5dZ$ G d de%      Z& G d de%      Z' G d de
jP                        Z) G d de
jP                        Z* G d de
jP                        Z+ G d de
jP                  e&e'      Z, G d de
jP                        Z- G d de
jP                        Z. G d d e
jP                  e&      Z/ G d! d"e
jP                        Z0 G d# d$e
jP                        Z1 G d% d&e
jP                        Z2 G d' d(e
jP                        Z3 G d) d*e
jP                        Z4 G d+ d,e&e
jP                        Z5 G d- d.e
jP                        Z6 G d/ d0e
jP                        Z7 G d1 d2e
jP                        Z8e9d3k(  r e
jt                          yy# e$ r dZY w xY w)6z;Regression tests for what was in Python 2's "urllib" module    N)patch)support)	os_helper)socket_helper)warnings_helper)url2pathnamepathname2url)	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     +/root/Python-3.12.4/Lib/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   ;   sB    		'	'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.sendallF   s    %)"r   c                 0    | xj                   dz  c_         | S )Nr   )io_refs)r-   argskwdss      r   makefilez%fakehttp.<locals>.FakeSocket.makefileI   s    LLALKr   Nc                 \    | j                   ryt        j                  j                  | |      S Nr   )closedioBytesIOread)r-   amts     r   r:   z!fakehttp.<locals>.FakeSocket.readM   s!    {{::??4--r   c                 \    | j                   ryt        j                  j                  | |      S r6   )r7   r8   r9   readline)r-   lengths     r   r=   z%fakehttp.<locals>.FakeSocket.readlineR   s#    {{::&&tV44r   c                     | xj                   dz  c_         | j                   dk(  r t        j                  j                  |        y y )Nr   r   )r1   r8   r9   closer-   s    r   r@   z"fakehttp.<locals>.FakeSocket.closeW   s3    LLAL||q 

  & !r   r+   )	__name__
__module____qualname__r1   r/   r4   r:   r=   r@   )r.   s   r   
FakeSocketr)   C   s!    	*		.
	5
	'r   rE   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-   rE   s    r   connectz,fakehttp.<locals>.FakeHTTPConnection.connecta   s#    "4==1DI"&))DJr   c                      y r+   r&   rA   s    r   r@   z*fakehttp.<locals>.FakeHTTPConnection.closej       r   )rB   rC   rD   r,   rM   r@   )rE   
mock_closes   r   r.   rG   \   s     	, 
 r   r.   )r8   r9   httpclientHTTPConnectionrI   )rI   rP   r.   rE   s    `@@r   fakehttprT   B   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rP   )rT   rQ   rR   rS   _connection_class)r-   rI   rP   fake_http_classs       r   rT   zFakeHTTPMixin.fakehttpr   s,    "8
C!%!;!;%4"r   c                 B    | j                   t        j                  _        y r+   )rY   rQ   rR   rS   rA   s    r   
unfakehttpzFakeHTTPMixin.unfakehttpw   s    %)%;%;"r   NF)rB   rC   rD   rT   r\   r&   r   r   rV   rV   q   s    5
<r   rV   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   )r8   r9   )r-   filerK   s      r   retrfilez5FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.retrfile   s    zz|Q&r   c                      y r+   r&   rA   s    r   r@   z2FakeFTPMixin.fakeftp.<locals>.FakeFtpWrapper.close   rO   r   )NT)rB   rC   rD   rk   rn   r@   r&   r   r   FakeFtpWrapperrb   }   s    HL $'r   rp   )objectr   r   
ftpwrapper_ftpwrapper_class)r-   rp   s     r   fakeftpzFakeFTPMixin.fakeftp|   s.    		V 		 "(!:!:$2!r   c                 B    | j                   t        j                  _        y r+   )rs   r   r   rr   rA   s    r   	unfakeftpzFakeFTPMixin.unfakeftp   s    $($:$:!r   N)rB   rC   rD   rt   rv   r&   r   r   r_   r_   {   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__rB   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   rA   s    r   tearDownzurlopen_FileTests.tearDown   s&    !
		)""#r   c                 d    dD ]+  }| j                  t        | j                  |      d|z         - y )N)	r:   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   s:    G 	DOOGD$5$5t<J	r   c                 l    | j                  | j                  | j                  j                                y r+   )assertEqualr   r   r:   rA   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=   rA   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   r:   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@   rA   s    r   
test_closezurlopen_FileTests.test_close   s     	!r   c                     | j                  | j                  j                  t        j                  j
                         y r+   )r   r   headersemailmessageMessagerA   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   rA   s    r   test_urlzurlopen_FileTests.test_url   s$    **..0D0DEr   c                 N    | j                  | j                  j                         y r+   )assertIsNoner   statusrA   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   rA   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   rA   s    r   test_geturlzurlopen_FileTests.test_geturl   s'    **113T5I5IJr   c                 V    | j                  | j                  j                                y r+   )r   r   r   rA   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   rA   s    r   test_relativelocalfilez(urlopen_FileTests.test_relativelocalfile   s'    *V^^%;%;D4==<PQr   N)rB   rC   rD   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r&   r   r   rx   rx      s]    F$
>4?="
PF4OK7.Rr   rx   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   sS    $$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:3128rQ   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)	rB   rC   rD   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OrderedDictrA   s    r   r   zProxyTests_withOrderedEnv.setUp5  s    ** ,,.
r   c                 .    | j                   t        _        y r+   )r   r   r   rA   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_ProxyrQ   )	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)rB   rC   rD   r   r   r   r&   r   r   r   r   3  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      rT   r"   r   r=   r   r   r\   )r-   verfps      r   
check_readzurlopen_HttpTests.check_readZ  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!)rT   r   r   r"   r   r   r\   )r-   r   r	  s      r   test_url_fragmentz#urlopen_HttpTests.test_url_fragmente  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)rT   r"   r   r	  
will_closer\   )r-   resps     r   test_willclosez urlopen_HttpTests.test_willcloseo  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chrrT   reprreplacerQ   rR   
InvalidURLassertRaisesRegexr   r   r"   r   r   r\   )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_rejectedw  sY   E!TN+tf4 	"Gw<D4TF!<NMM:;" %)J$6$6tU$C!![[33
++"34E3F GI ENN**U>2B+CDE ++"34E3F GI FNN**VN3C+DEF ~&678  t{{}5!/	"E EF F !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   
)rT   rQ   rR   r!  r"  r   r   r"   r   r   r\   )r-   rf   r$  r!  r  s        r   4test_url_path_with_newline_header_injection_rejectedzFurlopen_HttpTests.test_url_path_with_newline_header_injection_rejected  s4   67O';;	 //J''IK A&&~.>'?@A ''
4KL B&&/?'@AB U>"234DS$++-0T4;;=1T4;;=1OOA AB B 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  rT   r  r   rQ   rR   r!  r"  r"   r\   )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4 	"Gw<D*4&7NMM:;	"$(J$6$6tU$C!![[33
++"34E3F GI 6eN#3456 ++J:KL]K^8_` 7f^$4567 !	"6 67 7 !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  )rT   rQ   rR   r!  r"  r"   r\   )r-   rf   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 2%/012 ''
4KL 3& 0123 OO2 23 3 OOs;   ,B2 	BB2 2B&B2 B#B2 &B/+B2 2Cc                 &    | j                  d       y )Ns   0.9r
  rA   s    r   test_read_0_9zurlopen_HttpTests.test_read_0_9  s     	r   c                 &    | j                  d       y )Ns   1.0r4  rA   s    r   test_read_1_0zurlopen_HttpTests.test_read_1_0      r   c                 &    | j                  d       y )Ns   1.1r4  rA   s    r   test_read_1_1zurlopen_HttpTests.test_read_1_1  r8  r   c                     | j                  dd       	 | j                  t        t        d       | j	                          y # | 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
TrX   r  rT   r   OSErrorr"   r\   rA   s    r   test_read_bogusz!urlopen_HttpTests.test_read_bogus  sJ     
  	 		gw0DEOODOOs   A Ac                    | j                  dd       	 d}| j                  t        j                  j                  |      5  t        d       d d d        | j                          y # 1 sw Y   x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
TrX   zRedirection to url 'file:r  )rT   r"  r   error	HTTPErrorr"   r\   )r-   msgs     r   test_invalid_redirectz'urlopen_HttpTests.test_invalid_redirect  sx       	 		-C''(>(>D .,-. OO. . OOs#   ,A2 A&A2 &A/+A2 2B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
TrX   http://something)
r  r   maxtriesrT   r   r   r@  rA  r"   r\   )r-   is     r   test_redirect_limit_independentz1urlopen_HttpTests.test_redirect_limit_independent  sz     ~'001 		"AMM    "!!&,,"8"8'&( !		" !s   /A77B	c                     | j                  d       	 | j                  t        t        d       | j	                          y # | j	                          w xY w)Nr   rE  r<  rA   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   	exceptionfilenamereasonr-   es     r   test_missing_localfilez(urlopen_HttpTests.test_missing_localfile  sj    v||445 	GEF	G,,-**+	G 	G   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@  rL  )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 	! 	!& & HHRLIIh	! 	!s0   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@  rL  r"   r   rM  rN  r   rO  )r-   test_ftp_urlrQ  s      r   test_ftp_nohostz!urlopen_HttpTests.test_ftp_nohost   sl    $v||445 	"L!	"--.**+	" 	"s   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.pyra  rP  s     r   test_ftp_nonexistingz&urlopen_HttpTests.test_ftp_nonexisting'  sl    v||445 	FDE	F--.**+	F 	FrS  MAXFTPCACHEr   c                    | j                          	 t        j                  j                  ddddg       t        j                  j                  d<   t        d       | j                          y # | j                          w xY w)Nrd   passr      testzftp://localhost)rt   r   r   rr   ftpcacher"   rv   rA   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_inurl6  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)
Nrn  za b:c dzhttp://{}@python.org/zAuthorization: Basic %s
ASCIIzUTF-8r  r   r  )rT   formatrQ   rR   rS   r
   encodedecoder"   assertInr,   r   r=   assertNotEqualr   r   r\   )r-   userpassr   fakehttp_wrapperauthorizationr	  s         r   test_userpass_inurl_w_spacesz.urlopen_HttpTests.test_userpass_inurl_w_spacesA  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   	URLopenerrA   s    r   test_URLopener_deprecationz,urlopen_HttpTests.test_URLopener_deprecationT  s;    ++R0B,CD 	'NN$$&	' 	' 	's   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_contextX  s    ,,.++R1C,DE 	"":. &&'0CW ' 	 	 	 	s#   B#A:)B:B	?BBN)"rB   rC   rD   r   r
  r  r  unittest
skipUnlessr  r&  r.  r0  r2  r5  r7  r:  r>  rC  rH  rJ  rR  r_  rc  re  r   rq   r   r   rl  ro  rz  r~  r  r&   r   r   r  r  W  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| _        t        j                  j                  | j                        | _
        t        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"   text_url_resptext_url_base64_respimage_url_resprA   s    r   r   zurlopen_DataTests.setUpe  s    112 ;	F 	
( 	 	. 	
 $^^33DMMB$*NN$:$:  %"!$nn44T^^Dr   c                 d    dD ]+  }| j                  t        | j                  |      d|z         - y )N)r:   r=   r   r@   r   r   r   r   r   )r   r   r  r   s     r   r   z urlopen_DataTests.test_interface  s:    G 	DOOGD$6$6=J	r   c                 (   | j                  | j                  j                         t        j                  j
                         | j                  | j                  j                         j                         ddg       | j                  | j                  j                         d   t        t        | j                                     | j                  t        j                  j                  d      j                         j                         ddg       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"   rA   s    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 	"//9>>@KKM!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  rA   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  r:   rt  dictr   r  r   rA   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  r:   rt  r  r   r  r   rA   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  r:   r  rA   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"   rA   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  rA   s    r   test_invalid_base64_dataz*urlopen_DataTests.test_invalid_base64_data  s    *V^^%;%;<NOr   N)rB   rC   rD   r   r   r   r   r   r  r  r  r  r  r&   r   r   r  r  b  s8    ,E>;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.urlretriever{   )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'    NN 	D4	Ds   (,c                     t         j                  j                  |      }	 |j                  d       dt        j                  j                  |      z  S # t        $ r t        j                  d      w xY w)Nzutf-8z!filePath is not encodable to utf8z	file://%s)
r   rW  abspathrs  UnicodeEncodeErrorr  SkipTestr   r   r	   )r-   filePaths     r   constructLocalFileUrlz+urlretrieve_FileTests.constructLocalFileUrl  sg    77??8,	IOOG$ V^^88BBB " 	I##$GHH	Is   A A3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.r{   )rU  rV  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 )Nr|   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   rW  rY  r   r:   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>.hooktester2  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   )rB   rC   rD   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>._reporthookN  s    r   )
reporthookr  r   r   r  rT   r   r@  ContentTooShortErrorr  r   TEST_HTTP_URLr\   )r-   r  s     r   .test_short_content_raises_ContentTooShortErrorzDurlretrieve_HttpTests.test_short_content_raises_ContentTooShortErrorA  s    112  		 v||@@A 	""**7+@+@6A + C !	" 	"
 !	" 	"s$   '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  rA   s    r   Atest_short_content_raises_ContentTooShortError_without_reporthookzWurlretrieve_HttpTests.test_short_content_raises_ContentTooShortError_without_reporthookX  s    112  	 v||@@A 	""**7+@+@A!		" 	" !		" 	"s$   $C&-B-C-B??CCN)rB   rC   rD   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__rA   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  sI    -2"I6SC6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  rA   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   r  r   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)rB   rC   rD   r   r  r  r  r  r  r  r!  r(  r*  r&   r   r   r  r  k  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   r1  rA   s    r   #test_unquote_rejects_none_and_tuplez2UnquotingTests.test_unquote_rejects_none_and_tupleT  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   rz   using unquote_to_bytes(): r&   )	r   r   r1  r   r}   unquote_to_bytesr   r  r8  r   s       r   test_unquoting_badpercentz(UnquotingTests.test_unquoting_badpercentX  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  r0  )r   r   r   r1  r   r2  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+  r0  )r   r   r1  r   r   r2  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 )NrH  u   brückner_sapporo_20050930.docr/  r  r#  zbr%FCckner_sapporo_20050930.docr  r  r&  r%  z%F3%B1u   �r   )r$  r   ignorerI  u   漢üu   漢%FCr   r   r1  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øjrM  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)rB   rC   rD   r   r6  r9  r=  r@  rD  rF  rJ  rN  rQ  r&   r   r   r,  r,  8  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   	urlencoderu  r   r3  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''.( 	8HMM(F#Xv78	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_  rA   s    r   test_using_mappingz"urlencode_Tests.test_using_mapping0  s    3c=6	8r   c                 ,    | j                  g dd       y )N))rd  ra  )re  rb  )rf  rc  z*using sequence of two-item tuples as inputrg  rA   s    r   test_using_sequencez#urlencode_Tests.test_using_sequence5  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 )NrU  =zkey namezA bunch of pluseszkey+name=A+bunch+of+pluses)r   r   r   rW  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)ra  rb  rc  zsequence=%sTrU  r   zExpected 2 '&'s, got %s)r   r   r  r  rW  r   ru  r3  )r-   r  r
  r  values        r   
test_doseqzurlencode_Tests.test_doseqE  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 r|  r   r   r   rW  rA   s    r   test_empty_sequencez#urlencode_Tests.test_empty_sequenceR  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=Noners  rA   s    r   test_nonstring_valuesz%urlencode_Tests.test_nonstring_valuesV  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=2rv  r   r   Tz
a=None&a=a)rv  r   )br   za=a&a=b)r   r   r   rW  r   r   )r-   r    s     r   test_nonstring_seq_valuesz)urlencode_Tests.test_nonstring_seq_valuesZ  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=%3Frq  r   r#  %C2%A0=%C3%81%A0=%C1r  r  r   r   rW  r   r   s       r   test_urlencode_encodingz'urlencode_Tests.test_urlencode_encodingb  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  Trq  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_dosequ  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)rB   rC   rD   r   r_  rh  rj  rm  rq  rt  rw  rz  r  r  r  r  r&   r   r   rS  rS    sH    @@8
J
	)H9HD)&#)J)&)r   rS  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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ofrv  rW  zparts/of/a/pathpathname2url() failed; r  zurl2pathame() failed; )r   rW  r  r   r   r	   r   r   )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   rW  r  r   r   r   r   r	   r   r   r   s       r   rm  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 the nturl2path functions.c                     d}d}t         j                  j                  |      }| j                  ||d|d|       d}d}t         j                  j                  |      }| j                  ||d|d|       y )Nz
\\?\C:\dirz	///C:/dirr  r  z\\?\unc\server\share\dirz/server/share/dir)r   r   r	   r   r   s       r   test_prefixeszPathname_Tests.test_prefixes  sz     !,,U3 &*	+ 1$,,U3 &*	+r   z.test specific to the urllib.url2path function.c           	         d}d}|D ]:  }t         j                  j                  |      }| j                  ||d|d|       < d}d}t         j                  j                  |      }| j                  ||d|d|       y )N)z/C:/z///C:/z/C|//C:\z'urllib.request..url2pathname() failed; r  z
///C|/pathzC:\pathz&urllib.request.url2pathname() failed; )r   r   r   r   )r-   r  r
  r   r  s        r   test_ntpathzPathname_Tests.test_ntpath  s     , 	/C^^005FVV$f./	/
 ,,U3 &*	+r   N)rB   rC   rD   r   r  rm  r  r  sysplatformr  r  r&   r   r   r  r    sq    02+6 X0EG+G+  X0IK+K+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tuplerA   s    r   test_thishostzUtility_Tests.test_thishost!  s    fnn557?r   N)rB   rC   rD   r   r  r&   r   r   r  r    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)rB   rC   rD   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_open)  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_dirrU  rV  r   r@   r   r   r	   r}  retriever   rW  normcase)r-   tmpdirr[  tmpfilefileurlrN  _s          r   test_urlopener_retrieve_filez,URLopener_Tests.test_urlopener_retrieve_file8  s    ! 	TV"**v6KBHHRL ; ;G DDG ..224==gFKHaRWW--h79I9I'9RS	T 	T 	T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)rT   r  r\   r   r   r}  r  r   r   rW  splitext)r-   r   rN  r  s       r   test_urlopener_retrieve_remotez.URLopener_Tests.test_urlopener_retrieve_remoteB  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_fileN  r  r   N)rB   rC   rD   r  r&   r   r   r  r  M  r  r   r  )zlocal_file://examplezlocal-file://example)r   r   r}  r   r=  r"   r   r  )r-   r  r   s      r   test_local_file_openz$URLopener_Tests.test_local_file_openJ  s    	V^^55 	 D 	GCgv~~'='=sCgv~~'?'?'A'F'FLgv~~'?'?'A'J'JCPg~'7'<'<cBg~'7'@'@#F	Gr   N)rB   rC   rD   r   r  r   ignore_warningsr%   r  r  r  r&   r   r   r  r  &  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_argb  s    ..((1&A0++-v612fE0++-v61%@++-u5++-v6r   N)rB   rC   rD   r   r  r  r&   r   r   r  r  X  s    077r   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)URL2PathNameTestsc                     | j                  t        d      d       | j                  t        d      d       | j                  t        d      d       y )Nz///C|C:///C:z///C|/r  r   r   rA   s    r   test_converting_drive_letterz.URL2PathNameTests.test_converting_drive_letterr  sA    g.5g.5h/8r   c                 p    | j                  t        d      d       | j                  t        d      d       y )Nz
///C/test/z
\\\C\test\z////C/test/z	\\C\test\r  rA   s    r   $test_converting_when_no_drive_letterz6URL2PathNameTests.test_converting_when_no_drive_letterw  s.    l35FGm46FGr   c                 :    | j                  t        d      d       y )Nz///C|/foo/bar/spam.fooC:\foo\bar\spam.foor  rA   s    r   test_simple_comparez%URL2PathNameTests.test_simple_compare|  s    &>?/	1r   c                 :    | j                  t        t        d       y )Nu   ///è|/)r   IOErrorr   rA   s    r   test_non_ascii_drive_letterz-URL2PathNameTests.test_non_ascii_drive_letter  s    '<?r   c                 b    g d}|D ]&  }| j                  t        t        |            |       ( y )N)r  z\\\C\test\\r  )r   r   r	   r-   list_of_pathsrW  s      r   test_roundtrip_url2pathnamez-URL2PathNameTests.test_roundtrip_url2pathname  s6     " 	ED\,t*<=tD	Er   N)rB   rC   rD   r  r  r  r  r  r&   r   r   r  r  p  s    9
H
1@Er   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)PathName2URLTestsc                 p    | j                  t        d      d       | j                  t        d      d       y )Nr  r  r  r   r	   rA   s    r   r  z.PathName2URLTests.test_converting_drive_letter  s,    d+W5f-w7r   c                     | j                  t        d      d       | j                  t        d      d       | j                  t        d      d       y )Nz\\\folder\test\/////folder/test/z\\folder\test\z////folder/test/z\folder\test\z/folder/test/r  rA   s    r   r  z6PathName2URLTests.test_converting_when_no_drive_letter  sM    &<=,	.&;<+	-&:;(	*r   c                 :    | j                  t        d      d       y )Nr  ///C:/foo/bar/spam.foor  rA   s    r   r  z%PathName2URLTests.test_simple_compare  s    &<=1	4r   c                 :    | j                  t        t        d       y )NzXX:\)r   r  r	   rA   s    r   test_long_drive_letterz(PathName2URLTests.test_long_drive_letter  s    '<9r   c                 b    g d}|D ]&  }| j                  t        t        |            |       ( y )N)r  r  r  )r   r	   r   r  s      r   test_roundtrip_pathname2urlz-PathName2URLTests.test_roundtrip_pathname2url  s6    3 " 	ED\,t*<=tD	Er   N)rB   rC   rD   r  r  r  r  r  r&   r   r   r  r    s    8*4:Er   r  __main__)NNr]   );r   urllib.parser   urllib.requesturllib.errorhttp.clientrQ   email.messager   r8   r  unittest.mockr   rj  r   test.supportr   r   r   r   r  ImportErrorr  rU  
nturl2pathr   r	   base64r
   r   has_gethostnamer  r   r   r"   r   rT   rq   rV   r_   TestCaserx   r   r   r  r  r  r  r  r,  rS  r  r  r  r  r  r  rB   mainr&   r   r   <module>r     s$   A      	    " & ( 	   1   $$
(

9
:: 
& /,^<F <;6 ;(ZR)) ZRzD9"" D9N!C 1 1 !CHH))=, HVJP)) JPZL-H-- L-^*"H--} *"ZJL8$$ JLZRIX&& RIjA)h'' A)FL+X&& L+\@H%% @/GmX%6%6 /Gd78$$ 70E)) E6E)) E8 zHMMO k4  
Cs   H HH