
    7bhf                     P   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
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 ej&                  d       ej(                  j+                  e      Zej(                  j1                  edd	      Zej(                  j1                  edd
      Z G d dej6                  j8                        Z G d dej<                        Z G d d      Z  G d dej6                  jB                        Z" G d dej6                  jB                        Z# G d de	jH                        Z% ejL                  dd       G d de	jH                               Z'd Z( G d de	jH                        Z)d Z*e+dk(  r e	jX                          yy# e$ r dZY bw xY w)     N)support)hashlib_helper)threading_helper)warnings_helperT)modulecertdatazkeycert.pemzkeycert2.pemc                       e Zd ZdZd Zd Zy)LoopbackHttpServerz^HTTP server w/ a few modifications that make it useful for
    loopback testing purposes.
    c                     t         j                  j                  j                  | ||       | j                  j                  d       y )Ng?)httpserver
HTTPServer__init__socket
settimeout)selfserver_addressRequestHandlerClasss      D/opt/python-3.12.12/usr/lib/python3.12/test/test_urllib2_localnet.pyr   zLoopbackHttpServer.__init__%   s6    ''(6(;	= 	s#    c                 f    | j                   j                         \  }}|j                  d       ||fS )zHTTPServer method, overridden.g      $@)r   acceptr   )r   requestclient_addresss      r   get_requestzLoopbackHttpServer.get_request.   s6     #'++"4"4"6
 	4 ((r   N)__name__
__module____qualname____doc__r   r    r   r   r
   r
       s    $
)r   r
   c                   "    e Zd ZdZd Zd Zd Zy)LoopbackHttpServerThreadz2Stoppable thread that runs a loopback http server.c                     t         j                  j                  |        d| _        t        j                         | _        d|_        t        d|      | _        | j                  j                  | _
        y )NFzHTTP/1.0)z	127.0.0.1r   )	threadingThreadr   _stop_serverEventreadyprotocol_versionr
   httpdserver_portportr   request_handlers     r   r   z!LoopbackHttpServerThread.__init__=   sW    !!$'!__&
+5('(8(79
JJ**	r   c                 f    d| _         | j                          | j                  j                          y)z.Stops the webserver if it's currently running.TN)r&   joinr*   server_closer   s    r   stopzLoopbackHttpServerThread.stopF   s%     !		

!r   c                     | j                   j                          | j                  s(| j                  j	                          | j                  s'y y N)r(   setr&   r*   handle_requestr2   s    r   runzLoopbackHttpServerThread.runN   s4    

##JJ%%' ##r   N)r   r   r   r   r   r3   r8   r    r   r   r"   r"   :   s    <+"(r   r"   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)DigestAuthHandlerz-Handler for performing digest authentication.c                 J    d| _         g | _        i | _        d| _        d| _        y )Nr   z
Test Realmauth)_request_num_nonces_users_realm_name_qopr2   s    r   r   zDigestAuthHandler.__init__X   s'    '	r   c                     || _         y r5   )rA   )r   qops     r   set_qopzDigestAuthHandler.set_qop_   s	    	r   c                     || _         y r5   )r?   )r   userss     r   	set_userszDigestAuthHandler.set_usersb   s	    r   c                     || _         y r5   )r@   )r   realms     r   	set_realmzDigestAuthHandler.set_realmf   s
     r   c                     | xj                   dz  c_         t        j                  t        | j                         j	                  d            j                         }| j                  j                  |       |S )N   ascii)r=   hashlibmd5strencode	hexdigestr>   append)r   nonces     r   _generate_noncez!DigestAuthHandler._generate_noncei   sW    QC 1 1299'BCMMOE"r   c                 
   |j                  d      }||dz   d  }|j                  d      }i }|D ]Q  }|j                  d      \  }}|j                         }|d   dk(  r|d   dk(  r|dd }n|j                         }|||<   S |S )N rL   ,=r   ")findsplitstrip)r   auth_strfirst_space_indexparts	auth_dictpartnamevalues           r   _create_auth_dictz#DigestAuthHandler._create_auth_dicto   s    $MM#.-a/01s#	D**S/KD%::<DQx359#3a#IdO  r   c                    i }|j                  |       ||d<   ||d<   ||d<   d|z  }t        j                  |j                  d            j	                         }d|z  }t        j                  |j                  d            j	                         }	||d<   |	|d<   d	|z  }
