
    z%i=                    >   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	jJ                        Z& G d d      Z' G d d      Z( G d de	jR                        Z* G d d      Z+ G d d       Z,d! Z-d" Z. G d# d$ej^                  j`                        Z1 G d% d&ej^                  jd                        Z3 e4ejj                  d'      r G d( d)ej^                  jl                        Z7 G d* d+ej^                  jd                        Z8 G d, d-      Z9 G d. d/e j<                        Z:d0 Z; G d1 d2e j<                        Z< G d3 d4e j<                        Z= G d5 d6e j<                        Z>e?d7k(  r e j                          yy)8    N)support)	os_helper)warnings_helper)test_urllib)mock)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y)TrivialTestsc                    dD ]  }i }t        d|z  |       |d= |dk(  rmt        j                  dk(  rZ|j                  d      |j                  d      }}| j	                  |j
                  d       | j	                  |j
                  d       |j                         D ]7  \  }}| j	                  |j
                  d	|z  |d
|d|j
                         9  y )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          E/home/uftp/.pyenv/versions/3.12.0/lib/python3.12/test/test_urllib2.pytest___all__zTrivialTests.test___all__%   s    L 	/FG*V3W='"rww${{>2GKK4O1  |<  |< /1  {V/C.//	/    c                 "   | j                  t        j                  j                         | j	                  t
        t        j                  j                  d       t        j                  j                  t        j                  j                        j                  t        j                  d      }t        j                  dk(  rd|z  }nd|z  }t        j                  j                  |      5 }|j                          d d d        y # 1 sw Y   y xY w)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12*fnn&<&<kJ  7 78@@M77d?#e+H"U*H^^##H- 	FFH	 	 	s   +DDc                     dg dfdddgfdg dfdg d	fg}|D ]4  \  }}| j                  t        j                  j                  |      |       6 y )
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   sk    o&),?+KL*8://12 " 	KLFDV^^;;FCTJ	Kr-   c                     t         j                  j                  d      }| j                  |j                  t        |             y )Nreason)r2   r   URLErrorassertInrN   str)r%   errs     r+   test_URLError_reasonstrz$TrivialTests.test_URLError_reasonstrR   s,    ll##H-cjj#c(+r-   N)__name__r#   __qualname__r,   r@   rL   rS    r-   r+   r   r   #   s    /&	K,r-   r   c                   $    e Zd Zd Zd Zd Zd Zy)RequestHdrsTestsc                     d}| j                  t        |ddi      j                  d   d       | j                  t        |ddi      j                  d   d       y)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   si     #*5v)>##*7;89?	A 	*5v)>##*7;89?	Ar-   c                    d}t        |ddi      }| j                  |j                  d             | j                  |j	                         dg       |j                  dd       | j                  t        |j	                               ddg       | j                  |j                  d	             | j                  |j                  d	             | j                  |j                  d	d
      d
       |j                  d       | j                  |j                  d             |j                  dd       | j                  |j                  d             |j                  d       | j                  |j                  d             y)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{34))+.C-DEy%( 0 0 23,.CD	F45#..56Y?K+&45##$7@':;<-.(;<=r-   c                    t         j                  j                         }|j                  }|j                  } |dddd        |dddd        |dddd	        |dd
dd        |dddd       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d        |d	ddd        |d	ddd        |d	ddd       | j                   |d	d      d       | j                   |d	d      d       | j                   |d	d      d       | j                   |d	d      d       | j                   |d	d       d       | j                   |d	d!      d"        |d	d#d$d%       | j                   |d	d&      d"       | j                   |d	d#      d'       | j                   |d	d(      d'        |d)dd*d+        |d)dd,d-       | j                   |d)d      d.        |d/dd0d/       | j                   |d/d      d1       | j                   |d/d2      d"       | j                   |dd3      d"       | j                   |dd4      d5       | j                   |dd      d5       | j                   |dd
      d6       | j                   |dd      d7       y )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`   nn,,.//L/
CL14>L5sC@L/34L.S9 	mD,	.6MN,	.6JK,	.6KL,	.(AC,	. 	(FH,	. 	C)5$7C)5&9C-xA-EF&	(-EF(	*-FG&	(-IJ&	(-IJ&	(-HI%	' 	C*E8<-EF%	'-FG*	,-IJ*	,
 	C&8C&&9-BC+	-
 	C%sC0-BC#	% 	-DE%	'
 	oF%	'6JK#	%<)DE	 	oF#	%6JK#	%r-   c                    t         j                  j                         }|j                  }|j                  } |dddd        |dddd        |d	d
dd        |dddd       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |dd      d       | j                   |d	d      d       | j                   |d	d
      d       | j                   |d	d      d       | j                   |dd      d       | j                   |dd       d       | j                   |dd!      d       | j                   |dd"      d       y#)$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    nn,,.//C*D#6C's3C#T3/C$,-@A%	'-?@$	&_=%	'-GH%	'-FG$	&-CD$	&_={K-?@+N-FG$	&_=|L-?@+N-FG$	&_={K-?@%	'-CD$	&-FG%	'r-   N)rT   r#   rU   r`   rp   r   r   rV   r-   r+   rX   rX   W   s    A*">HT%l''r-   rX   c                   6    e Zd Zg Zdej
                  fdZd Zy)
MockOpenerNc                 0    |||c| _         | _        | _        y N)ro   datatimeout)r%   ro   r   r   s       r+   openzMockOpener.open  s    ,/w)$)T\r-   c                 "    ||c| _         | _        y r   )protoargs)r%   r   r   s      r+   r   zMockOpener.error  s     %t
DIr-   )rT   r#   rU   
addheaderssocket_GLOBAL_DEFAULT_TIMEOUTr   r   rV   r-   r+   r   r     s    J!6+I+I ?,r-   r   c                   "    e Zd ZddZddZd Zy)MockFileNc                      y r   rV   r%   counts     r+   r<   zMockFile.read      r-   c                      y r   rV   r   s     r+   readlinezMockFile.readline   r   r-   c                      y r   rV   r%   s    r+   closezMockFile.close#  r   r-   r   )rT   r#   rU   r<   r   r   rV   r-   r+   r   r     s    r-   r   c                       e Zd Zd Zy)MockHeadersc                 4    t        | j                               S r   )rK   values)r%   r    s     r+   
getheaderszMockHeaders.getheaders(  s    DKKM""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y)MockResponseNc                     t         j                  j                  | |       ||||f\  | _        | _        | _        | _        y r   )ioStringIO__init__codemsgr^   r_   )r%   r   r   r^   r   r_   s         r+   r   zMockResponse.__init__-  s5    
T4(6:C#6M3	48T\48r-   c                     | j                   S r   r]   r   s    r+   infozMockResponse.info1  s    ||r-   c                     | j                   S r   r_   r   s    r+   geturlzMockResponse.geturl4      xxr-   r   )rT   r#   rU   r   r   r   rV   r-   r+   r   r   ,  s    Nr-   r   c                       e Zd Zd Zd Zy)MockCookieJarc                     || _         y r   )ach_req)r%   r   s     r+   add_cookie_headerzMockCookieJar.add_cookie_header9  s	    r-   c                 "    ||c| _         | _        y r   )ec_reqec_r)r%   r   r   s      r+   extract_cookieszMockCookieJar.extract_cookies<  s    !((TYr-   N)rT   r#   rU   r   r   rV   r-   r+   r   r   8  s    3r-   r   c                       e Zd Zd Zd Zy)
FakeMethodc                 .    || _         || _        || _        y r   )	meth_namehandleaction)r%   r   r   r   s       r+   r   zFakeMethod.__init__A  s    "r-   c                 R     | j                   | j                  | j                  g| 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   @  s    
?r-   r   c                   $    e Zd Zd Zd Zd Zd Zy)MockHTTPResponsec                 J    || _         || _        || _        || _        d| _        y )N   )fpr   statusrN   r   )r%   r   r   r   rN   s        r+   r   zMockHTTPResponse.__init__K  s%    	r-   c                      y)N rV   r   s    r+   r<   zMockHTTPResponse.readR  s    r-   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  s    r-   r   c                   X    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y)MockHTTPClassc                 X    d| _         g | _        d | _        d| _        d | _        i | _        y )Nr   F)levelreq_headersr   raise_on_endheaderssock_tunnel_headersr   s    r+   r   zMockHTTPClass.__init__]  s/    
	#( 	!r-   c                 "    || _         || _        | S r   )hostr   )r%   r   r   s      r+   r   zMockHTTPClass.__call__e  s    	r-   c                     || _         y r   )r   )r%   r   s     r+   set_debuglevelzMockHTTPClass.set_debuglevelj  s	    
