
    <eK%                     *   d dl 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 ej        d            G d de j                  Z G d d	e j                  Z G d
 de j                  Zedk    r e j                     dS dS )    N)support)	os_helper)socket_helpernetworkc                        e Zd Zd Zd Zd ZdS )URLTimeoutTestc                 B    t          j        t          j                   d S N)socketsetdefaulttimeoutr   INTERNET_TIMEOUTselfs    */usr/lib/python3.11/test/test_urllibnet.pysetUpzURLTimeoutTest.setUp   s     !9:::::    c                 .    t          j        d            d S r
   )r   r   r   s    r   tearDownzURLTimeoutTest.tearDown   s     &&&&&r   c                 ~   |                      t          j        j                   t          j                            t          j                  j        }t          j
        |          5  t          j                            t          j                  }|                                 d d d            d S # 1 swxY w Y   d S r
   )
addCleanupurllibrequest
urlcleanupparseurlparser   TEST_HTTP_URLnetlocr   transient_interneturlopenread)r   domainfs      r   testURLreadzURLTimeoutTest.testURLread   s    1222&&w'<==D-f55 	 	&&w'<==AFFHHH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   '>B22B69B6N)__name__
__module____qualname__r   r   r#    r   r   r   r      sA        ; ; ;' ' '    r   r   c                   `    e Zd ZdZdZd Zej        d             Zd Z	d Z
d Zd Zd	 Zd
 ZdS )urlopenNetworkTestsaA  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.

    setUp is not used for always constructing a connection to
    http://www.pythontest.net/ since there a few tests that don't use that address
    and making a connection is expensive enough to warrant minimizing unneeded
    connections.

    http://www.pythontest.net/c                 N    |                      t          j        j                   d S r
   r   r   r   r   r   s    r   r   zurlopenNetworkTests.setUp6       122222r   c              /     K   |d         }t          j        |          5  t          j        j        |i |}	 |V  |                                 n# |                                 w xY w	 d d d            d S # 1 swxY w Y   d S Nr   )r   r   r   r   r   close)r   argskwargsresourcers        r   r   zurlopenNetworkTests.urlopen:   s      7-h77 	 	&777A										 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s'   A7AA7A&&A77A;>A;c                    |                      | j                  5 }dD ])}|                     t          ||          d|z             *|                     |                                d           d d d            d S # 1 swxY w Y   d S )N)r    readline	readlinesfilenor0   infogeturlz3object returned from urlopen lacks the %s attributezcalling 'read' failed)r   url
assertTruehasattrr    )r   open_urlattrs      r   
test_basiczurlopenNetworkTests.test_basicD   s    \\$(## 	Fx+ I I$ 7 7 :ACG:H I I I IOOHMMOO-DEEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs   AA==BBc                 $   |                      | j                  5 }|                     |                                t          d           |                     |                                t          d           d d d            d S # 1 swxY w Y   d S )Nz readline did not return a stringzreadlines did not return a list)r   r;   assertIsInstancer6   bytesr7   list)r   r>   s     r   test_readlinesz"urlopenNetworkTests.test_readlinesM   s    \\$(## 	Ex!!("3"3"5"5u"DF F F!!("4"4"6"6"CE E E	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es   ABB	B	c                 0   |                      | j                  5 }|                                }|                     |t          j        j        d           |                     |                                d           d d d            d S # 1 swxY w Y   d S )NzEobject returned by 'info' is not an instance of email.message.Messagehtml)	r   r;   r9   rB   emailmessageMessageassertEqualget_content_subtype)r   r>   info_objs      r   	test_infozurlopenNetworkTests.test_infoU   s    \\$(## 	Ex}}H!!(EM,A#FG G G X99;;VDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es   A#BBBc                     |                      | j                  5 }|                                }|                     || j                   d d d            d S # 1 swxY w Y   d S r
   )r   r;   r:   rK   )r   r>   
gotten_urls      r   test_geturlzurlopenNetworkTests.test_geturl^   s    \\$(## 	3x!**JZ222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s   0AAAc                    | j         dz   }t          j        |          5  |                     t                    5  t
          j                                                            |          }d d d            n# 1 swxY w Y   	 |	                                }|
                                 n# |
                                 w xY w|                     |d           d d d            d S # 1 swxY w Y   d S )NXXXinvalidXXXi  )r;   r   r   assertWarnsDeprecationWarningr   r   FancyURLopeneropengetcoder0   rK   )r   URLr>   codes       r   test_getcodez urlopenNetworkTests.test_getcoded   st   h(-c22 	( 	(!!"455 E E!>88::??DDE E E E E E E E E E E E E E E!''))        T3'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(sL   C)2A8,C)8A<	<C)?A<	 C)B-C)-CC))C-0C-c                 X   d}	 t          j        |           |                     d|z             n# t          $ r Y nw xY wd}|                     t          |          5  t
          j                            d                    |                     d d d            d S # 1 swxY w Y   d S )Nzsadflkjsasf.i.nvali.d.z&%r should not resolve for test to workzuopening an invalid URL did not raise OSError; can be caused by a broken DNS server (e.g. returns 404 or hijacks page)msgz
