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	d dl
Z
e
jZd dlmZ ej e¡Zej ed¡Zej ed¡Zej ed¡ZdZdZd	Zd
Zde d ZdZdZejZG dd„ dƒZG dd„ deƒZG dd„ dej ƒZ!G dd„ dej"ƒZ#G dd„ deƒZ$G dd„ deƒZ%G dd„ deƒZ&G dd„ deƒZ'G dd„ de'ƒZ(G d d!„ d!ƒZ)G d"d#„ d#eƒZ*G d$d%„ d%eƒZ+G d&d'„ d'eƒZ,G d(d)„ d)eƒZ-G d*d+„ d+eƒZ.G d,d-„ d-eƒZ/G d.d/„ d/eƒZ0G d0d1„ d1eƒZ1e2d2krüe
j3d3d4 dS )5é    N)Úclient)Úsupportzkeycert.pemzkeycert2.pemzselfsigned_pythontestdotnet.pemz{HTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello worl
3
d! 
8
and now 
22
for something completely different
s7   hello world! and now for something completely differentz;foo=barz0
Ú0z
zX-Dummy: foo
X-Dumm2: bar
c               @   sH   e Zd Zejddfdd„Zdd„ Zddd„Zdd	„ Zd
d„ Z	dd„ Z
dS )Ú
FakeSocketNc             C   sB   t |tƒr| d¡}|| _|| _d| _d| _d| _|| _|| _	d S )NÚasciió    r   F)
Ú
isinstanceÚstrÚencodeÚtextÚ	fileclassÚdataÚsendall_callsÚfile_closedÚhostÚport)Úselfr   r   r   r   © r   ú"/usr/lib/python3.7/test_httplib.pyÚ__init__/   s    

zFakeSocket.__init__c             C   s    |  j d7  _ |  j|7  _d S )Né   )r   r   )r   r   r   r   r   Úsendall:   s    zFakeSocket.sendallc             C   s6   |dkr|dkrt  ¡ ‚|  | j¡| _| j| j_| jS )NÚrÚrb)r   ZUnimplementedFileModer   r   ÚfileÚ
file_closeÚclose)r   ÚmodeÚbufsizer   r   r   Úmakefile>   s
    
zFakeSocket.makefilec             C   s
   d| _ d S )NT)r   )r   r   r   r   r   F   s    zFakeSocket.file_closec             C   s   d S )Nr   )r   r   r   r   r   I   s    zFakeSocket.closec             C   s   d S )Nr   )r   ÚlevelZoptnameÚvaluer   r   r   Ú
setsockoptL   s    zFakeSocket.setsockopt)N)Ú__name__Ú
__module__Ú__qualname__ÚioÚBytesIOr   r   r   r   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 )ÚEPipeSocketc             C   s   t  | |¡ || _d S )N)r   r   Úpipe_trigger)r   r   r)   r   r   r   r   Q   s    zEPipeSocket.__init__c             C   s(   | j |krttjdƒ‚|  j|7  _d S )NZgotcha)r)   ÚOSErrorÚerrnoZEPIPEr   )r   r   r   r   r   r   V   s    
zEPipeSocket.sendallc             C   s   d S )Nr   )r   r   r   r   r   [   s    zEPipeSocket.closeN)r#   r$   r%   r   r   r   r   r   r   r   r(   O   s   r(   c               @   s    e Zd Zddd„Zddd„ZdS )	ÚNoEOFBytesIOéÿÿÿÿc             C   s"   t j | |¡}|dkrtdƒ‚|S )Nr   zcaller tried to read past EOF)r&   r'   ÚreadÚAssertionError)r   Únr   r   r   r   r.   d   s    zNoEOFBytesIO.readNc             C   s"   t j | |¡}|dkrtdƒ‚|S )Nr   zcaller tried to read past EOF)r&   r'   Úreadliner/   )r   Úlengthr   r   r   r   r1   j   s    zNoEOFBytesIO.readline)r-   )N)r#   r$   r%   r.   r1   r   r   r   r   r,   ^   s   
r,   c                   s0   e Zd Z‡ fdd„Z‡ fdd„Zdd„ Z‡  ZS )ÚFakeSocketHTTPConnectionc                s$   d| _ tƒ  d¡ || _| j| _d S )Nr   zexample.com)ÚconnectionsÚsuperr   Úfake_socket_argsÚcreate_connectionÚ_create_connection)r   Úargs)Ú	__class__r   r   r   s   s    z!FakeSocketHTTPConnection.__init__c                s   |  j d7  _ tƒ  ¡ S )Nr   )r4   r5   Úconnect)r   )r:   r   r   r;   y   s    z FakeSocketHTTPConnection.connectc             O   s
   t | jŽ S )N)r   r6   )r   ÚposÚkwr   r   r   r7   ~   s    z*FakeSocketHTTPConnection.create_connection)r#   r$   r%   r   r;   r7   Ú__classcell__r   r   )r:   r   r3   p   s   r3   c               @   sL   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S )ÚHeaderTestsc             C   s   G dd„ dt ƒ}xzdD ]r}xldD ]d}t d¡}tdƒ|_|ƒ |_d}i }|r\tt|ƒƒ||< | dd	||¡ |  	|jj
| ¡  d
¡ q W qW d S )Nc               @   s   e Zd Zdd„ Zdd„ ZdS )z;HeaderTests.test_auto_headers.<locals>.HeaderCountingBufferc             S   s
   i | _ d S )N)Úcount)r   r   r   r   r   ‡   s    zDHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.__init__c             S   sX   |  d¡}t|ƒdkrH|d  d¡ ¡ }| j |d¡ | j|  d7  < t | |¡ d S )Nó   :r   r   r   )ÚsplitÚlenÚdecodeÚlowerr@   Ú
setdefaultÚlistÚappend)r   ÚitemÚkvZlcKeyr   r   r   rH   ‰   s    
zBHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.appendN)r#   r$   r%   r   rH   r   r   r   r   ÚHeaderCountingBuffer†   s   rK   )TF)zContent-lengthZHostzAccept-encodingzexample.comZblahblahblahZspamspamspamÚPOSTú/r   )rG   r   ÚHTTPConnectionr   ÚsockÚ_bufferr	   rC   ÚrequestÚassertEqualr@   rE   )r   rK   Zexplicit_headerÚheaderÚconnÚbodyÚheadersr   r   r   Útest_auto_headers‚   s    



zHeaderTests.test_auto_headersc             C   sv  G dd„ dt ƒ}d}d}xZt ||¡D ]J\}}t d¡}td ƒ|_|ƒ |_| |d|¡ |  	|jj
dd |¡¡ q&W d	}xN|D ]F}t d¡}td ƒ|_|ƒ |_| |dd ¡ |  	|jj
d d
 |¡¡ q~W xN|D ]F}t d¡}td ƒ|_|ƒ |_| |dd¡ |  	|jj
dd |¡¡ qÎW xXt ||¡D ]H}t d¡}td ƒ|_|ƒ |_| |dd¡ |  	|jj
dd |¡¡ q&W d S )Nc               @   s   e Zd Zdd„ Zdd„ ZdS )z?HeaderTests.test_content_length_0.<locals>.ContentLengthCheckerc             S   s   t  | ¡ d | _d S )N)rG   r   Úcontent_length)r   r   r   r   r   ¢   s    
zHHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.__init__c             S   sF   |  dd¡}t|ƒdkr6|d  ¡ dkr6|d  ¡ | _t | |¡ d S )NrA   r   r   s   content-length)rB   rC   rE   ÚstriprX   rG   rH   )r   rI   rJ   r   r   r   rH   ¥   s    zFHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.appendN)r#   r$   r%   r   rH   r   r   r   r   ÚContentLengthChecker¡   s   rZ   )NÚ )ÚPUTrL   ZPATCHzexample.comrM   ó   0z%Header Content-Length incorrect on {})ÚGETÚCONNECTZDELETEÚHEADZOPTIONSZTRACEz.Header Content-Length set for empty body on {}r[   ú ó   1)rG   Ú	itertoolsÚproductr   rN   r   rO   rP   rQ   rR   rX   ÚformatÚchain)r   rZ   ZbodiesZmethods_with_bodyÚmethodrU   rT   Zmethods_without_bodyr   r   r   Útest_content_length_0Ÿ   sH    









z!HeaderTests.test_content_length_0c             C   s’  t  d¡}td ƒ|_| dd¡ | dd¡ |  d|j¡ | dd¡ |  d	|j¡ | d
d¡ |  d|j¡ | dd¡ |  d|j¡ | ddd¡ |  d|j¡ | dd¡ |  d|j¡ | dd¡ |  d|j¡ | dd¡ |  d|j¡ | dd¡ |  d|j¡ | d d!¡ |  d"|j¡ | d#d$¡ |  d%|j¡ | d&d$¡ |  d'|j¡ | d(d$¡ |  d)|j¡ | d*d$¡ |  d+|j¡ d S ),Nzexample.comr^   rM   zContent-lengthé*   s   Content-length: 42ZFooz bar s
   Foo:  bar ZBarz	baz	s
   Bar: 	baz	ZAuthorizationzBearer mytokens   Authorization: Bearer mytokenZ
IterHeaderZIterAZIterBs   IterHeader: IterA
	IterBZLatinHeaderó   ÿs   LatinHeader: ÿZ
Utf8Headers   Ã€s   Utf8Header: Ã€z
C1-Controls	   next…lines   C1-Control: next…linezEmbedded-Fold-Spacezis
 alloweds!   Embedded-Fold-Space: is
 allowedzEmbedded-Fold-Tabzis
	alloweds   Embedded-Fold-Tab: is
	allowedz	Key Spacer!   s   Key Space: valuez	KeySpace s   KeySpace : values   Nonbreak Spaces   Nonbreak Space: values    NonbreakSpaces    NonbreakSpace: value)r   rN   r   rO   Ú