r-   Nc                 h    || _         || _        |r|| _        y | j                  j                          y r   )_tunnel_host_tunnel_portr   clear)r%   r   portr^   s       r+   
set_tunnelzMockHTTPClass.set_tunnelm  s/      #*D   &&(r-   F)encode_chunkedc                    || _         || _        |#| xj                  |j                         z  c_        | j                  j	                          |r|| _        || _        | j                  r
t               y r   )	methodselectorr   r$   sortr   r   r   OSError)r%   r   r_   bodyr^   r   s         r+   r   zMockHTTPClass.requestu  sf    /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                      y 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   \  s8    " &,%C%C 
)$;r-   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	MockHandler  c                 &    | j                  |       y r   )_define_methods)r%   methodss     r+   r   zMockHandler.__init__  s    W%r-   c                     |D ]H  }t        |      dk(  r|\  }}n|d }}t        ||| j                        }t        | j                  ||       J y )N   )lenr   r   setattr	__class__)r%   r  specr    r   meths         r+   r  zMockHandler._define_methods  sP     	0D4yA~#f#TfdFDKK8DDNND$/	0r-   c                    | j                   j                  j                  | |||f       |y |dk(  r| S |dk(  rt        ddi d      }|S |dk(  rt	        d      S |j                  d      rT||j                  d	      d
z   d  }	 t        |      }t        ddi d      }| j                   j                  d|d   ||di       S |dk(  rt        j                  j                  d      J # t        $ r Y bw xY w)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  s
     $t!<=>}$K((sD"b1CJ''>**w'&,,s+A-./D4y sD"b1C;;$$VT!Wc4RHHw,,''//  s   C$ $	C0/C0c                      y r   rV   r   s    r+   r   zMockHandler.close  r   r-   c                 4    || _         g | j                   _        y r   )r  r  )r%   r  s     r+   
add_parentzMockHandler.add_parent  s    r-   c                 N    t        |d      sy| j                  |j                  k  S )Nhandler_orderT)hasattrr  )r%   others     r+   __lt__zMockHandler.__lt__  s&    uo.!!E$7$777r-   N)
rT   r#   rU   r  r   r  r   r   r  r"  rV   r-   r+   r   r     s'     M&0.8r-   r   c                     g }d}|D ]f  } G d dt               } ||      }|xj                  |z  c_        |j                  |        |dz   }|j                  |       | j	                  |       h |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y)6add_ordered_mock_handlers.<locals>.MockHandlerSubclassNrT   r#   rU   rV   r-   r+   MockHandlerSubclassr%        r-   r'  r  )r   r  r  r  add_handler)opener	meth_spechandlersr   methsr'  rG   s          r+   add_ordered_mock_handlersr.    sy    & HE 		+ 	  &	5 	V	1	 Or-   c                  J    t               }| D ]  }|j                  |        |S r   )r	   r)  )handler_instancesr*  rG   s      r+   build_test_openerr1    s,    F 1Mr-   c                   &     e Zd Zd fd	Zd Z xZS )MockHTTPHandlerc                 L    t         t        |   |       t               | _        y )N
debuglevel)superr3  r   r   httpconn)r%   r6  r  s     r+   r   zMockHTTPHandler.__init__  s    ot--D%r-   c                 :    | j                  | j                  |      S r   do_openr8  r%   ro   s     r+   	http_openzMockHTTPHandler.http_open  s    ||DMM3//r-   r   )rT   r#   rU   r   r=  __classcell__r  s   @r+   r3  r3    s    (0r-   r3  c                       e Zd Zd Zd Zd Zy)MockHTTPHandlerRedirectc                 @    || _         || _        | j                          y r   )r   r^   reset)r%   r   r^   s      r+   r   z MockHTTPHandlerRedirect.__init__  s    	

r-   c                      d| _         g | _        y )Nr   )_countrequestsr   s    r+   rC  zMockHTTPHandlerRedirect.reset  s    r-   c                    dd l }dd l}| j                  j                  |j	                  |             | j
                  dk(  r| j
                  dz   | _        t        j                  j                  | j                     }|j                  | j                        }| j                  j                  d|t               | j                  ||      S || _        |j                  d      }t!        dd|d|j#                               S )Nr   r  r  

r   r   r   )emailcopyrF  r  deepcopyrE  r  client	responsesr   message_from_stringr^   r  r   r   ro   r   get_full_url)r%   ro   rI  rJ  r    r   s         r+   r=  z!MockHTTPHandlerRedirect.http_open   s    T]]3/0;;!++/DK;;((3D++DLL9C;;$$XZD#? ? DH++J7CT3C4D4D4FGGr-   NrT   r#   rU   r   rC  r=  rV   r-   r+   rA  rA    s    
Hr-   rA  HTTPSConnectionc                   &     e Zd Zd fd	Zd Z xZS )MockHTTPSHandlerc                 N    t         t        |   |||       t               | _        y r   )r7  rS  r   r   r8  )r%   r6  r&   check_hostnamer  s       r+   r   zMockHTTPSHandler.__init__  s     "D2:wW)ODMr-   c                 :    | j                  | j                  |      S r   r:  r<  s     r+   
https_openzMockHTTPSHandler.https_open  s    <<s33r-   )NNN)rT   r#   rU   r   rW  r>  r?  s   @r+   rS  rS    s    	,	4r-   rS  c                       e Zd Zd Zd Zd Zy)MockHTTPHandlerCheckAuthc                      || _         d| _        y NF)r   has_auth_header)r%   r   s     r+   r   z!MockHTTPHandlerCheckAuth.__init__   s    	$r-   c                     d| _         y r[  )r\  r   s    r+   rC  zMockHTTPHandlerCheckAuth.reset$  s
    $r-   c                     |j                  d      rd| _        t        j                  j                  | j
                     }t        | j
                  |t               d|j                               S )NAuthorizationTr   )	rf   r\  r  rL  rM  r   r   r   rO  )r%   ro   r    s      r+   r=  z"MockHTTPHandlerCheckAuth.http_open'  sP    >>/*#'D {{$$TYY/DIItXZS=M=M=OPPr-   NrP  rV   r-   r+   rY  rY    s    %%Qr-   rY  c                       e Zd Zd Zd Zy)MockPasswordManagerc                 <    || _         || _        || _        || _        y r   )realmr_   userrt   )r%   rc  urird  rt   s        r+   r   z MockPasswordManager.add_password0  s    
	 r-   c                 N    || _         || _        | j                  | j                  fS r   )target_realm
target_urlrd  rt   )r%   rc  authuris      r+   r   z&MockPasswordManager.find_user_password6  s$    !!yy$--''r-   N)rT   r#   rU   r   r   rV   r-   r+   ra  ra  /  s    !(r-   ra  c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	OpenerDirectorTestsc                 ~     G d dt               }| j                  t        t               j                   |              y )Nc                       e Zd Zy)<OpenerDirectorTests.test_add_non_handler.<locals>.NonHandlerNr&  rV   r-   r+   
NonHandlerrn  ?  r(  r-   ro  )objectr4   	TypeErrorr	   r)  )r%   ro  s     r+   test_add_non_handlerz(OpenerDirectorTests.test_add_non_handler>  s/    	 	)(*66
	Fr-   c                     ddl m} t               }ddgdgg}t        ||       |j	                  t
        j                  j                                dD ]"  }| j                  ||j                  |dz          $ y )Nr   )rO   )r;  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_methodsD  sw     	*')FG01	 	"!Y/	fnn3356/ 	JFh7H0HI	Jr-   c                 \   t               }g ddgdgdgg}t        ||      }t        d      }|j                  |      }| j	                  ||d          |d   df|d   dfg}t        ||j                        D ]3  \  }}|\  }	}
}}| j	                  |	|
f|       | j	                  ||f       5 y )N)r=  ftp_openhttp_error_302r  r=  r  rr   r  r   r=  )r	   r.  r   r   r"   zipr  )r%   r{  r+  r,  ro   rr  expectedgothandlerr    r   r  s                r+   test_handledz OpenerDirectorTests.test_handledY  s    7L)*)*		 -Q	:+,FF3K 	HQK(1+{+hqk;-GH 0 	+MHc(+%GT4gt_h7TC6*	+r-   c                 v   t               }g }dgdfdgdffD ]E  \  }} G d dt              } ||      }||_        |j                  |       |j	                  |       G |j                  d       | j                  |j                  d   d   |d          | j                  |j                  d   d   |d          y )	Nr  r   r=  r   c                       e Zd Zy)COpenerDirectorTests.test_handler_order.<locals>.MockHandlerSubclassNr&  rV   r-   r+   r'  r  x  s    r-   r'  rr   r  )r	   r   r  r  r)  r   r"   r  )r%   r{  r,  r-  r  r'  rG   s          r+   test_handler_orderz&OpenerDirectorTests.test_handler_orders  s    'C&Dc%J'2mQ%7%9 	 E=k  $E*A+AOOOAMM!	 	
$%A4A4r-   c                    t               }dgdgg}t        ||      }t        d      }| j                  t        j
                  j                  |j                  |       | j                  |j                  |d   d|fi fg       y )N)r=  r  r  rr   r   r=  )
