B
    u9aa  ã               @   sf  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 yd dlZW n ek
rx   dZY nX ej e¡Zej ed¡Zej ed¡ZG dd„ dejjƒZG dd„ dejƒZG d	d
„ d
ƒZG dd„ dejjƒZG dd„ dejjƒZG dd„ de	jƒZ G dd„ de	jƒZ!dd„ Z"G dd„ de	jƒZ#da$dd„ Z%dd„ Z&e'dkrbe	 (¡  dS )é    N)Úsupportzkeycert.pemzkeycert2.pemc               @   s   e Zd Zdd„ Zdd„ ZdS )ÚLoopbackHttpServerc             C   s"   t jj | ||¡ | j d¡ d S )Ngš™™™™™¹?)ÚhttpÚserverÚ
HTTPServerÚ__init__ÚsocketÚ
settimeout)ÚselfZserver_addressZRequestHandlerClass© r   ú+/usr/lib/python3.7/test_urllib2_localnet.pyr       s    
zLoopbackHttpServer.__init__c             C   s    | j  ¡ \}}| d¡ ||fS )Ng      $@)r   Zacceptr	   )r
   ÚrequestZclient_addressr   r   r   Úget_request)   s    
zLoopbackHttpServer.get_requestN)Ú__name__Ú
__module__Ú__qualname__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S )ÚLoopbackHttpServerThreadc             C   s<   t j | ¡ d| _t  ¡ | _d|_td|ƒ| _| jj	| _
d S )NFzHTTP/1.0)z	127.0.0.1r   )Ú	threadingÚThreadr   Ú_stop_serverZEventÚreadyZprotocol_versionr   ÚhttpdZserver_portÚport)r
   Úrequest_handlerr   r   r   r   8   s    
z!LoopbackHttpServerThread.__init__c             C   s   d| _ |  ¡  | j ¡  d S )NT)r   Újoinr   Zserver_close)r
   r   r   r   ÚstopA   s    zLoopbackHttpServerThread.stopc             C   s$   | j  ¡  x| js| j ¡  qW d S )N)r   Úsetr   r   Úhandle_request)r
   r   r   r   ÚrunI   s    
zLoopbackHttpServerThread.runN)r   r   r   r   r   r   r   r   r   r   r   5   s   	r   c               @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚDigestAuthHandlerc             C   s"   d| _ g | _i | _d| _d| _d S )Nr   z
Test RealmÚauth)Ú_request_numÚ_noncesÚ_usersÚ_realm_nameÚ_qop)r
   r   r   r   r   S   s
    zDigestAuthHandler.__init__c             C   s
   || _ d S )N)r%   )r
   Zqopr   r   r   Úset_qopZ   s    zDigestAuthHandler.set_qopc             C   s
   || _ d S )N)r#   )r
   Zusersr   r   r   Ú	set_users]   s    zDigestAuthHandler.set_usersc             C   s
   || _ d S )N)r$   )r
   Zrealmr   r   r   Ú	set_realma   s    zDigestAuthHandler.set_realmc             C   s8   |  j d7  _ t t| j ƒ d¡¡ ¡ }| j |¡ |S )Né   Úascii)r!   ÚhashlibÚmd5ÚstrÚencodeÚ	hexdigestr"   Úappend)r
   Únoncer   r   r   Ú_generate_nonced   s    z!DigestAuthHandler._generate_noncec             C   s†   |  d¡}||d d … }| d¡}i }xX|D ]P}| d¡\}}| ¡ }|d dkrn|d dkrn|dd… }n| ¡ }|||< q.W |S )Nú r)   ú,ú=r   ú"éÿÿÿÿ)ÚfindÚsplitÚstrip)r
   Zauth_strZfirst_space_indexÚpartsÚ	auth_dictÚpartÚnameÚvaluer   r   r   Ú_create_auth_dictj   s    