t        j                  |
j                  d            j	                         }||d
   k(  S )Npasswordmethoduriz#%(username)s:%(realm)s:%(password)srM   z%(method)s:%(uri)sHA1HA2z3%(HA1)s:%(nonce)s:%(nc)s:%(cnonce)s:%(qop)s:%(HA2)sresponse)updaterN   rO   rQ   rR   )r   rb   rh   ri   rj   
final_dictHA1_strrk   HA2_strrl   response_strrm   s               r   _validate_authz DigestAuthHandler._validate_auth   s    
)$!)
:%
8
57*Dkk'..12<<>&3kk'..12<<>
5
546@A;;|227;<FFH9Z000r   c           
         |j                  dd       |j                  dd       |j                  dd| j                  d| j                  d| j	                         d	       |j                          |j                  j                  d
       y)Ni  zProxy Authentication RequiredContent-Type	text/htmlzProxy-AuthenticatezDigest realm="z", qop="z	",nonce="z", s   Proxy Authentication Required.F)send_responsesend_headerr@   rA   rU   end_headerswfilewriter-   s     r   _return_auth_challengez(DigestAuthHandler._return_auth_challenge   s}    %%c+JK##NK@##  tyy$*>*>*@#B	C 	##%##$EFr   c                 X   t        | j                        dk(  ryd|j                  vr| j                  |      S | j	                  |j                  d         }|d   | j                  v r| j                  |d      }n| j                  |      S |j                  d      | j                  vr| j                  |      S | j                  j                  |d          d}|j                  |j                  fD ]#  }| j                  |||j                  |      s"d}% |s| j                  |      S y)a  Performs digest authentication on the given HTTP request
        handler.  Returns True if authentication was successful, False
        otherwise.

        If no users have been set, then digest auth is effectively
        disabled and this method will always return True.
        r   TzProxy-AuthorizationusernamerT   F)lenr?   headersr|   rf   getr>   removepath