http://{}/)	r   gethostbynameskipTestOSErrorassertRaisesr   r   r   format)r   bogus_domainfailure_explanations      r   test_bad_addressz$urlopenNetworkTests.test_bad_addressp   s"   * 0		S ... MMB\QRRRR  	 	 	 D		 D w,?@@ 	F 	FN""<#6#6|#D#DEEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs   1 
>>3BB#&B#N)r$   r%   r&   __doc__r;   r   
contextlibcontextmanagerr   r@   rE   rN   rQ   r[   rf   r'   r   r   r)   r)   %   s          'C3 3 3   F F FE E EE E E3 3 3
( 
( 
($F $F $F $F $Fr   r)   c                   Z    e Zd ZdZd Zej        d             Zd Zd Z	d Z
dZd Zd	 Zd
S )urlretrieveNetworkTestsz3Tests urllib.request.urlretrieve using the network.c                 N    |                      t          j        j                   d S r
   r,   r   s    r   r   zurlretrieveNetworkTests.setUp   r-   r   c              /     K   |d         }t          j        |          5  t          j        j        |i |\  }}	 ||fV  t          j        |           n# t          j        |           w xY w	 d d d            d S # 1 swxY w Y   d S r/   )r   r   r   r   urlretriever   unlink)r   r1   r2   r3   file_locationr9   s         r   rn   z#urlretrieveNetworkTests.urlretrieve   s      7-h77 	0 	0"(."<d"Mf"M"MM40#T)))) ////	 /////	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s(   A<A A<A++A<<B B c                 x   |                      | j                  5 \  }}|                     t          j                            |          d           t          |d          5 }|                     |                                d           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nz9file location returned by urlretrieve is not a valid pathrbz=reading from the file location returned by urlretrieve failed)rn   logor<   ospathexistsrW   r    r   rp   r9   r"   s       r   r@   z"urlretrieveNetworkTests.test_basic   s3   di(( 	:,A]DOOBGNN=99 <? @ @ @mT** :a +9 : : :: : : : : : : : : : : : : : :	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s6   AB/")BB/B	B/B	B//B36B3c                    |                      | j        t          j                  5 \  }}|                     |t          j                   |                     t          j                            |                     t          |d          5 }|                     |
                                d           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nrr   z"reading from temporary file failed)rn   rs   r   TESTFNrK   r<   rt   ru   rv   rW   r    rw   s       r   test_specified_pathz+urlretrieveNetworkTests.test_specified_path   sc   di'.0 0 	P3HM4]I,<===OOBGNN=99:::mT** Pa*NOOOP P P P P P P P P P P P P P P		P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	Ps6   A&C)C5CC	CC		CC Cc                     |                      | j                  5 \  }}|                     |t          j        j        d           d d d            d S # 1 swxY w Y   d S )Nz0info is not an instance of email.message.Message)rn   rs   rB   rH   rI   rJ   )r   rp   r9   s      r   test_headerz#urlretrieveNetworkTests.test_header   s    di(( 	V,A]D!!$(="TV V V	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	Vs   *AAAr*   c                    |                      | j                  5 \  }}|                    d          }d}	 t          j        ||           n(# t
          $ r |                     d|z             Y nw xY wd d d            d S # 1 swxY w Y   d S )NDatez%a, %d %b %Y %H:%M:%S GMTzDate value not in %r format)rn   rs   gettimestrptime
ValueErrorfail)r   rp   fileheaders	datevalue
dateformats        r   test_data_headerz(urlretrieveNetworkTests.test_data_header   s    di(( 	F,H]K#//I4JFi4444 F F F		7*DEEEEEF	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs4   BAB"A2/B1A22BB	Bc                    g fd}|                      | j        |          5 \  }}t          |d                   }d d d            n# 1 swxY w Y   t                    }|                     t                    dd           |                     d         d         d           |                     d         d         dd|z             |                     d         d	         |           |                     d
         d	         |           d D             }|                     d         d         h|d|z             |                     d
         d         d         d         z  |d|z             d S )Nc                 8                         | ||f           d S r
   )append)blocks
block_size
total_sizerecordss      r   recording_reporthookzEurlretrieveNetworkTests.test_reporthook.<locals>.recording_reporthook   s"    NNFJ
;<<<<<r   )
reporthookzContent-Length   zKThere should always be two calls; the first one before the transfer starts.r]   r   zblock size can't be 0 in %s   c                     h | ]\  }}}|	S r'   r'   ).0_r   s      r   	<setcomp>z:urlretrieveNetworkTests.test_reporthook.<locals>.<setcomp>   s    BBB&6aQzBBBr   zblock sizes in %s must be equalz9number of blocks * block size must be >= total size in %s)rn   rs   intreprassertGreaterlenrK   assertGreaterEqual)r   r   rp   r   expected_sizerecords_reprblock_sizesr   s          @r   test_reporthookz'urlretrieveNetworkTests.test_reporthook   s   	= 	= 	= 	= 	= di4HII 	? N,{,< =>>M	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? G}}3w<< 1N 	O 	O 	OA***71:a=!<|K 	 	M 	M 	MA666Q777BB'BBB'!*Q-+>M 	 	O 	O 	OAwqz!} <m%79E%F 	  	G 	G 	G 	G 	Gs   A

AAN)r$   r%   r&   rg   r   rh   ri   rn   r@   rz   r|   rs   r   r   r'   r   r   rk   rk      s        ==3 3 3 0 0 0: : :P P PV V V (DF F FG G G G Gr   rk   __main__)unittesttestr   test.supportr   r   rh   r   urllib.parser   urllib.requestrt   email.messagerH   r   requiresTestCaser   r)   rk   r$   mainr'   r   r   <module>r      ss          " " " " " " & & & & & &              				              X&   &oF oF oF oF oF(+ oF oF oFdLG LG LG LG LGh/ LG LG LG^ zHMOOOOO r   