r	   r.  r   r4   r2   r   rO   r   r"   r  )r%   r{  r+  r,  ro   s        r+   
test_raisezOpenerDirectorTests.test_raise  s|    #$)*	 -Q	:+,&,,//=HQKsfb#I"JKr-   c                    t               }dgddgg ddgg}t        ||      }t        d      }|j                  |       t	        |j
                        dk(  sJ |d   d|ff|d   d|t        j                  d	d
i ffg}t        ||j
                        D ]7  \  }}|\  }}	}
| j                  ||	f|d d        | j                  |
|d          9 y )N)r=  z	error 302)http_error_400r  r=  ))r  r  http_error_303
http_errorr  rr   r  r   .  r   )
r	   r.  r   r   r  r  r   	ALWAYS_EQr  r"   )r%   r{  r+  r,  ro   r  r  r  r  method_namer   s              r+   test_http_errorz#OpenerDirectorTests.test_http_error  s     '((+6 	 -Q	:+,	s177|q  1+{SF31+/))3B79: !0 	+MHc)1&G[$g{3S!W=T3q6*	+r-   c                 f   t               }ddgddgg}t        ||      }t        d      }|j                  |       |d   df|d   df|d   df|d   dfg}t	        |j
                        D ]  \  }\  }}}	}
|dk  rL| j                  ||f||          | j                  t        |	      d       | j                  |	d   t               \| j                  ||f||          | j                  t        |	      d       | j                  |	d   t               |	d   | j                  |	d   t                y )	N)http_requestr  )http_responser  rr   r   r  r  r  r  )
r	   r.  r   r   	enumerater  r"   r  assertIsInstancer   )r%   r{  r+  r,  ro   r  r   r  r    r   r  s              r+   test_processorsz#OpenerDirectorTests.test_processors  sV   /13/13	 -Q	:+,	s a[.)HQK+Ha[/*Xa[/,JL /8.@ 	A*A*tT1u  '4%(;  TA.%%d1gw7   '4%(;  TA.%%d1gw7 7&))$q'<@	Ar-   N)
rT   r#   rU   rr  r}  r  r  r  r  r  rV   r-   r+   rk  rk  <  s)    FJ*+45$L+.!Ar-   rk  c                     	 | j                  d       t        j
                  j                  |       }t        j                  dk(  r|j                  d      r|dd  }|S # t        $ r t        j                  d      w xY w)Nzutf-8zpath is not encodable to utf8r   z///r  )
encodeUnicodeEncodeErrorunittestSkipTestr2   r   r   r   r    r  )r7   urlpaths     r+   sanepathname2urlr    sw    AG nn))$/G	ww$7--e4!"+N  A ?@@As   A A<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 ej                   eej$                  d      d      d        Z ej                   eej$                  d      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 Z d Z!d Z"d Z#d Z$d  Z% ej                   eej$                  d      d      d!        Z& ej                  e'jP                  d"k(  d#      d$        Z)d% Z*d& Z+d' Z,d( Z-d) Z.d* Z/d+ Z0d, Z1d- Z2d. Z3d/ Z4y0)1HandlerTestsc                     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 ]s  \	  }}}}}	}
}}}t        |      }d |_        |j                  |      }| j                  |j                  |       | j                  |j                  |	       | j                  |j                  t        j                  |             | j                  |j                   |       | j                  |j"                  |       | j                  |j$                  j&                  |       | j                  |j$                  j(                  |
       |j+                         }| j                  |j-                  d      |       | j                  t/        |d         t1        |             v y )Nc                       e Zd Zd Zd Zd Zy)-HandlerTests.test_ftp.<locals>.MockFTPWrapperc                     || _         y r   r   r%   r   s     r+   r   z6HandlerTests.test_ftp.<locals>.MockFTPWrapper.__init__  	     	r-   c                     ||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  s2    /7,t}{{499-s499~==r-   c                      y r   rV   r   s    r+   r   z3HandlerTests.test_ftp.<locals>.MockFTPWrapper.close      r-   N)rT   r#   rU   r   r  r   rV   r-   r+   MockFTPWrapperr    s    !>r-   r  c                   6    e Zd Zd Zej
                  f fd	Zy)-HandlerTests.test_ftp.<locals>.NullFTPHandlerc                     || _         y r   r  r  s     r+   r   z6HandlerTests.test_ftp.<locals>.NullFTPHandler.__init__  r  r-   c                     ||c| _         | _        ||c| _        | _        || _         | j
                        | _        | j                  S r   )rd  passwdr   r   dirsr   
ftpwrapper)r%   rd  r  r   r   r  r   r  s          r+   connect_ftpz9HandlerTests.test_ftp.<locals>.NullFTPHandler.connect_ftp  sD    )-v&	4;'+T$	49 	"0";&r-   N)rT   r#   rU   r   r   r   r  )r  s   r+   NullFTPHandlerr    s    ! %+$B$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   r  r"   rd  r  r   r   gethostbynamer   r  r  r  r  r   getr  r  )r%   r  r  r   rG   r_   r   r   rd  r  type_r  r  mimetypero   r  r^   r  s                    @r+   test_ftpzHandlerTests.test_ftp  s=   		 		
	'V^^66 
	' 	!4 < 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JCtT65$(( #,CCK

3AQVVT*QXXv.QVVV%9%9$%?@QVVT*QVVT*Q\\22H=Q\\22E:ffhGW[[8(CS)9!:;SYGC!	Hr-   c           	         dd l }t        j                  j                         }t	               x}|_        t        j                  }t        t        j                  j                  |            }d}d|z  d|z  dt        j                  d      |g}	 t        j                  t        j                               }|r|j!                  d||       |D ]A  }	t#        |d      }
	 	 |
j%                  |       |
j'                          |j)                  t+        |	            }	 |j-                         }|j/                         }|j1                         }|j'                          t        j2                  |      }|j4                  j7                  |j8                  d	
      }t        j:                  |       | j=                  ||       | j=                  |d   d       | j=                  |d   d       | j=                  |d   |       | j=                  ||	       D d|z  dddt        j                  d      dt        j>                         d|dt        j>                         d|fD ]  }		 t#        |d      }
	 |
j%                  |       |
j'                          | jA                  t        jB                  jD                  |j(                  t+        |	             t        j:                  |        t        j                  j                         }t	               x}|_        dD ]y  \  }	}t+        |	      }	 |j)                  |       | jG                  |jH                  |       | j=                  |jJ                  d       | j=                  |jJ                  dk(  |       { y # t        j                  $ r d}Y w xY w# |
j'                          w xY w# |j'                          w xY w# t        j:                  |       w xY w# |
j'                          w xY w# t        j:                  |       w xY w# t        jB                  jD                  $ r | jM                  |       Y w xY w)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%   rI  rG   r{  r  r  towriteurls	localaddrr_   r?   r  r   r^   respurlstatsmodifiedr  ro   s                      r+   	test_filezHandlerTests.test_file  s   NN&&(!|#AH!!"277??6#:;# 7*'!#11+>H
	,,V-?-?-ABI KKG<= 	+CVT"A"GGG$GGIKK-668DffhGhhjGGGI ;;11%..1N		&!T7+W^4lCW%56=W_5x@Wc*/	+4 $g--7#)#7#7#D#%99;8 YY[&" 	"C
