
    <e6                       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 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j/        j0                  Z1 G d% d&ej/        j2                  Z3 G d' d(ej/        j0                  Z4 G d) d*          Z5 G d+ d,e j                  Z6d- Z7 G d. d/e j                  Z8 G d0 d1e j                  Z9 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_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          (/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_trivial4   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)rB   rC   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"   r2   r   parse_http_list)r%   testsstringlists       r+   test_parse_http_listz!TrivialTests.test_parse_http_listG   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)r2   r   URLErrorassertInrN   str)r%   errs     r+   test_URLError_reasonstrz$TrivialTests.test_URLError_reasonstrR   s9    l##H--cj#c((+++++r-   N)__name__r#   __qualname__r,   r@   rL   rS    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_dictY   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
        rZ   r[   r\   r]   )r[   r\   zFoo-Barbaz)zFoo-barrb   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_methodsn   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3rD   zd.example.com4dze.example.com:31285eexample.com)rs   rt   rZ   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|   ru   )r}   r~   zhttp://example.com/foo/zhttp://example.com/foo/bazzhttp://example.com/foobarNNzhttp://example.com/baz/rb   nininizhttp://example.com/baz)rb   r   zhttp://example.com/baz/barrC   firstr\   secondspam)r   r   rB   1)r   rB   zhttp://a.example.com/zc.example.comzc.example.com:3128)rv   rD   )rw   rx   )ry   rz   r2   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jrF   zhttp://h.example.com11r)   rG   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)rT   r#   rU   r`   rp   r   r   rV   r-   r+   rX   rX   W   sX        A A A*"> "> ">HT% T% T%l'' '' '' '' ''r-   rX   c                   .    e Zd Zg Zdej        fdZd ZdS )