putrequestÚ	putheaderÚassertInrP   )r   rT   r   r   r   Útest_putheaderá   s>    

zHeaderTests.test_putheaderc             C   sx   d}t  d¡}tdƒ}||_| dd¡ |  |j |¡¡ d}t  d¡}tdƒ}||_| dd¡ |  |j |¡¡ d S )NsC   GET /foo HTTP/1.1
Host: [2001::]:81
Accept-Encoding: identity

z[2001::]:81r[   r^   z/foosE   GET /foo HTTP/1.1
Host: [2001:102A::]
Accept-Encoding: identity

z[2001:102A::])r   rN   r   rO   rQ   Ú
assertTruer   Ú
startswith)r   ÚexpectedrT   rO   r   r   r   Útest_ipv6host_header  s    

z HeaderTests.test_ipv6host_headerc             C   sF   d}t |ƒ}t |¡}| ¡  |  | d¡d¡ |  | d¡d¡ d S )Nz4HTTP/1.1 200 OK
First: val
: nval
Second: val

ZFirstÚvalZSecond)r   r   ÚHTTPResponseÚbeginrR   Ú	getheader)r   rU   rO   Úrespr   r   r   Ú!test_malformed_headers_coped_with  s    
z-HeaderTests.test_malformed_headers_coped_withc             C   s0  dt tddƒƒ d t tddƒƒ d }t|ƒ}t |¡}| ¡  |  | d¡d	¡ |  |jd d	¡ |  | d
¡d¡ |  |jd
 d¡ d 	t
ttddƒƒ¡}|  | d¡|¡ |  |jd |¡ |  | d¡¡ |  d|j¡ xH| d¡|jd fD ]0}|  | d¡¡ |  d|¡ |  | d¡¡ qøW d S )Ns0   HTTP/1.1 200 OK
!#$%&'*+-.^_`|~: value
VCHAR: é!   é   s   
obs-text: é€   é   sM   
obs-fold: text
 folded with space
	folded with tab
Content-Length: 0

zContent-Lengthr   z!#$%&'*+-.^_`|~r!   r[   ZVCHARzobs-textzobs-foldr   z folded with spacezfolded with tab)ÚbytesÚranger   r   rt   ru   rR   rv   ÚmsgÚjoinÚmapÚchrZassertIsNotNonerm   ro   rp   Úendswith)r   rU   rO   rw   ZvcharZfoldedr   r   r   Útest_parse_all_octets   s"    (
z!HeaderTests.test_parse_all_octetsc             C   sx   t  d¡}tdƒ|_| dd¡ d}xN|D ]F\}}|  ||f¡* |  td¡ | ||¡ W d Q R X W d Q R X q*W d S )Nzexample.comr[   r^   rM   ))s   Invalid
Names
   ValidValue)s   InvalidNames
   ValidValue)s   Invalid
Names
   ValidValue)s   
InvalidNames
   ValidValue)s   InvalidNames
   ValidValue)s   
InvalidNames
   ValidValue)s    InvalidNames
   ValidValue)s   	InvalidNames
   ValidValue)s   Invalid:Names
   ValidValue)s   :InvalidNames
   ValidValue)s	   ValidNames   Invalid
Value)s	   ValidNames   InvalidValue)s	   ValidNames   Invalid
Value)s	   ValidNames   InvalidValue
)s	   ValidNames   InvalidValue)s	   ValidNames   InvalidValue
zInvalid header)	r   rN   r   rO   rk   ÚsubTestÚassertRaisesRegexÚ
ValueErrorrl   )r   rT   ÚcasesÚnamer!   r   r   r   Útest_invalid_headers>  s    

z HeaderTests.test_invalid_headersc          	   C   s†   d}t |ƒ}tj|dd}t ¡ }| ¡  W d Q R X | ¡  ¡ }|  |d d¡ |  |d d¡ |  |d d¡ |  |d	 d
¡ d S )Ns9   HTTP/1.1 200 OK
First: val
Second: val1
Second: val2
r   )Z
debuglevelr   zreply: 'HTTP/1.1 200 OK\r\n'zheader: First: valé   zheader: Second: val1é   zheader: Second: val2)	r   r   rt   r   Úcaptured_stdoutru   ÚgetvalueÚ
splitlinesrR   )r   rU   rO   rw   ÚoutputÚlinesr   r   r   Útest_headers_debuglevel\  s    
z#HeaderTests.test_headers_debuglevelN)r#   r$   r%   rW   rh   rn   rr   rx   r„   rŠ   r’   r   r   r   r   r?      s   B"
r?   c               @   sJ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zddd„Zdd„ Z	dd„ Z
dS )ÚTransferEncodingTests   It's just a flesh woundc             C   sb   t  d¡}tdƒ|_| dd¡ |j|  ¡ dd |  |jj¡\}}}|  	|¡}|  
|| j¡ d S )Nzexample.comr   rL   rM   T)Úencode_chunked)r   rN   r   rO   rk   Z
endheadersÚ
_make_bodyÚ_parse_requestr   Ú_parse_chunkedrR   Úexpected_body)r   rT   Ú_rU   r   r   r   Útest_endheaders_chunkedq  s    


z,TransferEncodingTest.test_endheaders_chunkedc             C   sv  t  d¡}tdƒ|_| dd|  ¡ ddi¡ |  |jj¡\}}}|  ddd	„ | 	¡ D ƒ¡ |  
|d d¡ |  
|| j¡ t  d¡}tdƒ|_| dd| j d
¡ddi¡ |  |jj¡\}}}|  ddd	„ | 	¡ D ƒ¡ |  
|d d¡ |  
|| j¡ t  d¡}tdƒ|_|jddddid|  ¡ d |  |jj¡\}}}|  ddd	„ |D ƒ¡ |  
|d d¡ |  
|  |¡| j¡ d S )Nzexample.comr   rL   rM   zTransfer-EncodingÚchunkedzcontent-lengthc             S   s   g | ]}|  ¡ ‘qS r   )rE   )Ú.0Úkr   r   r   ú
<listcomp>…  s    z>TransferEncodingTest.test_explicit_headers.<locals>.<listcomp>zlatin-1c             S   s   g | ]}|  ¡ ‘qS r   )rE   )rœ   r   r   r   r   rž   ‘  s    zgzip, chunkedT)rV   r”   rU   c             S   s   g | ]}|  ¡ ‘qS r   )rE   )rœ   r   r   r   r   rž     s    )r   rN   r   rO   rQ   r•   r–   r   ÚassertNotInÚkeysrR   r˜   rD   r—   )r   rT   r™   rV   rU   r   r   r   Útest_explicit_headers{  s6    






z*TransferEncodingTest.test_explicit_headersc             C   sŽ   xˆdD ]€}t  d¡}tdƒ|_| dd| j|d¡ |  |jj¡\}}}|  |¡}|  	|| j
¡ |  	|d d¡ |  d	d
d„ |D ƒ¡ qW d S )N)FTzexample.comr   rL   rM   )Úempty_lineszTransfer-Encodingr›   zcontent-lengthc             S   s   g | ]}|  ¡ ‘qS r   )rE   )rœ   r   r   r   r   rž   ¯  s    z5TransferEncodingTest.test_request.<locals>.<listcomp>)r   rN   r   rO   rQ   r•   r–   r   r—   rR   r˜   rŸ   )r   r¢   rT   r™   rV   rU   r   r   r   Útest_request¡  s    



z!TransferEncodingTest.test_requestc             C   sl   t  d¡}tdƒ|_| ddd¡ |  |jj¡\}}}|  |d d¡ |  dd	d
„ |D ƒ¡ |  |d¡ d S )Nzexample.comr   rL   rM   r   zTransfer-Encodingr›   zcontent-lengthc             S   s   g | ]}|  ¡ ‘qS r   )rE   )rœ   r   r   r   r   rž   ¸  s    z8TransferEncodingTest.test_empty_body.<locals>.<listcomp>s   0

)	r   rN   r   rO   rQ   r–   r   rR   rŸ   )r   rT   r™   rV   rU   r   r   r   Útest_empty_body±  s    

z$TransferEncodingTest.test_empty_bodyFc             c   sZ   | j  d¡}xHt|ƒD ]<\}}|r0|d r0dV  |t|ƒd k rL|d V  q|V  qW d S )Nó    r‹   r   r   )r˜   rB   Ú	enumeraterC   )r   r¢   r‘   ÚidxÚliner   r   r   r•   »  s    zTransferEncodingTest._make_bodyc             C   s’   |  d¡}|d }i }d}xZ|t|ƒk rtt|| ƒdkrt||   d¡\}}| d¡ ¡ }| d¡ ¡ ||< |d7 }qW ||d ||d d … ¡fS )Ns   
r   r   rA   zlatin-1)rB   rC   rD   rY   r€   )r   r   r‘   rQ   rV   r0   Úkeyrs   r   r   r   r–   Æ  s    
z#TransferEncodingTest._parse_requestc             C   s†   g }i }d}|  d¡}xd|||d … \}}t|dƒ}|dkrH|d7 }P |  |t|ƒ¡ | |¡ |d7 }|t|ƒkrP qW d |¡S )Nr   s   
r‹   é   r   r   )rB   ÚintrR   rC   rH   r€   )r   r   rU   Útrailersr0   r‘   ÚsizeÚchunkr   r   r   r—   Ó  s     


z#TransferEncodingTest._parse_chunkedN)F)r#   r$   r%   r˜   rš   r¡   r£   r¤   r•   r–   r—   r   r   r   r   r“   n  s   
&