"&GGG$GGI!!&,,"7"7"#++ws|= 		&!'	"* NN&&(!|#AH 	5HC #,C2C  aeeS)  51SXX.4	5y  	I	 GGI GGI 		&!* GGI
 		&!* <<(( &  %&s   'N %N96*O#!0OAO#,P9O=
AP2P,N65N69OO#O  O##O:=PPP),.QQc                    t         j                  j                         }t               x}|_        d}dD ]  \  }}t        ||ddi      }d |_        |j                  dd       t               }|j                  ||      }|j                   |j                   |j                   |j                   |j                  |j                  dk(  df |j                         }	|	j                    |	j"                   | j%                  |j                         |       | j%                  |j&                  d	       | j%                  |j(                  d
       | j%                  |j*                  |       | j%                  |j,                  d       | j%                  |j.                  g d       | j%                  |j0                  |        d_        | j5                  t         j6                  j8                  |j                  |       t        dd      }| j5                  t:        |j<                  |       dg|_        dD ]  }t        d|      }tA        ddi d      }|j=                  |      }
|9| jC                  d|jD                         | jC                  d|jD                         n>| j%                  |jD                  d   d       | j%                  |jD                  d   d       | j%                  |jD                  d   d	       | j%                  |jD                  d   d       |j                  dd       |j                  dd       |j                  dd       |j                  dd       |j=                  |      }
| j%                  |jD                  d   d       | j%                  |jD                  d   d       | j%                  |jD                  d   d       | j%                  |jD                  d   d        y )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   AbstractHTTPHandlerr   r  r   r   rn   r   r;  r<   r   r   r   r   r   r  __contains__r"   r   r   r   r   r   r   r   r4   r   rO   rq  do_request_r   r   assertNotInunredirected_hdrs)r%   rG   r{  r_   r   r   ro   r  r  hdrsnewreqs              r+   	test_httpzHandlerTests.test_httpw  s[   NN..0!|#AH#> 	.LFD#teU^4CCK''7 ?D		$$A FFAJJFFAHHFFAEESL$&668DHHd''QXXZ-TYY6TZZ+T[[&1T]]C0T--@A TYY--	.2 $( &,,//D#F +I6)Q]]C8 )) 	CD/6CS$B/A]]3'F|  !133H3HI  1F1FG  !6!67G!H#N  !6!6~!F@B S226:MJS226:FC ''(8%@''>''6''6]]3'FS223CDeLS22>BEJS226:EBS226:EB1	Cr-   c                 "   t         j                  j                         }t               x}|_        t        j                  dd      }|j                  }|j                          | j                  t        j                  |       t        |d      5 }t        d|i       }|j                  |      }|j                  d      }| j!                  |d       | j#                  |j%                  d             d d d        t        |d      5 }t        d|d	d
i      }|j                  |      }| j!                  t'        |j                  d            d
       | j#                  |j%                  d             d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nzw+bF)modedeleterbrr   Transfer-encodingchunkedr  Content-Length   )r2   r   r  r   r  tempfileNamedTemporaryFiler    r   r1   r   unlinkr   r   r  rl   r"   rj   rf   r  )	r%   rG   r{  file_obj	file_pathr?   ro   r  tes	            r+   test_http_body_filez HandlerTests.test_http_body_file  sc    NN..0!|#AH..E%HMM			9-)T" 	Ba/B7C]]3'F""#67BR+V../?@A	B )T" 	Ea/5Er4JKC]]3'FS!2!23C!DErJV../BCD		E 	E	B 	B	E 	Es   A"E9A+F9FFc                 @   t         j                  j                         }t               x}|_        t        j                         }t        d|i       }|j                  |      }| j                  |j                  d      d       | j                  |j                  d             ddi}t        d||      }|j                  |      }| j                  t        |j                  d            d       | j                  |j                  d             |j                          y )Nrr   r  r  r  r  r   )r2   r   r  r   r  r   BytesIOr   r  r"   rl   rj   rf   r  r   )r%   rG   r{  r  ro   r  r^   s          r+   test_http_body_fileobjz#HandlerTests.test_http_body_fileobj  s     NN..0!|#AH::<+Xr:s#**+>?K**+;<=#R(+Xw?s#V../?@A2F**+>?@r-   c           	      @   t         j                  j                         }t               x}|_        t
        j                  ddg}i ddifD ]  }t        j                  |t        j                        5 }t        d|j                  |      }|j                  |      }|sC| j                  |j                  d      d        | j                  |j                  d      d	       n*| j                  t        |j                  d            d       d d d         y # 1 sw Y   xY w)
Nz-cpassr  r   )stdoutrr   r  r  r  )r2   r   r  r   r  sys
executable
subprocessPopenPIPEr   r  r  r"   rl   r  )r%   rG   r{  cmdr^   procro   r  s           r+   test_http_body_pipez HandlerTests.test_http_body_pipe  s    NN..0!|#AH~~tW-,b11 
	)G!!#joo> 	)$3T[['Js+$$V%6%67G%H$O$$V%6%67J%K%.0 $$S):):;K)L%M%')	) 	)
	)	) 	)s   1BDD	c                    t         j                  j                         }t               x}|_        d }i ddifD ]  }t        d |       |      }|j                  |      }|sC| j                  |j                  d      d        | j                  |j                  d      d       k| j                  t        |j                  d            d        y )Nc               3      K   d y w)Ns   onerV   rV   r-   r+   iterable_bodyz;HandlerTests.test_http_body_iterable.<locals>.iterable_body  s     Ls   r     rr   r  r  r  )
