
    6\h;                       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Zd dlZd dlZd dlZd dlmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlZ ej        d	
            G d de j                  Z  G d de j                  Z! G d d          Z" G d d          Z# G d de$          Z% G d de	j&                  Z' G d d          Z( G d d          Z) G d de	j*                  Z+ G d d          Z, G d d           Z-d! Z.d" Z/ G d# d$ej0        j1                  Z2 G d% d&ej0        j3                  Z4 G d' d(ej0        j1                  Z5 G d) d*          Z6 G d+ d,e j                  Z7d- Z8 G d. d/e j                  Z9 G d0 d1e j                  Z: G d2 d3e j                  Z;e<d4k    r e j=                     dS dS )5    N)support)	os_helper)socket_helper)warnings_helper)test_urllib)RequestOpenerDirectorHTTPBasicAuthHandlerHTTPPasswordMgrWithPriorAuth_parse_proxy_proxy_bypass_winreg_override_proxy_bypass_macosx_sysconfAbstractDigestAuthHandler)urlparseT)modulec                   &    e Zd Zd Zd Zd Zd ZdS )TrivialTestsc                    dD ]}i }t          d|z  |           |d= |dk    rpt          j        dk    r`|                    d          |                    d          }}|                     |j        d           |                     |j        d           |                                D ]1\  }}|                     |j        d	|z  |d
|d|j                   2d S )N)requestresponseparseerrorrobotparserzfrom urllib.%s import *__builtins__r   nturl2pathnamepathname2url
nturl2pathz	urllib.%sz is exposed in 'urllib.z' but defined in )execosnamepopassertEqual
__module__items)selfr   contextupkvs          ;/opt/python-3.11.14/usr/lib/python3.11/test/test_urllib2.pytest___all__zTrivialTests.test___all__&   s   L 	/ 	/FG*V3W==='""rw${{>22GKK4O4O1  |<<<  |<<< / /1  {V/CQQ./ / / //	/ 	/    c                 
   |                      t          j        j                   |                     t
          t          j        j        d           t          j        	                    t          j        j
                                      t          j        d          }t          j        dk    rd|z  }nd|z  }t          j                            |          5 }|                                 d d d            d S # 1 swxY w Y   d S )Nz	bogus url/r   z
file:///%s	file://%s)
addCleanupurllibr   
urlcleanupassertRaises
ValueErrorurlopenr    pathabspath__file__replacesepr!   read)r&   fnamefile_urlfs       r,   test_trivialzTrivialTests.test_trivial5   s	    	1222*fn&<kJJJ  788@@MM7d??#e+HH"U*H^##H-- 	FFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   C88C<?C<c                     dg dfdddgfdg dfdg d	fg}|D ]8\  }}|                      t          j                            |          |           9d S )
Nza,b,c)abczpath"o,l"og"i"cal, examplezpath"o,l"og"i"calexampleza, b, "c", "d", "e,f", g, h)rC   rD   z"c"z"d"z"e,f"ghza="b\"c", d="e\,f", g="h\\i")za="b"c"zd="e,f"zg="h\i")r#   r3   r   parse_http_list)r&   testsstringlists       r,   test_parse_http_listz!TrivialTests.test_parse_http_listH   s    ooo&),?+KL*888:////12 " 	K 	KLFDV^;;FCCTJJJJ	K 	Kr.   c                     t           j                            d          }|                     |j        t          |                     d S )Nreason)r3   r   URLErrorassertInrO   str)r&   errs     r,   test_URLError_reasonstrz$TrivialTests.test_URLError_reasonstrS   s9    l##H--cj#c((+++++r.   N)__name__r$   __qualname__r-   rA   rM   rT    r.   r,   r   r   $   sS        / / /  &	K 	K 	K, , , , ,r.   r   c                   &    e Zd Zd Zd Zd Zd ZdS )RequestHdrsTestsc                     d}|                      t          |ddi          j        d         d           |                      t          |ddi          j        d         d           dS )a(  
        The Request.headers dictionary is not a documented interface.  It
        should stay that way, because the complete set of headers are only
        accessible through the .get_header(), .has_header(), .header_items()
        interface.  However, .headers pre-dates those methods, and so real code
        will be using the dictionary.

        The introduction in 2.4 of those methods was a mistake for the same
        reason: code that previously saw all (urllib2 user)-provided headers in
        .headers now sees only a subset.

        http://example.com	Spam-eggsblahheadersz	spam-EggSN)r#   r   r_   )r&   urls     r,   test_request_headers_dictz*RequestHdrsTests.test_request_headers_dictZ   s     #*5v)># # ##*;89?	A 	A 	A 	*5v)># # ##*;89?	A 	A 	A 	A 	Ar.   c                    d}t          |ddi          }|                     |                    d                     |                     |                                dg           |                    dd           |                     t          |                                          ddg           |                     |                    d	                     |                     |	                    d	                     |                     |	                    d	d
          d
           |
                    d           |                     |                    d                     |                    dd           |                     |                    d                     |
                    d           |                     |                    d                     dS )a;  
        Note the case normalization of header names here, to
        .capitalize()-case.  This should be preserved for
        backwards-compatibility.  (In the HTTP case, normalization to
        .title()-case is done by urllib2 before sending headers to
        http.client).

        Note that e.g. r.has_header("spam-EggS") is currently False, and
        r.get_header("spam-EggS") returns None, but that could be changed in
        future.

        Method r.remove_header should remove items both from r.headers and
        r.unredirected_hdrs dictionaries
        r[   r\   r]   r^   )r\   r]   zFoo-Barbaz)zFoo-barrc   z	Not-theredefaultzUnredirected-spamEggsN)r   
assertTrue
has_headerr#   header_items
add_headersortedassertFalseassertIsNone
get_headerremove_headeradd_unredirected_header)r&   r`   reqs      r,   test_request_headers_methodsz-RequestHdrsTests.test_request_headers_methodso   s    #cK#8999{33444))++.C-DEEEy%((( 0 0 2 233,.CD	F 	F 	F44555#..55666Y??KKK+&&&44555##$7@@@':;;<<<-...(;<<=====r.   c                 n   t           j                                        }|j        }|j        } |dddd            |dddd            |dddd	            |dd
dd            |dddd           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d            |d	ddd            |d	ddd            |d	ddd           |                      |d	d          d           |                      |d	d          d           |                      |d	d          d           |                      |d	d          d           |                      |d	d           d           |                      |d	d!          d"            |d	d#d$d%           |                      |d	d&          d"           |                      |d	d#          d'           |                      |d	d(          d'            |d)dd*d+            |d)dd,d-           |                      |d)d          d.            |d/dd0d/           |                      |d/d          d1           |                      |d/d2          d"           |                      |dd3          d"           |                      |dd4          d5           |                      |dd          d5           |                      |dd
          d6           |                      |dd          d7           d S )8Nz
Some Realmhttp://example.com/joepasswordzhttp://example.com/ninizhttp://c.example.com:31283rE   zd.example.com4dze.example.com:31285eexample.com)rt   ru   r[   zhttp://example.com/spamzhttp://example.com/spam/spamzhttp://example.com/foofoozhttp://example.com/barbarninizhttp://example.com/foo/barfoobarnibar)r}   rv   )r~   r   zhttp://example.com/foo/zhttp://example.com/foo/bazzhttp://example.com/foobarNNzhttp://example.com/baz/rc   nininizhttp://example.com/baz)rc   r   zhttp://example.com/baz/barrD   firstr]   secondspam)r   r   rC   1)r   rC   zhttp://a.example.com/zc.example.comzc.example.com:3128)rw   rE   )rx   ry   )rz   r{   r3   r   HTTPPasswordMgradd_passwordfind_user_passwordr#   r&   mgraddfind_user_passs       r,   test_password_managerz&RequestHdrsTests.test_password_manager   s$   n,,../L/
CCCL14>>>L5sC@@@L/3444L.S999 	mDD,	. 	. 	.6MNN,	. 	. 	.6JKK,	. 	. 	.6KLL,	. 	. 	.(AC C,	. 	. 	. 	(FH H,	. 	. 	. 	C)5$777C)5&999C-xAAA-EFF&	( 	( 	(-EFF(	* 	* 	*-FGG&	( 	( 	(-IJJ&	( 	( 	(-IJJ&	( 	( 	(-HII%	' 	' 	' 	C*E8<<<-EFF%	' 	' 	'-FGG*	, 	, 	,-IJJ*	, 	, 	,
 	C&888C&&999-BCC+	- 	- 	-
 	C%sC000-BCC#	% 	% 	% 	-DEE%	' 	' 	'
 	oFF%	' 	' 	'6JKK#	% 	% 	%N<)DEE	 	 	 	oFF#	% 	% 	%6JKK#	% 	% 	% 	% 	%r.   c                    t           j                                        }|j        }|j        } |dddd            |dddd            |d	d
dd            |dddd           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |dd          d           |                      |d	d          d           |                      |d	d
          d           |                      |d	d          d           |                      |dd          d           |                      |dd           d           |                      |dd!          d           |                      |dd"          d           d#S )$z
        The point to note here is that we can't guess the default port if
        there's no scheme.  This applies to both add_password and
        find_user_password.
        r@   zhttp://g.example.com:8010jrG   zhttp://h.example.com11r*   rH   zi.example.com:8012lizj.example.com13mzg.example.com:100r   zg.example.com:80)r   r   zg.example.comzhttp://g.example.com:100zhttp://g.example.comzh.example.com)r   r*   zh.example.com:80zhttp://h.example.com:80zi.example.com)r   r   zhttp://i.example.com:80)r   r   zj.example.com:80zhttp://j.example.comzhttp://j.example.com:80Nr   r   s       r,   "test_password_manager_default_portz3RequestHdrsTests.test_password_manager_default_port   s    n,,../C*D#666C's333C#T3///C$,,,-@AA%	' 	' 	'-?@@$	& 	& 	&_==%	' 	' 	'-GHH%	' 	' 	'-FGG$	& 	& 	&-CDD$	& 	& 	&_=={KKK-?@@+NNN-FGG$	& 	& 	&_==|LLL-?@@+NNN-FGG$	& 	& 	&_=={KKK-?@@%	' 	' 	'-CDD$	& 	& 	&-FGG%	' 	' 	' 	' 	'r.   N)rU   r$   rV   ra   rq   r   r   rW   r.   r,   rY   rY   X   sX        A A A*"> "> ">HT% T% T%l'' '' '' '' ''r.   rY   c                   .    e Zd Zg Zdej        fdZd ZdS )
MockOpenerNc                 2    |||c| _         | _        | _        d S N)rp   datatimeout)r&   rp   r   r   s       r,   openzMockOpener.open  s    ,/w)$)T\\\r.   c                 $    ||c| _         | _        d S r   )protoargs)r&   r   r   s      r,   r   zMockOpener.error  s     %t
DIIIr.   )rU   r$   rV   
addheaderssocket_GLOBAL_DEFAULT_TIMEOUTr   r   rW   r.   r,   r   r     sD        J!6+I ? ? ? ?, , , , ,r.   r   c                   $    e Zd ZddZddZd ZdS )MockFileNc                     d S r   rW   r&   counts     r,   r=   zMockFile.read      r.   c                     d S r   rW   r   s     r,   readlinezMockFile.readline!  r   r.   c                     d S r   rW   r&   s    r,   closezMockFile.close$  r   r.   r   )rU   r$   rV   r=   r   r   rW   r.   r,   r   r     sK                  r.   r   c                       e Zd Zd ZdS )MockHeadersc                 D    t          |                                           S r   )rL   values)r&   r!   s     r,   
getheaderszMockHeaders.getheaders)  s    DKKMM"""r.   N)rU   r$   rV   r   rW   r.   r,   r   r   (  s#        # # # # #r.   r   c                   "    e Zd ZddZd Zd ZdS )MockResponseNc                     t           j                            | |           ||||f\  | _        | _        | _        | _        d S r   )ioStringIO__init__codemsgr_   r`   )r&   r   r   r_   r   r`   s         r,   r   zMockResponse.__init__.  s=    
T4(((6:C#6M3	48T\4888r.   c                     | j         S r   r^   r   s    r,   infozMockResponse.info2  s
    |r.   c                     | j         S r   r`   r   s    r,   geturlzMockResponse.geturl5  	    xr.   r   )rU   r$   rV   r   r   r   rW   r.   r,   r   r   -  sJ        N N N N      r.   r   c                       e Zd Zd Zd ZdS )MockCookieJarc                     || _         d S r   )ach_req)r&   r   s     r,   add_cookie_headerzMockCookieJar.add_cookie_header:  s    r.   c                 $    ||c| _         | _        d S r   )ec_reqec_r)r&   r   r   s      r,   extract_cookieszMockCookieJar.extract_cookies=  s    !((TYYYr.   N)rU   r$   rV   r   r   rW   r.   r,   r   r   9  s2          3 3 3 3 3r.   r   c                       e Zd Zd Zd ZdS )