z#DigestAuthHandler._create_auth_dictc             C   s–   i }|  |¡ ||d< ||d< ||d< d| }t | d¡¡ ¡ }d| }t | d¡¡ ¡ }	||d< |	|d< d	| }
t |
 d¡¡ ¡ }||d
 kS )NÚpasswordÚmethodÚuriz#%(username)s:%(realm)s:%(password)sr*   z%(method)s:%(uri)sÚHA1ÚHA2z3%(HA1)s:%(nonce)s:%(nc)s:%(cnonce)s:%(qop)s:%(HA2)sÚresponse)Úupdater+   r,   r.   r/   )r
   r<   rA   rB   rC   Z
final_dictZHA1_strrD   ZHA2_strrE   Zresponse_strrF   r   r   r   Ú_validate_auth{   s    
z DigestAuthHandler._validate_authc             C   sN   |  dd¡ | dd¡ | dd| j| j|  ¡ f ¡ | ¡  |j d¡ dS )	Ni—  zProxy Authentication RequiredzContent-Typez	text/htmlzProxy-Authenticatez(Digest realm="%s", qop="%s",nonce="%s", s   Proxy Authentication Required.F)Úsend_responseÚsend_headerr$   r%   r2   Úend_headersÚwfileÚwrite)r
   r   r   r   r   Ú_return_auth_challenge   s    z(DigestAuthHandler._return_auth_challengec             C   sÎ   t | jƒdkrdS d|jkr&|  |¡S |  |jd ¡}|d | jkrT| j|d  }n
|  |¡S | d¡| jkrx|  |¡S | j |d ¡ d}x*|j|j	gD ]}|  
|||j|¡ršd}qšW |sÆ|  |¡S dS d S )Nr   TzProxy-AuthorizationZusernamer1   F)Úlenr#   ÚheadersrN   r@   Úgetr"   ÚremoveÚpathÚ
short_pathrH   Zcommand)r
   r   r<   rA   Zauth_validatedrS   r   r   r   r   œ   s,    	




z DigestAuthHandler.handle_requestN)r   r   r   r   r&   r'   r(   r2   r@   rH   rN   r   r   r   r   r   r   P   s   r   c               @   sb   e Zd ZdZdZdZdeef Ze e 	d¡¡ 
d¡Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ ZdS )ÚBasicAuthHandlerÚtestUserÚtestPassÚTestz%s:%sr*   c             O   s   t jjj| f|ž|Ž d S )N)r   r   ÚBaseHTTPRequestHandlerr   )r
   ÚargsÚkwargsr   r   r   r   Ò   s    zBasicAuthHandler.__init__c             G   s   d S )Nr   )r
   ÚformatrZ   r   r   r   Úlog_messageÕ   s    zBasicAuthHandler.log_messagec             C   s"   |   d¡ |  dd¡ |  ¡  d S )NéÈ   zContent-typez	text/html)rI   rJ   rK   )r
   r   r   r   Údo_HEADÙ   s    
zBasicAuthHandler.do_HEADc             C   s4   |   d¡ |  dd| j ¡ |  dd¡ |  ¡  d S )Ni‘  zWWW-AuthenticatezBasic realm="%s"zContent-typez	text/html)rI   rJ   ÚREALMrK   )r
   r   r   r   Údo_AUTHHEADÞ   s    
zBasicAuthHandler.do_AUTHHEADc             C   sh   | j  dd¡s$|  ¡  | j d¡ n@| j  dd¡d| j kr\|  d¡ |  ¡  | j d¡ n|  ¡  d S )NZAuthorizationÚ s   No Auth header receivedzBasic r^   s   It works)rP   rQ   ra   rL   rM   ÚENCODED_AUTHrI   rK   )r
   r   r   r   Údo_GETä   s    