r“   c               @   sd  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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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAdB„ Z#dCdD„ Z$dEdF„ Z%dGdH„ Z&dIdJ„ Z'dKdL„ Z(dMdN„ Z)dOdP„ Z*dQdR„ Z+dSdT„ Z,dUdV„ Z-dWS )XÚ	BasicTestc             C   s²   d}t |ƒ}t |¡}| ¡  |  | d¡d¡ |  | ¡ ¡ |  |j¡ |  | ¡ d¡ |  	| ¡ ¡ |  |j¡ | 
¡  |  	|j¡ d}t |ƒ}t |¡}|  tj|j¡ d S )NzHTTP/1.1 200 Ok

Textr   r   s   TextzHTTP/1.1 400.100 Not Ok

Text)r   r   rt   ru   rR   r.   ÚassertFalseÚisclosedÚclosedro   r   ÚassertRaisesÚBadStatusLine)r   rU   rO   rw   r   r   r   Útest_status_linesï  s     

zBasicTest.test_status_linesc             C   s   t  d¡}|  t|ƒd¡ d S )Nr[   zBadStatusLine("''"))r   r´   rR   Úrepr)r   Úexcr   r   r   Útest_bad_status_repr  s    
zBasicTest.test_bad_status_reprc             C   s‚   d}t |ƒ}t |¡}| ¡  |  | d¡d¡ |  | ¡ ¡ |  | d¡d¡ |  | ¡ ¡ |  |j	¡ | 
¡  |  |j	¡ d S )Nz*HTTP/1.1 200 Ok
Content-Length: 4

Textr‹   s   Tes   xt)r   r   rt   ru   rR   r.   r°   r±   ro   r²   r   )r   rU   rO   rw   r   r   r   Útest_partial_reads  s    
zBasicTest.test_partial_readsc             C   s~   d}t |ƒ}t |¡}| ¡  |  | ¡ d¡ |  | ¡ ¡ |  | ¡ d¡ |  	| ¡ ¡ |  |j
¡ | ¡  |  	|j
¡ d S )Nz4HTTP/1.1 200 Ok
Content-Length: 13

Text
Anothers   Text
s   Another)r   r   rt   ru   rR   r1   r°   r±   r.   ro   r²   r   )r   rU   rO   rw   r   r   r   Útest_mixed_reads  s    
zBasicTest.test_mixed_readsc             C   s²   d}t |ƒ}t |¡}| ¡  tdƒ}| |¡}|  |d¡ |  t|ƒd¡ |  | 	¡ ¡ | |¡}|  |d¡ |  t|ƒd¡ |  
| 	¡ ¡ |  |j¡ | ¡  |  
|j¡ d S )Nz*HTTP/1.1 200 Ok
Content-Length: 4

Textr‹   s   Tes   xt)r   r   rt   ru   Ú	bytearrayÚreadintorR   r}   r°   r±   ro   r²   r   )r   rU   rO   rw   Úbr0   r   r   r   Útest_partial_readintos&  s     


z BasicTest.test_partial_readintosc             C   s”   d}t |ƒ}t |¡}| ¡  |  | d¡d¡ |  | ¡ ¡ |  | d¡d¡ |  | d¡d¡ |  | ¡ ¡ |  |j	¡ | 
¡  |  |j	¡ d S )NzHTTP/1.1 200 Ok

Textr‹   s   Tes   xtr   r   )r   r   rt   ru   rR   r.   r°   r±   ro   r²   r   )r   rU   rO   rw   r   r   r   Ú$test_partial_reads_no_content_length:  s    
z.BasicTest.test_partial_reads_no_content_lengthc             C   s¨   d}t |ƒ}t |¡}| ¡  tdƒ}| |¡}|  |d¡ |  t|ƒd¡ |  | 	¡ ¡ | |¡}|  |d¡ |  t|ƒd¡ | |¡}|  |d¡ |  
| 	¡ ¡ d S )NzHTTP/1.1 200 Ok

Textr‹   s   Tes   xtr   )r   r   rt   ru   r»   r¼   rR   r}   r°   r±   ro   )r   rU   rO   rw   r½   r0   r   r   r   Ú(test_partial_readintos_no_content_lengthJ  s    



z2BasicTest.test_partial_readintos_no_content_lengthc             C   st   d}t |ƒ}t |¡}| ¡  |  | d¡d¡ |  | ¡ ¡ |  | d¡d¡ |  | d¡d¡ |  | ¡ ¡ d S )Nz+HTTP/1.1 200 Ok
Content-Length: 10

Textr‹   s   Tes   xtr   r   )	r   r   rt   ru   rR   r.   r°   r±   ro   )r   rU   rO   rw   r   r   r   Ú"test_partial_reads_incomplete_body]  s    
z,BasicTest.test_partial_reads_incomplete_bodyc             C   sÈ   d}t |ƒ}t |¡}| ¡  tdƒ}| |¡}|  |d¡ |  t|ƒd¡ |  | 	¡ ¡ | |¡}|  |d¡ |  t|ƒd¡ | |¡}|  |d¡ |  
| 	¡ ¡ |  |j¡ | ¡  |  
|j¡ d S )Nz+HTTP/1.1 200 Ok
Content-Length: 10

Textr‹   s   Tes   xtr   )r   r   rt   ru   r»   r¼   rR   r}   r°   r±   ro   r²   r   )r   rU   rO   rw   r½   r0   r   r   r   Ú&test_partial_readintos_incomplete_bodyj  s$    



z0BasicTest.test_partial_readintos_incomplete_bodyc             C   s^   xdD ]}|   tjtj|¡ qW x8dD ]0\}}}t |¡}|  ||j¡ |  ||j¡ q&W d S )N)zwww.python.org:abczuser:password@www.python.org))z[fe80::207:e9ff:fe9b]:8000zfe80::207:e9ff:fe9bi@  )zwww.python.org:80zwww.python.orgéP   )zwww.python.org:zwww.python.orgrÃ   )zwww.python.orgzwww.python.orgrÃ   )z[fe80::207:e9ff:fe9b]zfe80::207:e9ff:fe9brÃ   )z[fe80::207:e9ff:fe9b]:zfe80::207:e9ff:fe9brÃ   )r³   r   Ú
InvalidURLrN   rR   r   r   )r   ÚhpÚhÚpÚcr   r   r   Útest_host_port€  s    

zBasicTest.test_host_portc             C   s<   d}d}t |ƒ}t |¡}| ¡  | d¡}|  ||¡ d S )Nz¨HTTP/1.1 200 OK
Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"

No body
zrCustomer="WILE_E_COYOTE"; Version="1"; Path="/acme", Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"z
Set-Cookie)r   r   rt   ru   rv   rR   )r   r   ZhdrÚsr   Zcookiesr   r   r   Útest_response_headers‘  s    

zBasicTest.test_response_headersc             C   s6   t dtƒ}tj|dd}| ¡  | ¡ r2|  d¡ d S )Nz*HTTP/1.1 200 OK
Content-Length: 14432

r`   )rg   z)Did not expect response from HEAD request)r   r,   r   rt   ru   r.   Úfail)r   rO   rw   r   r   r   Útest_read_head£  s    zBasicTest.test_read_headc             C   sT   t dtƒ}tj|dd}| ¡  tdƒ}| |¡dkr@|  d¡ |  t	|ƒd¡ d S )Nz*HTTP/1.1 200 OK
Content-Length: 14432

r`   )rg   é   r   z)Did not expect response from HEAD requests        )
r   r,   r   rt   ru   r»   r¼   rÌ   rR   r}   )r   rO   rw   r½   r   r   r   Útest_readinto_head°  s    
zBasicTest.test_readinto_headc             C   sR   d  dd„ ttjd ƒD ƒ¡d }d| }t|ƒ}t |¡}|  tjd|j¡ d S )Nz
c             s   s   | ]}d | V  qdS )zHeader%d: fooNr   )rœ   Úir   r   r   ú	<genexpr>À  s   z2BasicTest.test_too_many_headers.<locals>.<genexpr>r   zHTTP/1.1 200 OK
zgot more than \d+ headers)	r€   r~   r   Z_MAXHEADERSr   rt   r†   ZHTTPExceptionru   )r   rV   r   rÊ   r   r   r   r   Útest_too_many_headers¿  s    

zBasicTest.test_too_many_headersc          	   C   sn   d}t tdƒV}t d¡}t|ƒ}||_| dd|¡ |  |j 	|¡d|jd t
|ƒ… |f ¡ W d Q R X d S )Ns_   GET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Transfer-Encoding: chunked

r   zexample.comr^   z/fooz%r != %r)ÚopenÚ__file__r   rN   r   rO   rQ   ro   r   rp   rC   )r   rq   rU   rT   rO   r   r   r   Útest_send_fileÈ  s    
zBasicTest.test_send_filec             C   s‚   d}t  d¡}td ƒ}||_| |¡ |  ||j¡ d|_| t d|¡¡ |  ||j¡ d|_| t 	|¡¡ |  ||j¡ d S )Ns"   this is a test this is only a testzexample.comr   r½   )
r   rN   r   rO   ÚsendrR   r   Úarrayr&   r'   )r   rq   rT   rO   r   r   r   Ú	test_sendÖ  s    

