B
    u9a“1  ã               @   sæ   d dl 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Zdd„ Zdd	„ Ze  d
ejkd¡Zeejjejjƒ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 )é    N)Úsupport)Úsanepathname2urlZnetworké<   c             O   sN   xDt dƒD ]8}y
| ||ŽS  |k
r@ } z
|}w
W d d }~X Y q
X q
W |‚d S )Né   )Úrange)ÚfuncÚexcÚargsÚkwargsÚiÚeÚlast_exc© r   ú%/usr/lib/python3.7/test_urllib2net.pyÚ_retry_thrice   s    
r   c                s   ‡ ‡fdd„}|S )Nc                 s   t ˆˆ f| ž|ŽS )N)r   )r	   r
   )r   r   r   r   Úwrapped   s    z(_wrap_with_retry_thrice.<locals>.wrappedr   )r   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S )Ú	AuthTestsz&Tests urllib2 authentication features.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   +   s   r   c               @   s   e Zd Zdd„ ZdS )ÚCloseSocketTestc          	   C   sN   t j}t  |¡4 t|ƒ}|j}|  |j¡ | ¡  |  |j¡ W d Q R X d S )N)	r   ÚTEST_HTTP_URLÚtransient_internetÚ_urlopen_with_retryÚfpÚassertFalseÚclosedÚcloseÚ
assertTrue)ÚselfÚurlZresponseZsockr   r   r   Ú
test_closeT   s    zCloseSocketTest.test_closeN)r   r   r   r#   r   r   r   r   r   R   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   )r!   r   r   r   ÚsetUp`   s    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)ÚurllibÚerrorÚURLErrorÚ
_test_urlsÚ_extra_handlers)r!   Úurlsr   r   r   Útest_ftpi   s    zOtherNetworkTests.test_ftpc          	   C   s€   t j}t|dƒ}zL| d¡ | ¡  dttj |¡ƒ dd t	j
jfg}| j||  ¡ dd W d t |¡ X |  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Úwriter   r   ÚosÚpathÚabspathr&   r'   r(   r)   r*   ÚremoveZassertRaisesÚ
ValueErrorÚrequestÚurlopen)r!   r/   Úfr+   r   r   r   Ú	test_filer   s    

zOtherNetworkTests.test_filec          	   C   sF   d}t  |¡. tj |¡}tj |¡}|  | ¡ d¡ W d Q R X d S )Nz)http://www.pythontest.net/index.html#frag)r   r   r&   r7   ÚRequestr8   ÚassertEqualÚgeturl)r!   Zurlwith_fragÚreqÚresr   r   r   Útest_urlwithfragŸ   s    
z"OtherNetworkTests.test_urlwithfragc          	   C   sF   d}t  |¡. tj |¡}tj |¡}|  | ¡ d¡ W d Q R X d S )Nz*http://www.pythontest.net/redir/with_frag/z)http://www.pythontest.net/elsewhere/#frag)r   r   r&   r7   r;   r8   r<   r=   )r!   Zredirect_url_with_fragr>   r?   r   r   r   Útest_redirect_url_withfrag§   s    
z,OtherNetworkTests.test_redirect_url_withfragc          	   C   s”   t j}t  |¡z tj ¡ }tj |¡}|  | ¡ ¡ | 	|¡ |  
| ¡ ¡ |  
| d¡¡ | dd¡ | 	|¡ |  | d¡d¡ W d Q R X d S )Nz
User-agentz
User-Agentz
Test-Agent)r   r   r   r&   r7   Úbuild_openerr;   r   Zheader_itemsr0   r    Z
has_headerZ
add_headerr<   Z
get_header)r!   r"   Úopenerr7   r   r   r   Útest_custom_headers¯   s    