short_pathrs   command)r   r.   rb   rh   auth_validatedr   s         r   r7   z DigestAuthHandler.handle_request   s,    t{{q  (?(??..??..''(=>I $3;;	*(=?22?CC==)T\\922?CC##Ig$67"N )--/I/IJ&&y'/'6'>'>'+- &*N K "22?CCr   N)r   r   r   r   r   rD   rG   rJ   rU   rf   rs   r|   r7   r    r   r   r:   r:   U   s2    7!"1$*r   r:   c                       e Zd ZdZdZdZdZedeZ ej                  ej                  d            j                  d      Zd Zd Zd	 Zd
 Zd Zy)BasicAuthHandlerz,Handler for performing basic authentication.testUsertestPassTest:rM   c                 ^    t        j                  j                  j                  | g|i | y r5   )r   r   BaseHTTPRequestHandlerr   )r   argskwargss      r   r   zBasicAuthHandler.__init__   s$    **33DJ4J6Jr   c                      y r5   r    r   formatr   s      r   log_messagezBasicAuthHandler.log_message   s    r   c                 j    | j                  d       | j                  dd       | j                          y )N   Content-typerv   )rw   rx   ry   r2   s    r   do_HEADzBasicAuthHandler.do_HEAD   s,    35r   c                     | j                  d       | j                  dd| j                  z         | j                  dd       | j                          y )Ni  zWWW-AuthenticatezBasic realm="%s"r   rv   )rw   rx   REALMry   r2   s    r   do_AUTHHEADzBasicAuthHandler.do_AUTHHEAD   sG    3+-ADJJ-NO5r   c                    | j                   j                  dd      s,| j                          | j                  j	                  d       y | j                   j                  dd      d| j
                  z   k(  r=| j                  d       | j                          | j                  j	                  d       y | j                          y )NAuthorization s   No Auth header receivedzBasic r   s   It works)r   r   r   rz   r{   ENCODED_AUTHrw   ry   r2   s    r   do_GETzBasicAuthHandler.do_GET   s    ||4JJ78\\%(043D3D(DEs#JJ[) r   N)r   r   r   r   USERPASSWDr   USER_PASSWDbase64	b64encoderQ   decoder   r   r   r   r   r   r    r   r   r   r      s`    6DFE!6*K#6##K$6$6w$?@GGPLK
r   r   c                   "    e Zd ZdZd Zd Zd Zy)FakeProxyHandlerzThis is a 'fake proxy' that makes it look like the entire
    internet has gone down due to a sudden zombie invasion.  It main
    utility is in providing us with authentication support for
    testing.
    c                 l    || _         t        j                  j                  j                  | g|i | y r5   )digest_auth_handlerr   r   r   r   )r   r   r   r   s       r   r   zFakeProxyHandler.__init__  s.     $7 **33DJ4J6Jr   c                      y r5   r    r   s      r   r   zFakeProxyHandler.log_message  s     	r   c                    t         j                  j                  | j                  d      \  }}}}}}|| _        | j
                  j                  |       r| j                  dd       | j                  dd       | j                          | j                  j                  t        d| j                  z  d             | j                  j                  d       y y )	Nr   r   OKru   rv   zYou've reached %s!<BR>rM   sF   Our apologies, but our server is down due to a sudden zombie invasion.)urllibparseurlparser   r   r   r7   rw   rx   ry   rz   r{   bytes)r   scmnetlocr   paramsqueryfragments          r   r   zFakeProxyHandler.do_GET  s    7=||7L7LIIv84fdFE8##2248sD)^[9JJU#;dii#G#*, -JJ : ; 9r   N)r   r   r   r   r   r   r   r    r   r   r   r      s    K
;r   r   c                   J     e Zd ZdZdZdZdZ fdZd Z fdZ	d Z
d	 Z xZS )
BasicAuthTestsr   r   	Incorrectr   c                 >   t         t        |           d }t        |      | _        | j                  | j                         d| j                  j                  z  | _        | j                  j                          | j                  j                  j                          y )Nc                      t        | i |S r5   )r   )r   r   s     r   #http_server_with_basic_auth_handlerzABasicAuthTests.setUp.<locals>.http_server_with_basic_auth_handler$  s    #T4V44r   zhttp://127.0.0.1:%s)superr   setUpr"   r   
addCleanupstop_serverr,   
server_urlstartr(   wait)r   r   	__class__s     r   r   zBasicAuthTests.setUp!  sp    nd)+	5./RS(()/$++2B2BB r   c                 F    | j                   j                          d | _         y r5   r   r3   r2   s    r   r   zBasicAuthTests.stop_server,      r   c                 *    t         t        |           y r5   )r   r   tearDown)r   r   s    r   r   zBasicAuthTests.tearDown0  s    nd,.r   c                 $   t         j                  j                         }|j                  | j                  | j
                  | j                  | j                         t         j                  j                  t         j                  j                  |             	 | j                  t         j                  j                  | j
                               y # t         j                  j                  $ r! | j                  d| j
                  z         Y y w xY w)Nz!Basic auth failed for the url: %s)r   r   HTTPBasicAuthHandleradd_passwordr   r   r   r   install_openerbuild_opener
assertTrueurlopenerror	HTTPErrorfail)r   ahs     r   test_basic_auth_successz&BasicAuthTests.test_basic_auth_success3  s    ^^002