r2   r   r  r   r  r   r  r"   rl   r  )r%   rG   r{  r  r^   ro   r  s          r+   test_http_body_iterablez$HandlerTests.test_http_body_iterable  s     NN..0!|#AH	 ,b11 	OG/'JC]]3'F  !2!23C!DdK  !2!23F!G!*,   V%6%67G%H!I2N	Or-   c                    t         j                  j                         }t               |_        |j                  t        dd            }| j                  |j                  d      d       | j                  |j                  d             y )Nrr   rV   r  r  r  )r2   r   r  r   r  r  r   r"   rl   rj   rf   )r%   rG   ro   s      r+   test_http_body_empty_seqz%HandlerTests.test_http_body_empty_seq  se    NN..0<mmG$92>?(;<iH(89:r-   c                 8   t         j                  j                         }t               x}|_        t        j
                  dg d      }i ddifD ]J  }t        d||      }|j                  |      }| j                  t        |j                  d            d       L y )Nr  )r  r        r     rr   r  )r2   r   r  r   r  arrayr   r  r"   r  rl   )r%   rG   r{  iterable_arrayr^   ro   r  s          r+   test_http_body_arrayz!HandlerTests.test_http_body_array  s     NN..0!|#AHS3,b11 	JG/IC]]3'FS!2!23C!DEbI	Jr-   c                 D   t         j                  j                  t        j                  j
                  dd      5  t               }t               }|j                  |       |j                  d       | j                  |j                  d       d d d        y # 1 sw Y   y xY w)Nr6     http://www.example.com)r   patchrp  r  rL  HTTPConnectionr	   r3  r)  r   r"   _debuglevelr%   r{  rG   s      r+   #test_http_handler_global_debuglevelz0HandlerTests.test_http_handler_global_debuglevel(  st    ZZt{{99<K 	/ A!AMM!FF+,Q]]A.	/ 	/ 	/   ABBc                     t               }t        d      }|j                  |       |j                  d       | j	                  |j
                  d       y )N   r5  r&  )r	   r3  r)  r   r"   r)  r*  s      r+   "test_http_handler_local_debuglevelz/HandlerTests.test_http_handler_local_debuglevel0  sA    q)	a	'(*r-   rQ  z)HTTPSConnection required for HTTPS tests.c                 D   t         j                  j                  t        j                  j
                  dd      5  t               }t               }|j                  |       |j                  d       | j                  |j                  d       d d d        y # 1 sw Y   y xY w)Nr6     https://www.example.com)r   r'  rp  r  rL  rQ  r	   rS  r)  r   r"   r)  r*  s      r+   $test_https_handler_global_debuglevelz1HandlerTests.test_https_handler_global_debuglevel7  st    ZZt{{::L!L 	/ A "AMM!FF,-Q]]A.	/ 	/ 	/r,  c                     t               }t        d      }|j                  |       |j                  d       | j	                  |j
                  d       y )Nr  r5  r2  )r	   rS  r)  r   r"   r)  r*  s      r+   #test_https_handler_local_debuglevelz0HandlerTests.test_https_handler_local_debuglevel@  sA    *	a	()*r-   c                 t   t         j                  j                         }t               |_        d}g d}|D ]  }t        ||      }|j                  |      }| j                  |j                  d   d       |j                  dd        |j                  |      }| j                  |j                  d   d        y )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   r  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_doubleslashH  s     NN..0<  
	PFVT*F f-IY88@-P -t4}}V,HX77?O
	Pr-   c                 b   g d}t        d      }|D ]  }||_        t        |      }| j                  |j	                         |       | j                  |j
                  xs d|j
                         | j                  t        |j	                               j                  |j                          y )N)zhttp://example.com?foo=bar#bazz)http://example.com?foo=bar&spam=eggs#bashrZ   rZ   r   )r   full_urlr   r"   rO  fragmentquery)r%   r  r  r_   parseds        r+   test_full_url_setterz!HandlerTests.test_full_url_setterc  s    
 () 	MCAJc]FQ^^-s3 QZZ-2v?Xann&67==v||L	Mr-   c                     t        d      }|`| j                  |j                         | j                  |j                         | j	                  |j
                  d       y )Nr&  r   )r   r?  rk   r@  r"   r   )r%   r  s     r+   test_full_url_deleterz"HandlerTests.test_full_url_deleterz  sJ    ,-J!**%!**%R(r-   c                    t         j                  j                         }t               |_        d}t        |      }|j                  |      }| j                  |j                  d       | j                  |j                  d       d}t        |      }|j                  |      }| j                  |j                  d       | j                  |j                  d       y )Nzhttp://www.python.org?getspamwww.python.orgz	/?getspamhttp://www.python.orgr   )
r2   r   r  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     NN..0<3	i s#&67+62&'s#&67"-r-   c                 *   t         j                  j                         }t               x}|_        d}t        |      }t        ddi d|      }|j                  ||      }| j                  ||       | j                  t        |d             t        ddi d|      }|j                  ||      }| j                  ||       | j                  t        |d             t        dd	i d|      }|j                  ||      }| j                  ||       | j                  t        |d             t        d
di d|      }| j                  |j                  ||             | j                  |j                  d       | j                  |j                  ||d
di f       y )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   r{  r_   ro   r  newrs          r+   test_errorszHandlerTests.test_errors  s^   NN--/!|#AH#cldBC0sA&aG,-j"b#6sA&aG,-/R=sA&aG,-mRS9!//#q12&)#q#}b!ABr-   c                 ^   t               }t        j                  j                  |      }t	               |_        t        d      }t        ddi d      }|j                  |      }| j                  |j                  |       | j                  |j                  |       | j                  |j                  d       | j                  |j                         |j                  ||      }| j                  |j                   |       | j                  |j"                  |       | j                  ||       y )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   r  r  rQ  s          r+   test_cookieszHandlerTests.test_cookies  s    _NN..r2<+,dB+$bjj#&bjj&),,m<))*sA&bii%bggq!ar-   c                    d}d}t         j                  j                         }t               x}|_        dD ]  }dD ]  }t        |d|z        }t        ||      }t        j                  |_	        |j                  dd       |$|j                  dt        t        |                   |j                  d	d
       	  ||t               |dt        d|i             | j)                  |j*                  j-                         |       	 | j)                  |j*                  j/                         d       |j*                  j6                  D 	cg c]  }	|	j9                          }
}	| j;                  d|
       | j;                  d|
       | j)                  |j*                  j6                  d   d       | j;                  d	|j*                  j6                         | j;                  d	|j*                  j<                           t        |      }t        j                  |_	        |fd}t        |d      }d}t        j                  |_	        	 	  |||d       |dz   }# t         j                   j"                  $ r' | j%                  |d       | j'                  |       Y w xY w# t0        $ r) | j3                  |j*                  j4                         Y w xY wc c}	w # t         j                   j"                  $ r7 | j)                  |t         j                  j                  j>                         Y nw xY wt        |d      }d}t        j                  |_	        	 	  |||d|z         |dz   }# t         j                   j"                  $ r7 | j)                  |t         j                  j                  j@                         Y y 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)r[  r\  r  zcontent-lengthzcontent-typec           
      T    | j                  |t               ddt        d|i             y )Nr  r^  r_  )r  r   r   )rG   ro   r_   s      r+   rx  z,HandlerTests.test_redirect.<locals>.redirect  s'    S(*c6(*c):;=r-   r{   )rU  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   rO  
get_methodAttributeErrorrj   r   r^   lowerr  r  max_repeatsmax_redirections)r%   from_urlto_urlrG   r{  r   r   r   ro   xr^   rx  r   s                r+   test_redirectzHandlerTests.test_redirect  s@   .,NN..0!|#AH , 	BD, B Od$:;h-$<<z+<=#NN#3ST^D++FF;/3
D&&
F';<>   !3!3!5v>1$$QUU%5%5%7? /0eemm<1779<<  !17;  9  z!:!24  7  )@)@A=B	BD h44!' 	= h>44	TC!67	 I ||-- /MM$
3((./ & 1$$QUUZZ01
 =6 ||%% 	TUFNN$F$F$R$RS	T
 h>44	RC!85!@A	  ||%% 	RU#^^??PPR	RsQ   ;I*J!K	K A JJ!.KKAL/.L/M+ +AN?>N?c                 2   d}g d}g d}d}t         j                  j                         }t               x}|_        t        |      }t        j                  |_        |D ]V  }|dz   |z   }	| j                  t         j                  j                  |j                  |t               ddt        d|	i             X |D ]\  }|dz   |z   }
|j                  |t               dd	t        d|
i             | j                  |j                   j#                         |
       ^ y )
NrZ  )r  httpsr  )fileimapldapzexample.com/b.htmlz://r  zSecurity Loopholer_  That's fine)r2   r   ra  r   r  r   r   r   r   r4   r   rc  r  r   r   r"   ro   rO  )r%   rj  valid_schemesinvalid_schemesschemeless_urlrG   r{  ro   r|  invalid_url	valid_urls              r+   test_invalid_redirectz"HandlerTests.test_invalid_redirect  s   ..0-NN..0!|#AHh44% 	<F 5.>9Kfll44a6F6FS*=[ 9:<	< $ 	>F7IS(*c=Z346QUU//19=		>r-   c           
         d}d}t         j                  j                         }t               x}|_        t        |      }t        j                  |_        t         j                  j                  ||      }|j                  |t               ddt        d|i             | j                  |j                  j!                         |       y )NrZ  z/b.htmlr  rs  r_  )r2   r   ra  r   r  r   r   r   r   r   urljoinr  r   r   r"   ro   rO  )r%   rj  relative_urlrG   r{  ro   rx  s          r+   test_relative_redirectz#HandlerTests.test_relative_redirect  s    . NN..0!|#AHh44LL((,?		hj#}Y/0	2++-y9r-   c                    ddl m} ddlm}  |       } ||dd       t	        dd      }t
        j                  j                         }t
        j                  j                         }t
        j                  j                  |      }t        ||||      }|j                  d       | j                  |j                  j                  d             y )	Nr   )	CookieJar)interact_netscapezhttp://www.example.com/z	spam=eggsr  z%Location: http://www.cracker.com/

Cookie)http.cookiejarr  test.test_http_cookiejarr  rA  r2   r   HTTPDefaultErrorHandlerra  rT  r1  r   rj   ro   rf   )	r%   r  r  rW  hhhdehhrhcpr{  s	            r+   test_cookie_redirectz!HandlerTests.test_cookie_redirect+  s    ,>["7E$S*UV~~557nn002^^//3b$R0	()**845r-   c                 6   d}t        dd|z         }t        j                  j                         }t        j                  j	                         }t        |||      }|j                  d      }| j                  |j                         |j                                y )Nz(http://www.example.com/index.html#OK

r  z
Location: r&  )
rA  r2   r   r  ra  r1  r   r"   r   strip)r%   redirected_urlr  r  r  r{  r   s          r+   test_redirect_fragmentz#HandlerTests.test_redirect_fragment:  sx    G$S,*GH~~557nn002b$,VV,-n&:&:&<=r-   c                      j                  t        j                  j                         t        j
                  j                  d}t        j                  |      t        j
                  _         j                  t        t        j
                  d       t        d       fd}|t        j
                  j                  _        t        j                  j                  d      } j                  |j                         d       y )Ns(   HTTP/1.1 302 Found
Location: ?query

r(  )z/pathz/path?queryc                     j                  |t                      j                  | ||g|i | d| j                  _        y )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>.requestN  sA    S$t*-JtVS=3="=&FDNN#r-   zhttp://python.org/pathzhttp://python.org/path?query)r1   r2   r   r3   r  rL  r(  r   fakehttpr  iterr6   r"   r   )r%   	response1r   r   r  r  s   `   @@r+   test_redirect_no_pathz"HandlerTests.test_redirect_no_pathC  s     	112[[//
E	%0%9%9)%D".>
K,-	G
 .5""*^^##$<=&DEr-   c                     G d dt         j                  j                        } |       }t         j                  j                  |      }d}|D ]  \  }}| j	                  t        |            5  t        j                  d|z   dz         |_        |j                  d      }d|z   dz   }|j                  }	| j                  |	j                  |      t        |	             d d d         y # 1 sw Y   xY w)	Nc                       e Zd Zd Zy)4HandlerTests.test_redirect_encoding.<locals>.Handlerc                     | j                  | j                  |      }| j                  j                  | _        t	        j
                  d      | _        |S )Ns)   HTTP/1.1 200 OK