zBasicTest.test_sendc                sX   dd„ ‰ G ‡ fdd„dt jƒ}d}t d¡}tdƒ}||_| |ƒ ¡ |  |j|¡ d S )Nc               s   s   dV  d V  dV  d S )Nr   Zdata_twor   r   r   r   r   r   å  s    z/BasicTest.test_send_updating_file.<locals>.datac                   s    e Zd ZdZ” ƒ Zddd„ZdS )z7BasicTest.test_send_updating_file.<locals>.UpdatingFiler   r-   c             S   s
   t | jƒS )N)ÚnextÚd)r   Ú	blocksizer   r   r   r.   í  s    z<BasicTest.test_send_updating_file.<locals>.UpdatingFile.readN)r-   )r#   r$   r%   r   rÚ   r.   r   )r   r   r   ÚUpdatingFileê  s   rÜ   s   datazexample.comr[   )	r&   Ú
TextIOBaser   rN   r   rO   rÖ   rR   r   )r   rÜ   rq   rT   rO   r   )r   r   Útest_send_updating_fileä  s    
z!BasicTest.test_send_updating_filec             C   sL   d}dd„ }t  d¡}tdƒ}||_| dd|ƒ dd	i¡ |  |j|¡ d S )
Nsb   GET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Content-Length: 11

onetwothreec               s   s   dV  dV  dV  d S )Ns   ones   twos   threer   r   r   r   r   rU   þ  s    z&BasicTest.test_send_iter.<locals>.bodyzexample.comr[   r^   z/foozContent-LengthZ11)r   rN   r   rO   rQ   rR   r   )r   rq   rU   rT   rO   r   r   r   Útest_send_iterù  s    
zBasicTest.test_send_iterc             C   sv   d}t jd|d}td ƒ}||_d| d }| ddt |¡dd	i¡ |  |jd
¡ |j	 
dd¡d }|  ||¡ d S )Né   zexample.com)rÛ   ó   aó   br\   rM   zContent-LengthÚ9rŒ   s   

r   )r   rN   r   rO   rQ   r&   r'   rR   r   r   rB   )r   rÛ   rT   rO   rq   rU   r   r   r   Útest_blocksize_request	  s    z BasicTest.test_blocksize_requestc             C   s\   d}t jd|d}td ƒ}||_d| d }| t |¡¡ |  |jd¡ |  |j	|¡ d S )Nrà   zexample.com)rÛ   rá   râ   r‹   )
r   rN   r   rO   rÖ   r&   r'   rR   r   r   )r   rÛ   rT   rO   rq   r   r   r   Útest_blocksize_send  s    zBasicTest.test_blocksize_sendc          	   C   s<   t  d¡}tdƒ|_|  t¡ | dd|¡ W d Q R X d S )Nzexample.comr[   rL   Útest)r   rN   r   rO   r³   Ú	TypeErrorrQ   )r   rT   r   r   r   Útest_send_type_error   s    

zBasicTest.test_send_type_errorc             C   sd  t }ttt t ƒ}tj|dd}| ¡  |  | 	¡ |¡ | 
¡  xdtddƒD ]V}ttt t ƒ}tj|dd}| ¡  |  | 	|¡| 	|¡ | 	¡  |¡ | 
¡  qNW x¶dD ]®}tt| ƒ}tj|dd}| ¡  z|y| 	¡  W n` tjk
rB } z>|  |j|¡ dt|ƒ }|  t|ƒ|¡ |  t|ƒ|¡ W d d }~X Y nX |  d¡ W d | 
¡  X q®W d S )Nr^   )rg   r   é   )r[   zfoo
zIncompleteRead(%d bytes read)zIncompleteRead expected)Úchunked_expectedr   Úchunked_startÚ
last_chunkÚchunked_endr   rt   ru   rR   r.   r   r~   ÚIncompleteReadÚpartialrC   r¶   r	   rÌ   )r   rq   rO   rw   r0   ÚxrÐ   Úexpected_messager   r   r   Útest_chunked'  s2    $
"zBasicTest.test_chunkedc             C   sØ  t }t|ƒ}tdƒ}ttt t ƒ}tj|dd}| 	¡  | 
|¡}|  |d |… |¡ |  ||¡ | ¡  xªtddƒD ]œ}ttt t ƒ}tj|dd}| 	¡  t|ƒ}| 
|d|… ¡}|| 
|||| … ¡7 }|| 
||d … ¡7 }|  |d |… |¡ |  ||¡ | ¡  qxW xºdD ]²}	tt|	 ƒ}tj|dd}| 	¡  z~y| 
|¡}W n` tjk
r´ } z>|  |j|¡ dt|ƒ }
|  t|ƒ|
¡ |  t|ƒ|
¡ W d d }~X Y nX |  d	¡ W d | ¡  X qW d S )
Nr{   r^   )rg   r   ré   r   )r[   zfoo
zIncompleteRead(%d bytes read)zIncompleteRead expected)rê   rC   r»   r   rë   rì   rí   r   rt   ru   r¼   rR   r   r~   Ú
memoryviewrî   rï   r¶   r	   rÌ   )r   rq   Z	nexpectedr½   rO   rw   r0   ÚmrÐ   rð   rñ   r   r   r   Útest_readinto_chunkedG  sD    

"zBasicTest.test_readinto_chunkedc             C   sˆ   d}t |t t ƒ}tj|dd}| ¡  |  | ¡ d¡ |  |jd¡ |  |j	d¡ |  
| ¡ ¡ |  |j¡ | ¡  |  
|j¡ d S )NzEHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello world
1
d
r`   )rg   r   éÈ   ÚOK)r   rì   rí   r   rt   ru   rR   r.   ÚstatusÚreasonro   r±   r°   r²   r   )r   rë   rO   rw   r   r   r   Útest_chunked_headr  s    zBasicTest.test_chunked_headc             C   s¦   d}t |t t ƒ}tj|dd}| ¡  tdƒ}| |¡}|  |d¡ |  t	|ƒd¡ |  |j
d¡ |  |jd¡ |  | ¡ ¡ |  |j¡ | ¡  |  |j¡ d S )	NzEHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello world
1
d
r`   )rg   rÎ   r   s        rö   r÷   )r   rì   rí   r   rt   ru   r»   r¼   rR   r}   rø   rù   ro   r±   r°   r²   r   )r   rë   rO   rw   r½   r0   r   r   r   Útest_readinto_chunked_head†  s    
z$BasicTest.test_readinto_chunked_headc             C   s@   t dƒ}tj|dd}| ¡  |  | ¡ d¡ |  | ¡ ¡ d S )Nz.HTTP/1.1 200 OK
Content-Length: -1

Hello
r^   )rg   s   Hello
)r   r   rt   ru   rR   r.   ro   r±   )r   rO   rw   r   r   r   Útest_negative_content_length  s    z&BasicTest.test_negative_content_lengthc          
   C   sš   t dƒ}tj|dd}| ¡  y| ¡  W n` tjk
rŠ } z@|  |jd¡ |  t|ƒd¡ |  t	|ƒd¡ |  
| ¡ ¡ W d d }~X Y nX |  d¡ d S )Nz.HTTP/1.1 200 OK
Content-Length: 10

Hello
r^   )rg   s   Hello
z-IncompleteRead(7 bytes read, 3 more expected)zIncompleteRead expected)r   r   rt   ru   r.   rî   rR   rï   r¶   r	   ro   r±   rÌ   )r   rO   rw   rÐ   r   r   r   Útest_incomplete_read¥  s    

 zBasicTest.test_incomplete_readc                sZ   t ddƒ}t d¡‰ |ˆ _|  t‡ fdd„¡ ˆ  ¡ }|  d|j¡ |  d| 	d¡¡ d S )	NzgHTTP/1.0 401 Authorization Required
Content-type: text/html
WWW-Authenticate: Basic realm="example"
s   Content-Lengthzexample.comc                  s   ˆ   ddd¡S )Nr\   z/urlrU   )rQ   r   )rT   r   r   Ú<lambda>¾  r   z&BasicTest.test_epipe.<locals>.<lambda>i‘  zBasic realm="example"zwww-authenticate)
r(   r   rN   rO   r³   r*   ÚgetresponserR   rø   rv   )r   rO   rw   r   )rT   r   Ú
test_epipeµ  s    
zBasicTest.test_epipec             C   s8   ddd  d }t  t|ƒ¡}|  t jt jf|j¡ d S )NzHTTP/1.1 200 Okr   i   z
)r   rt   r   r³   ÚLineTooLongr´   ru   )r   rU   rw   r   r   r   Útest_overflowing_status_lineÆ  s    z&BasicTest.test_overflowing_status_linec             C   s2   ddd  d }t  t|ƒ¡}|  t j|j¡ d S )NzHTTP/1.1 200 OK
X-Foo: barr   i   z

)r   rt   r   r³   r  ru   )r   rU   rw   r   r   r   Útest_overflowing_header_lineË  s    z&BasicTest.test_overflowing_header_linec             C   s:   ddd  d }t  t|ƒ¡}| ¡  |  t j|j¡ d S )Nz/HTTP/1.1 200 OK
Transfer-Encoding: chunked

r   i   za
hello world
0

)r   rt   r   ru   r³   r  r.   )r   rU   rw   r   r   r   Útest_overflowing_chunked_lineÓ  s    z'BasicTest.test_overflowing_chunked_linec             C   s`   d}t |ƒ}t |¡}| ¡  |  | ¡ d¡ |  | ¡ ¡ |  |j	¡ | 
¡  |  |j	¡ d S )NzHTTP/1.1 200 Okr   )r   r   rt   ru   rR   r.   ro   r±   r°   r²   r   )r   rU   rO   rw   r   r   r   Útest_early_eofà  s    
zBasicTest.test_early_eofc                sn   t  d¡}d ‰ G ‡ fdd„dt jƒ}||_tdƒ|_| dd¡ |  t j|j	¡ |  
ˆ j¡ |  
|jj¡ d S )Nzexample.comc                   s   e Zd Z‡ fdd„ZdS )z+BasicTest.test_error_leak.<locals>.Responsec                s   | ‰ t jj| f|ž|Ž d S )N)r   rt   r   )r   r<   r=   )Úresponser   r   r   ñ  s    z4BasicTest.test_error_leak.<locals>.Response.__init__N)r#   r$   r%   r   r   )r  r   r   ÚResponseð  s   r  zInvalid status liner^   rM   )r   rN   rt   Zresponse_classr   rO   rQ   r³   r´   rÿ   ro   r²   r   )r   rT   r  r   )r  r   Útest_error_leakì  s    