DOOTYYL%%fnn&A&A"&EF	MOOFNN224??CD||%% 	MII9DOOKL	Ms   8C ;DDc                 2   t         j                  j                         }|j                  | j                  | j
                  | j                  | j                         t         j                  j                  t         j                  j                  |             | j                  t         j                  j                        5 }t         j                  j                  | j
                         d d d        j                  j                          y # 1 sw Y   $xY wr5   )r   r   r   r   r   r   r   INCORRECT_PASSWDr   r   assertRaisesr   r   r   	exceptionclose)r   r   cms      r   test_basic_auth_httperrorz(BasicAuthTests.test_basic_auth_httperror<  s    ^^002


DOOTYY@U@UV%%fnn&A&A"&EFv||556"NN""4??3 7
 76s    *DD)r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r   r   r     s2    DF"E	!/Mr   r   rO   )opensslc                   L     e Zd ZdZdZdZdZ fdZd Zd Z	d Z
d	 Zd
 Z xZS )ProxyAuthTestszhttp://localhosttestertest123	TestRealmc                     t         t                   d } j                  |t        j
                  j                                dt        j
                  d<   dt        j
                  d<   t                _         j                  j                   j                   j                  i        j                  j                   j                          fd}t        |       _         j                   j                           j                  j#                           j                  j$                  j'                          d j                  j(                  z  }t*        j,                  j/                  d|i      }t*        j,                  j1                          _        t*        j,                  j5                  | j2                         _        y )Nc                 ~    t         j                  j                          t         j                  j                  |        y r5   osenvironclearrn   old_environs    r   restore_environz-ProxyAuthTests.setUp.<locals>.restore_environP  $    JJJJk*r   r   NO_PROXYno_proxyc                  6    t        j                  g| i |S r5   )r   r   )r   r   r   s     r   create_fake_proxy_handlerz7ProxyAuthTests.setUp.<locals>.create_fake_proxy_handler[  s    #D$<$<NtNvNNr   zhttp://127.0.0.1:%dr   )r   r   r   r   r   r   copyr:   r   rG   r   r   rJ   r   r"   r   r   r   r(   r   r,   r   r   ProxyHandlerProxyDigestAuthHandlerproxy_digest_handlerr   opener)r   r   r   	proxy_urlhandlerr   s   `    r   r   zProxyAuthTests.setUpM  s@   nd)+	+ 	):;!#

:!#

