B
    u9a›#  ã               @   s    d dl 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 d¡ 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)ÚsupportZnetworkc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚURLTimeoutTestg      >@c             C   s   t  | j¡ d S )N)ÚsocketÚsetdefaulttimeoutÚTIMEOUT)Úself© r   ú$/usr/lib/python3.7/test_urllibnet.pyÚsetUp   s    zURLTimeoutTest.setUpc             C   s   t  d ¡ d S )N)r   r   )r   r   r   r	   ÚtearDown   s    zURLTimeoutTest.tearDownc          	   C   s@   t j tj¡j}t |¡ t j tj¡}| 	¡  W d Q R X d S )N)
ÚurllibÚparseZurlparser   ZTEST_HTTP_URLZnetlocÚtransient_internetÚrequestÚurlopenÚread)r   ÚdomainÚfr   r   r	   ÚtestURLread   s    zURLTimeoutTest.testURLreadN)Ú__name__Ú
__module__Ú__qualname__r   r
   r   r   r   r   r   r	   r      s   r   c               @   sR   e Zd ZdZdZejdd„ ƒZdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zd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.

    zhttp://www.pythontest.net/c          	   o   sF   |d }t  |¡* tjj||Ž}z
|V  W d | ¡  X W d Q R X d S )Nr   )r   r   r   r   r   Úclose)r   ÚargsÚkwargsÚresourceÚrr   r   r	   r   3   s    
zurlopenNetworkTests.urlopenc          	   C   sP   |   | j¡:}x"dD ]}|  t||ƒd| ¡ qW |  | ¡ d¡ W d Q R X d S )N)r   ÚreadlineÚ	readlinesÚfilenor   ÚinfoÚgeturlz3object returned from urlopen lacks the %s attributezcalling 'read' failed)r   ÚurlÚ
assertTrueÚhasattrr   )r   Úopen_urlÚattrr   r   r	   Ú
test_basic=   s
    
zurlopenNetworkTests.test_basicc          	   C   s@   |   | j¡*}|  | ¡ td¡ |  | ¡ td¡ W d Q R X d S )Nz readline did not return a stringzreadlines did not return a list)r   r#   ÚassertIsInstancer   Úbytesr   Úlist)r   r&   r   r   r	   Útest_readlinesF   s
    z"urlopenNetworkTests.test_readlinesc          	   C   sF   |   | j¡0}| ¡ }|  |tjjd¡ |  | ¡ d¡ W d Q R X d S )NzEobject returned by 'info' is not an instance of email.message.MessageZhtml)	r   r#   r!   r)   ÚemailÚmessageÚMessageÚassertEqualZget_content_subtype)r   r&   Zinfo_objr   r   r	   Ú	test_infoN   s
    zurlopenNetworkTests.test_infoc          	   C   s2   |   | j¡}| ¡ }|  || j¡ W d Q R X d S )N)r   r#   r"   r0   )r   r&   Z
gotten_urlr   r   r	   Útest_geturlW   s    zurlopenNetworkTests.test_geturlc          
   C   sn   | j d }t |¡P |  t¡ tj ¡  |¡}W d Q R X z| 	¡ }W d | 
¡  X |  |d¡ W d Q R X d S )NZXXXinvalidXXXi”  )r#   r   r   ZassertWarnsÚDeprecationWarningr   r   ZFancyURLopenerÚopenZgetcoder   r0   )r   ZURLr&   Úcoder   r   r	   Útest_getcode]   s    