FakeMethodc                 0    || _         || _        || _        d S r   )	meth_namehandleaction)r&   r   r   r   s       r,   r   zFakeMethod.__init__B  s    "r.   c                 4     | j         | j        | j        g|R  S r   )r   r   r   )r&   r   s     r,   __call__zFakeMethod.__call__G  s"    t{4>4;>>>>>r.   N)rU   r$   rV   r   r   rW   r.   r,   r   r   A  s2          
? ? ? ? ?r.   r   c                   &    e Zd Zd Zd Zd Zd ZdS )MockHTTPResponsec                 L    || _         || _        || _        || _        d| _        d S )N   )fpr   statusrO   r   )r&   r   r   r   rO   s        r,   r   zMockHTTPResponse.__init__L  s)    			r.   c                     dS )N rW   r   s    r,   r=   zMockHTTPResponse.readS  s    rr.   c                     i S r   rW   r   s    r,   r   zMockHTTPResponse.infoV  s    	r.   c                     | j         S r   r   r   s    r,   r   zMockHTTPResponse.geturlY  r   r.   N)rU   r$   rV   r   r=   r   r   rW   r.   r,   r   r   K  sP                  r.   r   c                   P    e Zd Zd Zej        fdZd ZddZddddZ	d	 Z
d
 ZdS )MockHTTPClassc                 Z    d| _         g | _        d | _        d| _        d | _        i | _        d S )Nr   F)levelreq_headersr   raise_on_endheaderssock_tunnel_headersr   s    r,   r   zMockHTTPClass.__init__^  s5    
	#( 	!r.   c                 "    || _         || _        | S r   )hostr   )r&   r   r   s      r,   r   zMockHTTPClass.__call__f  s    	r.   c                     || _         d S r   )r   )r&   r   s     r,   set_debuglevelzMockHTTPClass.set_debuglevelk  s    


r.   Nc                 j    || _         || _        |r	|| _        d S | j                                         d S r   )_tunnel_host_tunnel_portr   clear)r&   r   portr_   s       r,   
set_tunnelzMockHTTPClass.set_tunneln  sC       	)#*D    &&(((((r.   F)encode_chunkedc                    || _         || _        |"| xj        |                                z  c_        | j                                         |r|| _        || _        | j        rt                      d S r   )	methodselectorr   r%   sortr   r   r   OSError)r&   r   r`   bodyr_   r   s         r,   r   zMockHTTPClass.requestv  s|    / 	DI,# 	))O	 	r.   c                 >    t          t                      i dd          S )Nr   OK)r   r   r   s    r,   getresponsezMockHTTPClass.getresponse  s    

BT:::r.   c                     d S r   rW   r   s    r,   r   zMockHTTPClass.close  r   r.   r   )rU   r$   rV   r   r   r   r   r   r   r   r   r   rW   r.   r,   r   r   ]  s        " " " &,%C    
  ) ) ) )$    ; ; ;    r.   r   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	MockHandler  c                 0    |                      |           d S r   )_define_methods)r&   methodss     r,   r   zMockHandler.__init__  s    W%%%%%r.   c                     |D ]K}t          |          dk    r|\  }}n|d }}t          ||| j                  }t          | j        ||           Ld S )N   )lenr   r   setattr	__class__)r&   r  specr!   r   meths         r,   r  zMockHandler._define_methods  sh     	0 	0D4yyA~~#ff#TfdFDK88DDND$////	0 	0r.   c                 &   | j         j                            | |||f           |d S |dk    r| S |dk    rt          ddi d          }|S |dk    rt	          d          S |                    d          rx||                    d	          d
z   d          }	 t          |          }n# t          $ r Y nw xY wt          ddi d          }| j         	                    d|d         ||di           S |dk    rt          j	                            d          d S )Nreturn selfreturn responser   r   r   return requestzhttp://blah/r       httpr   raiser]   )parentcallsappendr   r   
startswithrfindintr6   r   r3   rP   )r&   fn_namer   r   kwdsresr   s          r,   r   zMockHandler.handle  sE     $t!<===>4}$$K(((sD"b11CJ'''>***w'' 		0&,,s++A-../D4yy   sD"b11C;$$VT!Wc4RHHHw,''/// s   B% %
B21B2c                     d S r   rW   r   s    r,   r   zMockHandler.close  r   r.   c                 ,    || _         g | j         _        d S r   )r  r  )r&   r  s     r,   
add_parentzMockHandler.add_parent  s    r.   c                 F    t          |d          sdS | j        |j        k     S )Nhandler_orderT)hasattrr   )r&   others     r,   __lt__zMockHandler.__lt__  s*    uo.. 	4!E$777r.   N)
rU   r$   rV   r   r   r  r   r   r  r#  rW   r.   r,   r   r     su         M& & &0 0 0  .    8 8 8 8 8r.   r   c                     g }d}|D ]t} G d dt                     } ||          }|xj        |z  c_        |                    |            |dz   }|                    |           |                     |           u|S )a  Create MockHandlers and add them to an OpenerDirector.

    meth_spec: list of lists of tuples and strings defining methods to define
    on handlers.  eg:

    [["http_error", "ftp_open"], ["http_open"]]

    defines methods .http_error() and .ftp_open() on one handler, and
    .http_open() on another.  These methods just record their arguments and
    return None.  Using a tuple instead of a string causes the method to
    perform some action (see MockHandler.handle()), eg:

    [["http_error"], [("http_open", "return request")]]

    defines .http_error() on one handler (which simply returns None), and
    .http_open() on another handler, which returns a Request object.

    r   c                       e Zd ZdS )6add_ordered_mock_handlers.<locals>.MockHandlerSubclassNrU   r$   rV   rW   r.   r,   MockHandlerSubclassr&            Dr.   r(  r  )r   r   r  r  add_handler)opener	meth_spechandlersr   methsr(  rH   s          r,   add_ordered_mock_handlersr/    s    & HE 	 		 	 	 	 	+ 	 	 	  &&	5 	V	1Or.   c                  V    t                      }| D ]}|                    |           |S r   )r	   r*  )handler_instancesr+  rH   s      r,   build_test_openerr2    s8    F  1Mr.   c                        e Zd Zd Zd Zd ZdS )MockHTTPHandlerc                 J    || _         || _        |                                  d S r   )r   r_   reset)r&   r   r_   s      r,   r   zMockHTTPHandler.__init__  s!    	

r.   c                 "    d| _         g | _        d S )Nr   )_countrequestsr   s    r,   r6  zMockHTTPHandler.reset  s    r.   c                    dd l }dd l}| j                            |                    |                     | j        dk    ru| j        dz   | _        t          j        j        | j	                 }|
                    | j                  }| j                            d|t                      | j	        ||          S || _        |
                    d          }t!          dd|d|                                          S )Nr   r  r  

r   r   r   )emailcopyr9  r  deepcopyr8  r  client	responsesr   message_from_stringr_   r  r   r   rp   r   get_full_url)r&   rp   r<  r=  r!   r   s         r,   	http_openzMockHTTPHandler.http_open  s    T]]3//000;!+/DK;(3D++DL99C;$$XZZD#? ? ? DH++J77CT3C4D4D4F4FGGGr.   NrU   r$   rV   r   r6  rC  rW   r.   r,   r4  r4    sF          
  H H H H Hr.   r4  c                       e Zd ZddZd ZdS )MockHTTPSHandlerr   c                 x    t           j        j                            | |           t	                      | _        d S )N