MockOpenerNc                 2    |||c| _         | _        | _        d S N)ro   datatimeout)r%   ro   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-   )rT   r#   rU   
addheaderssocket_GLOBAL_DEFAULT_TIMEOUTr   r   rV   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   rV   r%   counts     r+   r<   zMockFile.read      r-   c                     d S r   rV   r   s     r+   readlinezMockFile.readline   r   r-   c                     d S r   rV   r%   s    r+   closezMockFile.close#  r   r-   r   )rT   r#   rU   r<   r   r   rV   r-   r+   r   r     sK                  r-   r   c                       e Zd Zd ZdS )MockHeadersc                 D    t          |                                           S r   )rK   values)r%   r    s     r+   
getheaderszMockHeaders.getheaders(  s    DKKMM"""r-   N)rT   r#   rU   r   rV   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.info1  s
    |r-   c                     | j         S r   r_   r   s    r+   geturlzMockResponse.geturl4  	    xr-   r   )rT   r#   rU   r   r   r   rV   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_header9  s    r-   c                 $    ||c| _         | _        d S r   )ec_reqec_r)r%   r   r   s      r+   extract_cookieszMockCookieJar.extract_cookies<  s    !((TYYYr-   N)rT   r#   rU   r   r   rV   r-   r+   r   r   8  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__A  s    "r-   c                 4     | j         | j        | j        g|R  S r   )r   r   r   )r%   r   s     r+   __call__zFakeMethod.__call__F  s"    t{4>4;>>>>>r-   N)rT   r#   rU   r   r   rV   r-   r+   r   r   @  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   statusrN   r   )r%   r   r   r   rN   s        r+   r   zMockHTTPResponse.__init__K  s)    			r-   c                     dS )N rV   r   s    r+   r<   zMockHTTPResponse.readR  s    rr-   c                     i S r   rV   r   s    r+   r   zMockHTTPResponse.infoU  s    	r-   c                     | j         S r   r   r   s    r+   r   zMockHTTPResponse.geturlX  r   r-   N)rT   r#   rU   r   r<   r   r   rV   r-   r+   r   r   J  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__e  s    	r-   c                     || _         d S r   )r   )r%   r   s     r+   set_debuglevelzMockHTTPClass.set_debuglevelj  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_tunnelm  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.requestu  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   rV   r   s    r+   r   zMockHTTPClass.close  r   r-   r   )rT   r#   rU   r   r   r   r   r   r   r   r   r   rV   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intr5   r   r2   rO   )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   rV   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)
rT   r#   rU   r  r   r  r   r   r  r"  rV   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rT   r#   rU   rV   r-   r+   MockHandlerSubclassr%            Dr-   r'  r  )r   r  r  r  add_handler)opener	meth_spechandlersr   methsr'  rG   s          r+   add_ordered_mock_handlersr.    s    & HE 	 		 	 	 	 	+ 	 	 	  &&	5 	V	1Or-   c                  V    t                      }| D ]}|                    |           |S r   )r	   r)  )handler_instancesr*  rG   s      r+   build_test_openerr1    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+   r5  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copyr8  r  deepcopyr7  r  client	responsesr   message_from_stringr^   r  r   r   ro   r   get_full_url)r%   ro   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rT   r#   rU   r   r5  rB  rV   r-   r+   r3  r3    sF          
  H H H H Hr-   r3  c                       e Zd ZddZd ZdS )MockHTTPSHandlerr   c                 x    t           j        j                            | |           t	                      | _        d S )N
debuglevel)r2   r   AbstractHTTPHandlerr   r   httpconn)r%   rH  s     r+   r   zMockHTTPSHandler.__init__  s/    *33DZ3PPP%r-   c                 8    |                      | j        |          S r   )do_openrJ  r%   ro   s     r+   
https_openzMockHTTPSHandler.https_open  s    ||DM3///r-   N)r   )rT   r#   rU   r   rN  rV   r-   r+   rE  rE    s7        ( ( ( (0 0 0 0 0r-   rE  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 rR  )rS  r   s    r+   r5  zMockHTTPHandlerCheckAuth.reset  s    $r-   c                     |                     d          rd| _        t          j        j        | j                 }t          | j        |t                      d|                                          S )NAuthorizationTr   )	rf   rS  r  r>  r?  r   r   r   rA  )r%   ro   r    s      r+   rB  z"MockHTTPHandlerCheckAuth.http_open  sW    >>/** 	(#'D {$TY/DItXZZS=M=M=O=OPPPr-   NrC  rV   r-   r+   rP  rP    sF        % % %% % %Q Q Q Q Qr-   rP  c                       e Zd Zd Zd ZdS )MockPasswordManagerc                 >    || _         || _        || _        || _        d S r   )realmr_   userrt   )r%   rZ  urir[  rt   s        r+   r   z MockPasswordManager.add_password$  s"    
	 r-   c                 :    || _         || _        | j        | j        fS r   )target_realm
target_urlr[  rt   )r%   rZ  authuris      r+   r   z&MockPasswordManager.find_user_password*  s     !!y$-''r-   N)rT   r#   rU   r   r   rV   r-   r+   rX  rX  #  s2        ! ! !( ( ( ( (r-   rX  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&  rV   r-   r+   
NonHandlerre  3  r(  r-   rf  )objectr4   	TypeErrorr	   r)  )r%   rf  s     r+   test_add_non_handlerz(OpenerDirectorTests.test_add_non_handler2  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   )rO   )rL  r  )
proxy_openr  )redirect_requestr  )doproxyredirectz://example.com/)
urllib.errorrO   r	   r.  r)  r2   r   UnknownHandlerr4   r   )r%   rO   or+  schemes        r+   test_badly_named_methodsz,OpenerDirectorTests.test_badly_named_methods8  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)rB  ftp_openhttp_error_302rv  rB  r  rr   r  r   rB  )r	   r.  r   r   r"   zipr  )r%   rr  r+  r,  ro   rr  expectedgothandlerr    r   r  s                r+   test_handledz OpenerDirectorTests.test_handledM  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 )	Nrx  r   rB  r   c                       e Zd ZdS )COpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassNr&  rV   r-   r+   r'  r  l  s        r-   r'  rr   r  )r	   r   r  r  r)  r   r"   r  )r%   rr  r,  r-  r  r'  rG   s          r+   test_handler_orderz&OpenerDirectorTests.test_handler_orderg  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)rB  r  rx  rr   r   rB  )
r	   r.  r   r4   r2   r   rO   r   r"   r  )r%   rr  r+  r,  ro   s        r+   
test_raisezOpenerDirectorTests.test_raisey  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)rB  z	error 302)http_error_400r  rB  ))rw  r  http_error_303
http_errorrw  rr   r   r  .  r   )	r	   r.  r   r   r   	ALWAYS_EQry  r  r"   )r%   rr  r+  r,  ro   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  rr   r   r  r  r  r  )
r	   r.  r   r   	enumerater  r"   r  assertIsInstancer   )r%   rr  r+  r,  ro   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)
rT   r#   rU   ri  rt  r~  r  r  r  r  rV   r-   r+   rb  rb  0  s        F F FJ J J*+ + +45 5 5$L L L+ + +.!A !A !A !A !Ar-   rb  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SkipTestr2   r   r   r   r    r  )r7   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#d  Z$d! Z%d" Z&d# Z'd$ Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+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   rV   r   s    r+   r   z3HandlerTests.test_ftp.<locals>.MockFTPWrapper.close      r-   N)rT   r#   rU   r   r  r   rV   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)rT   r#   rU   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.gifContent-typeContent-length)r2   r   
FTPHandlerftplibr   r  FTP_PORTr   r   rv  r"   r[  r  r   r   gethostbynamer   r  r  r  r  r   getr  r  )r%   r  r  r   rG   r_   r   r   r[  r  type_r  r  mimetypero   rz  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D"!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%sr0   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:80r/   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.utilsr2   r   FileHandlerr   r  r   TESTFNr  r   r7   r8   r   r  gethostnamegaierrorr  r   writer   	file_openr   r<   r   r   statutils
formatdatest_mtimeremover"   getcwdr4   r   rO   assertIsro   typerj   )r%   r;  rG   rr  r  r  towriteurls	localaddrr_   r?   rz  r   r^   respurlstatsmodifiedr  ro   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 )Nrr   ))GETN)POSTs   blahFoor}   Spameggsr   r   r{   r   r/   ))
Connectionr   )r  r}   r  r  Tbadpostr  )r-   Nr   r  r  0z!application/x-www-form-urlencodedHostr|   rb   )#r2   r   rI  r   r  r   r   rn   r   rL  r<   r   r   r   r   r   r  __contains__r"   r   r   r   r   r   r   r   r4   r   rO   rh  do_request_r   r   assertNotInunredirected_hdrs)r%   rG   rr  r_   r   r   ro   r  rz  hdrsnewreqs              r+   	test_httpzHandlerTests.test_httpk  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rbrr   Transfer-encodingchunkedr  Content-Length   )r2   r   rI  r   r  tempfileNamedTemporaryFiler    r   r1   r   unlinkr   r   r  rl   r"   rj   rf   r  )	r%   rG   rr  file_obj	file_pathr?   ro   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 )Nrr   r  r  r  r  r  )r2   r   rI  r   r  r   BytesIOr   r  r"   rl   rj   rf   r  r   )r%   rG   rr  r  ro   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  )stdoutrr   r  r  r  )r2   r   rI  r   r  sys
executable
subprocessPopenPIPEr   r  r  r"   rl   r  )r%   rG   rr  cmdr^   procro   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   onerV   rV   r-   r+   iterable_bodyz;HandlerTests.test_http_body_iterable.<locals>.iterable_body  s      LLLLLr-   r     rr   r  r  r  )
r2   r   rI  r   r  r   r  r"   rl   r  )r%   rG   rr  r  r^   ro   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 )Nrr   rV   r  r  r  )r2   r   rI  r   r  r  r   r"   rl   rj   rf   )r%   rG   ro   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     rr   r  )r2   r   rI  r   r  arrayr   r  r"   r  rl   )r%   rG   rr  iterable_arrayr^   ro   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  rG  zhttps://www.example.com)r	   rE  r)  r   r"   _debuglevel)r%   rr  rG   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)
r2   r   rI  r   r  r   r  r"   r  	set_proxy)r%   rG   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#bashrZ   rZ   r   )r   full_urlr   r"   rA  fragmentquery)r%   r  rz  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&  rk   r'  r"   r   )r%   rz  s     r+   test_full_url_deleterz"HandlerTests.test_full_url_deleterU  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   )
r2   r   rI  r   r  r   r  r"   r   r   )r%   rG   	weird_urlro   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 )Nrr   r   r   r   r      Accepted   zPartial contenti  zBad gatewayr  )r2   r   HTTPErrorProcessorr   r  r   r   r  r  rj   r   rk   r"   r   r   )r%   rG   rr  r_   ro   rz  newrs          r+   test_errorszHandlerTests.test_errorso  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 )Nrr   r   r   r   r{   )r   r2   r   HTTPCookieProcessorr   r  r   r   r  r  r   r"   origin_req_hostrj   unverifiabler  r   r   )r%   cjrG   ro   rz  r  r9  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)rC  rD  r  c                 6    g | ]}|                                 S rV   )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  rF  rG  )rw  r   r   )rG   ro   r_   s      r+   ro  z,HandlerTests.test_redirect.<locals>.redirect  s@    S(**c6(*c):;;= = = = =r-   r{   )r=  r   r  rr   zhttp://example.com/%d) r2   r   HTTPRedirectHandlerr   r  getattrr   r   r   r   rh   rQ   r  rn   r   r   r   	HTTPErrorrP   assertIsNotNoner"   ro   rA  
get_methodAttributeErrorrj   r   r^   r  r  max_repeatsmax_redirections)r%   from_urlto_urlrG   rr  r   r   r   ro   r^   ro  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 )
NrB  )r  httpsr  )fileimapldapzexample.com/b.htmlz://r  zSecurity LoopholerG  That's fine)r2   r   rN  r   r  r   r   r   r   r4   r   rP  rw  r   r   r"   ro   rA  )r%   rV  valid_schemesinvalid_schemesschemeless_urlrG   rr  ro   rs  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 )NrB  z/b.htmlr  r^  rG  )r2   r   rN  r   r  r   r   r   r   r   urljoinrw  r   r   r"   ro   rA  )r%   rV  relative_urlrG   rr  ro   rc  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.cookiejarrj  test.test_http_cookiejarrk  r3  r2   r   HTTPDefaultErrorHandlerrN  r<  r1  r   rj   ro   rf   )	r%   rj  rk  r?  hhhdehhrhcprr  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,  )
r3  r2   r   ro  rN  r1  r   r"   r   strip)r%   redirected_urlrp  rq  rr  rr  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)r1   r2   r   r3   r  r>  rz  r   fakehttpr  iterr6   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)rL  
connectionbuflast_bufr   r  )r%   ro   results      r+   rB  z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_open6  sC    dos;; $ 3"-"6# # r-   N)rT   r#   rU   rB  rV   r-   r+   Handlerr  5  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   

rr   s   GET     )r2   r   HTTPHandlerbuild_openersubTestreprr   r  r  r   r  re   r  )
r%   r  r}  r*  rI   rG  r  r   r{  r   s
             r+   test_redirect_encodingz#HandlerTests.test_redirect_encoding2  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)rB  r  zhttp://acme.example.com/zacme.example.comr   rB  c                 "    g | ]}|d d         S r   r  rV   rJ  tups     r+   rL  z+HandlerTests.test_proxy.<locals>.<listcomp>e  s     :::3c!A#h:::r-   )dictr	   r2   r   ProxyHandlerr)  r.  r   r"   r   r   r  )r%   r'   rx   rr  phr+  r,  ro   s           r+   
test_proxyzHandlerTests.test_proxyU  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.orgr0  r/  r   environr	   r2   r   r  r  r)  r   r"   r   r   r%   rr  r  ro   s       r+   test_proxy_no_proxyz HandlerTests.test_proxy_no_proxyg  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  r0  r/  r  r  s       r+   test_proxy_no_proxy_allz$HandlerTests.test_proxy_no_proxy_allv  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  rZ  )rN  r  https://www.example.com/www.example.comr   rN  c                 "    g | ]}|d d         S r  rV   r  s     r+   rL  z1HandlerTests.test_proxy_https.<locals>.<listcomp>  s     666s#ac(666r-   )r	   r2   r   r  r  r)  r.  r   r"   r   r   r  )r%   rr  r  r+  r,  ro   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	   r2   r   r  r  r)  rE  r   rh   r"   r   rk   r   r   r  rJ  r   rP   rQ  rl   )r%   rr  r  https_handlerro   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-   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notinbypassz!expected bypass of %s to be FalseTtestz10.0.0.0/40z172.19.10.0/24z172.19.10.5z10.0.1.5)re   r   rj   )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)rZ  r^   z
r:    rV  !http://acme.example.com/protected)
r  r	   rX  r2   r   r
   joinr3  r)  _test_basic_auth)r%   r^   rZ  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  rV   )rJ  	challenges     r+   rL  z0HandlerTests.test_basic_auth.<locals>.<listcomp>  s0     4 4 4$ 8I77 4 4 4r-   )r  r   check_warningsUserWarning)
r%   rZ  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	   r2   r   r  r  r)  rX  ProxyBasicAuthHandlerr3  r  )r%   r*  r  r  r  rZ  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__   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)rT   r#   rU   r   r  rV   r-   r+   RecordingOpenerDirectorr    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  r2   r   HTTPDigestAuthHandlerhttp_error_401r%   r   r  s      r+   r  z^HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandler.http_error_401(  sa    ""8,,,4CD L>BL L LFJL L L L Lr-   NrT   r#   rU   r  rV   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  r2   r   r
   r  r  s      r+   r  z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401.  sa    ""7+++3B4 K=AK K KEIK K K K Kr-   Nr  rV   r-   r+   TestBasicAuthHandlerr  -  s(        K K K K Kr-   r  r  r  &WWW-Authenticate: Basic realm="%s"

rV  r  r  r  r  )r	   r2   r   r  r
   rX  r3  r)  r  r"   r  )
r%   r  r  r  r*  r  digest_handlerbasic_handlerrZ  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	   r2   r   r  r3  r)  r4   r5   r   )r%   r*  digest_auth_handlerr  s       r+   $test_unsupported_auth_digest_handlerz1HandlerTests.test_unsupported_auth_digest_handlerH  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	   r2   r   r
   r3  r)  r4   r5   r   )r%   r*  basic_auth_handlerr  s       r+   #test_unsupported_auth_basic_handlerz0HandlerTests.test_unsupported_auth_basic_handlerR  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"   rZ  r_   r[  rt   r   r^  r_  r  r8  rj   rf   bytesencodebytesrv  decoderl   r  r5  )r%   r*  r  auth_headerrZ  r  r  request_urlprotected_urlr  r[  rt   userpassauth_hdr_values                 r+   r  zHandlerTests._test_basic_auth\  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)rP  r   r
   r   re   r  rj   r	   r)  r   rS  )r%   r[  rt   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  rj   r	   r)  r3  r   re   rP  rS  )
r%   r[  rt   r  rZ  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  r2   r   rI  r   r   rL  r<   re   fakesockclosed)r%   transferr   headerr~  r}  ro   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  r2   r   rI  r   r   r4   r  r>  BadStatusLinerL  re   r  r  )r%   r~  r}  ro   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)/rT   r#   rU   r  r  r  r  r   r  r  r  r  r  r$  r*  r-  r3  r:  r@  rX  rd  rh  rt  rx  r  r  r  r  r  r  r  r  
skipUnlessr  platformr  r  r  r  r  r  r  r  r  r  r"  r%  rV   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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  )rJ  rG   handler_classs     r+   	<genexpr>z/MiscTests.opener_has_handler.<locals>.<genexpr>  s>       6 6 ! K=8 6 6 6 6 6 6r-   )re   anyr,  )r%   r*  r,  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&  rV   r-   r+   MyHTTPHandlerr2    r(  r-   r3  c                       e Zd Zd ZdS )/MiscTests.test_build_opener.<locals>.FooHandlerc                     d S r   rV   r   s    r+   foo_openz8MiscTests.test_build_opener.<locals>.FooHandler.foo_open  r  r-   N)rT   r#   rU   r7  rV   r-   r+   
FooHandlerr5    #            r-   r8  c                       e Zd Zd ZdS )/MiscTests.test_build_opener.<locals>.BarHandlerc                     d S r   rV   r   s    r+   bar_openz8MiscTests.test_build_opener.<locals>.BarHandler.bar_open  r  r-   N)rT   r#   rU   r=  rV   r-   r+   
BarHandlerr;    r9  r-   r>  c                       e Zd ZdS )7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerNr&  rV   r-   r+   MyOtherHTTPHandlerr@    r(  r-   rA  )r2   r   r  BaseHandlerr  r/  )r%   r3  r8  r>  r  rr  rA  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: 42rN   r^   zHTTP Error z: z<HTTPError >)r2   r   rP  re   r   r"   rN   r^   r   r   rQ   r  )r%   r   r_   r   r   r  rR   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__rV   )	r2   r   rP  r"   rO  r  r   r<   r  )r%   rK  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  rL  rM  )zjoe:password@proxy.example.com)Nrs   rt   r  )z#joe:password@proxy.example.com:3128)Nrs   rt   r  )zhttp://proxy.example.com/)r  NNr  )zhttp://proxy.example.com:3128/)r  NNr  )z&http://joe:password@proxy.example.com/r  rs   rt   r  )z*http://joe:password@proxy.example.com:3128)r  rs   rt   r  )z1ftp://joe:password@proxy.example.com/rubbish:3128)r  rs   rt   r  )z%http://joe:password@proxy.example.comrN  )z&http://user/name:password@localhost:22)r  	user/namert   localhost:22)z&http://username:pass/word@localhost:22)r  username	pass/wordrP  )z'http://user/name:pass/word@localhost:22)r  rO  rR  rP  zfile:/ftp.example.com)r"   r   r4   r5   )r%   parse_proxy_test_casestcr{  s       r+   test_parse_proxyzMiscTests.test_parse_proxy)  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   r4   r5   get_algorithm_implsr"   rQ   	exception)r%   r}  excs      r+   test_unsupported_algorithmz$MiscTests.test_unsupported_algorithmV  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)	rT   r#   rU   r/  rC  rG  rJ  rU  r[  rV   r-   r+   r)  r)    ss        7 7 7*7 *7 *7X5 5 5$2 2 2
+N +N +NZ
 
 
 
 
r-   r)  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)rT   r#   rU   r   rV   r-   r+   
PutRequestr_  a  s        r-   ra  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headra  put
force_postr   s    r+   setUpzRequestTests.setUpd  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  rd  r`  )r"   re  rR  r  rf  rg  rh  r   s    r+   test_methodzRequestTests.test_methodn  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  )rj   r  r   r"   rR  re   r   s    r+   	test_datazRequestTests.test_datau  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  rn   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   rn   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 )Nrc  )r"   r  rA  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/r/   )r"   r  r   r   rM  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 )Nr/  )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/r/  r   r"   r   rM  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  r/  )rj   r  	has_proxyr  re   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>r/  r|  rM  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=truer/   .http://docs.python.org/library/urllib2.html#OK)r   r"   r   rA  )r%   ro   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"   rA  r&  )r%   r  r_   ro   s       r+   test_url_fullurl_get_full_urlz*RequestTests.test_url_fullurl_get_full_url  sc    A A A  	? 	?C#,,CS--//>>>>	? 	?r-   N)rT   r#   rU   r   ra  ri  rk  rm  rp  rr  rt  rv  rx  rz  r}  r  r  r  r  rV   r-   r+   r]  r]  `  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-   r]  __main__)=r  r  r   test.supportr   r   r   r   r   r   r   r  r  r  r  urllib.requestr2   r   r	   r
   r   r   r   r   urllib.parser   rp  http.clientr  requires_working_socketTestCaser   rX   r   r   r  r   r   r   r   r   IOBaser   r   r   r.  r1  r   rB  r3  rI  rE  rP  rX  rb  r  r  r)  r]  rT   mainrV   r-   r+   <module>r     s+          " " " " " " & & & & & & ( ( ( ( ( (       				 				   



         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	 	 	PG PG PG PG PG8$ PG PG PGf }
 }
 }
 }
 }
! }
 }
 }
@^? ^? ^? ^? ^?8$ ^? ^? ^?B zHMOOOOO r-   