zBasicTest.test_error_leakc             C   sR   d}t d }tt| t t ƒ}tj|dd}| ¡  |  | 	¡ |¡ | 
¡  d S )Nz3;foo=bar
abc
s   abcr^   )rg   )rê   r   rë   Úlast_chunk_extendedrí   r   rt   ru   rR   r.   r   )r   Zextrarq   rO   rw   r   r   r   Útest_chunked_extensionü  s    z BasicTest.test_chunked_extensionc             C   sB   t }ttt ƒ}tj|dd}| ¡  |  | ¡ |¡ | 	¡  d S )Nr^   )rg   )
rê   r   rë   rì   r   rt   ru   rR   r.   r   )r   rq   rO   rw   r   r   r   Útest_chunked_missing_end  s    z"BasicTest.test_chunked_missing_endc             C   s\   t }ttt t t ƒ}tj|dd}| ¡  |  	| 
¡ |¡ |  	|j 
¡ d¡ | ¡  d S )Nr^   )rg   r   )rê   r   rë   rì   r¬   rí   r   rt   ru   rR   r.   r   r   )r   rq   rO   rw   r   r   r   Útest_chunked_trailers  s    zBasicTest.test_chunked_trailersc             C   sj   t }d}ttt t t | ƒ}tj|dd}| ¡  |  	| 
¡ |¡ |  	|j 
¡ | d¡¡ | ¡  d S )NÚ	extradatar^   )rg   r   )rê   r   rë   rì   r¬   rí   r   rt   ru   rR   r.   r   r
   r   )r   rq   r  rO   rw   r   r   r   Útest_chunked_sync  s    zBasicTest.test_chunked_syncc             C   s\   d}d}t d| | ƒ}tj|dd}| ¡  |  | ¡ |¡ |  |j ¡ |¡ | ¡  d S )Ns	   extradatas
   Hello123
s'   HTTP/1.1 200 OK
Content-Length: 10

r^   )rg   )r   r   rt   ru   rR   r.   r   r   )r   r  rq   rO   rw   r   r   r   Útest_content_length_sync&  s    z"BasicTest.test_content_length_syncc             C   s`   d}d}t d| | ƒ}tj|dd}| ¡  |  | d¡|g¡ |  |j ¡ |¡ | ¡  d S )Ns	   extradatas
   Hello123
s'   HTTP/1.1 200 OK
Content-Length: 10

r^   )rg   iÐ  )	r   r   rt   ru   rR   Ú	readlinesr   r.   r   )r   r  rq   rO   rw   r   r   r   Útest_readlines_content_length2  s    z'BasicTest.test_readlines_content_lengthc             C   s^   d}d}t d| | ƒ}tj|dd}| ¡  |  | d¡|¡ |  |j ¡ |¡ | ¡  d S )Ns	   extradatas
   Hello123
s'   HTTP/1.1 200 OK
Content-Length: 10

r^   )rg   iÐ  )	r   r   rt   ru   rR   Úread1r   r.   r   )r   r  rq   rO   rw   r   r   r   Útest_read1_content_length=  s    z#BasicTest.test_read1_content_lengthc             C   sp   d}d}t d| | ƒ}tj|dd}| ¡  |  | d¡|¡ |  | d¡d¡ |  |j ¡ |¡ | ¡  d S )Ns	   extradatas
   Hello123
s'   HTTP/1.1 200 OK
Content-Length: 10

r^   )rg   é
   r   )	r   r   rt   ru   rR   r1   r   r.   r   )r   r  rq   rO   rw   r   r   r   Ú"test_readline_bound_content_lengthH  s    z,BasicTest.test_readline_bound_content_lengthc             C   sv   d}d}t d|d  | ƒ}tj|dd}| ¡  |  | d¡|d ¡ |  | ¡ |¡ |  |j ¡ |¡ | ¡  d S )	Ns	   extradatas
   Hello123
s'   HTTP/1.1 200 OK
Content-Length: 30

rŒ   r^   )rg   é   r‹   )	r   r   rt   ru   rR   r  r.   r   r   )r   r  rq   rO   rw   r   r   r   Útest_read1_bound_content_lengthT  s    z)BasicTest.test_read1_bound_content_lengthc                sú   t   t jt jt j¡‰|  ˆj¡ ˆ tdf¡ ˆ ¡  d ‰ ‡ ‡fdd„}t	j
|d}| ¡  |  |jtdƒ¡ tjˆ ¡ Ž }| dd¡ | ¡ }z>|  |jtj¡ t j | ¡ d}z| d	¡ W d | ¡  X W d | ¡  | ¡  X | ¡  |  ˆ d	¡ d S )
Nr   c           
      sb   ˆ  ¡ \} }| H |  d¡2}x| ¡ }| d¡s P q W |  d¡ | ¡ ‰ W d Q R X W d Q R X d S )Nr   s   
s'   HTTP/1.1 200 Connection established

)Zacceptr   r1   Úrstripr   r.   )rT   ÚaddressÚreaderr¨   )ÚresultÚservr   r   Ú
run_serveri  s    

z2BasicTest.test_response_fileno.<locals>.run_server)Útargetr   r_   z
dummy:1234)Úfilenos   proxied data
)ÚsocketÚAF_INETÚSOCK_STREAMZIPPROTO_TCPÚ
addCleanupr   ZbindÚHOSTÚlistenÚ	threadingZThreadÚstartr€   Úfloatr   rN   ÚgetsocknamerQ   rÿ   rR   rø   r÷   r  r   Údetach)r   r  ZthreadrT   r  rÊ   r   )r  r  r   Útest_response_fileno`  s.    
zBasicTest.test_response_filenoN).r#   r$   r%   rµ   r¸   r¹   rº   r¾   r¿   rÀ   rÁ   rÂ   rÉ   rË   rÍ   rÏ   rÒ   rÕ   rØ   rÞ   rß   rä   rå   rè   rò   rõ   rú   rû   rü   rý   r   r  r  r  r  r  r
  r  r  r  r  r  r  r  r  r+  r   r   r   r   r¯   î  sV   	 +
	r¯   c               @   st   e Zd ZdZee d¡d…  d¡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 )ÚExtendedReadTestzOHTTP/1.1 200 OK

hello world!
and now 
for something completely different
fooZhelloNr   zŠHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello worl
3
d!

9
and now 

23
for something completely different

3
foo
0

c             C   s8   t | jƒ}tj|dd}| ¡  t |j¡|_|| _d S )Nr^   )rg   )	r   r‘   r   rt   ru   r&   ÚBufferedReaderÚfprw   )r   rO   rw   r   r   r   ÚsetUp¦  s
    
zExtendedReadTest.setUpc                sÎ   | j }|jj‰ d‡ fdd„	}||j_g }xŒ| d¡}|r|  t|ƒd¡ | ¡ }|  t|ƒt|ƒ¡ |  | |¡¡ | t|ƒ¡}|  	||¡ n| ¡ }|  
|¡ | |¡ |s*P q*W |  	d |¡| j¡ d S )Nr-   c                s(   ˆ | ƒ}| dkr|d | … S |d d… S )Nr   r  r   )r0   rÇ   )Úoldpeekr   r   Úmypeek³  s    z*ExtendedReadTest.test_peek.<locals>.mypeekrŒ   r   r   )r-   )rw   r.  ÚpeekZassertGreaterrC   ZassertGreaterEqualro   rp   r.   rR   r°   rH   r€   Úlines_expected)r   rw   r1  ÚallrÇ   Zp2rÙ   r   )r0  r   Ú	test_peek¯  s(    


zExtendedReadTest.test_peekc             C   s   | j }|  | j j| j¡ d S )N)rw   Ú_verify_readliner1   r3  )r   rw   r   r   r   Útest_readlineÎ  s    zExtendedReadTest.test_readlinec             C   s`   g }xD|dƒ}|r6|dkr6t |ƒdk r6|  | d¡¡ | |¡ |sP qW |  d |¡|¡ d S )NrÎ   s   fooó   
r   )rC   ro   rƒ   rH   rR   r€   )r   r1   rq   r4  r¨   r   r   r   r6  Ò  s    
z!ExtendedReadTest._verify_readlinec                s0   ˆj ‰ ‡ ‡fdd„}t|ƒ}ˆ |jˆj¡ d S )Nc                 s   ˆ   d¡} ˆ t| ƒd¡ | S )Né   )r  ÚassertLessEqualrC   )Zres)rw   r   r   r   r   á  s    
z&ExtendedReadTest.test_read1.<locals>.r)rw   Ú	Readlinerr6  r1   r3  )r   r   Z	readlinerr   )rw   r   r   Ú
test_read1ß  s    zExtendedReadTest.test_read1c             C   s@   | j }g }x| ¡ }|sP | |¡ qW |  d |¡| j¡ d S )Nr   )rw   r  rH   rR   r€   r3  )r   rw   r4  r   r   r   r   Útest_read1_unboundedè  s    z%ExtendedReadTest.test_read1_unboundedc             C   sR   | j }g }x.| d¡}|sP |  t|ƒd¡ | |¡ qW |  d |¡| j¡ d S )Nr  r   )rw   r  r:  rC   rH   rR   r€   r3  )r   rw   r4  r   r   r   r   Útest_read1_boundedò  s    
z#ExtendedReadTest.test_read1_boundedc             C   s   |   | j d¡d¡ d S )Nr   r   )rR   rw   r  )r   r   r   r   Útest_read1_0ý  s    zExtendedReadTest.test_read1_0c             C   s    | j  d¡}|  dt|ƒ¡ d S )Nr   )rw   r2  r:  rC   )r   rÇ   r   r   r   Útest_peek_0   s    zExtendedReadTest.test_peek_0)r#   r$   r%   r‘   Úfindr
   r3  Zlines_chunkedr/  r5  r7  r6  r<  r=  r>  r?  r@  r   r   r   r   r,  ˆ  s   		