debuglevel)r3   r   AbstractHTTPHandlerr   r   httpconn)r&   rI  s     r,   r   zMockHTTPSHandler.__init__	  s/    *33DZ3PPP%r.   c                 8    |                      | j        |          S r   )do_openrK  r&   rp   s     r,   
https_openzMockHTTPSHandler.https_open  s    ||DM3///r.   N)r   )rU   r$   rV   r   rO  rW   r.   r,   rF  rF    s7        ( ( ( (0 0 0 0 0r.   rF  c                        e Zd Zd Zd Zd ZdS )MockHTTPHandlerCheckAuthc                 "    || _         d| _        d S NF)r   has_auth_header)r&   r   s     r,   r   z!MockHTTPHandlerCheckAuth.__init__  s    	$r.   c                     d| _         d S rS  )rT  r   s    r,   r6  zMockHTTPHandlerCheckAuth.reset  s    $r.   c                     |                     d          rd| _        t          j        j        | j                 }t          | j        |t                      d|                                          S )NAuthorizationTr   )	rg   rT  r  r?  r@  r   r   r   rB  )r&   rp   r!   s      r,   rC  z"MockHTTPHandlerCheckAuth.http_open  sW    >>/** 	(#'D {$TY/DItXZZS=M=M=O=OPPPr.   NrD  rW   r.   r,   rQ  rQ    sF        % % %% % %Q Q Q Q Qr.   rQ  c                       e Zd Zd Zd ZdS )MockPasswordManagerc                 >    || _         || _        || _        || _        d S r   )realmr`   userru   )r&   r[  urir\  ru   s        r,   r   z MockPasswordManager.add_password%  s"    
	 r.   c                 :    || _         || _        | j        | j        fS r   )target_realm
target_urlr\  ru   )r&   r[  authuris      r,   r   z&MockPasswordManager.find_user_password+  s     !!y$-''r.   N)rU   r$   rV   r   r   rW   r.   r,   rY  rY  $  s2        ! ! !( ( ( ( (r.   rY  c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	OpenerDirectorTestsc                      G d dt                     }|                     t          t                      j         |                       d S )Nc                       e Zd ZdS )<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerNr'  rW   r.   r,   
NonHandlerrf  4  r)  r.   rg  )objectr5   	TypeErrorr	   r*  )r&   rg  s     r,   test_add_non_handlerz(OpenerDirectorTests.test_add_non_handler3  si    	 	 	 	 	 	 	 	)(**6

	F 	F 	F 	F 	Fr.   c                    ddl m} t                      }ddgdgg}t          ||           |                    t
          j                                                   dD ]!}|                     ||j	        |dz              "d S )Nr   )rP   )rM  r  )
proxy_openr  )redirect_requestr  )doproxyredirectz://example.com/)
urllib.errorrP   r	   r/  r*  r3   r   UnknownHandlerr5   r   )r&   rP   or,  schemes        r,   test_badly_named_methodsz,OpenerDirectorTests.test_badly_named_methods9  s     	*)))))')FG01	 	"!Y///	fn3355666/ 	J 	JFh7H0HIIII	J 	Jr.   c                    t                      }g ddgdgdgg}t          ||          }t          d          }|                    |          }|                     ||d                    |d         df|d         dfg}t          ||j                  D ];\  }}|\  }	}
}}|                     |	|
f|           |                     ||f           <d S )N)rC  ftp_openhttp_error_302rw  rC  r  rs   r  r   rC  )r	   r/  r   r   r#   zipr  )r&   rs  r,  r-  rp   rr  expectedgothandlerr!   r   r  s                r,   test_handledz OpenerDirectorTests.test_handledN  s    777L)*)*		 -Q	::+,,FF3KK 	HQK(((1+{+hqk;-GH 00 	+ 	+MHc(+%GT4gt_h777TC6****	+ 	+r.   c                    t                      }g }dgdfdgdffD ]T\  }} G d dt                    } ||          }||_        |                    |           |                    |           U|                    d           |                     |j        d         d         |d                    |                     |j        d         d         |d                    d S )	Nry  r   rC  r   c                       e Zd ZdS )COpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassNr'  rW   r.   r,   r(  r  m  s        r.   r(  rs   r  )r	   r   r   r  r*  r   r#   r  )r&   rs  r-  r.  r   r(  rH   s          r,   test_handler_orderz&OpenerDirectorTests.test_handler_orderh  s   'C&Dc%J'2mQ%7%9 	 	 E=    k    $#E**A+AOOOAMM!	$%%%A444A44444r.   c                    t                      }dgdgg}t          ||          }t          d          }|                     t          j        j        |j        |           |                     |j	        |d         d|fi fg           d S )N)rC  r  ry  rs   r   rC  )
r	   r/  r   r5   r3   r   rP   r   r#   r  )r&   rs  r,  r-  rp   s        r,   
test_raisezOpenerDirectorTests.test_raisez  s    #$)*	 -Q	::+,,&,/===HQKsfb#I"JKKKKKr.   c                    t                      }dgddgg ddgg}t          ||          }t          d          }|                    |           |d         d|ff|d         d|t          j        d	d
i ffg}t          ||j                  D ]G\  }}|\  }}	}
|                     ||	f|d d                    |                     |
|d                    Hd S )N)rC  z	error 302)http_error_400r  rC  ))rx  r  http_error_303
http_errorrx  rs   r   r  .  r   )	r	   r/  r   r   r   	ALWAYS_EQrz  r  r#   )r&   rs  r,  r-  rp   r  r|  r}  r~  method_namer   s              r,   test_http_errorz#OpenerDirectorTests.test_http_error  s    '((+6   	 -Q	::+,,	s1+{SF31+/)3B79: !00 	+ 	+MHc)1&G[$g{3S!W===T3q6****	+ 	+r.   c                    t                      }ddgddgg}t          ||          }t          d          }|                    |           |d         df|d         df|d         df|d         dfg}t	          |j                  D ]\  }\  }}}	}
|dk     rc|                     ||f||                    |                     t          |	          d           |                     |	d         t                     s|                     ||f||                    |                     t          |	          d           |                     |	d         t                     |	d         !|                     |	d         t                     d S )	N)http_requestr  )http_responser  rs   r   r  r  r  r  )
r	   r/  r   r   	enumerater  r#   r  assertIsInstancer   )r&   rs  r,  r-  rp   r  r   r~  r!   r   r  s              r,   test_processorsz#OpenerDirectorTests.test_processors  s   /13/13	 -Q	::+,,	s a[.)HQK+Ha[/*Xa[/,JL /8.@.@ 	A 	A*A*tT1uu  '4%(;;;  TA...%%d1gw7777   '4%(;;;  TA...%%d1gw777 7&))$q'<@@@	A 	Ar.   N)
rU   r$   rV   rj  ru  r  r  r  r  r  rW   r.   r,   rc  rc  1  s        F F FJ J J*+ + +45 5 5$L L L+ + +.!A !A !A !A !Ar.   rc  c                    	 |                      d           n"# t          $ r t          j        d          w xY wt          j                            |           }t          j        dk    r|	                    d          r
|dd          }|S )Nzutf-8zpath is not encodable to utf8r   z///r  )
encodeUnicodeEncodeErrorunittestSkipTestr3   r   r   r    r!   r  )r8   urlpaths     r,   sanepathname2urlr    s    AG A A A ?@@@An))$//G	w$7--e44!""+Ns    7c                      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d Zd Zd Zd Zd Zd Zd 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&d# Z'd$ Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+ Z/d, Z0d- Z1d.S )/HandlerTestsc                 v    G d d           G fddt           j        j                  }dd l}d} ||          }t	                      |_        dd|j        d	d	d
ddgddf	dd|j        dd	d
ddgddf	dd|j        dd	d
ddgddf	dd|j        dd	d
ddgddf	dddd	d	dddgd	d f	dd|j        d	d	dg ddf	fD ]\	  }}}}}	}
}}}t          |          }d |_        |	                    |          }| 
                    |j        |           | 
                    |j        |	           | 
                    |j        t          j        |                     | 
                    |j        |           | 
                    |j        |           | 
                    |j        j        |           | 
                    |j        j        |
           |                                }| 
                    |                    d          |           | 
                    t/          |d                   t1          |                     d S )Nc                        e Zd Zd Zd Zd ZdS )-HandlerTests.test_ftp.<locals>.MockFTPWrapperc                     || _         d S r   r   r&   r   s     r,   r   z6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__       			r.   c                 z    ||c| _         | _        t          j        | j                  t          | j                  fS r   )filenamefiletyper   r   r   r  )r&   r  r  s      r,   retrfilez6HandlerTests.test_ftp.<locals>.MockFTPWrapper.retrfile  s1    /7,t}{49--s49~~==r.   c                     d S r   rW   r   s    r,   r   z3HandlerTests.test_ftp.<locals>.MockFTPWrapper.close      r.   N)rU   r$   rV   r   r  r   rW   r.   r,   MockFTPWrapperr    sA        ! ! !> > >    r.   r  c                   .    e Zd Zd Zej        f fd	ZdS )-HandlerTests.test_ftp.<locals>.NullFTPHandlerc                     || _         d S r   r  r  s     r,   r   z6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__  r  r.   c                     ||c| _         | _        ||c| _        | _        || _         | j                  | _        | j        S r   )r\  passwdr   r   dirsr   