Content-Length: 3

123)r;  
connectionbuflast_bufr   r  )r%   ro   results      r+   r=  z>HandlerTests.test_redirect_encoding.<locals>.Handler.http_open[  sE    doos; $ 3 3"-"6"6# r-   N)rT   r#   rU   r=  rV   r-   r+   Handlerr  Z  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   r_  r  r   r  r   s
             r+   test_redirect_encodingz#HandlerTests.test_redirect_encodingW  s    	fnn00 	 ),,W5
 #( 
	MXvd8n- 	M%0%9%9"$,-0&"
 ";;'<="V+d2!** 2 28 <d7mL	M 	M
	M	M 	Ms   .A0C))C2	c           
         d}t        |      t        |      fD ]  }t               }t        j                  j	                  |      }|j                  |       dgg}t        ||      }t        d      }| j                  |j                  d       |j                  |       | j                  |j                  |       | j                  |d   dfg|j                  D cg c]  }|dd	 	 c}        y c c}w )
Nproxy.example.com:3128r  )HTTP)r=  r  zhttp://acme.example.com/zacme.example.comr   r=  r  )dictr	   r2   r   ProxyHandlerr)  r.  r   r"   r   r   r  )	r%   r'   rx   r{  phr+  r,  ro   tups	            r+   
test_proxyzHandlerTests.test_proxyz  s    $1t|+ 	<A A,,Q/BMM"12I 1I>H45CSXX'9:FF3KSXXq)x{K8923'':3c!Ah:<	< ;s   C2c                 $   dt         j                  d<   t               }t        j                  j                  t        d            }|j                  |       t        d      }| j                  |j                  d       |j                  |       | j                  |j                  d       t        d      }| j                  |j                  d       |j                  |       | j                  |j                  d       t         j                  d= y )	Nz
python.orgno_proxyproxy.example.comr  zhttp://www.perl.org/www.perl.orgrH  rG  r   environr	   r2   r   r  r  r)  r   r"   r   r   r%   r{  r  ro   s       r+   test_proxy_no_proxyz HandlerTests.test_proxy_no_proxy  s    !-

:^^((3F)GH	b,->2	s#67-.#34	s#34JJz"r-   c                 |   dt         j                  d<   t               }t        j                  j                  t        d            }|j                  |       t        d      }| j                  |j                  d       |j                  |       | j                  |j                  d       t         j                  d= y )N*r  r  r  rH  rG  r  r  s       r+   test_proxy_no_proxy_allz$HandlerTests.test_proxy_no_proxy_all  s    !$

:^^((3F)GH	b-.#34	s#34JJz"r-   c           	         t               }t        j                  j                  t	        d            }|j                  |       dgg}t        ||      }t        d      }| j                  |j                  d       |j                  |       | j                  |j                  d       | j                  |d   dfg|j                  D cg c]  }|dd 	 c}       y c c}w )	Nr  ro  )rW  r  https://www.example.com/www.example.comr   rW  r  )r	   r2   r   r  r  r)  r.  r   r"   r   r   r  )r%   r{  r  r+  r,  ro   r  s          r+   test_proxy_httpszHandlerTests.test_proxy_https  s    ^^((4L)MN	b./
	 -Q	:01#45	s#;<8A;56./gg6s#a(6	86s   	C
c                    t               }t        j                  j                  t	        d            }|j                  |       t               }|j                  |       t        d      }|j                  dd       |j                  dd       | j                  |j                  d       | j                  |j                         |j                  |       | j                  d	|j                  j                          | j#                  d
|j                  j                          | j%                  |j                         | j                  |j                  d       | j                  |j'                  d      d       y )Nr  r  r  Proxy-AuthorizationFooBar
User-AgentGrailr  )r  r  )r  r  Proxy-authorization)r	   r2   r   r  r  r)  rS  r   rh   r"   r   rk   r   r   r  r8  r   rP   rd  rl   )r%   r{  r  https_handlerro   s        r+   $test_proxy_https_proxy_authorizationz1HandlerTests.test_proxy_https_proxy_authorization  s#   ^^((4L)MN	b(*	m$01,h7|W-#45#**+	s 	:&//;;	=-#,,88	:S--.#;<(=>Ir-   darwinzonly relevant for OSXc                 |   dg dd}dD ]!  }| j                  t        ||      d|z         # dD ]!  }| j                  t        ||      d|z         # dg d}| j                  t        d	|             dd
dgd}d}| j                  t        ||      d|z         d}| j                  t        ||      d|z         y )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  s     $&
! 	GDOO8vF>EG	G
 	ID9$G@4GI	I %);4VVDE $)+;=
 4T6B:TA	C5dFC;dB	Dr-   c                 n   | j                  ||      5  t               }t               }t        j                  j                  |      }dj                  |      dz   }t        d|      }|j                  |       |j                  |       | j                  ||d|||dd       d d d        y # 1 sw Y   y xY w)N)rc  r^   z
rH    r_  !http://acme.example.com/protected)
r  r	   ra  r2   r   r
   joinrA  r)  _test_basic_auth)r%   r^   rc  r*  password_managerauth_handlerr   http_handlers           r+   check_basic_authzHandlerTests.check_basic_auth  s    \\w\7 	G#%F24!>>>>?OPL;;w'*4D23=L|,|,!!&,"'7G"E"EG	G 	G 	Gs   BB++B4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}| j                  ||        t        j                  dt        f      5  d| g}| j                  ||       d d d        ||g||g||gfD ]&  }	|	D 
cg c]  }
d|
 	 }}
| j                  ||       ( y # 1 sw Y   >xY wc c}
w )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, zWWW-Authenticate: zBasic Auth Realm was unquotedzWWW-Authenticate: Basic realm=)r  r   check_warningsUserWarning)r%   rc  realm2basicbasic2other_no_realmdigest	realm_strr^   
challenges	challenges              r+   test_basic_authzHandlerTests.test_basic_auth  s   $%wa( **"6( +> ? E7!$E7!$ E7"45 gRx gR'(b(gRx hb 
 	2I ,I;78G!!'51!	2& ++-L%0-2 3 	27w?@G!!'51	2 VVUO
 	2J )34$ ,I;7 4G 4!!'51	2	2 	24s    C3C"Cc           
      t   t               }t        j                  j                  t	        d            }|j                  |       t               }t        j                  j                  |      }d}t        dd|z        }|j                  |       |j                  |       | j                  ||d|||dd       y )Nr  r  ACME Networksi  z(Proxy-Authenticate: Basic realm="%s"

r  z&http://acme.example.com:3128/protected)
r	   r2   r   r  r  r)  ra  ProxyBasicAuthHandlerrA  r  )r%   r*  r  r  r  rc  r  s          r+   test_proxy_basic_authz"HandlerTests.test_proxy_basic_auth+  s    !^^((3K)LM2.0~~;;<LM.?%G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        }	|j                  |       |j                  |       |j                  |	       | j                  ||d
||	|dd       | j                  |j                  ddgdz         y )Nc                       e Zd Zd Zd Zy)QHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirectorc                 <    t        j                  |        g | _        y r   )r	   r   recordedr   s    r+   r   zZHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.__init__F  s    ''- "r-   c                 :    | j                   j                  |       y r   )r  r  )r%   r   s     r+   recordzXHandlerTests.test_basic_and_digest_auth_handlers.<locals>.RecordingOpenerDirector.recordJ  s    $$T*r-   N)rT   r#   rU   r   r  rV   r-   r+   RecordingOpenerDirectorr  E  s    #+r-   r  c                       e Zd Zd Zy)OHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestDigestAuthHandlerc                     | j                   j                  d       t        j                  j                  j
                  | g|i | y )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_401N  sA    ""8,44CCD L>BLFJLr-   NrT   r#   rU   r  rV   r-   r+   TestDigestAuthHandlerr  M  s    Lr-   r  c                       e Zd Zd Zy)NHandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandlerc                     | j                   j                  d       t        j                  j                  j
                  | g|i | y )Nr  )r  r  r2   r   r
   r  r  s      r+   r  z]HandlerTests.test_basic_and_digest_auth_handlers.<locals>.TestBasicAuthHandler.http_error_401T  sA    ""7+33BB4 K=AKEIKr-   Nr  rV   r-   r+   TestBasicAuthHandlerr  S  s    Kr-   r  r  r  &WWW-Authenticate: Basic realm="%s"