z urlopenNetworkTests.test_getcodec          	   C   sj   d}yt  |¡ W n tk
r&   Y nX |  d| ¡ d}| jt|d tj d |¡¡ W d Q R X 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   ZgethostbynameÚOSErrorZskipTestZassertRaisesr   r   r   Úformat)r   Zbogus_domainZfailure_explanationr   r   r	   Útest_bad_addressi   s    z$urlopenNetworkTests.test_bad_addressN)r   r   r   Ú__doc__r#   Ú
contextlibÚcontextmanagerr   r(   r,   r1   r2   r6   r:   r   r   r   r	   r   "   s   
		r   c               @   sJ   e Zd ZdZejdd„ ƒZdd„ Zdd„ Zdd	„ Z	d
Z
dd„ Zdd„ ZdS )ÚurlretrieveNetworkTestsz3Tests urllib.request.urlretrieve using the network.c          
   o   sP   |d }t  |¡4 tjj||Ž\}}z||fV  W d t  |¡ X W d Q R X d S )Nr   )r   r   r   r   ÚurlretrieveÚunlink)r   r   r   r   Úfile_locationr!   r   r   r	   r?   “   s    z#urlretrieveNetworkTests.urlretrievec          
   C   sZ   |   | j¡D\}}|  tj |¡d¡ t|dƒ}|  | ¡ d¡ W d Q R X W d Q R X d S )Nz9file location returned by urlretrieve is not a valid pathÚrbz=reading from the file location returned by urlretrieve failed)r?   Úlogor$   ÚosÚpathÚexistsr4   r   )r   rA   r!   r   r   r   r	   r(      s    z"urlretrieveNetworkTests.test_basicc          
   C   sj   |   | jtj¡P\}}|  |tj¡ |  tj |¡¡ t	|dƒ}|  | 
¡ d¡ W d Q R X W d Q R X d S )NrB   z"reading from temporary file failed)r?   rC   r   ZTESTFNr0   r$   rD   rE   rF   r4   r   )r   rA   r!   r   r   r   r	   Útest_specified_path¦   s    z+urlretrieveNetworkTests.test_specified_pathc          	   C   s2   |   | j¡\}}|  |tjjd¡ W d Q R X d S )Nz0info is not an instance of email.message.Message)r?   rC   r)   r-   r.   r/   )r   rA   r!   r   r   r	   Útest_header¯   s    z#urlretrieveNetworkTests.test_headerzhttp://www.pythontest.net/c          	   C   sb   |   | j¡L\}}| d¡}d}yt ||¡ W n" tk
rR   |  d| ¡ Y nX W d Q R X d S )NZDatez%a, %d %b %Y %H:%M:%S GMTzDate value not in %r format)r?   rC   ÚgetÚtimeZstrptimeÚ
ValueErrorZfail)r   rA   ÚfileheadersZ	datevalueZ
dateformatr   r   r	   Útest_data_header·   s    
z(urlretrieveNetworkTests.test_data_headerc          	      s  g ‰ ‡ fdd„}| j | j|d\}}t|d ƒ}W d Q R X tˆ ƒ}| jtˆ ƒddd |  ˆ d d d¡ | jˆ d d dd	| d |  ˆ d d
 |¡ |  ˆ d d
 |¡ dd„ ˆ D ƒ}| jˆ d d h|d| d | jˆ d d ˆ d d  |d| d d S )Nc                s   ˆ   | ||f¡ d S )N)Úappend)ZblocksÚ
block_sizeZ
total_size)Úrecordsr   r	   Úrecording_reporthookÃ   s    zEurlretrieveNetworkTests.test_reporthook.<locals>.recording_reporthook)Z
reporthookzContent-Lengthé   zKThere should always be two calls; the first one before the transfer starts.)r7   r   zblock size can't be 0 in %sé   éÿÿÿÿc             S   s   h | ]\}}}|’qS r   r   )Ú.0Ú_rO   r   r   r	   ú	<setcomp>Ó   s    z:urlretrieveNetworkTests.test_reporthook.<locals>.<setcomp>zblock sizes in %s must be equalz9number of blocks * block size must be >= total size in %s)r?   rC   ÚintÚreprZassertGreaterÚlenr0   ZassertGreaterEqual)r   rQ   rA   rL   Zexpected_sizeZrecords_reprZblock_sizesr   )rP   r	   Útest_reporthookÀ   s$    z'urlretrieveNetworkTests.test_reporthookN)r   r   r   r;   r<   r=   r?   r(   rG   rH   rC   rM   r[   r   r   r   r	   r>      s   
			r>   Ú__main__)ZunittestZtestr   r<   r   Zurllib.parser   Zurllib.requestrD   Zemail.messager-   rJ   ZrequiresZTestCaser   r   r>   r   Úmainr   r   r   r	   Ú<module>   s   
nK