ftpwrapper)r&   r\  r  r   r   r  r   r  s          r,   connect_ftpz9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftp  sE    )-v&	4;'+T$	49 	"0.";";&r.   N)rU   r$   rV   r   r   r   r  )r  s   r,   NullFTPHandlerr    sJ        ! ! ! %+$B' ' ' ' ' ' ' 'r.   r  r   zrheum rhaponicumz ftp://localhost/foo/bar/baz.html	localhostr   Ir}   r~   zbaz.htmlz	text/htmlz'ftp://parrot@localhost/foo/bar/baz.htmlparrotz*ftp://%25parrot@localhost/foo/bar/baz.htmlz%parrotz,ftp://%2542parrot@localhost/foo/bar/baz.htmlz	%42parrotzftp://localhost:80/foo/bar/P   Dzftp://localhost/baz.gif;type=aAzbaz.gifz	image/gifContent-typeContent-length)r3   r   
FTPHandlerftplibr   r  FTP_PORTr   r   rw  r#   r\  r  r   r   gethostbynamer   r  r  r  r  r   getr  r  )r&   r  r  r   rH   r`   r   r   r\  r  type_r  r  mimetyperp   r{  r_   r  s                    @r,   test_ftpzHandlerTests.test_ftp  s   		 		 		 		 		 		 		 		
	' 
	' 
	' 
	' 
	' 
	' 
	'V^6 
	' 
	' 
	' 	!N4  << 0&/2r3U^Z6 7&/8RU^Z6 :&/9b#U^Z6 <&/;CU^Z6 +"b"cU^R' .&/2r3K)!O !	H !	HJCtT65$(( #,,CCK

3AQVT***QXv...QVV%9$%?%?@@@QVT***QVT***Q\2H===Q\2E:::ffhhGW[[88(CCCS)9!:;;SYYGGGGC!	H !	Hr.   c           	      V	   dd l }t          j                                        }t	                      x}|_        t          j        }t          t          j
                            |                    }d}d|z  d|z  dt          j        d          |g}	 t          j        t          j                              }n# t          j        $ r d}Y nw xY w|r|                    d||           |D ]}	t#          |d          }
	 	 |
                    |           |
                                 n# |
                                 w xY w|                    t+          |	                    }	 |                                }|                                }|                                }|                                 n# |                                 w xY wt          j        |          }|j                            |j        d	
          }t          j        |           n# t          j        |           w xY w|                     ||           |                     |d         d           |                     |d         d           |                     |d         |           |                     ||	           d|z  dddt          j        d          dt          j                    d|dt          j                    d|fD ]}		 t#          |d          }
	 |
                    |           |
                                 n# |
                                 w xY w|                      t          j!        j"        |j        t+          |	                     t          j        |           # t          j        |           w xY wt          j                                        }t	                      x}|_        dD ]\  }	}t+          |	          }	 |                    |           | #                    |j$        |           |                     |j%        d           n/# t          j!        j"        $ r | &                    |           Y nw xY w|                     |j%        dk    |           d S )Nr   s   hello, world
zfile://localhost%sr1   zfile://r  r   wbT)usegmtr  z
text/plainr  r   zLast-modifiedzfile://localhost:80%szfile:///file_does_not_exist.txtz)file://not-a-local-host.com//dir/file.txtz:80r0   z'file://somerandomhost.ontheinternet.com))zfile://ftp.example.com//foo.txtF)z file://ftp.example.com///foo.txtF)zfile://ftp.example.com/foo.txtF)z"file://somehost//foo/something.txtF)z#file://localhost//foo/something.txtFftp)'email.utilsr3   r   FileHandlerr   r  r   TESTFNr  r    r8   r9   r   r  gethostnamegaierrorr  r   writer   	file_openr   r=   r   r   statutils
formatdatest_mtimeremover#   getcwdr5   r   rP   assertIsrp   typerk   )r&   r<  rH   rs  r  r  towriteurls	localaddrr`   r@   r{  r   r_   respurlstatsmodifiedr  rp   s                      r,   	test_filezHandlerTests.test_file  s   N&&((!||#AH!"27??6#:#:;;# 7*'!!#1+>>>H
	,V-?-A-ABBII 	 	 	III	 	>KKKGG<=== 	+ 	+CVT""A"GGG$$$GGIIIIAGGIIIIKK--6688DffhhGhhjjGGGIIIIAGGIIII ;11%.1NN	&!!!!	&!!!!T7+++W^4lCCCW%56===W_5x@@@Wc**** $g--77#)#7#D#D#D#D#%9;;;;8 8 Y[[[[&&" 	" 	"C
"&&GGG$$$GGIIIIAGGIIII!!&,"7"#+ws||= = = 	&!!!!	&!!!!N&&((!||#AH 	5 	5HC #,,C2C    aeS)))  51111	 <( & & &  %%%%%&
 SX.4444	5 	5s~   %B; ;CCD/H/E%H+<F<'H<G8HH5
N+M0N+M;N++OQ)RRc                    t           j                                        }t                      x}|_        d}dD ]w\  }}t          ||ddi          }d |_        |                    dd           t                      }|	                    ||          }|j
         |j         |j         |j         |j        |j        dk    df |                                }	|	j         |	j         |                     |                                |           |                     |j        d	           |                     |j        d
           |                     |j        |           |                     |j        d           |                     |j        g d           |                     |j        |           yd|_        |                     t           j        j        |j	        ||           t          dd          }|                     t:          |j        |           dg|_        dD ]}t          d|          }tA          ddi d          }|                    |          }
|7| !                    d|j"                   | !                    d|j"                   nB|                     |j"        d         d           |                     |j"        d         d           |                     |j"        d         d	           |                     |j"        d         d           |                    dd           |                    dd           |                    dd           |                    dd           |                    |          }
|                     |j"        d         d           |                     |j"        d         d           |                     |j"        d         d           |                     |j"        d         d           d S )Nrs   ))GETN)POSTs   blahFoor~   Spameggsr   r   r|   r   r0   ))
Connectionr   )r  r~   r  r  Tbadpostr  )r.   Nr   r  r  0z!application/x-www-form-urlencodedHostr}   rc   )#r3   r   rJ  r   r  r   r   ro   r   rM  r=   r   r   r   r   r   r  __contains__r#   r   r   r   r   r   r   r   r5   r   rP   ri  do_request_r   r   assertNotInunredirected_hdrs)r&   rH   rs  r`   r   r   rp   r  r{  hdrsnewreqs              r,   	test_httpzHandlerTests.test_httpl  s   N..00!||#AH#> 	. 	.LFD#teU^44CCK''777 ??D		$$$A FFAJJFFAHHFAESL$&&6688DHHd''QXXZZ---TY666TZ+++T[&111T]C000T-@ @ @A A A TY---- $( &,/D#FFF +I66)Q]C888 )) 	C 	CD/66CS$B//A]]3''F|  !133HIII  1FGGGG  !67G!H#NNN  !6~!F@B B B S26:MJJJS26:FCCC ''(8%@@@''>>>''666''666]]3''FS23CDeLLLS2>BEJJJS26:EBBBS26:EBBBB1	C 	Cr.   c                    t           j                                        }t                      x}|_        t          j        dd          }|j        }|                                 | 	                    t          j        |           t          |d          5 }t          d|i           }|                    |          }|                    d          }|                     |d           |                     |                    d                     d d d            n# 1 swxY w Y   t          |d          5 }t          d|d	d
i          }|                    |          }|                     t'          |                    d                    d
           |                     |                    d                     d d d            d S # 1 swxY w Y   d S )Nzw+bF)modedeleterbrs   Transfer-encodingchunkedr  Content-Length   )r3   r   rJ  r   r  tempfileNamedTemporaryFiler!   r   r2   r    unlinkr   r   r  rm   r#   rk   rg   r  )	r&   rH   rs  file_obj	file_pathr@   rp   r  tes	            r,   test_http_body_filez HandlerTests.test_http_body_file  sJ    N..00!||#AH.E%HHHM		9---)T"" 	Ba/B77C]]3''F""#677BR+++V../?@@AAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B )T"" 	Ea/5Er4JKKC]]3''FS!2!23C!D!DEErJJJV../BCCDDD		E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es&   A:DD"D6BG

GGc                    t           j                                        }t                      x}|_        t          j                    }t          d|i           }|                    |          }| 	                    |
                    d          d           |                     |                    d                     ddi}t          d||          }|                    |          }| 	                    t          |
                    d                    d           |                     |                    d                     |                                 d S )Nrs   r  r  r  r  r  )r3   r   rJ  r   r  r   BytesIOr   r  r#   rm   rk   rg   r  r   )r&   rH   rs  r  rp   r  r_   s          r,   test_http_body_fileobjz#HandlerTests.test_http_body_fileobj  s0    N..00!||#AH:<<+Xr::s##**+>??KKK**+;<<===#R(+Xw??s##V../?@@AA2FFF**+>??@@@r.   c           	      x   t           j                                        }t                      x}|_        t
          j        ddg}i ddifD ]}t          j        |t          j	                  5 }t          d|j        |          }|                    |          }|sS|                     |                    d          d            |                     |                    d          d	           n6|                     t          |                    d                    d           d d d            n# 1 swxY w Y   d S )
Nz-cpassr  r  )stdoutrs   r  r  r  )r3   r   rJ  r   r  sys
executable
subprocessPopenPIPEr   r  r  r#   rm   r  )r&   rH   rs  cmdr_   procrp   r  s           r,   test_http_body_pipez HandlerTests.test_http_body_pipe  s}    N..00!||#AH~tW-,b11 
	) 
	)G!#jo>>> 	)$3T['JJs++ )$$V%6%67G%H%H$OOO$$V%6%67J%K%K%.0 0 0 0 $$S):):;K)L)L%M%M%') ) )	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)
	) 
	)s   +B7D..D2	5D2	c                    t           j                                        }t                      x}|_        d }i ddifD ]}t          d |            |          }|                    |          }|sS|                     |                    d          d            |                     |                    d          d           |                     t          |                    d                    d           d S )Nc               3      K   dV  d S )Ns   onerW   rW   r.   r,   iterable_bodyz;HandlerTests.test_http_body_iterable.<locals>.iterable_body  s      LLLLLr.   r     rs   r  r  r  )
r3   r   rJ  r   r  r   r  r#   rm   r  )r&   rH   rs  r  r_   rp   r  s          r,   test_http_body_iterablez$HandlerTests.test_http_body_iterable  s    N..00!||#AH	 	 	 ,b11 	O 	OG/'JJC]]3''F O  !2!23C!D!DdKKK  !2!23F!G!G!*, , , ,   V%6%67G%H%H!I!I2NNNN	O 	Or.   c                 P   t           j                                        }t                      |_        |                    t          dd                    }|                     |                    d          d           | 	                    |
                    d                     d S )Nrs   rW   r  r  r  )r3   r   rJ  r   r  r  r   r#   rm   rk   rg   )r&   rH   rp   s      r,   test_http_body_empty_seqz%HandlerTests.test_http_body_empty_seq  s    N..00<<mmG$92>>??(;<<iHHH(899:::::r.   c                 d   t           j                                        }t                      x}|_        t          j        dg d          }i ddifD ]^}t          d||          }|                    |          }|                     t          |
                    d                    d           _d S )Nr  )r  r        r     rs   r  )r3   r   rJ  r   r  arrayr   r  r#   r  rm   )r&   rH   rs  iterable_arrayr_   rp   r  s          r,   test_http_body_arrayz!HandlerTests.test_http_body_array  s     N..00!||#AHS33,b11 	J 	JG/IIC]]3''FS!2!23C!D!DEEbIIII	J 	Jr.   c                     t                      }t          d          }|                    |           |                    d           |                     |j        d           d S )Nr  rH  zhttps://www.example.com)r	   rF  r*  r   r#   _debuglevel)r&   rs  rH   s      r,   test_http_handler_debuglevelz)HandlerTests.test_http_handler_debuglevel  s`    ***	a	()))*****r.   c                    t           j                                        }t                      |_        d}g d}|D ]}t          ||          }|                    |          }|                     |j        d         d           |	                    dd            |                    |          }|                     |j        d         d           d S )Nr.   )z#http://example.com/foo/bar/baz.htmlz$http://example.com//foo/bar/baz.htmlz$http://example.com/foo//bar/baz.htmlz$http://example.com/foo/bar//baz.htmlr  r|   zsomeproxy:3128)