zBasicAuthHandler.do_GETN)r   r   r   ÚUSERÚPASSWDr`   ZUSER_PASSWDÚbase64Z	b64encoder.   Údecoderc   r   r]   r_   ra   rd   r   r   r   r   rU   É   s   rU   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚFakeProxyHandlerc             O   s    || _ tjjj| f|ž|Ž d S )N)Údigest_auth_handlerr   r   rY   r   )r
   rj   rZ   r[   r   r   r   r   ü   s    zFakeProxyHandler.__init__c             G   s   d S )Nr   )r
   r\   rZ   r   r   r   r]     s    zFakeProxyHandler.log_messagec             C   sv   t j | jd¡\}}}}}}|| _| j | ¡rr|  dd¡ |  dd¡ |  	¡  | j
 td| j dƒ¡ | j
 d¡ d S )	Nr   r^   ZOKzContent-Typez	text/htmlzYou've reached %s!<BR>r*   sF   Our apologies, but our server is down due to a sudden zombie invasion.)ÚurllibÚparseZurlparserS   rT   rj   r   rI   rJ   rK   rL   rM   Úbytes)r
   ZscmZnetlocrS   ÚparamsZqueryZfragmentr   r   r   rd     s    zFakeProxyHandler.do_GETN)r   r   r   r   r]   rd   r   r   r   r   ri   õ   s   ri   c                   sP   e Zd ZdZdZdZdZ‡ fdd„Zdd„ Z‡ fd	d
„Z	dd„ Z
dd„ Z‡  ZS )ÚBasicAuthTestsrV   rW   Z	IncorrectrX   c                sT   t t| ƒ ¡  dd„ }t|ƒ| _|  | j¡ d| jj | _| j 	¡  | jj
 ¡  d S )Nc              _   s
   t | |ŽS )N)rU   )rZ   r[   r   r   r   Ú#http_server_with_basic_auth_handler  s    zABasicAuthTests.setUp.<locals>.http_server_with_basic_auth_handlerzhttp://127.0.0.1:%s)Úsuperro   ÚsetUpr   r   Ú
addCleanupÚstop_serverr   Ú
server_urlÚstartr   Úwait)r
   rp   )Ú	__class__r   r   rr     s    

zBasicAuthTests.setUpc             C   s   | j  ¡  d | _ d S )N)r   r   )r
   r   r   r   rt   '  s    
zBasicAuthTests.stop_serverc                s   t t| ƒ ¡  d S )N)rq   ro   ÚtearDown)r
   )rx   r   r   ry   +  s    zBasicAuthTests.tearDownc             C   s|   t j ¡ }| | j| j| j| j¡ t j t j 	|¡¡ y|  
t j | j¡¡ W n( t jjk
rv   |  d| j ¡ Y nX d S )Nz!Basic auth failed for the url: %s)rk   r   ÚHTTPBasicAuthHandlerÚadd_passwordr`   ru   re   rf   Úinstall_openerÚbuild_openerÚ
assertTrueÚurlopenÚerrorÚ	HTTPErrorÚfail)r
   Úahr   r   r   Útest_basic_auth_success.  s    