r,  c               @   s   e Zd ZdZdS )ÚExtendedReadTestChunkedzŠHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello worl
3
d!

9
and now 

23
for something completely different

3
foo
0

N)r#   r$   r%   r‘   r   r   r   r   rB    s   rB  c               @   s   e Zd Zdd„ Zdd„ ZdS )r;  c             C   s   || _ d| _d S )Nr   )ÚreadfuncÚ	remainder)r   rC  r   r   r   r     s    zReadliner.__init__c             C   s²   g }d}| j }y„xP| d¡}|dkr&P |t|ƒ |krB|| d }| |¡ |  ¡ }|sd}P qW |d7 }| |d |… ¡ ||d … | _ d |¡S    d |¡| _ ‚ Y nX d S )Nr   r8  r-   r   r   )rD  rA  rC   rH   rC  r€   )r   Úlimitr   Zdatalenr.   r§   r   r   r   r1   "  s,    


zReadliner.readlineN)r#   r$   r%   r   r1   r   r   r   r   r;    s   r;  c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚOfflineTestc             C   sj   dh}ddh}xHt tƒD ]<}| d¡s||kr0qtt|ƒ}t|dd ƒdkr| |¡ qW |  tj|¡ d S )NÚ	responsesZHTTPMessageÚparse_headersr™   r$   zhttp.client)Údirr   rp   ÚgetattrÚaddZassertCountEqualÚ__all__)r   rq   Z	blacklistr‰   Zmodule_objectr   r   r   Útest_all=  s    
zOfflineTest.test_allc             C   s   |   tjtj d¡ d S )Nz	Not Found)rR   r   rG  Ú	NOT_FOUND)r   r   r   r   Útest_responsesK  s    zOfflineTest.test_responsesc          6   C   sª   ddddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6g6}x4|D ],}| j |d7 |  tt|ƒ¡ W d Q R X qvW d S )8NZCONTINUEZSWITCHING_PROTOCOLSZ
PROCESSINGr÷   ZCREATEDZACCEPTEDZNON_AUTHORITATIVE_INFORMATIONZ
NO_CONTENTZRESET_CONTENTZPARTIAL_CONTENTZMULTI_STATUSZIM_USEDZMULTIPLE_CHOICESZMOVED_PERMANENTLYZFOUNDZ	SEE_OTHERZNOT_MODIFIEDZ	USE_PROXYZTEMPORARY_REDIRECTZBAD_REQUESTZUNAUTHORIZEDZPAYMENT_REQUIREDZ	FORBIDDENrN  ZMETHOD_NOT_ALLOWEDZNOT_ACCEPTABLEZPROXY_AUTHENTICATION_REQUIREDZREQUEST_TIMEOUTZCONFLICTZGONEZLENGTH_REQUIREDZPRECONDITION_FAILEDZREQUEST_ENTITY_TOO_LARGEZREQUEST_URI_TOO_LONGZUNSUPPORTED_MEDIA_TYPEZREQUESTED_RANGE_NOT_SATISFIABLEZEXPECTATION_FAILEDZMISDIRECTED_REQUESTZUNPROCESSABLE_ENTITYZLOCKEDZFAILED_DEPENDENCYZUPGRADE_REQUIREDZPRECONDITION_REQUIREDZTOO_MANY_REQUESTSZREQUEST_HEADER_FIELDS_TOO_LARGEZINTERNAL_SERVER_ERRORZNOT_IMPLEMENTEDZBAD_GATEWAYZSERVICE_UNAVAILABLEZGATEWAY_TIMEOUTZHTTP_VERSION_NOT_SUPPORTEDZINSUFFICIENT_STORAGEZNOT_EXTENDEDZNETWORK_AUTHENTICATION_REQUIRED)Zconstant)r…   ro   Úhasattrr   )r   rq   Úconstr   r   r   Útest_client_constantsN  sr    
z!OfflineTest.test_client_constantsN)r#   r$   r%   rM  rO  rR  r   r   r   r   rF  <  s   rF  c               @   s@   e Zd Zdd„ Zdd„ Zdd„ Ze ee	dƒ d¡d	d
„ ƒZ
dS )ÚSourceAddressTestc             C   s>   t   t jt j¡| _t | j¡| _t ¡ | _| j 	¡  d | _
d S )N)r   r!  r"  r  r   Ú	bind_portr   Zfind_unused_portÚsource_portr%  rT   )r   r   r   r   r/  Ž  s
    

zSourceAddressTest.setUpc             C   s*   | j r| j  ¡  d | _ | j ¡  d | _d S )N)rT   r   r  )r   r   r   r   ÚtearDown•  s
    

zSourceAddressTest.tearDownc             C   sB   t jt| jd| jfd| _| j ¡  |  | jj 	¡ d | j¡ d S )Nr[   )Úsource_addressr   )
r   rN   r$  r   rU  rT   r;   rR   rO   r)  )r   r   r   r   ÚtestHTTPConnectionSourceAddressœ  s    

z1SourceAddressTest.testHTTPConnectionSourceAddressÚHTTPSConnectionz'http.client.HTTPSConnection not definedc             C   s   t jt| jd| jfd| _d S )Nr[   )rW  )r   rY  r$  r   rU  rT   )r   r   r   r   Ú testHTTPSConnectionSourceAddress¢  s    
z2SourceAddressTest.testHTTPSConnectionSourceAddressN)r#   r$   r%   r/  rV  rX  ÚunittestÚskipIfrP  r   rZ  r   r   r   r   rS    s
   rS  c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )ÚTimeoutTestNc             C   s.   t   t jt j¡| _t | j¡t_| j ¡  d S )N)	r   r!  r"  r  r   rT  r]  ÚPORTr%  )r   r   r   r   r/  ¯  s    zTimeoutTest.setUpc             C   s   | j  ¡  d | _ d S )N)r  r   )r   r   r   r   rV  ´  s    
zTimeoutTest.tearDownc          	   C   sð   |   t ¡ ¡ t d¡ zt ttj¡}| 	¡  W d t d ¡ X |  
|j ¡ d¡ | ¡  |   t ¡ ¡ t d¡ ztjttjd d}| 	¡  W d t d ¡ X |  
|j ¡ d ¡ | ¡  tjttjdd}| 	¡  |  
|j ¡ d¡ | ¡  d S )Né   )Útimeout)ÚassertIsNoner   ZgetdefaulttimeoutZsetdefaulttimeoutr   rN   r$  r]  r^  r;   rR   rO   Z
gettimeoutr   )r   ZhttpConnr   r   r   ÚtestTimeoutAttribute¸  s*    


z TimeoutTest.testTimeoutAttribute)r#   r$   r%   r^  r/  rV  rb  r   r   r   r   r]  ¬  s   r]  c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚPersistenceTestc             C   sÖ   d}xÌ|D ]Ä\}}}| j ||d¦ d ||¡}t|ƒ}|  |j¡ | dd¡ | ¡ "}|  |jd k| ¡ | ¡  W d Q R X |  |jd k| ¡ |  |j	d¡ | dd¡ |  |j	|r¾dnd¡ W d Q R X q
W d S )	N))z1.0r[   F)z1.0zConnection: keep-alive
T)z1.1r[   T)z1.1zConnection: close
F)z1.0zConnection: keep-ALIVE
T)z1.1zConnection: cloSE
F)ÚversionrS   z4HTTP/{} 200 OK
{}Content-Length: 12

Dummy body
r^   z/open-connectionr   z/subsequent-requestr‹   )
r…   re   r3   ra  rO   rQ   rÿ   rR   r.   r4   )r   Útestsrd  rS   Zreuser   rT   r  r   r   r   Útest_reuse_reconnectÜ  s    
z$PersistenceTest.test_reuse_reconnectc          
   C   s”   dd„ }t jtjf|tff}xr|D ]j\}}| j|dP td|ƒ}| dd¡ |  ||j	¡ |  
|j¡ | dd¡ |  |jd¡ W d Q R X q"W d S )	Nc                s&   t  | ¡‰ ‡ fdd„}|ˆ _t  ˆ ¡S )Nc                s    t j ˆ | ¡}|dkrtƒ ‚|S )Nr   )r&   r'   r¼   ÚConnectionResetError)Úbufferr­   )Ústreamr   r   r¼   ÿ  s    zNPersistenceTest.test_disconnected.<locals>.make_reset_reader.<locals>.readinto)r&   r'   r¼   r-  )r   r¼   r   )ri  r   Úmake_reset_readerü  s    
z<PersistenceTest.test_disconnected.<locals>.make_reset_reader)Ú	exceptionr   r^   z/eof-responsez
/reconnectr‹   )r&   r'   r   ÚRemoteDisconnectedrg  r…   r3   rQ   r³   rÿ   ra  rO   rR   r4   )r   rj  re  Zstream_factoryrk  rT   r   r   r   Útest_disconnectedú  s    