r_  r  r  r  r  )r	   r2   r   r  r
   ra  rA  r)  r  r"   r  )
r%   r  r  r  r*  r  digest_handlerbasic_handlerrc  r  s
             r+   #test_basic_and_digest_auth_handlersz0HandlerTests.test_basic_and_digest_auth_handlers<  s    	+n 	+	LFNN$H$H 	L	K6>>#F#F 	K )*.0./?@,-=>.=EG=)>*<( 	fm_#\3CAA	  	8W*=a*?@r-   c                     t               }t        j                  j                  d       }t	        dd      }|j                  |       |j                  |       | j                  t        |j                  d       y )Nr  zWWW-Authenticate: Kerberos

r&  )	r	   r2   r   r  rA  r)  r4   r5   r   )r%   r*  digest_auth_handlerr  s       r+   $test_unsupported_auth_digest_handlerz1HandlerTests.test_unsupported_auth_digest_handlern  sc    !$nnBB4H.57./<(*fkk3KLr-   c                     t               }t        j                  j                  d       }t	        dd      }|j                  |       |j                  |       | j                  t        |j                  d       y )Nr  zWWW-Authenticate: NTLM

r&  )	r	   r2   r   r
   rA  r)  r4   r5   r   )r%   r*  basic_auth_handlerr  s       r+   #test_unsupported_auth_basic_handlerz0HandlerTests.test_unsupported_auth_basic_handlerx  sc    !#^^@@F.13-.<(*fkk3KLr-   c	                    dd l }	d\  }
}|j                  |||
|       | j                  ||j                         | j                  ||j                         | j                  |
|j
                         | j                  ||j                         |j                  |       | j                  |j                  |       | j                  |j                  |       | j                  t        |j                        d       | j                  |j                  d   j                  |             t        |
d|d      }d|	j                  |      j!                         j#                         z   }| j                  |j                  d   j%                  |      |       | j                  |j                  d   j&                  |   |       d x|_        |_        |j)                          |j                  |       | j                  t        |j                        d       | j                  |j                  d   j                  |             y )Nr   wilecoyoter  :asciizBasic r  )base64r   r"   rc  r_   rd  rt   r   rg  rh  r  rF  rj   rf   bytesencodebytesr  decoderl   r  rC  )r%   r*  r  auth_headerrc  r  r  request_urlprotected_urlr  rd  rt   userpassauth_hdr_values                 r+   r  zHandlerTests._test_basic_auth  s    	)h 	!!%dHE 0 6 67&6&:&:;/445#3#<#<=K  	)66>)44mD 	\223Q7..q1<<[IJD(3W="x(..0779:..q1<<[I'	)..q1CCKP'	) =A@ 0 9K \223Q7..q1<<[IJr-   c                    d\  }}d}t        d      }t               }t        |      }|j                  d |||d       | j	                  |j                  |             | j	                  |j                  |dz                | j                  |j                  |dz                t               }|j                  |       |j                  |       |j                  |       | j	                  |j                         y )Nr  r  r   T)is_authenticatedz/nestedplain)rY  r   r
   r   re   r  rj   r	   r)  r   r\  )r%   rd  rt   r  r  pwd_managerauth_prior_handlerr*  s           r+   test_basic_prior_auth_auto_sendz,HandlerTests.test_basic_prior_auth_auto_send  s     *h9/4241+>''+tX 	( 	F 	44[AB44[95LMN55kG6KLM!-.<(K  	445r-   c                    d\  }}d}d}t               }t        |      }|j                  ||||       |j                  |      }| j	                  |       t               }|j                  |       t        ddd z        }	|j                  |	       |j                  |       |j                  |      }| j                  |       t        d      }	| j	                  |	j                         t               }|j                  |       |j                  |	       |j                  |       | j                  |	j                         y )Nr  r  ACMEr  r  r   )r   r
   r   r  rj   r	   r)  rA  r   re   rY  r\  )
