o
    ±ó®g8  ã                   @   s@  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	d dl
Z
d dlZd dlZd dlZe d¡ dd„ Zd	d
„ Ze de	jv d¡ZeejjejjƒZG dd„ deƒZeee jdZeee jdZ eee jdZ!G dd„ dej"ƒZ#G dd„ dej"ƒZ$G dd„ dej"ƒZ%G dd„ dej"ƒZ&e'dkrže (¡  dS dS )é    N)Úsupport)Ú	os_helper)Úsocket_helper)Úsanepathname2urlZnetworkc                 O   sN   t dƒD ] }z
| |i |¤ŽW   S  |y$ } z|}W Y d }~qd }~ww |‚)Né   )Úrange)ÚfuncÚexcÚargsÚkwargsÚiÚeÚlast_exc© r   úJ/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_urllib2net.pyÚ_retry_thrice   s   €þr   c                    s   ‡ ‡fdd„}|S )Nc                     s   t ˆˆ g| ¢R i |¤ŽS ©N)r   )r
   r   ©r	   r   r   r   Úwrapped   s   z(_wrap_with_retry_thrice.<locals>.wrappedr   )r   r	   r   r   r   r   Ú_wrap_with_retry_thrice   s   r   ZTRAVISz%bpo-35411: skip FTP test on Travis CIc                   @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
ÚTransientResourcezŽRaise ResourceDenied if an exception is raised while the context manager
    is in effect that matches the specified exception and attributes.c                 K   s   || _ || _d S r   )r	   Úattrs)Úselfr	   r   r   r   r   Ú__init__1   s   
zTransientResource.__init__c                 C   s   | S r   r   ©r   r   r   r   Ú	__enter__5   ó   zTransientResource.__enter__Nc                 C   s\   |dur*t | j|ƒr,| j ¡ D ]\}}t||ƒs dS t||ƒ|kr% dS qtdƒ‚dS dS )z§If type_ is a subclass of self.exc and value has attributes matching
        self.attrs, raise ResourceDenied.  Otherwise let the exception
        propagate (if any).Nz%an optional resource is not available)Ú
issubclassr	   r   ÚitemsÚhasattrÚgetattrZResourceDenied)r   Ztype_ÚvalueÚ	tracebackÚattrZ
attr_valuer   r   r   Ú__exit__8   s   
ÿùzTransientResource.__exit__)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   r   r   r   r   r   ,   s
    r   )Úerrnoc                   @   s   e Zd ZdZdS )Ú	AuthTestsz&Tests urllib2 authentication features.N)r%   r&   r'   r(   r   r   r   r   r*   M   s    r*   c                   @   s   e Zd Zdd„ ZdS )ÚCloseSocketTestc                 C   sr   |   tjj¡ tj}t |¡  t|ƒ}|j	}|  
|j¡ | ¡  |  |j¡ W d   ƒ d S 1 s2w   Y  d S r   )Ú
addCleanupÚurllibÚrequestÚ
urlcleanupr   ÚTEST_HTTP_URLr   Útransient_internetÚ_urlopen_with_retryÚfpÚassertFalseÚclosedÚcloseÚ
assertTrue)r   ÚurlÚresponseÚsockr   r   r   Ú
test_closev   s   "ûzCloseSocketTest.test_closeN)r%   r&   r'   r;   r   r   r   r   r+   t   s    r+   c                   @   sd   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	d
„ Zdd„ Z	e
 d¡dd„ ƒZddd„Zdd„ ZdS )ÚOtherNetworkTestsc                 C   s   d S )Nr   Ztest_urllib2net)ÚloggingÚ	getLoggerÚ
addHandlerÚStreamHandler)r   r=   Úloggerr   r   r   ÚsetUp…   r   zOtherNetworkTests.setUpc                 C   s&   ddd t jjfg}|  ||  ¡ ¡ d S )Nzftp://www.pythontest.net/READMEz*ftp://www.pythontest.net/non-existent-file)r-   ÚerrorÚURLErrorÚ
_test_urlsÚ_extra_handlers)r   Úurlsr   r   r   Útest_ftpŽ   s   ÿþzOtherNetworkTests.test_ftpc              	   C   sŠ   t j}t|dƒ}z+| d¡ | ¡  dttj |¡ƒ dd t	j
jfg}| j||  ¡ dd W t |¡ nt |¡ w |  tt	jjd¡ d S )NÚwz	hi there
zfile:zfile:///nonsensename/etc/passwdT)Úretryz./relative_path/to/file)r   ÚTESTFNÚopenÚwriter6   r   ÚosÚpathÚabspathr-   rC   rD   rE   rF   ÚremoveZassertRaisesÚ
ValueErrorr.   Úurlopen)r   rK   ÚfrG   r   r   r   Ú	test_file—   s   