r3   r   rJ  r   r  r   r  r#   r  	set_proxy)r&   rH   r   ds_urlsds_urlds_req	np_ds_reqp_ds_reqs           r,   test_http_doubleslashz"HandlerTests.test_http_doubleslash$  s     N..00<<    
	P 
	PFVT**F f--IY8@-PPP -t444}}V,,HX7?OOOO
	P 
	Pr.   c                 t   g d}t          d          }|D ]}||_        t          |          }|                     |                                |           |                     |j        pd|j                   |                     t          |                                          j        |j                   d S )N)zhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashr[   r[   r   )r   full_urlr   r#   rB  fragmentquery)r&   r  r{  r`   parseds        r,   test_full_url_setterz!HandlerTests.test_full_url_setter?  s    
 
 
 ()) 	M 	MCAJc]]FQ^^--s333 QZ-2v???Xann&6&677=v|LLLL	M 	Mr.   c                     t          d          }|`|                     |j                   |                     |j                   |                     |j        d           d S )Nhttp://www.example.comr   )r   r'  rl   r(  r#   r   )r&   r{  s     r,   test_full_url_deleterz"HandlerTests.test_full_url_deleterV  s_    ,--J!*%%%!*%%%R(((((r.   c                    t           j                                        }t                      |_        d}t          |          }|                    |          }|                     |j        d           |                     |j	        d           d}t          |          }|                    |          }|                     |j        d           |                     |j	        d           d S )Nzhttp://www.python.org?getspamwww.python.orgz	/?getspamhttp://www.python.orgr   )
r3   r   rJ  r   r  r   r  r#   r   r   )r&   rH   	weird_urlrp   r  url_without_paths         r,   test_fixpath_in_weirdurlsz&HandlerTests.test_fixpath_in_weirdurls]  s     N..00<<3	i  s##&6777+6662&''s##&6777"-----r.   c                    t           j                                        }t                      x}|_        d}t          |          }t          ddi d|          }|                    ||          }|                     ||           | 	                    t          |d                     t          ddi d|          }|                    ||          }|                     ||           | 	                    t          |d                     t          dd	i d|          }|                    ||          }|                     ||           | 	                    t          |d                     t          d
di d|          }|                     |                    ||                     |                     |j        d           |                     |j        ||d
di f           d S )Nrs   r   r   r   r      Accepted   zPartial contenti  zBad gatewayr  )r3   r   HTTPErrorProcessorr   r  r   r   r  r  rk   r!  rl   r#   r   r   )r&   rH   rs  r`   rp   r{  newrs          r,   test_errorszHandlerTests.test_errorsp  s   N--//!||#AH#clldBC00sA&&aG,,---j"b#66sA&&aG,,---/R==sA&&aG,,---mRS99!//#q11222&)))#q#}b!ABBBBBr.   c                    t                      }t          j                            |          }t	                      |_        t          d          }t          ddi d          }|                    |          }| 	                    |j
        |           | 	                    |j
        |           |                     |j        d           |                     |j                   |                    ||          }| 	                    |j        |           | 	                    |j        |           | 	                    ||           d S )Nrs   r   r   r   r|   )r   r3   r   HTTPCookieProcessorr   r  r   r   r  r  r   r#   origin_req_hostrk   unverifiabler  r   r   )r&   cjrH   rp   r{  r  r:  s          r,   test_cookieszHandlerTests.test_cookies  s   __N..r22<<+,,dB++$$bj#&&&bj&))),m<<<)***sA&&bi%%%bgq!!!ar.   c                 >   d}d}t           j                                        }t                      x}|_        dD ]@}dD ]9}t          |d|z            }t          ||          }t          j        |_	        |
                    dd           |0|
                    dt          t          |                               |                    d	d
           	  ||t                      |dt          d|i                     nE# t           j        j        $ r. |                     |d           |                     |           Y nw xY w|                     |j                                        |           	 |                     |j                                        d           n/# t0          $ r" |                     |j        j                   Y nw xY wd |j        j        D             }	|                     d|	           |                     d|	           |                     |j        j        d         d           |                     d	|j        j                   |                     d	|j        j                   ;Bt          |          }t          j        |_	        |fd}
t          |d          }d}t          j        |_	        	 	  |
||d           |dz   }# t           j        j        $ r- |                     |t           j        j        j                   Y nw xY wt          |d          }d}t          j        |_	        	 	  |
||d|z             |dz   }# t           j        j        $ r. |                     |t           j        j        j                   Y d S w xY w)Nhttp://example.com/a.htmlzhttp://example.com/b.html)i-  r  i/  3  4  )Nz
blah
blah
zhttp_error_%sNonsensezviking=withholdr  r  r   Blahlocation)rD  rE  r  c                 6    g | ]}|                                 S rW   )lower).0xs     r,   
<listcomp>z.HandlerTests.test_redirect.<locals>.<listcomp>  s     <<<17799<<<r.   zcontent-lengthzcontent-typec           
      n    |                      |t                      ddt          d|i                     d S )Nr  rG  rH  )rx  r   r   )rH   rp   r`   s      r,   rp  z,HandlerTests.test_redirect.<locals>.redirect  s@    S(**c6(*c):;;= = = = =r.   r|   )r>  r   r  rs   zhttp://example.com/%d) r3   r   HTTPRedirectHandlerr   r  getattrr   r   r   r   ri   rR   r  ro   r   r   r   	HTTPErrorrQ   assertIsNotNoner#   rp   rB  
get_methodAttributeErrorrk   r   r_   r  r  max_repeatsmax_redirections)r&   from_urlto_urlrH   rs  r   r   r   rp   r_   rp  r   s               r,   test_redirectzHandlerTests.test_redirect  s   .,N..00!||#AH , 	B 	BD, B B Od$:;;h--$<z+<===#NN#3ST^^DDD++FF;;;/F3

D&&
F';<<> > > >|- / / /MM$
333((...../   !3!3!5!5v>>>1$$QU%5%5%7%7????% 1 1 1$$QUZ000001
 =<aem<<<  !17;;;  999  z!:!24 4 4  777  )@AAAA=BB h4!' 	= 	= 	= 	= h>>>4	T"C!6777	" |% 	T 	T 	TUFN$F$RSSSSS	T
 h>>>4	R"C!85!@AAA	" |% 	R 	R 	RU#^?PR R R R R R	RsH   *D  ?EE3-F!!)GGK >LLM >NNc                 N   d}g d}g d}d}t           j                                        }t                      x}|_        t          |          }t          j        |_        |D ]T}|dz   |z   }	| 	                    t           j
        j        |j        |t                      ddt          d|	i                     U|D ]k}|dz   |z   }
|                    |t                      dd	t          d|
i                     |                     |j                                        |
           ld S )
NrC  )r  httpsr  )fileimapldapzexample.com/b.htmlz://r  zSecurity LoopholerH  That's fine)r3   r   rO  r   r  r   r   r   r   r5   r   rQ  rx  r   r   r#   rp   rB  )r&   rW  valid_schemesinvalid_schemesschemeless_urlrH   rs  rp   rt  invalid_url	valid_urls              r,   test_invalid_redirectz"HandlerTests.test_invalid_redirect  sD   ....000-N..00!||#AHh4% 	< 	<F 5.>9Kfl4a6FS*=[ 9::< < < < $ 	> 	>F7IS(**c=Z3446 6 6QU//119====		> 	>r.   c           
         d}d}t           j                                        }t                      x}|_        t          |          }t          j        |_        t           j	        
                    ||          }|                    |t                      ddt          d|i                     |                     |j                                        |           d S )NrC  z/b.htmlr  r_  rH  )r3   r   rO  r   r  r   r   r   r   r   urljoinrx  r   r   r#   rp   rB  )r&   rW  relative_urlrH   rs  rp   rd  s          r,   test_relative_redirectz#HandlerTests.test_relative_redirect  s    . N..00!||#AHh4L((,??		hjj#}Y/00	2 	2 	2++--y99999r.   c                    ddl m} ddlm}  |            } ||dd           t	          dd          }t
          j                                        }t
          j                                        }t
          j        	                    |          }t          ||||          }|                    d           |                     |j                            d                     d S )	Nr   )	CookieJar)interact_netscapezhttp://www.example.com/z	spam=eggsr  z%Location: http://www.cracker.com/

Cookie)http.cookiejarrk  test.test_http_cookiejarrl  r4  r3   r   HTTPDefaultErrorHandlerrO  r=  r2  r   rk   rp   rg   )	r&   rk  rl  r@  hhhdehhrhcprs  s	            r,   test_cookie_redirectz!HandlerTests.test_cookie_redirect  s    ,,,,,,>>>>>>Y[["7EEES"MNN~5577n0022^//33b$R00	()))**84455555r.   c                 h   d}t          dd|z             }t          j                                        }t          j                                        }t          |||          }|                    d          }|                     |                                |	                                           d S )Nz(http://www.example.com/index.html#OK

r  z
Location: r-  )
r4  r3   r   rp  rO  r2  r   r#   r   strip)r&   redirected_urlrq  rr  rs  rs  r   s          r,   test_redirect_fragmentz#HandlerTests.test_redirect_fragment  s    GS,"?@@~5577n0022b$,,VV,--n&:&:&<&<=====r.   c                                           t          j        j                   t          j        j        d}t          j        |          t          j        _                              t          t          j        d           t          d           fd}|t          j        j        _        t          j                            d          }                     |                                d           d S )Ns(   HTTP/1.1 302 Found
Location: ?query

HTTPConnection)z/pathz/path?queryc                                          |t                                j        | ||g|R i | d| j        _        d S )Ns   HTTP/1.1 200 OK

Hello!)r#   nextr   r  fakedata)connr   r`   poskw
real_classr&   r  s        r,   r   z3HandlerTests.test_redirect_no_path.<locals>.request*  sV    S$t**---JtVS=3==="===&FDN###r.   zhttp://python.org/pathzhttp://python.org/path?query)r2   r3   r   r4   r  r?  r{  r   fakehttpr  iterr7   r#   r   )r&   	response1r   r   r  r  s   `   @@r,   test_redirect_no_pathz"HandlerTests.test_redirect_no_path  s     	1222[/
E	%0%9)%D%D".>
KKK,--	G 	G 	G 	G 	G 	G 	G
 .5"*^##$<==&DEEEEEr.   c                     G d dt           j        j                  } |            }t           j                            |          }d}|D ]\  }}|                     t          |                    5  t          j        d|z   dz             |_        |	                    d          }d|z   dz   }|j
        }	|                     |	                    |          t          |	                     d d d            n# 1 swxY w Y   d S )	Nc                       e Zd Zd ZdS )4HandlerTests.test_redirect_encoding.<locals>.Handlerc                     |                      | j        |          }| j        j        | _        t	          j        d          | _        |S )Ns)   HTTP/1.1 200 OK