r%   rd  rt   r  rc  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1+>''{D(K..{;!!-..=DF<(K ..{; /4556!-.<( 	K  	445r-   c                    dD ]  \  }}dj                  |      }t        j                  |j                         |z         }t        j
                  j                         }t        d      }d|_        |j                  ||      5 }|j                          ddd       | j                  |j                  j                  dj                  |              y# 1 sw Y   AxY w)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   r  r   r   r;  r<   re   fakesockclosed)r%   transferr   headerr  r  ro   resps           r+   test_http_closedzHandlerTests.test_http_closed  s    !
 	DXt
 5;;HEF''$(>?Dnn88:G/*CCKs+ t		OODMM00188BD	D s   CC	c                 z   t        j                  d      }t        j                  j	                         }t        d      }d|_        | j                  t        j                  j                        5  |j                  ||       ddd       | j                  |j                  j                  d       y# 1 sw Y   0xY w)z;Test the connection is cleaned up after an invalid responser-   r)  NzConnection not closed)r   r  r2   r   r  r   r   r4   r  rL  BadStatusLiner;  re   r+  r,  )r%   r  r  ro   s       r+   test_invalid_closedz HandlerTests.test_invalid_closed  s    ##C(..446o&t{{889 	'OOD#&	',,.EF	' 	's   /B11B:N)5rT   r#   rU   r  r  r  r  r
  r  r  r  r#  r+  r/  r  
skipUnlessr   r  rL  r3  r5  r=  rC  rE  rK  rR  rX  rm  ry  r}  r  r  r  r  r  r  r  r  r  r  platformr  r  r  r  r  r
  r  r  r"  r&  r0  r3  rV   r-   r+   r  r    s   >H@Z5x@CDE2.),O(;J/+ X.?@Bmn/ o/ X.?@Bmn+ o+P6M.).&C2"HRT>,:6>F(!MF<$#	#8  X.?@BmnJ oJ. X13JKD LDBG+2Z "0AdMM#KJ66&6PD"Gr-   r  c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)	MiscTestsc                 `    | j                  t        fd|j                  D                     y )Nc              3   <   K   | ]  }|j                   k(    y wr   r?  ).0rG   handler_classs     r+   	<genexpr>z/MiscTests.opener_has_handler.<locals>.<genexpr>	  s"      6 ! KK=8 6s   )re   anyr,  )r%   r*  r;  s     `r+   opener_has_handlerzMiscTests.opener_has_handler  s'     6%+__6 6 	7r-   c                 .    G d dt         j                  j                        } G d dt         j                  j                        } G d dt         j                  j                        }t         j                  j                  } |||      }| j                  ||       | j                  ||        || |             }| j                  ||       | j                  ||        ||      }| j                  ||        |       }| j                  |t         j                  j                          |t         j                  j                        }| j                  |t         j                  j                          |t         j                  j                               }| j                  |t         j                  j                          G d dt         j                  j                        } |||      }| j                  ||       | j                  ||       y )	Nc                       e Zd Zy)2MiscTests.test_build_opener.<locals>.MyHTTPHandlerNr&  rV   r-   r+   MyHTTPHandlerrA    r(  r-   rB  c                       e Zd Zd Zy)/MiscTests.test_build_opener.<locals>.FooHandlerc                      y r   rV   r   s    r+   foo_openz8MiscTests.test_build_opener.<locals>.FooHandler.foo_open  r  r-   N)rT   r#   rU   rF  rV   r-   r+   
FooHandlerrD        r-   rG  c                       e Zd Zd Zy)/MiscTests.test_build_opener.<locals>.BarHandlerc                      y r   rV   r   s    r+   bar_openz8MiscTests.test_build_opener.<locals>.BarHandler.bar_open  r  r-   N)rT   r#   rU   rL  rV   r-   r+   
BarHandlerrJ    rH  r-   rM  c                       e Zd Zy)7MiscTests.test_build_opener.<locals>.MyOtherHTTPHandlerNr&  rV   r-   r+   MyOtherHTTPHandlerrO  1  r(  r-   rP  )r2   r   r  BaseHandlerr  r>  )r%   rB  rG  rM  r  r{  rP  s          r+   test_build_openerzMiscTests.test_build_opener  s   	FNN66 		33 		33 	 ~~22Z0:.:. Z\2:.:. '=1 N6>>#=#=>3346>>#=#=>33566>>#=#=>	!;!; 	 (:;=1#56r-   c                    d}dx}x}}d}t         j                  j                  |||||      }| j                  t	        |d             | j                  |j                  d       | j                  t	        |d             | j                  |j                  d       d|j                  d|j                  }| j                  t        |      |       d|j                  d|j                  d	}| j                  t        |      |       y)
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   rc  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_interface8  s    
 'dR#ll$$S$T2>X./%=>Y/0&:;14377CS?325((CGGDcO4r-   c                     t         j                  j                  dddd d       }| j                  t	        |dd      d       | j                  |j                  j                         t               y )Nr_   i  zMETHOD NOT ALLOWED	__notes__rV   )	r2   r   rc  r"   rb  r  r   r<   r  )r%   rl  s     r+   test_gh_98778zMiscTests.test_gh_98778J  sR    LL""5#/CT4PK4b9addiik51r-   c                     g d}|D ]   \  }}| j                  t        |      |       " | j                  t        t        d      f y )N)r  )NNNr  r  )NNNr  r[  r\  )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.comr]  )z&http://user/name:password@localhost:22)r  	user/namert   localhost:22)z&http://username:pass/word@localhost:22)r  username	pass/wordr_  )z'http://user/name:pass/word@localhost:22)r  r^  ra  r_  zfile:/ftp.example.com)r"   r   r4   r5   )r%   parse_proxy_test_casestcr  s       r+   test_parse_proxyzMiscTests.test_parse_proxyO  sM    $"
N 3 	9LB\"-x8	9 	*l4KLMr-   c                     t               }| j                  t              5 }|j                  d       d d d        | j	                  t        j                        d       y # 1 sw Y   /xY w)Ninvalidz5Unsupported digest authentication algorithm 'invalid')r   r4   r5   get_algorithm_implsr"   rQ   	exception)r%   r  excs      r+   test_unsupported_algorithmz$MiscTests.test_unsupported_algorithm|  sZ    +-z* 	3c''	2	3C	
	3 	3s   A  A)N)	rT   r#   rU   r>  rR  rV  rY  rd  rj  rV   r-   r+   r7  r7    s#    7*7X5$2
+NZ
r-   r7  c                   v    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y)RequestTestsc                       e Zd ZdZy)RequestTests.PutRequestPUTN)rT   r#   rU   r   rV   r-   r+   
PutRequestrn    s    r-   rp  c                     t        d      | _        t        ddddi      | _        t        dd      | _        | j	                  d      | _        | j	                  dd      | _        y )	Nhttp://www.python.org/~jeremy/r   zX-Testr  r]   HEAD)r   r  )r   r  postheadrp  put
force_postr   s    r+   setUpzRequestTests.setUp  sf    ;<<"%-v$68	 <VL	??#CD//*J * r-   c                    | j                  d| j                  j                                | j                  d| j                  j                                | j                  d| j                  j                                | j                  d| j
                  j                                | j                  d| j                  j                                y )Nr  r  rs  ro  )r"   rt  re  r  ru  rv  rw  r   s    r+   test_methodzRequestTests.test_method  s    !5!5!78 3 3 56!5!5!78 3 3 56!;!;!=>r-   c                 b   | j                  | j                  j                         | j                  d| j                  j	                                d| j                  _        | j                  | j                  j                         | j                  d| j                  j	                                y )Nr  r   r  )rj   r  r   r"   re  re   r   s    r+   	test_datazRequestTests.test_data  sq    ' 3 3 56&!4!4!67r-   c                 H   | j                  d| j                  j                         | j                  j                  dd       | j	                  d| j                  j                  d          d| j                  _        | j                  d| j                  j                         y )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  sx    )488+E+EF(()92>TXX778HIJ)488+E+EFr-   c                 `   | j                  d| j                  j                         d| j                  _        | j                  j	                  dd       | j                  d| j                  j                  d          | j                  `| j                  d| j                  j                         y )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    )488+E+EF(()91=DHH667GHIHHM)488+E+EFr-   c                 X    | j                  d| j                  j                                y )Nrr  )r"   r  rO  r   s    r+   test_get_full_urlzRequestTests.test_get_full_url  s#    9..0	2r-   c                     | j                  d| j                  j                         t        d      }| j                  d|j                         y )Nz	/~jeremy/zhttp://www.python.org/r/   )r"   r  r   r   r<  s     r+   test_selectorzRequestTests.test_selector  s;    dhh&7&78./cll+r-   c                 P    | j                  d| j                  j                         y )Nr  )r"   r  r  r   s    r+   test_get_typezRequestTests.test_get_type  s    /r-   c                 P    | j                  d| j                  j                         y )NrG  )r"   r  r   r   s    r+   test_get_hostzRequestTests.test_get_host  s    )488==9r-   c                 R    t        d      }| j                  d|j                         y )Nzhttp://www.%70ython.org/rG  r   r"   r   r<  s     r+   test_get_host_unquotez"RequestTests.test_get_host_unquote  s"    01)3884r-   c                 x   | j                  | j                  j                                | j                  j                  dd       | j	                  | j                  j                                | j                  d| j                  j                         | j                  d| j                  j                         y )Nr  r  rG  )rj   r  	has_proxyr7  re   r"   rU  r   r   s    r+   r  zRequestTests.test_proxy  s}    ++-.>62**,-)488+C+CD7r-   c                 R    t        d      }| j                  d|j                         y )Nz<URL:http://www.python.org>rG  r  r<  s     r+   test_wrapped_urlzRequestTests.test_wrapped_url  s"    34)3884r-   c                     t        d      }| j                  d|j                         t        d      }| j                  d|j                         d}t        |      }| j                  |j                         |       y )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   rO  )r%   ro   r_   s      r+   test_url_fragmentzRequestTests.test_url_fragment  si    EFs||478cll+ ?cl))+S1r-   c                     g d}|D ]7  }t        |      }| j                  |j                         |j                         9 y )N)zhttp://docs.python.orgr  r  )r   r"   rO  r?  )r%   r  r_   ro   s       r+   test_url_fullurl_get_full_urlz*RequestTests.test_url_fullurl_get_full_url  sA    A  	?C#,CS--/>	?r-   N)rT   r#   rU   r   rp  rx  rz  r|  r  r  r  r  r  r  r  r  r  r  r  rV   r-   r+   rl  rl    sV    W ?8GG2,
0:585	2?r-   rl  __main__)Ar  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   ry  http.clientr  requires_working_socketTestCaser   rX   r   r   r  r   r   r   r   r   IOBaser   r   r   r.  r1  r   r  r3  rQ  rA  r   rL  HTTPSHandlerrS  rY  ra  rk  r  r  r7  rl  rT   mainrV   r-   r+   <module>r     s     " (   	 	   
   7 7 7 "     t ,1,8$$ 1,hx'x(( x'v, , #$ #
	2;; 	3 3? ?ryy $* *Z38 38lD0fnn00 0Hfnn88 H8 4;;)*	46>>66 	4Qv~~99 Q&
( 
(NA(++ NAb	jG8$$ jGZ!}
!! }
@^?8$$ ^?B zHMMO r-   