ÿþzOtherNetworkTests.test_filec                 C   s\   d}t  |¡ tj |¡}tj |¡}|  | ¡ d¡ W d   ƒ d S 1 s'w   Y  d S )Nz)http://www.pythontest.net/index.html#frag©r   r1   r-   r.   ÚRequestrS   ÚassertEqualÚgeturl)r   Zurlwith_fragÚreqÚresr   r   r   Útest_urlwithfragÄ   ó   
ÿ"ýz"OtherNetworkTests.test_urlwithfragc                 C   s\   d}t  |¡ tj |¡}tj |¡}|  | ¡ d¡ W d   ƒ d S 1 s'w   Y  d S )Nz*http://www.pythontest.net/redir/with_frag/z)http://www.pythontest.net/elsewhere/#fragrV   )r   Zredirect_url_with_fragrZ   r[   r   r   r   Útest_redirect_url_withfragÌ   r]   z,OtherNetworkTests.test_redirect_url_withfragc                 C   sª   t j}t |¡C tj ¡ }tj |¡}|  | 	¡ ¡ | 
|¡ |  | 	¡ ¡ |  | d¡¡ | dd¡ | 
|¡ |  | d¡d¡ W d   ƒ d S 1 sNw   Y  d S )Nz
User-agentz
User-Agentz
Test-Agent)r   r0   r   r1   r-   r.   Úbuild_openerrW   r4   Zheader_itemsrL   r7   Z
has_headerZ
add_headerrX   Z
get_header)r   r8   Úopenerr.   r   r   r   Útest_custom_headersÔ   s   