:#4#6   **DIIt{{+CD  **4::6	O //HI(() )DKK,<,<<	..--v	.BC$*NN$I$I$K!nn11T..0r   c                 F    | j                   j                          d | _         y r5   r   r2   s    r   r   zProxyAuthTests.stop_serverh  r   r   c                    | j                   j                  | j                  | j                  | j                  | j
                  dz          | j                  j                  d       | j                  t        j                  j                        5 }| j                  j                  | j                         d d d        j                  j                          y # 1 sw Y   $xY w)Nbadr<   )r   r   r   URLr   r   r   rD   r   r   r   r   r   openr   r   r   r   s     r   -test_proxy_with_bad_password_raises_httperrorz<ProxyAuthTests.test_proxy_with_bad_password_raises_httperrorl  s    !!..tzz488/3yy$++e:K	M  ((0v||556"KKTXX& 7
 76s   &CC c                 4   | j                   j                  d       | j                  t        j                  j
                        5 }| j                  j                  | j                         d d d        j                  j                          y # 1 sw Y   $xY wNr<   )r   rD   r   r   r   r   r   r   r   r   r   r   s     r   ,test_proxy_with_no_password_raises_httperrorz;ProxyAuthTests.test_proxy_with_no_password_raises_httperrort  sd      ((0v||556"KKTXX& 7
 76s   &BBc                 ~   | j                   j                  | j                  | j                  | j                  | j
                         | j                  j                  d       | j                  j                  | j                        5 }|j                         r	 |j                         rd d d        y # 1 sw Y   y xY wr  )r   r   r   r   r   r   r   rD   r   r   readr   results     r   test_proxy_qop_auth_worksz(ProxyAuthTests.test_proxy_qop_auth_worksz  s    !!..tzz488/3yy$++	G  ((0[[dhh'6++- ++- (''s   "B33B<c                    | j                   j                  | j                  | j                  | j                  | j
                         | j                  j                  d       	 | j                  j                  | j                        }|5  |j                         r	 |j                         rd d d        y # 1 sw Y   y xY w# t        j                  j                  $ r Y y w xY w)Nzauth-int)r   r   r   r   r   r   r   rD   r   r   r  r   r   URLErrorr  s     r   0test_proxy_qop_auth_int_works_or_throws_urlerrorz?ProxyAuthTests.test_proxy_qop_auth_int_works_or_throws_urlerror  s    !!..tzz488/3yy$++	G  ((4
	[[%%dhh/F kkm kkm  ||$$ 	 		s   #%C 
"B66B?C"!C")r   r   r   r   r   r   r   r   r   r   r  r  r  r   r   s   @r   r   r   E  s5    
CDFE06r   r   c                 V      G  fddt         j                  j                        S )Nc                   <    e Zd ZdZg Zg ZdZd Zd Z fdZ	d Z
y)1GetRequestHandler.<locals>.FakeHTTPRequestHandlerz	TestHTTP/P   c                 p    | j                         }|r$| j                  j                  |      }||d  }|r#y y r5   )	send_headrz   r{   )r   bodydones      r   r   z8GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_GET  s5    >>#Dzz''-DE{ r   c                     | j                   d   }| j                  j                  t        |            }| j	                          | j
                  j                  |       y )NzContent-Length)r   rfiler  intr   requestsrS   )r   content_length	post_datas      r   do_POSTz9GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_POST  sC    !\\*:;N

N(;<IKKMMM  +r   c                    | j                   _        | j                  j                  | j                         j                  d      \  }}}| j                  |       |D ]&  \  }}| j                  ||d| j                  iz         ( |r$| j                  dd       | j                          |S | j                          y )Nr   r,   r   z
text/plain)
r   headers_receivedr  rS   r   poprw   rx   r,   ry   )r   response_coder   r  headerre   FakeHTTPRequestHandler	responsess         r   r  z;GetRequestHandler.<locals>.FakeHTTPRequestHandler.send_head  s    6:ll"3MM  ++4==+;(M7D}-#*  &1C)CD $+  >  "r   c                      y r5   r    )r   r   s     r   r   z=GetRequestHandler.<locals>.FakeHTTPRequestHandler.log_message  s    r   N)r   r   r   server_versionr  r  r,   r   r  r  r   )r   r!  s   r   r   r    s,    $	#	,		r   r   )r   r   r   )r!  r   s   `@r   GetRequestHandlerr$    s"    #!C!C #L "!r   c                        e Zd ZdZ fdZddZd ZddZ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 xZS )TestUrlopenaN  Tests urllib.request.urlopen using the network.

    These tests are not exhaustive.  Assuming that testing using files does a
    good job overall of some of the basic interface features.  There are no
    tests exercising the optional 'data' and 'proxies' arguments.  No tests
    for transparent redirection have been written.
    c                 *   t         t        |           | j                  t        j
                  j                         d }| j                  |t        j                  j                                dt        j                  d<   dt        j                  d<   y )Nc                 ~    t         j                  j                          t         j                  j                  |        y r5   r   r   s    r   r   z*TestUrlopen.setUp.<locals>.restore_environ  r   r   *r   r   )
r   r&  r   r   r   r   
urlcleanupr   r   r   )r   r   r   s     r   r   zTestUrlopen.setUp  sd    k4&( 	112	+ 	):;!$

:!$