z!PersistenceTest.test_disconnectedc             C   sV   t dƒ}|jddddid |  tj|j¡ |  |j¡ | dd¡ |  |j	d¡ d S )	Ns   HTTP/1.1 100 Continue

r^   rM   ZExpectz100-continue)rV   z
/reconnectr‹   )
r3   rQ   r³   r   rl  rÿ   ra  rO   rR   r4   )r   rT   r   r   r   Útest_100_close  s    zPersistenceTest.test_100_closeN)r#   r$   r%   rf  rm  rn  r   r   r   r   rc  Ú  s   rc  c               @   sŽ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zej	dd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Ze eedƒ d¡dd„ ƒZdd„ ZdS )Ú	HTTPSTestc             C   s   t tdƒs|  d¡ d S )NrY  zssl support required)rP  r   ÚskipTest)r   r   r   r   r/  $  s    
zHTTPSTest.setUpc             C   s   ddl m} || |dS )Nr   )Úmake_https_server)Úcertfile)Ztest.ssl_serversrq  )r   rr  rq  r   r   r   Úmake_server(  s    zHTTPSTest.make_serverc             C   s$   t jttjdd}|  |jd¡ d S )Nr_  )r`  )r   rY  r$  r]  r^  rR   r`  )r   rÆ   r   r   r   Útest_attributes,  s    zHTTPSTest.test_attributesc          
   C   sl   dd l }t d¡ t d¡F t dd¡}|  |j¡}| dd¡ W d Q R X |  	|j
jd¡ W d Q R X d S )Nr   Únetworkzself-signed.pythontest.neti»  r^   rM   ÚCERTIFICATE_VERIFY_FAILED)Ússlr   ÚrequiresÚtransient_internetr   rY  r³   ÚSSLErrorrQ   rR   rk  rù   )r   rw  rÆ   Úexc_infor   r   r   Útest_networked1  s    
zHTTPSTest.test_networkedc          	   C   sz   dd l }t d¡ t d¡T | ¡ }tjdd|d}| dd¡ | ¡ }| 	¡  |  
d| d	¡¡ | 	¡  W d Q R X d S )
Nr   ru  zself-signed.pythontest.neti»  )Úcontextr^   rM   ÚnginxÚserver)rw  r   rx  ry  Z_create_unverified_contextr   rY  rQ   rÿ   r   rm   rv   )r   rw  r}  rÆ   rw   r   r   r   Útest_networked_noverification;  s    
z'HTTPSTest.test_networked_noverificationc          	   C   sj   t  d¡ t  d¡L t dd¡}| dd¡ | ¡ }| d¡}| ¡  | ¡  |  	d|¡ W d Q R X d S )Nru  zwww.python.orgi»  r^   rM   zcontent-typez	text/html)
r   rx  ry  r   rY  rQ   rÿ   rv   r   rm   )r   rÆ   rw   Zcontent_typer   r   r   Ú&test_networked_trusted_by_default_certI  s    

z0HTTPSTest.test_networked_trusted_by_default_certc       	      C   s  dd l }t d¡ d}t |¡Ü | |j¡}|  |j|j¡ |  |j	d¡ | 
t¡ y(tj|d|d}| dd¡ | ¡ }W nR |jk
rÐ } z2t|ƒ}t d	|¡r¾t d
|› d|› d¡‚‚ W d d }~X Y nX | d¡}| ¡  | ¡  |  d|¡ W d Q R X d S )Nr   ru  zself-signed.pythontest.netTi»  )r}  r^   rM   z(?i)key.too.weakzGot z trying to connect to z). See https://bugs.python.org/issue36816.r  r~  )rw  r   rx  ry  Ú
SSLContextÚPROTOCOL_TLS_CLIENTrR   Zverify_modeZCERT_REQUIREDÚcheck_hostnameÚload_verify_locationsÚ CERT_selfsigned_pythontestdotnetr   rY  rQ   rÿ   rz  r	   ÚreÚsearchr[  ZSkipTestrv   r   rm   )	r   rw  Zselfsigned_pythontestdotnetr}  rÆ   rw   Zssl_errZssl_err_strZserver_stringr   r   r   Útest_networked_good_certV  s.    


z"HTTPSTest.test_networked_good_certc          
   C   s†   dd l }t d¡ t d¡` | |j¡}| t¡ tj	dd|d}|  
|j¡}| dd¡ W d Q R X |  |jjd¡ W d Q R X d S )	Nr   ru  zself-signed.pythontest.neti»  )r}  r^   rM   rv  )rw  r   rx  ry  r‚  rƒ  r…  ÚCERT_localhostr   rY  r³   rz  rQ   rR   rk  rù   )r   rw  r}  rÆ   r{  r   r   r   Útest_networked_bad_certw  s    

z!HTTPSTest.test_networked_bad_certc          	   C   sX   dd l }|  t¡}t d|j¡}|  |j¡}| dd¡ W d Q R X |  	|j
jd¡ d S )Nr   Ú	localhostr^   rM   rv  )rw  rs  rŠ  r   rY  r   r³   rz  rQ   rR   rk  rù   )r   rw  r  rÆ   r{  r   r   r   Útest_local_unknown_certƒ  s    
z!HTTPSTest.test_local_unknown_certc             C   sx   dd l }|  t¡}| |j¡}| t¡ tjd|j|d}|  	|j
¡ | dd¡ | ¡ }|  	|j
¡ |  |jd¡ d S )Nr   rŒ  )r}  r^   z/nonexistenti”  )rw  rs  rŠ  r‚  rƒ  r…  r   rY  r   r#  r   rQ   rÿ   rR   rø   )r   rw  r  r}  rÆ   rw   r   r   r   Útest_local_good_hostnameŒ  s    

z"HTTPSTest.test_local_good_hostnamec          	   C   s¶  dd l }|  t¡}| |j¡}| t¡ tjd|j|d}|  	|j
¡ | dd¡ W d Q R X t dtf¡ tjd|j|dd}W d Q R X |  	|j
¡ | dd¡ W d Q R X d	|_t dtf¡ tjd|j|d	d}W d Q R X | dd
¡ | ¡ }| ¡  | ¡  |  |jd¡ d	|_tjd|j|d}| dd
¡ | ¡ }|  |jd¡ | ¡  | ¡  t dtf¡ tjd|j|dd}W d Q R X |  	|j
¡ | dd¡ W d Q R X d S )Nr   rŒ  )r}  r^   rM   r[   T)r}  r„  Fz/nonexistenti”  )rw  rs  ÚCERT_fakehostnamer‚  rƒ  r…  r   rY  r   r³   ZCertificateErrorrQ   r   Zcheck_warningsÚDeprecationWarningr„  rÿ   r   rR   rø   )r   rw  r  r}  rÆ   rw   r   r   r   Útest_local_bad_hostname™  sB    




z!HTTPSTest.test_local_bad_hostnamerY  z)http.client.HTTPSConnection not availablec             C   s^   xdD ]}|   tjtj|¡ qW x8dD ]0\}}}t |¡}|  ||j¡ |  ||j¡ q&W d S )N)zwww.python.org:abczuser:password@www.python.org))z[fe80::207:e9ff:fe9b]:8000zfe80::207:e9ff:fe9bi@  )zwww.python.org:443zwww.python.orgi»  )zwww.python.org:zwww.python.orgi»  )zwww.python.orgzwww.python.orgi»  )z[fe80::207:e9ff:fe9b]zfe80::207:e9ff:fe9bi»  )z[fe80::207:e9ff:fe9b]:zfe80::207:e9ff:fe9bi»  )r³   r   rÄ   rY  rR   r   r   )r   rÅ   rÆ   rÇ   rÈ   r   r   r   rÉ   Ã  s    

zHTTPSTest.test_host_portc             C   sš   dd l }|js|  d¡ t dd¡}|  |jj¡ | |j	¡}|  
|j¡ tjdd|d}|  |j|¡ |  
|jj¡ tjdd|td}|  |jj¡ d S )Nr   zTLS 1.3 support requiredrŒ  i»  )r}  )r}  Z	cert_file)rw  ZHAS_TLSv1_3rp  r   rY  ro   Z_contextZpost_handshake_authr‚  rƒ  r°   ZassertIsrŠ  )r   rw  rÆ   r}  r   r   r   Útest_tls13_pha×  s    

zHTTPSTest.test_tls13_phaN)r#   r$   r%   r/  rs  rt  r|  r€  r   Zsystem_must_validate_certr  r‰  r‹  r  rŽ  r‘  r[  r\  rP  r   rÉ   r’  r   r   r   r   ro  "  s   
!	*ro  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 )ÚRequestBodyTestc             C   s,   t  d¡| _tdƒ | j_| _| j| j_d S )Nzexample.comr[   )r   rN   rT   r   rO   )r   r   r   r   r/  í  s    zRequestBodyTest.setUpc             C   s(   t  | jj¡}| ¡  t |¡}||fS )N)r&   r'   rO   r   r1   r   rH  )r   ÚfÚmessager   r   r   Úget_headers_and_fpò  s    
z"RequestBodyTest.get_headers_and_fpc          
   C   s°   ddgdfdf}xš|D ]’\}}|   |¡z t d¡| _tdƒ | j_| _| j dd|¡ |  ¡ \}}|  d	|¡ |  d
|¡ |  	| 
d¡d¡ |  	|| ¡ ¡ W d Q R X qW d S )Ns   foos   bars   3
foo
3
bar
0

))s   foos   bars   3
foo
3
bar
0

zexample.comr[   r\   z/urlzContent-TypezContent-LengthzTransfer-Encodingr›   )r…   r   rN   rT   r   rO   rQ   r–  rŸ   rR   Úgetr.   )r   rˆ   rU   rq   r   r”  r   r   r   Útest_list_bodyø  s    
zRequestBodyTest.test_list_bodyc             C   sL   | j  dddddi¡ |  ¡ \}}|  d| d¡¡ |  dt| ¡ ƒ¡ d S )Nr\   z/urlrU   zContent-LengthZ42zcontent-lengthr9  )rT   rQ   r–  rR   r—  rC   r.   )r   r•  r”  r   r   r   Útest_manual_content_length  s
    
z*RequestBodyTest.test_manual_content_lengthc             C   s`   | j  ddd¡ |  ¡ \}}|  d| ¡ ¡ |  | ¡ ¡ |  d| d¡¡ |  d| ¡ ¡ d S )Nr\   z/urlrU   z
text/plainÚ4zcontent-lengths   body)	rT   rQ   r–  rR   Úget_content_typera  Úget_charsetr—  r.   )r   r•  r”  r   r   r   Útest_ascii_body  s    zRequestBodyTest.test_ascii_bodyc             C   s`   | j  ddd¡ |  ¡ \}}|  d| ¡ ¡ |  | ¡ ¡ |  d| d¡¡ |  d| ¡ ¡ d S )Nr\   z/urlu   bodyÃz
text/plainÚ5zcontent-lengths   bodyÁ)	rT   rQ   r–  rR   r›  ra  rœ  r—  r.   )r   r•  r”  r   r   r   Útest_latin1_body  s    z RequestBodyTest.test_latin1_bodyc             C   s`   | j  ddd¡ |  ¡ \}}|  d| ¡ ¡ |  | ¡ ¡ |  d| d¡¡ |  d| ¡ ¡ d S )Nr\   z/urls   bodyÁz
text/plainrž  zcontent-length)	rT   rQ   r–  rR   r›  ra  rœ  r—  r.   )r   r•  r”  r   r   r   Útest_bytes_body%  s    zRequestBodyTest.test_bytes_bodyc          	   C   s¸   |   tjtj¡ ttjdƒ}| d¡ W d Q R X ttjƒr}| j dd|¡ |  ¡ \}}|  	d| 
¡ ¡ |  | ¡ ¡ |  | d¡¡ |  	d| d¡¡ |  	d	| ¡ ¡ W d Q R X d S )
NÚwrU   r\   z/urlz
text/plainzcontent-lengthr›   ztransfer-encodings   4
body
0

)r#  r   ÚunlinkÚTESTFNrÓ   ÚwriterT   rQ   r–  rR   r›  ra  rœ  r—  r.   )r   r”  r•  r   r   r   Útest_text_file_body-  s    z#RequestBodyTest.test_text_file_bodyc          	   C   s¶   |   tjtj¡ ttjdƒ}| d¡ W d Q R X ttjdƒn}| j dd|¡ |  ¡ \}}|  	d| 
¡ ¡ |  | ¡ ¡ |  	d| d¡¡ |  d	|¡ |  	d
| ¡ ¡ W d Q R X d S )NÚwbs   bodyÁr   r\   z/urlz
text/plainr›   zTransfer-EncodingzContent-Lengths   5
bodyÁ
0

)r#  r   r¢  r£  rÓ   r¤  rT   rQ   r–  rR   r›  ra  rœ  r—  rŸ   r.   )r   r”  r•  r   r   r   Útest_binary_file_body<  s    z%RequestBodyTest.test_binary_file_bodyN)r#   r$   r%   r/  r–  r˜  r™  r  rŸ  r   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	d
„ Zdd„ ZdS )ÚHTTPResponseTestc             C   s&   d}t |ƒ}t |¡| _| j ¡  d S )NzXHTTP/1.1 200 Ok
My-Header: first-value
My-Header:                 second-value