z&BasicAuthTests.test_basic_auth_successc             C   sR   t j ¡ }| | j| j| j| j¡ t j t j 	|¡¡ |  
t jjt jj| j¡ d S )N)rk   r   rz   r{   r`   ru   re   ÚINCORRECT_PASSWDr|   r}   ÚassertRaisesr€   r   r   )r
   rƒ   r   r   r   Útest_basic_auth_httperror7  s    
z(BasicAuthTests.test_basic_auth_httperror)r   r   r   re   rf   r…   r`   rr   rt   ry   r„   r‡   Ú__classcell__r   r   )rx   r   ro     s   	ro   c                   sT   e Zd ZdZdZdZdZ‡ fdd„Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Z‡  ZS )ÚProxyAuthTestszhttp://localhostZtesterZtest123Z	TestRealmc                sÜ   t tˆ ƒ ¡  dd„ }ˆ  |tj ¡ ¡ dtjd< dtjd< tƒ ˆ _ˆ j 	ˆ j
ˆ ji¡ ˆ j ˆ j¡ ‡ fdd„}t|ƒˆ _ˆ  ˆ j¡ ˆ j ¡  ˆ jj ¡  dˆ jj }tj d	|i¡}tj ¡ ˆ _tj |ˆ j¡ˆ _d S )
Nc             S   s   t j ¡  t j | ¡ d S )N)ÚosÚenvironÚclearrG   )Úold_environr   r   r   Úrestore_environH  s    
z-ProxyAuthTests.setUp.<locals>.restore_environrb   ÚNO_PROXYÚno_proxyc                 s   t ˆ jf| ž|ŽS )N)ri   rj   )rZ   r[   )r
   r   r   Úcreate_fake_proxy_handlerS  s    z7ProxyAuthTests.setUp.<locals>.create_fake_proxy_handlerzhttp://127.0.0.1:%dr   )rq   r‰   rr   rs   rŠ   r‹   Úcopyr   rj   r'   re   rf   r(   r`   r   r   rt   rv   r   rw   r   rk   r   ZProxyHandlerZProxyDigestAuthHandlerÚproxy_digest_handlerr}   Úopener)r
   rŽ   r‘   Z	proxy_urlÚhandler)rx   )r
   r   rr   E  s$    



zProxyAuthTests.setUpc             C   s   | j  ¡  d | _ d S )N)r   r   )r
   r   r   r   rt   `  s    
zProxyAuthTests.stop_serverc             C   sF   | j  | j| j| j| jd ¡ | j d¡ |  t	j
j| jj| j¡ d S )NZbadr    )r“   r{   r`   ÚURLre   rf   rj   r&   r†   rk   r€   r   r”   Úopen)r
   r   r   r   Ú-test_proxy_with_bad_password_raises_httperrord  s    
z<ProxyAuthTests.test_proxy_with_bad_password_raises_httperrorc             C   s(   | j  d¡ |  tjj| jj| j¡ d S )Nr    )	rj   r&   r†   rk   r€   r   r”   r—   r–   )r
   r   r   r   Ú,test_proxy_with_no_password_raises_httperrorl  s    