:r   c                    g }t        j                  j                  ||fi |}	 |j                  |j	                  d             |j                  |j                                |j                  |j                  d             |j                  |j                                |j                          dj                  |      S # |j                          w xY w)Nr   i   r   )
r   r   r   extend	readlinesrS   readliner  r   r0   )r   urldatar   lfs         r   r   zTestUrlopen.urlopen  s    NN""377	HHQ[[%&HHQZZ\"HHQVVD\"HHQVVXGGIxx{ GGIs   A>C Cc                 F    | j                   j                          d | _         y r5   r   r2   s    r   r   zTestUrlopen.stop_server  r   r   c                 8   |dg dfg}t        |      }t        |      | _        | j                  | j                         | j                  j                          | j                  j                  j                          | j                  j                  }||_        |S )Nr   s   we don't care)	r$  r"   r   r   r   r   r(   r   r,   )r   r!  r   r,   s       r   start_serverzTestUrlopen.start_server  s    r#345I#I..w7(() {{r   c                     t        t        j                  d      s| j                  d       ddlm} |dg dfg}t        |      } || fd|i|}|j                  |_        |S )NHTTPSHandlerzssl support requiredr   )make_https_serverr      we care a bithandler_class)hasattrr   r   skipTesttest.ssl_serversr8  r$  r,   )r   r!  r   r8  r   r   s         r   start_https_serverzTestUrlopen.start_https_server  sd    v~~~6MM016r#345I#I."4IwI&I{{r   c                     d}ddgdfdg |fg}| j                  |      }| j                  d|j                  z        }| j                  ||       | j                  |j                  ddg       y )	Ns   We got here...i.  )Locationz(http://localhost:%(port)s/somewhere_elser   r   http://localhost:%s//z/somewhere_elser5  r   r,   assertEqualr  )r   expected_responser!  r   r0  s        r   test_redirectionzTestUrlopen.test_redirection  s    -KL"'(
	 ##I.||2W\\AB01))C1B+CDr   c                     d}d}ddg|fg}| j                  |      }| j                  d|j                  z        }| j                  ||       y )Ns   hello worlds   a
hello worl
1
d
0
r   )zTransfer-EncodingchunkedrA  )r5  r   r,   rD  )r   rE  chunked_startrm   r   r0  s         r   test_chunkedzTestUrlopen.test_chunked  s`    *! 	 ;<mLM##H-||2W\\AB01r   c                    d}| j                  dg |fg      }	 | j                  d|j                  z         | j                  d       | j                  |       | j                  |j                  dg       y # t        j
                  j                  $ r*}|j                         }|j                          Y d }~rd }~ww xY w)Ns   Bad bad bad...i  zhttp://localhost:%s/weeblez404 should raise URLErrorz/weeble)r5  r   r,   r   r   r   r
  r  r   rD  r  )r   rE  r   r2  r0  s        r   test_404zTestUrlopen.test_404  s    -##c2/@%A$BC	3LL5DE
 II1201))I;7 ||$$ 	668DGGII	s   A8 8B? B::B?c                     d}| j                  dg |fg      }| j                  d|j                  z        }| j                  ||       | j                  |j                  dg       y )N   pycon 2008...r   http://localhost:%s/bizarre/bizarrerC  r   rE  r   r0  s       r   test_200zTestUrlopen.test_200(  sd    ,##c2/@%A$BC||9GLLHI01))J<8r   c                     d}| j                  dg |fg      }| j                  d|j                  z  d      }| j                  ||       | j                  |j                  ddg       y )NrN  r   rO  s   get=with_feelingrP  rC  rQ  s       r   test_200_with_parametersz$TestUrlopen.test_200_with_parameters/  sm    ,##c2/@%A$BC||9GLLH0201))J8K+LMr   c                     | j                         }t        j                  t              }| j	                  d|j
                  z  |      }| j                  |d       y )Ncafilehttps://localhost:%s/bizarrecontextr9  )r>  sslcreate_default_contextCERT_localhostr   r,   rD  )r   r   rZ  r0  s       r   
test_httpszTestUrlopen.test_https7  sN    ))+,,NC||:W\\ISZ|[/0r   c                    | j                  t              }t        j                  dt        f      5  | j                  d|j                  z  t              }| j                  |d       | j                  t        j                  j                        5 }| j                  d|j                  z  t               d d d        | j                  t              }| j                  t        j                  j                        5 }| j                  d|j                  z  t               d d d        d d d        y # 1 sw Y   ~xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Ncertfiler   rX  rV  r9  )r>  r]  r   check_warningsDeprecationWarningr   r,   rD  r   r   r   r
  CERT_fakehostname)r   r   r0  r   s       r   test_https_with_cafilez"TestUrlopen.test_https_with_cafile=  s   ))>)B++R1C,DE<< > M'5   7DT#34""6<<#8#89R;gllJ$5  7 : --7H-IG""6<<#8#89R;gllJ$5  7 : FE :9
 :9 FEs=   A E%D47AE>%E #E4D=	9E E		EEc                 L   | j                  t              }t        j                  dt        f      5  | j                  t        j                  j                        5 }| j                  d|j                  z  d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr`  r   rX  T)	cadefault)r>  r]  r   rb  rc  r   r   r   r
  r   r,   )r   r   r   s      r   test_https_with_cadefaultz%TestUrlopen.test_https_with_cadefaultN  s    ))>)B++R1C,DE""6<<#8#89R;gllJ'+  - : FE99 FEs#   *B!B=BB	BB#c                    t         | j                  d       t         j                  s| j                  d       d fd}t        j                  t         j                        }|j                  |       | j                  |t              }t        j                  t              }| j                  d|j                  z  |       | j                  d       y )	Nzssl module requiredzSNI support required in OpenSSLc                 
    |y r5   r    )ssl_sockserver_nameinitial_contextsni_names      r   cb_sniz*TestUrlopen.test_https_sni.<locals>.cb_sni\  s    "Hr   )rZ  ra  rV  zhttps://localhost:%srY  	localhost)r[  r<  HAS_SNI