Content-Length: 3

123)rM  
connectionbuflast_bufr   r  )r&   rp   results      r,   rC  z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_open7  sC    dos;; $ 3"-"6# # r.   N)rU   r$   rV   rC  rW   r.   r,   Handlerr  6  s#        
 
 
 
 
r.   r  ))s   /på-dansk/s   /p%C3%A5-dansk/)   /spaced%20path/r  )s   /spaced path/r  )s   /?på-dansks   /?p%C3%A5-dansks!   HTTP/1.1 302 Redirect
Location: s   

rs   s   GET     )r3   r   HTTPHandlerbuild_openersubTestreprr   r  r  r   r  rf   r  )
r&   r  r~  r+  rJ   rH  r  r   r|  r   s
             r,   test_redirect_encodingz#HandlerTests.test_redirect_encoding3  s|   	 	 	 	 	fn0 	 	 	 ')),,W55
 #( 
	M 
	MXvd8nn-- 	M 	M%0%9"$,-0& &"
 ";;'<=="V+d2!* 2 28 < <d7mmLLL	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M
	M 
	Ms   2A:C88C<	?C<	c                    d}t          |          t          |          fD ]}t                      }t          j                            |          }|                    |           dgg}t          ||          }t          d          }|                     |j	        d           |
                    |           |                     |j	        |           |                     |d         dfgd	 |j        D                        d S )
Nproxy.example.com:3128r  )HTTP)rC  r  zhttp://acme.example.com/zacme.example.comr   rC  c                 "    g | ]}|d d         S r   r  rW   rK  tups     r,   rM  z+HandlerTests.test_proxy.<locals>.<listcomp>f  s     :::3c!A#h:::r.   )dictr	   r3   r   ProxyHandlerr*  r/  r   r#   r   r   r  )r&   r(   ry   rs  phr,  r-  rp   s           r,   
test_proxyzHandlerTests.test_proxyV  s   $1t|||+ 	< 	<A  A,,Q//BMM"12I 1I>>H455CSX'9:::FF3KKKSXq)))x{K89::!':::< < < <	< 	<r.   c                 F   dt           j        d<   t                      }t          j                            t          d                    }|                    |           t          d          }| 	                    |j
        d           |                    |           | 	                    |j
        d           t          d          }| 	                    |j
        d           |                    |           | 	                    |j
        d           t           j        d= d S )	Nz
python.orgno_proxyproxy.example.comr  zhttp://www.perl.org/www.perl.orgr1  r0  r    environr	   r3   r   r  r  r*  r   r#   r   r   r&   rs  r  rp   s       r,   test_proxy_no_proxyz HandlerTests.test_proxy_no_proxyh  s    !-
:^((3F)G)G)GHH	b,-->222	s#6777-..#3444	s#3444Jz"""r.   c                    dt           j        d<   t                      }t          j                            t          d                    }|                    |           t          d          }| 	                    |j
        d           |                    |           | 	                    |j
        d           t           j        d= d S )N*r  r  r  r1  r0  r  r  s       r,   test_proxy_no_proxy_allz$HandlerTests.test_proxy_no_proxy_allw  s    !$
:^((3F)G)G)GHH	b-..#3444	s#3444Jz"""r.   c                    t                      }t          j                            t	          d                    }|                    |           dgg}t          ||          }t          d          }|                     |j	        d           |
                    |           |                     |j	        d           |                     |d         dfgd |j        D                        d S )	Nr  r[  )rO  r  https://www.example.com/www.example.comr   rO  c                 "    g | ]}|d d         S r  rW   r  s     r,   rM  z1HandlerTests.test_proxy_https.<locals>.<listcomp>  s     666s#ac(666r.   )r	   r3   r   r  r  r*  r/  r   r#   r   r   r  )r&   rs  r  r,  r-  rp   s         r,   test_proxy_httpszHandlerTests.test_proxy_https  s    ^((4L)M)M)MNN	b./
	 -Q	::011#4555	s#;<<<8A;5666ag666	8 	8 	8 	8 	8r.   c                 2   t                      }t          j                            t	          d                    }|                    |           t                      }|                    |           t          d          }|                    dd           |                    dd           | 	                    |j
        d           |                     |j                   |                    |           |                     d	|j        j                   |                     d
|j        j                   |                     |j                   | 	                    |j
        d           | 	                    |                    d          d           d S )Nr  r  r  Proxy-AuthorizationFooBar
User-AgentGrailr  )r  r  )r  r  Proxy-authorization)r	   r3   r   r  r  r*  rF  r   ri   r#   r   rl   r   r   r  rK  r   rQ   rR  rm   )r&   rs  r  https_handlerrp   s        r,   $test_proxy_https_proxy_authorizationz1HandlerTests.test_proxy_https_proxy_authorization  st   ^((4L)M)M)MNN	b(**	m$$$011,h777|W---#4555#*+++	s 	:&/;	= 	= 	=-#,8	: 	: 	:S-...#;<<<(=>>IIIIIr.   r   zonly relevant for Windowsc                    d}t           }dD ]%}|                      |||          d|z             &dD ]%}|                      |||          d|z             &d}|                      |d|          d|z             |                      |d|          d|z             d	D ]%}|                      |||          d
|z             &d S )Nz*www.example.com;*.example.net; 192.168.0.1)r  zwww.example.netz192.168.0.1z expected bypass of %s to be true)r|   zwww.example.orgexample.netz192.168.0.2!expected bypass of %s to be Falsezexample.com; <local>r|   r  )testr  z.expect <local> to bypass intranet address '%s')r   rf   rk   )r&   proxy_overrideproxy_bypassr   s       r,   test_winreg_proxy_bypassz%HandlerTests.test_winreg_proxy_bypass  sP   E4I 	G 	GDOOLL~>>>EG G G G$ 	I 	ID\\$??@4GI I I I 0]NCC:TA	C 	C 	Cm^DD<tC	E 	E 	E) 	$ 	$DOOLL~>>L"#$ $ $ $	$ 	$r.   darwinzonly relevant for OSXc                    dg dd}dD ])}|                      t          ||          d|z             *dD ])}|                     t          ||          d|z             *dg d}|                      t          d	|                     dd
dgd}d}|                      t          ||          d|z             d}|                     t          ||          d|z             d S )NF)foo.barz	*.bar.com	127.0.0.1z10.10z10.0/16)exclude_simple
exceptions)r  zwww.bar.comr  z	10.10.0.1z10.0.0.1z expected bypass of %s to be True)zabc.foo.barzbar.comz	127.0.0.2z	10.11.0.1notinbypassr  Tr  z10.0.0.0/40z172.19.10.0/24z172.19.10.5z10.0.1.5)rf   r   rk   )r&   bypassr   s      r,   test_osx_proxy_bypassz"HandlerTests.test_osx_proxy_bypass  sn    $& & &
 
! 	G 	GDOO8vFF>EG G G G 	I 	ID9$GG@4GI I I I %);;4VVDDEEE $)+;=
 
 4T6BB:TA	C 	C 	C5dFCC;dB	D 	D 	D 	D 	Dr.   c                    |                      ||          5  t                      }t                      }t          j                            |          }d                    |          dz   }t          d|          }|                    |           |                    |           | 	                    ||d|||dd           d d d            d S # 1 swxY w Y   d S )N)r[  r_   z
r;    rW  !http://acme.example.com/protected)
r  r	   rY  r3   r   r
   joinr4  r*  _test_basic_auth)r&   r_   r[  r+  password_managerauth_handlerr   http_handlers           r,   check_basic_authzHandlerTests.check_basic_auth  s9   \\w\77 	G 	G#%%F244!>>>?OPPL;;w''*4D*355L|,,,|,,,!!&,"'7G"E"EG G G	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	Gs   B*CCCc           
         d}d}d| d}d| d}d}d| d}d| dd| dd| d	| d
| | d
| | d
| | d
| | d
| fD ]}d| g}|                      ||           t          j        dt          f          5  d| g}|                      ||           d d d            n# 1 swxY w Y   ||g||g||gfD ]$}	d |	D             }|                      ||           %d S )Nzrealm2@example.comzBasic realm=""zOtherscheme xxxzDigest realm="zn", qop="auth, auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"zBasic realm=''z", charset="UTF-8"z, WWW-Authenticate: zBasic Auth Realm was unquotedzWWW-Authenticate: Basic realm=c                     g | ]}d | S )r  rW   )rK  	challenges     r,   rM  z0HandlerTests.test_basic_auth.<locals>.<listcomp>  s0     4 4 4$ 8I77 4 4 4r.   )r  r   check_warningsUserWarning)
r&   r[  realm2basicbasic2other_no_realmdigest	realm_strr_   
challengess
             r,   test_basic_authzHandlerTests.test_basic_auth  s   $%((((*****?6 ? ? ? %E$$$$E$$$ 6E555     ((((((((        
 	2 	2I 8I778G!!'51111 +-L%0-2 3 3 	2 	2???@G!!'5111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 VVUO
 	2 	2J
4 4(24 4 4G!!'51111	2 	2s   B++B/2B/c           
         t                      }t          j                            t	          d                    }|                    |           t                      }t          j                            |          }d}t          dd|z            }|                    |           |                    |           | 	                    ||d|||dd           d S )Nr  r  ACME Networksi  z(Proxy-Authenticate: Basic realm="%s"

r  z&http://acme.example.com:3128/protected)
r	   r3   r   r  r  r*  rY  ProxyBasicAuthHandlerr4  r  )r&   r+  r  r  r  r[  r  s          r,   test_proxy_basic_authz"HandlerTests.test_proxy_basic_auth  s    !!^((3K)L)L)LMM2.00~;;<LMM&?%GI I<(((<(((fl4I#\3CF6	  	  	  	  	 r.   c           
          G d dt                     } G d dt          j        j                  } G d dt          j        j                  } |            }t                      } ||          } ||          }d}t          dd	|z            }	|                    |           |                    |           |                    |	           |                     ||d