z%OtherNetworkTests.test_custom_headersz XXX: http://www.imdb.com is gonec             C   s   d}t  |¡x ytj |¡}W d Q R X W n, tk
rX } z|  d¡ W d d }~X Y nX |  |¡ tj |¡}| ¡ }|  |¡ W d Q R X d S )Nzhttp://www.imdb.comzOurlopen failed for site not sending                            Connection:close)	r   r   r&   r7   r8   r6   Zfailr    Úread)r!   ZURLr?   r   r>   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
ƒ}xL|D ]B}| j|d* t|tƒrp|\}}	}
nd  }	}
t |¡ì y|||	tƒ}W nN tk
râ } z0|
rÐd|
||	t|ƒ|f }|  ||
|¡ n‚ W d d }~X Y nˆX yRtjB tj0 tj | ¡ }|dt|ƒ ƒ W d Q R X W d Q R X W d Q R X W n* tjk
r`   td| tjd Y nX | ¡  W d Q R X | d¡ W d Q R X qDW d S )	Nr   Ztest_urllib2)r"   z5Didn't get expected error(s) %s for %s %s, got %s: %szread %d bytesz<timeout: %s>)Úfilegš™™™™™¹?) ÚtimeÚloggingZ	getLoggerÚdebugr&   r7   rB   r0   r   r'   r(   ZsubTestÚ
isinstanceÚtupler   r   ÚTIMEOUTÚOSErrorÚtypeZassertIsInstanceZtime_outZsocket_peer_resetZioerror_peer_resetrE   ÚlenÚsocketÚtimeoutÚprintÚsysÚstderrr   Zsleep)r!   r+   Úhandlersr.   rH   rI   rJ   r8   r"   r>   Zexpected_errr9   ÚerrÚmsgÚbufr   r   r   r)   Ñ   s<    
2zOtherNetworkTests._test_urlsc             C   s2   g }t j ¡ }|  |j¡ | d¡ | |¡ |S )Né   )r&   r7   ZCacheFTPHandlerÚ
addCleanupZclear_cacheZ
setTimeoutÚappend)r!   rV   Zcfhr   r   r   r*   ø   s    


z!OtherNetworkTests._extra_handlersN)T)r   r   r   r%   Úskip_ftp_test_on_travisr,   r:   r@   rA   rD   ÚunittestÚskiprF   r)   r*   r   r   r   r   r$   _   s   		-
'r$   c               @   s`   e Z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   sZ   |   t ¡ ¡ tj}tj|d d. t|ƒ}|  |j¡ |   |j	j
j ¡ ¡ W d Q R X d S )N)rR   )ÚassertIsNonerQ   Úgetdefaulttimeoutr   r   r   r   r[   r   r   ÚrawÚ_sockÚ
gettimeout)r!   r"   Úur   r   r   Útest_http_basic  s    zTimeoutTest.test_http_basicc          
   C   st   |   t ¡ ¡ tj}t |¡L t d¡ zt|ƒ}|  |j	¡ W d t d ¡ X |  
|jjj ¡ d¡ W d Q R X d S )Nr   )ra   rQ   rb   r   r   r   Úsetdefaulttimeoutr   r[   r   r<   r   rc   rd   re   )r!   r"   rf   r   r   r   Útest_http_default_timeout  s    
z%TimeoutTest.test_http_default_timeoutc          
   C   sv   |   t ¡ ¡ tj}t |¡N t d¡ zt|d d}|  |j	¡ W d t d ¡ X |   |j
jj ¡ ¡ W d Q R X d S )Nr   )rR   )ra   rQ   rb   r   r   r   rh   r   r[   r   r   rc   rd   re   )r!   r"   rf   r   r   r   Útest_http_no_timeout  s    
z TimeoutTest.test_http_no_timeoutc          	   C   sN   t j}t  |¡4 t|dd}|  |j¡ |  |jjj	 
¡ d¡ W d Q R X d S )Néx   )rR   )r   r   r   r   r[   r   r<   r   rc   rd   re   )r!   r"   rf   r   r   r   Útest_http_timeout$  s
    zTimeoutTest.test_http_timeoutzftp://www.pythontest.net/c          	   C   sZ   |   t ¡ ¡ tj| jd d2 t| jƒ}|  |j¡ |   |j	j	j
j ¡ ¡ W d Q R X d S )N)rR   )ra   rQ   rb   r   r   ÚFTP_HOSTr   r[   r   r   rc   rd   re   )r!   rf   r   r   r   Útest_ftp_basic-  s
    
zTimeoutTest.test_ftp_basicc          
   C   st   |   t ¡ ¡ t | j¡P t d¡ zt| jƒ}|  |j	¡ W d t d ¡ X |  
|jjjj ¡ d¡ W d Q R X d S )Nr   )ra   rQ   rb   r   r   rm   rh   r   r[   r   r<   r   rc   rd   re   )r!   rf   r   r   r   Útest_ftp_default_timeout5  s    

z$TimeoutTest.test_ftp_default_timeoutc          
   C   sv   |   t ¡ ¡ t | j¡R t d¡ zt| jd d}|  |j	¡ W d t d ¡ X |   |j
j
jj ¡ ¡ W d Q R X d S )Nr   )rR   )ra   rQ   rb   r   r   rm   rh   r   r[   r   r   rc   rd   re   )r!   rf   r   r   r   Útest_ftp_no_timeoutA  s    
zTimeoutTest.test_ftp_no_timeoutc          	   C   sN   t  | j¡8 t| jdd}|  |j¡ |  |jjjj	 
¡ d¡ W d Q R X d S )Nr   )rR   )r   r   rm   r   r[   r   r<   r   rc   rd   re   )r!   rf   r   r   r   Útest_ftp_timeoutM  s    zTimeoutTest.test_ftp_timeoutN)r   r   r   rg   ri   rj   rl   rm   r]   rn   ro   rp   rq   r   r   r   r   r`     s   r`   Ú__main__)r^   Ztestr   Ztest.test_urllib2r   r2   rQ   Zurllib.errorr&   Zurllib.requestrT   ZrequiresrM   r   r   ZskipIfÚenvironr]   r7   r8   r'   r(   r   ZTestCaser   r   r$   r`   r   Úmainr   r   r   r   Ú<module>   s,   
	
' %R