SSLContextPROTOCOL_TLS_SERVERset_servername_callbackr>  r]  r\  r   r,   rD  )r   ro  rZ  r   rn  s       @r   test_https_snizTestUrlopen.test_https_sniV  s    ;MM/0{{MM;<	# ..!8!89''/))'N)S,,NC+gll:GL;/r   c                 ,   | j                         }t        j                  j                  d|j                  z  ddi      }t        j                  j                  |      5  	 d d d        | j                  |j                  d   d       y # 1 sw Y   )xY w)NrA  Rangezbytes=20-39r   )r5  r   r   Requestr,   r   rD  r  r   r   reqs      r   test_sending_headersz TestUrlopen.test_sending_headersf  s|    ##%nn$$%;gll%J.5}-E % G^^##C( )11':MJ )(s    B

Bc                    | j                         }t        j                  j                  d|j                  z  ddi      }t        j                  j                  |      5  	 d d d        | j                  d|j                  j                                | j                  d|j                  j                                y # 1 sw Y   ^xY w)NrA  zX-SoMe-hEaderfoobarrx  zX-Some-Header)
r5  r   r   ry  r,   r   assertInr  keysassertNotInrz  s      r   test_sending_headers_camelz&TestUrlopen.test_sending_headers_cameln  s    ##%nn$$%;gll%J.=x-H % J^^##C( )ow'?'?'D'D'FG'*B*B*G*G*IJ )(s    B??Cc                 2   | j                         }t        j                  j                  d|j                  z        5 }dD ]!  }| j                  t        ||      d|z         # | j                  |j                         d       d d d        y # 1 sw Y   y xY w)Nhttp://localhost:%s)r  r   infogeturlz3object returned from urlopen lacks the %s attributezcalling 'read' failed)r5  r   r   r   r,   r   r;  r  )r   r   open_urlattrs       r   
test_basiczTestUrlopen.test_basicw  s    ##%^^##$9GLL$HIX;$ 7 :>@D:E F < OOHMMO-DE	 JIIs   ABBc                 `   | j                         }t        j                  j                  d|j                  z        }|5  |j                         }d d d        | j                  t        j                  j                  d       | j                  |j                         d       y # 1 sw Y   UxY w)Nr  zEobject returned by 'info' is not an instance of email.message.Messageplain)r5  r   r   r   r,   r  assertIsInstanceemailmessageMessagerD  get_content_subtype)r   r   r  info_objs       r   	test_infozTestUrlopen.test_info  s    ##%>>))!GLL02}}H h(=(=B	C 	557A Xs   B$$B-c                    | j                         }t        j                  j                  d|j                  z        }|5  |j                         }d d d        | j                  d|j                  z         y # 1 sw Y   )xY w)Nr  )r5  r   r   r   r,   r  rD  )r   r   r  r/  s       r   test_geturlzTestUrlopen.test_geturl  sd    ##%>>))*?',,*NO//#C 3gllBC Xs   A88Bc                     d}| j                  dg |fg      }t        j                  j                  d|j                  z        }|D ]  }| j                  ||        y )NrN  r   r  )r5  r   r   r   r,   rD  )r   rE  r   r0  lines        r   test_iterationzTestUrlopen.test_iteration  s\    ,##c2/@%A$BC~~%%&;gll&JKDT#45 r   c                    dddddz  dz   g}dj                  |      }| j                  dg |fg      }t        j                  j	                  d	|j
                  z        }t        |      D ]9  \  }}| j                  |||   d