||	|dd           | 	                    |j
        ddgdz             d S )Nc                       e Zd Zd Zd ZdS )QHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorc                 <    t          j        |            g | _        d S r   )r	   r   recordedr   s    r,   r   zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__9  s    '--- "r.   c                 :    | j                             |           d S r   )r  r  )r&   r   s     r,   recordzXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.record=  s    $$T*****r.   N)rU   r$   rV   r   r  rW   r.   r,   RecordingOpenerDirectorr  8  s2        # # #+ + + + +r.   r  c                       e Zd Zd ZdS )OHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc                 x    | j                             d           t          j        j        j        | g|R i | d S )Nr  )r  r  r3   r   HTTPDigestAuthHandlerhttp_error_401r&   r   r  s      r,   r  z^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401A  sa    ""8,,,4CD L>BL L LFJL L L L Lr.   NrU   r$   rV   r  rW   r.   r,   TestDigestAuthHandlerr  @  s(        L L L L Lr.   r  c                       e Zd Zd ZdS )NHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc                 x    | j                             d           t          j        j        j        | g|R i | d S )Nr  )r  r  r3   r   r
   r  r  s      r,   r  z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401G  sa    ""7+++3B4 K=AK K KEIK K K K Kr.   Nr  rW   r.   r,   TestBasicAuthHandlerr  F  s(        K K K K Kr.   r  r  r  &WWW-Authenticate: Basic realm="%s"

rW  r  r  r  r  )r	   r3   r   r  r
   rY  r4  r*  r  r#   r  )
r&   r  r  r  r+  r  digest_handlerbasic_handlerr[  r  s
             r,   #test_basic_and_digest_auth_handlersz0HandlerTests.test_basic_and_digest_auth_handlers/  s   	+ 	+ 	+ 	+ 	+n 	+ 	+ 	+	L 	L 	L 	L 	LFN$H 	L 	L 	L	K 	K 	K 	K 	K6>#F 	K 	K 	K )(**.00../?@@,,-=>>&=EG G=)))>***<((( 	fm_#\3CAA	  	  	  	8W*=a*?@@@@@r.   c                    t                      }t          j                            d           }t	          dd          }|                    |           |                    |           |                     t          |j        d           d S )Nr  zWWW-Authenticate: Kerberos

r-  )	r	   r3   r   r  r4  r*  r5   r6   r   )r&   r+  digest_auth_handlerr  s       r,   $test_unsupported_auth_digest_handlerz1HandlerTests.test_unsupported_auth_digest_handlera  s    !!$nBB4HH&57 7.///<(((*fk3KLLLLLr.   c                    t                      }t          j                            d           }t	          dd          }|                    |           |                    |           |                     t          |j        d           d S )Nr  zWWW-Authenticate: NTLM

r-  )	r	   r3   r   r
   r4  r*  r5   r6   r   )r&   r+  basic_auth_handlerr  s       r,   #test_unsupported_auth_basic_handlerz0HandlerTests.test_unsupported_auth_basic_handlerk  s    !!#^@@FF&13 3-...<(((*fk3KLLLLLr.   c	                    dd l }	d\  }
}|                    |||
|           |                     ||j                   |                     ||j                   |                     |
|j                   |                     ||j                   |                    |           |                     |j        |           |                     |j	        |           |                     t          |j                  d           |                     |j        d                             |                     t          |
d|d          }d|	                    |                                                                          z   }|                     |j        d                             |          |           |                     |j        d         j        |         |           d x|_        |_        |                                 |                    |           |                     t          |j                  d           |                     |j        d                             |                     d S )Nr   wilecoyoter  :asciizBasic r  )base64r   r#   r[  r`   r\  ru   r   r_  r`  r  r9  rk   rg   bytesencodebytesrw  decoderm   r  r6  )r&   r+  r  auth_headerr[  r  r  request_urlprotected_urlr  r\  ru   userpassauth_hdr_values                 r,   r  zHandlerTests._test_basic_authu  sf    	)h 	!!%dHEEE 0 6777&6&:;;;/4555#3#<===K    	)6>>>)4mDDD 	\233Q777.q1<<[IIJJJDDD((3W=="x((..007799:.q1<<[II'	) 	) 	).q1CKP'	) 	) 	) =A@ 0 9K   \233Q777.q1<<[IIJJJJJr.   c                 j   d\  }}d}t          d          }t                      }t          |          }|                    d |||d           |                     |                    |                     |                     |                    |dz                        |                     |                    |dz                        t                      }|                    |           |                    |           |	                    |           |                     |j
                   d S )Nr  r  r   T)is_authenticatedz/nestedplain)rQ  r   r
   r   rf   r  rk   r	   r*  r   rT  )r&   r\  ru   r  r  pwd_managerauth_prior_handlerr+  s           r,   test_basic_prior_auth_auto_sendz,HandlerTests.test_basic_prior_auth_auto_send  s4    *h9/442441+>>''+tX 	( 	F 	F 	F 	44[AABBB44[95LMMNNN55kG6KLLMMM!!-...<(((K    	455555r.   c                 
   d\  }}d}d}t                      }t          |          }|                    ||||           |                    |          }|                     |           t                      }|                    |           t          ddd z            }	|                    |	           |                    |           |                    |          }| 	                    |           t          d          }	|                     |	j                   t                      }|                    |           |                    |	           |                    |           | 	                    |	j                   d S )Nr  r  ACMEr  r  r   )r   r
   r   r  rk   r	   r*  r4  r   rf   rQ  rT  )
r&   r\  ru   r  r[  r  r  is_authr+  r  s
             r,   .test_basic_prior_auth_send_after_first_successz;HandlerTests.test_basic_prior_auth_send_after_first_success  s    *h92441+>>''{D(KKK..{;;!!!!!-...&=DF F<(((K   ..{;;   /445666!!-...<((( 	K    	455555r.   c                    dD ]\  }}d                     |          }t          j        |                                |z             }t          j                                        }t          d          }d|_        |	                    ||          5 }|
                                 ddd           n# 1 swxY w Y   |                     |j        j        d                     |                     dS )z=Test the connection is cleaned up when the response is closed))zConnection: close   data)zTransfer-Encoding: chunkeds   4
data
0

)zContent-Length: 4r!  zHTTP/1.1 200 OK
{}

http://dummy/NzConnection not closed with {!r})formatr   r  r  r3   r   rJ  r   r   rM  r=   rf   fakesockclosed)r&   transferr   headerr  r~  rp   resps           r,   test_http_closedzHandlerTests.test_http_closed  s)   !
 	D 	DXt
 5;;HEEF'$(>??Dn88::G/**CCKs++ t		              OODM0188BBD D D D	D 	Ds   B11B5	8B5	c                 z   t          j        d          }t          j                                        }t          d          }d|_        |                     t          j	        j
                  5  |                    ||           ddd           n# 1 swxY w Y   |                     |j        j        d           dS )z;Test the connection is cleaned up after an invalid responser.   r"  NzConnection not closed)r   r  r3   r   rJ  r   r   r5   r  r?  BadStatusLinerM  rf   r$  r%  )r&   r  r~  rp   s       r,   test_invalid_closedz HandlerTests.test_invalid_closed  s    #C((.4466o&&t{899 	' 	'OOD#&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	',.EFFFFFs   -BBBN)2rU   r$   rV   r  r  r  r  r  r  r  r  r  r  r%  r+  r.  r4  r;  rA  rY  re  ri  ru  ry  r  r  r  r  r  r  r  r  
skipUnlessr    r!   r  r  platformr  r  r  r  r   r  r  r  r  r  r)  r,  rW   r.   r,   r  r    s       >H >H >H@Z5 Z5 Z5x@C @C @CDE E E2  .) ) ),O O O(; ; ;J J J+ + +P P P6M M M.) ) ). . .&C C C2  "HR HR HRT> > >,: : :6 6 6> > >F F F(!M !M !MF< < <$# # #	# 	# 	#8 8 8 J J J. XD*EFF$ $ GF$. X13JKKD D LKDBG G G+2 +2 +2Z     "0A 0A 0AdM M MM M M#K #K #KJ6 6 66&6 &6 &6PD D D"G G G G Gr.   r  c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )	MiscTestsc                 n    |                      t          fd|j        D                                  d S )Nc              3   .   K   | ]}|j         k    V  d S r   )r  )rK  rH   handler_classs     r,   	<genexpr>z/MiscTests.opener_has_handler.<locals>.<genexpr>  s>       6 6 ! K=8 6 6 6 6 6 6r.   )rf   anyr-  )r&   r+  r3  s     `r,   opener_has_handlerzMiscTests.opener_has_handler  sW     6 6 6 6%+_6 6 6 6 6 	7 	7 	7 	7 	7r.   c                 &    G d dt           j        j                  } G d dt           j        j                  } G d dt           j        j                  }t           j        j        } |||          }|                     ||           |                     ||            || |                      }|                     ||           |                     ||            ||          }|                     ||            |            }|                     |t           j        j                    |t           j        j                  }|                     |t           j        j                    |t           j                                                  }|                     |t           j        j                    G d dt           j        j                  } |||          }|                     ||           |                     ||           d S )	Nc                       e Zd ZdS )2MiscTests.test_build_opener.<locals>.MyHTTPHandlerNr'  rW   r.   r,   MyHTTPHandlerr9     r)  r.   r:  c                       e Zd Zd ZdS )/MiscTests.test_build_opener.<locals>.FooHandlerc                     d S r   rW   r   s    r,   foo_openz8MiscTests.test_build_opener.<locals>.FooHandler.foo_open  r  r.   N)rU   r$   rV   r>  rW   r.   r,   
FooHandlerr<    #            r.   r?  c                       e Zd Zd ZdS )/MiscTests.test_build_opener.<locals>.BarHandlerc                     d S r   rW   r   s    r,   bar_openz8MiscTests.test_build_opener.<locals>.BarHandler.bar_open  r  r.   N)rU   r$   rV   rD  rW   r.   r,   
BarHandlerrB    r@  r.   rE  c                       e Zd ZdS )7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerNr'  rW   r.   r,   MyOtherHTTPHandlerrG  $  r)  r.   rH  )r3   r   r  BaseHandlerr  r6  )r&   r:  r?  rE  r  rs  rH  s          r,   test_build_openerzMiscTests.test_build_opener  sI   	 	 	 	 	FN6 	 	 		 	 	 	 	3 	 	 		 	 	 	 	3 	 	 	 ~2LZ00:...:... LZZ\\22:...:... L''=111 LNN6>#=>>>L3446>#=>>>L3355666>#=>>>	 	 	 	 	!; 	 	 	 L(:;;=111#566666r.   c                 .   d}dx}x}}d}t           j                            |||||          }|                     t	          |d                     |                     |j        d           |                     t	          |d                     |                     |j        d           d|j        d|j	        }|                     t          |          |           d|j        d|j	        d	}|                     t          |          |           dS )
z
        Issue 13211 reveals that HTTPError didn't implement the URLError
        interface even though HTTPError is a subclass of URLError.
        zsomething bad happenedNzContent-Length: 42rO   r_   zHTTP Error z: z<HTTPError >)r3   r   rQ  rf   r!  r#   rO   r_   r   r   rR   r  )r&   r   r`   r   r   r  rS   expected_errmsgs           r,   test_HTTPError_interfacez"MiscTests.test_HTTPError_interface+  s   
 'dR#l$$S$T2>>X..///%=>>>Y//000&:;;;;14377CS?333325(((CGGGDcO44444r.   c                     t           j                            dddd d           }|                     t	          |dd          d           |                     |j                                        t                     d S )Nr`   i  zMETHOD NOT ALLOWED	__notes__rW   )	r3   r   rQ  r#   rP  r  r   r=   r  )r&   rL  s     r,   test_gh_98778zMiscTests.test_gh_98778=  sh    L""5#/CT4PPK44b999adiikk511111r.   c                     g d}|D ](\  }}|                      t          |          |           )|                     t          t          d          f d S )N)r  )NNNr  r  )NNNr  rS  rT  )zjoe:password@proxy.example.com)Nrt   ru   r  )z#joe:password@proxy.example.com:3128)Nrt   ru   r  )zhttp://proxy.example.com/)r  NNr  )zhttp://proxy.example.com:3128/)r  NNr  )z&http://joe:password@proxy.example.com/r  rt   ru   r  )z*http://joe:password@proxy.example.com:3128)r  rt   ru   r  )z1ftp://joe:password@proxy.example.com/rubbish:3128)r  rt   ru   r  )z%http://joe:password@proxy.example.comrU  )z&http://user/name:password@localhost:22)r  	user/nameru   localhost:22)z&http://username:pass/word@localhost:22)r  username	pass/wordrW  )z'http://user/name:pass/word@localhost:22)r  rV  rY  rW  zfile:/ftp.example.com)r#   r   r5   r6   )r&   parse_proxy_test_casestcr|  s       r,   test_parse_proxyzMiscTests.test_parse_proxyB  sm    $"
 $"
 $"