z;ProxyAuthTests.test_proxy_with_no_password_raises_httperrorc             C   sN   | j  | j| j| j| j¡ | j d¡ | j 	| j¡}x| 
¡ r@q6W | ¡  d S )Nr    )r“   r{   r`   r–   re   rf   rj   r&   r”   r—   ÚreadÚclose)r
   Úresultr   r   r   Útest_proxy_qop_auth_worksr  s    
z(ProxyAuthTests.test_proxy_qop_auth_worksc             C   st   | j  | j| j| j| j¡ | j d¡ y| j 	| j¡}W n t
jjk
rT   d }Y nX |rpx| ¡ rfq\W | ¡  d S )Nzauth-int)r“   r{   r`   r–   re   rf   rj   r&   r”   r—   rk   r€   ÚURLErrorrš   r›   )r
   rœ   r   r   r   Ú0test_proxy_qop_auth_int_works_or_throws_urlerror{  s    

z?ProxyAuthTests.test_proxy_qop_auth_int_works_or_throws_urlerror)r   r   r   r–   re   rf   r`   rr   rt   r˜   r™   r   rŸ   rˆ   r   r   )rx   r   r‰   >  s   	r‰   c                s   G ‡ ‡fdd„dt jjƒ‰ ˆ S )Nc                   sB   e Zd ZdZg Zg ZdZdd„ Zdd„ Z‡ ‡fdd„Z	d	d
„ Z
dS )z1GetRequestHandler.<locals>.FakeHTTPRequestHandlerz	TestHTTP/éP   c             S   s.   |   ¡ }x |r(| j |¡}||d … }q
W d S )N)Ú	send_headrL   rM   )r
   ÚbodyZdoner   r   r   rd   •  s    z8GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_GETc             S   s2   | j d }| j t|ƒ¡}|  ¡  | j |¡ d S )NzContent-Length)rP   Zrfilerš   Úintrd   Úrequestsr0   )r
   Zcontent_lengthZ	post_datar   r   r   Údo_POST›  s    
z9GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_POSTc                s€   | j ˆ _| j | j¡ ˆ d¡\}}}|  |¡ x&|D ]\}}|  ||d| ji ¡ q6W |rt|  dd¡ |  	¡  |S |  	¡  d S )Nr   r   zContent-typez
text/plain)
rP   Úheaders_receivedr¤   r0   rS   ÚpoprI   rJ   r   rK   )r
   Zresponse_coderP   r¢   Úheaderr?   )ÚFakeHTTPRequestHandlerÚ	responsesr   r   r¡   ¡  s    
z;GetRequestHandler.<locals>.FakeHTTPRequestHandler.send_headc             W   s   d S )Nr   )r
   rZ   r   r   r   r]   °  s    z=GetRequestHandler.<locals>.FakeHTTPRequestHandler.log_messageN)r   r   r   Zserver_versionr¤   r¦   r   rd   r¥   r¡   r]   r   )r©   rª   r   r   r©   Ž  s   r©   )r   r   rY   )rª   r   )r©   rª   r   ÚGetRequestHandlerŒ  s    &r«   c                   sº   e Zd Z‡ fdd„Zd*dd„Zdd„ Zd+dd	„Zd,d
d„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Z‡  ZS )-ÚTestUrlopenc                s@   t t| ƒ ¡  dd„ }|  |tj ¡ ¡ dtjd< dtjd< d S )Nc             S   s   t j ¡  t j | ¡ d S )N)rŠ   r‹   rŒ   rG   )r   r   r   r   rŽ   Ä  s    
z*TestUrlopen.setUp.<locals>.restore_environÚ*r   r   )rq   r¬   rr   rs   rŠ   r‹   r’   )r
   rŽ   )rx   r   r   rr   À  s
    
zTestUrlopen.setUpNc             K   sl   g }t jj||f|Ž}z@| | d¡¡ | | ¡ ¡ | | d¡¡ | | ¡ ¡ W d | ¡  X d 	|¡S )Nr^   i   ó    )
rk   r   r   ÚextendÚ	readlinesr0   Úreadlinerš   r›   r   )r
   ÚurlÚdatar[   ÚlÚfr   r   r   r   Ë  s    
zTestUrlopen.urlopenc             C   s   | j  ¡  d | _ d S )N)r   r   )r
   r   r   r   rt   Ø  s    
zTestUrlopen.stop_serverc             C   sZ   |d krdg dfg}t |ƒ}t|ƒ| _|  | j¡ | j ¡  | jj ¡  | jj}||_|S )Nr^   s   we don't care)	r«   r   r   rs   rt   rv   r   rw   r   )r
   rª   r•   r   r   r   r   Ústart_serverÜ  s    

zTestUrlopen.start_serverc             K   s^   t tjdƒs|  d¡ ddlm} |d kr6dg dfg}t|ƒ}|| fd|i|—Ž}|j|_|S )NZHTTPSHandlerzssl support requiredr   )Úmake_https_serverr^   s   we care a bitZhandler_class)Úhasattrrk   r   ÚskipTestZtest.ssl_serversr·   r«   r   )r
   rª   r[   r·   r•   r   r   r   r   Ústart_https_serveré  s    
zTestUrlopen.start_https_serverc             C   sV   d}ddgdfdg |fg}|   |¡}|  d|j ¡}|  ||¡ |  |jddg¡ d S )	Ns   We got here...i.  )ZLocationz(http://localhost:%(port)s/somewhere_elserb   r^   zhttp://localhost:%s/ú/z/somewhere_else)r¶   r   r   ÚassertEqualr¤   )r
   Úexpected_responserª   r•   r³   r   r   r   Útest_redirectionô  s    
zTestUrlopen.test_redirectionc             C   s@   d}d}ddg|fg}|   |¡}|  d|j ¡}|  ||¡ d S )Ns   hello worlds   a
hello worl
1
d
0
r^   )zTransfer-EncodingZchunkedzhttp://localhost:%s/)r¶   r   r   r¼   )r
   r½   Zchunked_startrF   r•   r³   r   r   r   Útest_chunked  s    
zTestUrlopen.test_chunkedc          
   C   sŒ   d}|   dg |fg¡}y|  d|j ¡ W n6 tjjk
r` } z| ¡ }| ¡  W d d }~X Y nX |  d¡ |  	||¡ |  	|j
dg¡ d S )Ns   Bad bad bad...i”  zhttp://localhost:%s/weeblez404 should raise URLErrorz/weeble)r¶   r   r   rk   r€   rž   rš   r›   r‚   r¼   r¤   )r
   r½   r•   rµ   r³   r   r   r   Útest_404  s    