Text)r   r   rt   rw   ru   )r   rU   rO   r   r   r   r/  L  s    zHTTPResponseTest.setUpc             C   s6   | j  d¡}|  |d¡ | j  dd¡}|  |d¡ d S )Nz	My-Headerzfirst-value, second-valuezsome default)rw   rv   rR   )r   rS   r   r   r   Útest_getting_headerS  s    z$HTTPResponseTest.test_getting_headerc             C   s   | j  dd¡}|  |d¡ d S )NzNo-Such-Headerzdefault-value)rw   rv   rR   )r   rS   r   r   r   Ú3test_getting_nonexistent_header_with_string_defaultZ  s    zDHTTPResponseTest.test_getting_nonexistent_header_with_string_defaultc             C   s<   | j  dddg¡}|  |d¡ | j  dd¡}|  |d¡ d S )NzNo-Such-HeaderÚdefaultÚvalueszdefault, values)r«  r¬  )rw   rv   rR   )r   rS   r   r   r   Ú5test_getting_nonexistent_header_with_iterable_default^  s    zFHTTPResponseTest.test_getting_nonexistent_header_with_iterable_defaultc             C   s   | j  d¡}|  |d ¡ d S )NzNo-Such-Header)rw   rv   rR   )r   rS   r   r   r   Ú/test_getting_nonexistent_header_without_defaulte  s    z@HTTPResponseTest.test_getting_nonexistent_header_without_defaultc             C   s    | j jddd}|  |d¡ d S )NzNo-Such-Headerri   )r«  )rw   rv   rR   )r   rS   r   r   r   Útest_getting_header_defaultinti  s    z/HTTPResponseTest.test_getting_header_defaultintN)	r#   r$   r%   r/  r©  rª  r­  r®  r¯  r   r   r   r   r¨  J  s   r¨  c               @   sL   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S )ÚTunnelTestsc             C   s*   d}d| _ t | j ¡| _|  |¡| j_d S )Nz:HTTP/1.0 200 OK

HTTP/1.1 200 OK
Content-Length: 42

z	proxy.com)r   r   rN   rT   r8   )r   Úresponse_textr   r   r   r/  n  s    zTunnelTests.setUpc             C   s   | j  ¡  d S )N)rT   r   )r   r   r   r   rV  x  s    zTunnelTests.tearDownc                s   d‡ fdd„	}|S )Nc                s   t ˆ | d | d dS )Nr   r   )r   r   )r   )r  r`  rW  )r±  r   r   r7   |  s    z9TunnelTests._create_connection.<locals>.create_connection)NNr   )r   r±  r7   r   )r±  r   r8   {  s    zTunnelTests._create_connectionc             C   sŽ   d}d}ddi}| j j|||d | j  ddd¡ |  | j jj| j¡ |  | j jjtj¡ |  | j j	|¡ |  | j j
|¡ |  | j j|¡ d S )	Nzdestination.comi¸"  z
User-Agentz!Mozilla/5.0 (compatible, MSIE 11))r   rV   r`   rM   r[   )rT   Ú
set_tunnelrQ   rR   rO   r   r   r   Ú	HTTP_PORTZ_tunnel_hostZ_tunnel_portZ_tunnel_headers)r   Ztunnel_hostZtunnel_portZtunnel_headersr   r   r   Ú!test_set_tunnel_host_port_headers€  s    
z-TunnelTests.test_set_tunnel_host_port_headersc             C   s    | j  ¡  |  t| j jd¡ d S )Nzdestination.com)rT   r;   r³   ÚRuntimeErrorr²  )r   r   r   r   Ú&test_disallow_set_tunnel_after_connect  s    
z2TunnelTests.test_disallow_set_tunnel_after_connectc             C   s   | j  d¡ | j  ddd¡ |  | j jj| j¡ |  | j jjtj¡ |  	d| j jj
¡ |  d| j jj
¡ |  	d| j jj
¡ |  d| j jj
¡ d S )	Nzdestination.comr`   rM   r[   s   CONNECT destination.coms   Host: destination.com:Nones   Host: destination.coms   Host: proxy.com)rT   r²  rQ   rR   rO   r   r   r   r³  rm   r   rŸ   )r   r   r   r   Útest_connect_with_tunnel“  s    z$TunnelTests.test_connect_with_tunnelc             C   sl   | j  d¡ | j  ddd¡ |  | j jj| j¡ |  | j jjtj¡ |  	d| j jj
¡ |  	d| j jj
¡ d S )Nzdestination.comr\   rM   r[   s   CONNECT destination.coms   Host: destination.com)rT   r²  rQ   rR   rO   r   r   r   r³  rm   r   )r   r   r   r   Útest_connect_put_request   s    z$TunnelTests.test_connect_put_requestc          	   C   sz   d}d  |¡}| j d¡ |  |¡| j_| j d¡ t ¡ }| j ddd¡ W d Q R X | ¡  	¡ }|  
d  |¡|¡ d S )	Nz
X-Dummy: 1zHTTP/1.0 200 OK
{}

r   zdestination.comr\   rM   r[   z
header: {})re   rT   Zset_debuglevelr8   r²  r   r   rQ   rŽ   r   rm   )r   Zexpected_headerr±  r   r‘   r   r   r   Útest_tunnel_debuglog¨  s    

z TunnelTests.test_tunnel_debuglogN)r#   r$   r%   r/  rV  r8   r´  r¶  r·  r¸  r¹  r   r   r   r   r°  m  s   
r°  Ú__main__r‹   )Ú	verbosity)4r+   Zhttpr   r&   rc   Úosr×   r‡  r   r&  r[  ZTestCaseræ   r   ÚpathÚdirnamerÔ   Úherer€   rŠ  r  r†  rë   rê   Zchunk_extensionrì   r	  r¬   rí   r$  r   r(   r'   r,   rN   r3   r?   r“   r¯   r,  rB  r;  rF  rS  r]  rc  ro  r“  r¨  r°  r#   Úmainr   r   r   r   Ú<module>   sf   ! n      |"Q.H I`#I