N 3 	9 	9LB\"--x8888*l4KLLMMMMr.   c                     t                      }|                     t                    5 }|                    d           d d d            n# 1 swxY w Y   |                     t          |j                  d           d S )Ninvalidz5Unsupported digest authentication algorithm 'invalid')r   r5   r6   get_algorithm_implsr#   rR   	exception)r&   r~  excs      r,   test_unsupported_algorithmz$MiscTests.test_unsupported_algorithmo  s    +--z** 	3c''	222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3C	
 	
 	
 	
 	
s   AAAN)	rU   r$   rV   r6  rJ  rN  rQ  r\  rb  rW   r.   r,   r0  r0    ss        7 7 7*7 *7 *7X5 5 5$2 2 2
+N +N +NZ
 
 
 
 
r.   r0  c                   ~    e Zd Z G d d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S )RequestTestsc                       e Zd ZdZdS )RequestTests.PutRequestPUTN)rU   r$   rV   r   rW   r.   r,   
PutRequestrf  z  s        r.   rh  c                     t          d          | _        t          ddddi          | _        t          dd          | _        |                     d          | _        |                     dd          | _        d S )	Nhttp://www.python.org/~jeremy/r   zX-Testr  r^   HEAD)r   r  )r   r  postheadrh  put
force_postr   s    r,   setUpzRequestTests.setUp}  s    ;<<<"%-v$68 8 8	 <VLLL	??#CDD//*J *  r.   c                    |                      d| j                                                   |                      d| j                                                   |                      d| j                                                   |                      d| j                                                   |                      d| j                                                   d S )Nr  r  rk  rg  )r#   rl  rS  r  rm  rn  ro  r   s    r,   test_methodzRequestTests.test_method  s    !5!5!7!7888 3 3 5 5666!5!5!7!7888 3 3 5 5666!;!;!=!=>>>>>r.   c                 N   |                      | j        j                   |                     d| j                                                   d| j        _        |                     | j        j                   |                     d| j                                                   d S )Nr  r   r  )rk   r  r   r#   rS  rf   r   s    r,   	test_datazRequestTests.test_data  s    ''' 3 3 5 5666&&&!4!4!6!677777r.   c                     |                      d| j        j                   | j                            dd           |                     d| j        j        d                    d| j        _        |                      d| j        j                   d S )Nr  *   r   )r  r  r  ro   r#   r   r   s    r,   .test_setting_data_should_remove_content_lengthz;RequestTests.test_setting_data_should_remove_content_length  s    )48+EFFF(()92>>>TX78HIJJJ)48+EFFFFFr.   c                 .   |                      d| j        j                   d| j        _        | j                            dd           |                     d| j        j        d                    | j        `|                      d| j        j                   d S )Nr  r}   r  )r  r  r  r   ro   r#   r   s    r,   /test_deleting_data_should_remove_content_lengthz<RequestTests.test_deleting_data_should_remove_content_length  s    )48+EFFF(()91===DH67GHIIIHM)48+EFFFFFr.   c                 `    |                      d| j                                                   d S )Nrj  )r#   r  rB  r   s    r,   test_get_full_urlzRequestTests.test_get_full_url  s8    9..00	2 	2 	2 	2 	2r.   c                     |                      d| j        j                   t          d          }|                      d|j                   d S )Nz	/~jeremy/zhttp://www.python.org/r0   )r#   r  r   r   rN  s     r,   test_selectorzRequestTests.test_selector  sI    dh&7888.//cl+++++r.   c                 F    |                      d| j        j                   d S )Nr  )r#   r  r  r   s    r,   test_get_typezRequestTests.test_get_type  s"    /////r.   c                 F    |                      d| j        j                   d S )Nr0  )r#   r  r   r   s    r,   test_get_hostzRequestTests.test_get_host  s#    )48=99999r.   c                 Z    t          d          }|                     d|j                   d S )Nzhttp://www.%70ython.org/r0  r   r#   r   rN  s     r,   test_get_host_unquotez"RequestTests.test_get_host_unquote  s/    011)3844444r.   c                 l   |                      | j                                                   | j                            dd           |                     | j                                                   |                     d| j        j                   |                     d| j        j                   d S )Nr  r  r0  )rk   r  	has_proxyr  rf   r#   r>  r   r   s    r,   r  zRequestTests.test_proxy  s    ++--...>6222**,,---)48+CDDD77777r.   c                 Z    t          d          }|                     d|j                   d S )Nz<URL:http://www.python.org>r0  r  rN  s     r,   test_wrapped_urlzRequestTests.test_wrapped_url  s/    344)3844444r.   c                     t          d          }|                     d|j                   t          d          }|                     d|j                   d}t          |          }|                     |                                |           d S )N-http://www.python.org/?qs=query#fragment=truez
/?qs=queryzhttp://www.python.org/#fun=truer0   .http://docs.python.org/library/urllib2.html#OK)r   r#   r   rB  )r&   rp   r`   s      r,   test_url_fragmentzRequestTests.test_url_fragment  s    EFFs|444788cl+++ ?cll))++S11111r.   c                     g d}|D ]>}t          |          }|                     |                                |j                   ?d S )N)zhttp://docs.python.orgr  r  )r   r#   rB  r'  )r&   r  r`   rp   s       r,   test_url_fullurl_get_full_urlz*RequestTests.test_url_fullurl_get_full_url  sc    A A A  	? 	?C#,,CS--//>>>>	? 	?r.   N)rU   r$   rV   r   rh  rp  rr  rt  rw  ry  r{  r}  r  r  r  r  r  r  r  rW   r.   r,   rd  rd  y  s           W     ? ? ?8 8 8G G GG G G2 2 2, , ,
0 0 0: : :5 5 58 8 85 5 5	2 	2 	2? ? ? ? ?r.   rd  __main__)>r  r  r   test.supportr   r   r   r   r    r   r   r  r  r  r  urllib.requestr3   r   r	   r
   r   r   r   r   r   urllib.parser   rq  http.clientr  requires_working_socketTestCaser   rY   r   r   r  r   r   r   r   r   IOBaser   r   r   r/  r2  r   rI  r4  rJ  rF  rQ  rY  rc  r  r  r0  rd  rU   mainrW   r.   r,   <module>r     s5          " " " " " " & & & & & & ( ( ( ( ( (       				 				   



         7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 " ! ! ! ! !          t , , , ,1, 1, 1, 1, 1,8$ 1, 1, 1,hx' x' x' x' x'x( x' x' x'v, , , , , , , ,       # # # # #$ # # #
	 	 	 	 	2; 	 	 	3 3 3 3 3 3 3 3? ? ? ? ? ? ? ?    ry   $* * * * * * * *Z38 38 38 38 38 38 38 38l  D  H H H H Hfn0 H H H8	0 	0 	0 	0 	0v~9 	0 	0 	0Q Q Q Q Qv~9 Q Q Q&
( 
( 
( 
( 
( 
( 
( 
(NA NA NA NA NA(+ NA NA NAb	 	 	hG hG hG hG hG8$ hG hG hGV!}
 }
 }
 }
 }
! }
 }
 }
@^? ^? ^? ^? ^?8$ ^? ^? ^?B zHMOOOOO r.   