zTestUrlopen.test_404c             C   sF   d}|   dg |fg¡}|  d|j ¡}|  ||¡ |  |jdg¡ d S )Ns   pycon 2008...r^   zhttp://localhost:%s/bizarrez/bizarre)r¶   r   r   r¼   r¤   )r
   r½   r•   r³   r   r   r   Útest_200  s
    zTestUrlopen.test_200c             C   sJ   d}|   dg |fg¡}|  d|j d¡}|  ||¡ |  |jddg¡ d S )Ns   pycon 2008...r^   zhttp://localhost:%s/bizarres   get=with_feelingz/bizarre)r¶   r   r   r¼   r¤   )r
   r½   r•   r³   r   r   r   Útest_200_with_parameters%  s    z$TestUrlopen.test_200_with_parametersc             C   s8   |   ¡ }tjtd}| jd|j |d}|  |d¡ d S )N)Úcafilezhttps://localhost:%s/bizarre)Úcontexts   we care a bit)rº   ÚsslÚcreate_default_contextÚCERT_localhostr   r   r¼   )r
   r•   rÄ   r³   r   r   r   Ú
test_https-  s    zTestUrlopen.test_httpsc          
   C   s²   | j td}t dtf¡Ž | jd|j td}|  |d¡ |  t	j
j¡}| jd|j td W d Q R X | j td}|  t	j
j¡}| jd|j td W d Q R X W d Q R X d S )N)Úcertfilerb   zhttps://localhost:%s/bizarre)rÃ   s   we care a bit)rº   rÇ   r   Úcheck_warningsÚDeprecationWarningr   r   r¼   r†   rk   r€   rž   ÚCERT_fakehostname)r
   r•   r³   Úcmr   r   r   Útest_https_with_cafile3  s    z"TestUrlopen.test_https_with_cafilec          
   C   sX   | j td}t dtf¡4 |  tjj¡}| j	d|j
 dd W d Q R X W d Q R X d S )N)rÉ   rb   zhttps://localhost:%s/bizarreT)Z	cadefault)rº   rÇ   r   rÊ   rË   r†   rk   r€   rž   r   r   )r
   r•   rÍ   r   r   r   Útest_https_with_cadefaultD  s
    z%TestUrlopen.test_https_with_cadefaultc                s†   t d kr|  d¡ t js"|  d¡ d ‰ ‡ fdd„}t  t j¡}| |¡ | j|td}t jtd}| j	d|j
 |d |  ˆ d	¡ d S )
Nzssl module requiredzSNI support required in OpenSSLc                s   |‰ d S )Nr   )Zssl_sockZserver_nameZinitial_context)Úsni_namer   r   Úcb_sniR  s    z*TestUrlopen.test_https_sni.<locals>.cb_sni)rÄ   rÉ   )rÃ   zhttps://localhost:%s)rÄ   Z	localhost)rÅ   r¹   ZHAS_SNIZ
SSLContextZPROTOCOL_TLS_SERVERZset_servername_callbackrº   rÇ   rÆ   r   r   r¼   )r
   rÑ   rÄ   r•   r   )rÐ   r   Útest_https_sniL  s    