|dt        ||         dt        |             ; | j                  dz   t        |             y )Ns   We
s   got
s   here
s	   verylong i       
r   r   r  zFetched line number z1 doesn't match expected:
    Expected length was z, got rL   )	r0   r5  r   r   r   r,   	enumeraterD  r   )r   linesrE  r   r0  indexr  s          r   test_line_iterationzTestUrlopen.test_line_iteration  s    (I|d/BU/JKHHUO##c2/@%A$BC~~%%&;gll&JK$T?KE4T5< $Su%6D	CD +
 	CJ/r   c                 z   | j                  dg dfdg dfg      }t        j                  j                         }t        j                  j	                  d|j
                  z        }| j                  d |j                         |j                  |dj                  d             | j                  d|j                         | j                  d|j                  d             |j                  |dj                  d             | j                  d	|j                         | j                  d
|j                  d             y )Nr   s   anyr  1zus-ascii   1zContent-length
1234567890s
   123456789010)r5  r   r   r   ry  r,   rD  r0  r   rQ   
get_header)r   r   r   r   s       r   test_issue16464zTestUrlopen.test_issue16464  s    ##"f"f%
  ,,...(()>)MNw||,GSZZ
34w||,g001ABCG\00<=5w112BCDr   r5   )r   r   r   r   r   r   r   r5  r>  rF  rJ  rL  rR  rT  r^  re  rh  ru  r|  r  r  r  r  r  r  r  r   r   s   @r   r&  r&    s~    %	E289N17"-0 KKF	BD6
0Er   r&  c                  t    t        j                         } t        j                  t         j                  g|   y r5   )r   threading_setupunittestaddModuleCleanupthreading_cleanup)thread_infos    r   setUpModuler    s*    "224K.@@O;Or   __main__)-r   r   r  urllib.parser   urllib.requesthttp.serverr   r$   r  rN   testr   test.supportr   r   r   r[  ImportErrorrequires_working_socketr   dirname__file__herer0   r]  rd  r   r   r
   r%   r"   r:   r   r   r   TestCaser   requires_hashdigestr   r$  r&  r  r   mainr    r   r   <module>r     s    	         ' ) (    t ,	wwx dJ>GGLLz>B 
)// )4(y// (6v vr&t{{99 &X;t{{99 ;B'X&& 'T $##E48JX&& J 9JZ("VxE(## xEtP
 zHMMO W  
Cs   F F%$F%