"÷z%OtherNetworkTests.test_custom_headersz XXX: http://www.imdb.com is gonec              	   C   sª   d}t  |¡D ztj |¡}W d   ƒ n1 sw   Y  W n ty.   |  d¡ Y nw |  |¡ tj |¡}| ¡ }|  |¡ W d   ƒ d S 1 sNw   Y  d S )Nzhttp://www.imdb.comzOurlopen failed for site not sending                            Connection:close)	r   r1   r-   r.   rS   rR   Zfailr7   Úread)r   ZURLr[   rZ   r   r   r   Útest_sites_no_connection_closeá   s   ÿ€ÿ
"ôz0OtherNetworkTests.test_sites_no_connection_closeTc                 C   sâ  dd l }dd l}| d¡j}tjj|Ž j}|rt|tj	j
ƒ}|D ]Î}| j|d¾ t|tƒr4|\}}	}
nd  }	}
t |¡• z	|||	tjƒ}W n( tyo } z|
rdd|
||	t|ƒ|f }|  ||
|¡ n‚ W Y d }~n`d }~ww zDt8 t& t | ¡ }|dt|ƒ ƒ W d   ƒ n1 sw   Y  W d   ƒ n1 sŸw   Y  W d   ƒ n1 s®w   Y  W n tyÆ   td| tjd Y nw | ¡  W d   ƒ n1 sÕw   Y  | d¡ W d   ƒ q 1 séw   Y  q d S )	Nr   Ztest_urllib2)r8   z5Didn't get expected error(s) %s for %s %s, got %s: %szread %d bytesz<timeout: %s>)Úfilegš™™™™™¹?) Útimer=   r>   Údebugr-   r.   r_   rL   r   rC   rD   ZsubTestÚ
isinstanceÚtupler   r1   r   ZINTERNET_TIMEOUTÚOSErrorÚtypeZassertIsInstanceÚtime_outÚsocket_peer_resetÚioerror_peer_resetrb   ÚlenÚTimeoutErrorÚprintÚsysÚstderrr6   Úsleep)r   rG   ÚhandlersrJ   re   r=   rf   rS   r8   rZ   Zexpected_errrT   ÚerrÚmsgÚbufr   r   r   rE   ö   s^   
ÿþ€üÿþü€ € €ÿ€ìåÿzOtherNetworkTests._test_urlsc                 C   s2   g }t j ¡ }|  |j¡ | d¡ | |¡ |S )Né   )r-   r.   ZCacheFTPHandlerr,   Úclear_cacheZ
setTimeoutÚappend)r   rt   Zcfhr   r   r   rF     s   


z!OtherNetworkTests._extra_handlersN)T)r%   r&   r'   rB   Úskip_ftp_test_on_travisrH   rU   r\   r^   ra   ÚunittestÚskiprc   rE   rF   r   r   r   r   r<   „   s    	
-

'r<   c                   @   sh   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdZe	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	dd„ ƒZdS )ÚTimeoutTestc                 C   s   |   tjj¡ d S r   )r,   r-   r.   r/   r   r   r   r   rB   )  s   zTimeoutTest.setUpc                 C   sp   |   t ¡ ¡ tj}tj|d d t|ƒ}|  |j	¡ |   |j
jj ¡ ¡ W d   ƒ d S 1 s1w   Y  d S ©N©Útimeout)ÚassertIsNoneÚsocketÚgetdefaulttimeoutr   r0   r   r1   r2   r,   r6   r3   ÚrawÚ_sockÚ
gettimeout©r   r8   Úur   r   r   Útest_http_basic-  s   "ýzTimeoutTest.test_http_basicc              
   C   s”   |   t ¡ ¡ tj}t |¡1 t d¡ zt|ƒ}|  	|j
¡ W t d ¡ nt d ¡ w |  |jjj ¡ d¡ W d   ƒ d S 1 sCw   Y  d S ©Né<   )r‚   rƒ   r„   r   r0   r   r1   Úsetdefaulttimeoutr2   r,   r6   rX   r3   r…   r†   r‡   rˆ   r   r   r   Útest_http_default_timeout5  s   
"ùz%TimeoutTest.test_http_default_timeoutc              
   C   s–   |   t ¡ ¡ tj}t |¡2 t d¡ zt|d d}|  	|j
¡ W t d ¡ nt d ¡ w |   |jjj ¡ ¡ W d   ƒ d S 1 sDw   Y  d S ©NrŒ   r€   )r‚   rƒ   r„   r   r0   r   r1   r   r2   r,   r6   r3   r…   r†   r‡   rˆ   r   r   r   Útest_http_no_timeoutA  s   
"ùz TimeoutTest.test_http_no_timeoutc                 C   sd   t j}t |¡  t|dd}|  |j¡ |  |jj	j
 ¡ d¡ W d   ƒ d S 1 s+w   Y  d S )Néx   r€   )r   r0   r   r1   r2   r,   r6   rX   r3   r…   r†   r‡   rˆ   r   r   r   Útest_http_timeoutM  s   "ýzTimeoutTest.test_http_timeoutzftp://www.pythontest.net/c                 C   sp   |   t ¡ ¡ tj| jd d t| jƒ}|  |j¡ |   |j	j	j
j ¡ ¡ W d   ƒ d S 1 s1w   Y  d S r   )r‚   rƒ   r„   r   r1   ÚFTP_HOSTr2   r,   r6   r3   r…   r†   r‡   ©r   r‰   r   r   r   Útest_ftp_basicV  s   
"ýzTimeoutTest.test_ftp_basicc              
   C   s”   |   t ¡ ¡ t | j¡3 t d¡ zt| jƒ}|  |j	¡ W t d ¡ nt d ¡ w |  
|jjjj ¡ d¡ W d   ƒ d S 1 sCw   Y  d S r‹   )r‚   rƒ   r„   r   r1   r“   r   r2   r,   r6   rX   r3   r…   r†   r‡   r”   r   r   r   Útest_ftp_default_timeout^  s   

"ùz$TimeoutTest.test_ftp_default_timeoutc              
   C   s–   |   t ¡ ¡ t | j¡4 t d¡ zt| jd d}|  |j	¡ W t d ¡ nt d ¡ w |   |j
j
jj ¡ ¡ W d   ƒ d S 1 sDw   Y  d S r   )r‚   rƒ   r„   r   r1   r“   r   r2   r,   r6   r3   r…   r†   r‡   r”   r   r   r   Útest_ftp_no_timeoutj  s   
"ùzTimeoutTest.test_ftp_no_timeoutc                 C   sd   t  | j¡" t| jdd}|  |j¡ |  |jjjj	 
¡ d¡ W d   ƒ d S 1 s+w   Y  d S r   )r   r1   r“   r2   r,   r6   rX   r3   r…   r†   r‡   r”   r   r   r   Útest_ftp_timeoutv  s
   "ýzTimeoutTest.test_ftp_timeoutN)r%   r&   r'   rB   rŠ   rŽ   r   r’   r“   r{   r•   r–   r—   r˜   r   r   r   r   r~   (  s    


r~   Ú__main__))r)   r|   Útestr   Ztest.supportr   r   Ztest.test_urllib2r   rN   rƒ   Zurllib.errorr-   Zurllib.requestrq   Zrequiresr   r   ZskipIfÚenvironr{   r.   rS   rC   rD   r2   Úobjectr   ri   Z	ETIMEDOUTrk   Z
ECONNRESETrl   rm   ZTestCaser*   r+   r<   r~   r%   Úmainr   r   r   r   Ú<module>   s@    
	ÿÿ' %Vÿ