zTestUrlopen.test_https_snic          	   C   sP   |   ¡ }tjjd|j ddid}tj |¡ W d Q R X |  |jd d¡ d S )Nzhttp://localhost:%s/ZRangezbytes=20-39)rP   )r¶   rk   r   ZRequestr   r   r¼   r¦   )r
   r•   Zreqr   r   r   Útest_sending_headers\  s    
z TestUrlopen.test_sending_headersc             C   sb   |   ¡ }tj d|j ¡}x"dD ]}|  t||ƒd| ¡ q W z|  | ¡ d¡ W d | ¡  X d S )Nzhttp://localhost:%s)rš   r›   ÚinfoÚgeturlz3object returned from urlopen lacks the %s attributezcalling 'read' failed)	r¶   rk   r   r   r   r~   r¸   rš   r›   )r
   r•   Úopen_urlÚattrr   r   r   Ú
test_basicd  s    
zTestUrlopen.test_basicc          	   C   sX   |   ¡ }tj d|j ¡}| | ¡ }W d Q R X |  |tjj	d¡ |  
| ¡ d¡ d S )Nzhttp://localhost:%szEobject returned by 'info' is not an instance of email.message.MessageZplain)r¶   rk   r   r   r   rÔ   ZassertIsInstanceÚemailÚmessageZMessager¼   Zget_content_subtype)r
   r•   rÖ   Zinfo_objr   r   r   Ú	test_infoo  s    zTestUrlopen.test_infoc          	   C   sH   |   ¡ }tj d|j ¡}| | ¡ }W d Q R X |  |d|j ¡ d S )Nzhttp://localhost:%s)r¶   rk   r   r   r   rÕ   r¼   )r
   r•   rÖ   r²   r   r   r   Útest_geturlz  s
    zTestUrlopen.test_geturlc             C   sF   d}|   dg |fg¡}tj d|j ¡}x|D ]}|  ||¡ q.W d S )Ns   pycon 2008...r^   zhttp://localhost:%s)r¶   rk   r   r   r   r¼   )r
   r½   r•   r³   Úliner   r   r   Útest_iteration‚  s
    
zTestUrlopen.test_iterationc          
   C   s˜   ddddd d g}d  |¡}|  dg |fg¡}tj d	|j ¡}x<t|ƒD ]0\}}|  ||| d
|t|| ƒt|ƒf ¡ qLW |  |d t|ƒ¡ d S )Ns   We
s   got
s   here
s	   verylong i    ó   
r®   r^   zhttp://localhost:%szQFetched line number %s doesn't match expected:
    Expected length was %s, got %sr)   )	r   r¶   rk   r   r   r   Ú	enumerater¼   rO   )r
   Úlinesr½   r•   r³   ÚindexrÝ   r   r   r   Útest_line_iteration‰  s    
zTestUrlopen.test_line_iteration)N)N)N)r   r   r   rr   r   rt   r¶   rº   r¾   r¿   rÀ   rÁ   rÂ   rÈ   rÎ   rÏ   rÒ   rÓ   rØ   rÛ   rÜ   rÞ   rã   rˆ   r   r   )rx   r   r¬   ·  s(   	


r¬   c               C   s   t  ¡ ad S )N)r   Zthreading_setupÚthreads_keyr   r   r   r   ÚsetUpModule˜  s    rå   c               C   s   t rtjt Ž  d S )N)rä   r   Zthreading_cleanupr   r   r   r   ÚtearDownModulež  s    ræ   Ú__main__))rg   rŠ   rÙ   Zurllib.parserk   Zurllib.requestZhttp.serverr   r   Zunittestr+   Ztestr   rÅ   ÚImportErrorrS   ÚdirnameÚ__file__Úherer   rÇ   rÌ   r   r   r   r   r   r   rY   rU   ri   ZTestCasero   r‰   r«   r¬   rä   rå   ræ   r   Úmainr   r   r   r   Ú<module>   s>   
y,!(N+ `
