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Zddd„Zd	dd„ZdS )
ÚNoEOFBytesIOz«Like BytesIO, but raises AssertionError on EOF.

    This is used below to test that http.client doesn't try to read
    more from the underlying file than it should.
    éÿÿÿÿ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%   Ú__doc__r.   r1   r   r   r   r   r,   ^   s   
r,   c                   s4   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Z‡  ZS )ÚFakeSocketHTTPConnectionz@HTTPConnection subclass using FakeSocket; counts connect() callsc                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 )z.Count the number of times connect() is invokedr   )r5   r6   Úconnect)r   )r;   r   r   r<   y   s    z FakeSocketHTTPConnection.connectc             O   s
   t | jŽ S )N)r   r7   )r   ÚposÚkwr   r   r   r8   ~   s    z*FakeSocketHTTPConnection.create_connection)r#   r$   r%   r3   r   r<   r8   Ú__classcell__r   r   )r;   r   r4   p   s   r4   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ÚlowerrA   Ú
setdefaultÚlistÚappend)r   ÚitemÚkvZlcKeyr   r   r   rI   ‰   s    
zBHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.appendN)r#   r$   r%   r   rI   r   r   r   r   ÚHeaderCountingBuffer†   s   rL   )TF)zContent-lengthZHostzAccept-encodingzexample.comZblahblahblahZspamspamspamÚPOSTú/r   )rH   r   ÚHTTPConnectionr   ÚsockÚ_bufferr	   rD   ÚrequestÚassertEqualrA   rF   )r   rL   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)rH   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 )NrB   r   r   s   content-length)rC   rD   rF   ÚstriprY   rH   rI   )r   rJ   rK   r   r   r   rI   ¥   s    zFHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.appendN)r#   r$   r%   r   rI   r   r   r   r   ÚContentLengthChecker¡   s   r[   )NÚ )ÚPUTrM   ZPATCHzexample.comrN   ó   0z%Header Content-Length incorrect on {})ÚGETÚCONNECTZDELETEÚHEADZOPTIONSZTRACEz.Header Content-Length set for empty body on {}r\   ú ó   1)rH   Ú	itertoolsÚproductr   rO   r   rP   rQ   rR   rS   rY   ÚformatÚchain)r   r[   ZbodiesZmethods_with_bodyÚmethodrV   rU   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_   rN   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   rO   r   rP   Ú
putrequestÚ	putheaderÚassertInrQ   )r   rU   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   rO   r   rP   rR   Ú
assertTruer   Ú
startswith)r   ÚexpectedrU   rP   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ÚbeginrS   Ú	getheader)r   rV   rP   Ú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   ru   rv   rS   rw   ÚmsgÚjoinÚmapÚchrZassertIsNotNonern   rp   rq   Úendswith)r   rV   rP   rx   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_   rN   ))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   rO   r   rP   rl   ÚsubTestÚassertRaisesRegexÚ
ValueErrorrm   )r   rU   Ú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   ru   r   Úcaptured_stdoutrv   ÚgetvalueÚ
splitlinesrS   )r   rV   rP   rx   ÚoutputÚlinesr   r   r   Útest_headers_debuglevel\  s    
z#HeaderTests.test_headers_debuglevelN)r#   r$   r%   rX   ri   ro   rs   ry   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   rM   rN   T)Úencode_chunked)r   rO   r   rP   rl   Z
endheadersÚ
_make_bodyÚ_parse_requestr   Ú_parse_chunkedrS   Úexpected_body)r   rU   Ú_rV   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   rM   rN   zTransfer-EncodingÚchunkedzcontent-lengthc             S   s   g | ]}|  ¡ ‘qS r   )rF   )Ú.0Úkr   r   r   ú
<listcomp>…  s    z>TransferEncodingTest.test_explicit_headers.<locals>.<listcomp>zlatin-1c             S   s   g | ]}|  ¡ ‘qS r   )rF   )r   rž   r   r   r   rŸ   ‘  s    zgzip, chunkedT)rW   r•   rV   c             S   s   g | ]}|  ¡ ‘qS r   )rF   )r   rž   r   r   r   rŸ     s    )r   rO   r   rP   rR   r–   r—   r   ÚassertNotInÚkeysrS   r™   rE   r˜   )r   rU   rš   rW   rV   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   rM   rN   )Úempty_lineszTransfer-Encodingrœ   zcontent-lengthc             S   s   g | ]}|  ¡ ‘qS r   )rF   )r   rž   r   r   r   rŸ   ¯  s    z5TransferEncodingTest.test_request.<locals>.<listcomp>)r   rO   r   rP   rR   r–   r—   r   r˜   rS   r™   r    )r   r£   rU   rš   rW   rV   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   rM   rN   r   zTransfer-Encodingrœ   zcontent-lengthc             S   s   g | ]}|  ¡ ‘qS r   )rF   )r   rž   r   r   r   rŸ   ¸  s    z8TransferEncodingTest.test_empty_body.<locals>.<listcomp>s   0

)	r   rO   r   rP   rR   r—   r   rS   r    )r   rU   rš   rW   rV   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™   rC   Ú	enumeraterD   )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   rB   zlatin-1)rC   rD   rE   rZ   r   )r   r   r’   rR   rW   r0   Úkeyrt   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   )rC   ÚintrS   rD   rI   r   )r   r   rV   Ú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   ru   rv   rS   r.   ÚassertFalseÚisclosedÚclosedrp   r   ÚassertRaisesÚBadStatusLine)r   rV   rP   rx   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µ   rS   Ú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   ru   rv   rS   r.   r±   r²   rp   r³   r   )r   rV   rP   rx   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   ru   rv   rS   r1   r±   r²   r.   rp   r³   r   )r   rV   rP   rx   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   ru   rv   Ú	bytearrayÚreadintorS   r~   r±   r²   rp   r³   r   )r   rV   rP   rx   Ú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   ru   rv   rS   r.   r±   r²   rp   r³   r   )r   rV   rP   rx   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   ru   rv   r¼   r½   rS   r~   r±   r²   rp   )r   rV   rP   rx   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   ru   rv   rS   r.   r±   r²   rp   )r   rV   rP   rx   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   ru   rv   r¼   r½   rS   r~   r±   r²   rp   r³   r   )r   rV   rP   rx   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   Ú
InvalidURLrO   rS   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   ru   rv   rw   rS   )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

ra   )rh   z)Did not expect response from HEAD request)r   r,   r   ru   rv   r.   Úfail)r   rP   rx   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

ra   )rh   é   r   z)Did not expect response from HEAD requests        )
r   r,   r   ru   rv   r¼   r½   rÍ   rS   r~   )r   rP   rx   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   ru   r‡   ZHTTPExceptionrv   )r   rW   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   rO   r   rP   rR   rp   r   rq   rD   )r   rr   rV   rU   rP   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   rO   r   rP   ÚsendrS   r   Úarrayr&   r'   )r   rr   rU   rP   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   rO   r   rP   r×   rS   r   )r   rÝ   rr   rU   rP   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   rV   þ  s    z&BasicTest.test_send_iter.<locals>.bodyzexample.comr\   r_   z/foozContent-LengthZ11)r   rO   r   rP   rR   rS   r   )r   rr   rV   rU   rP   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 )z8Check that request() respects the configured block size.é   zexample.com)rÜ   Nó   aó   br]   rN   zContent-LengthÚ9r   s   

r   )r   rO   r   rP   rR   r&   r'   rS   r   r   rC   )r   rÜ   rU   rP   rr   rV   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 )z5Check that send() respects the configured block size.rá   zexample.com)rÜ   Nrâ   rã   rŒ   )
r   rO   r   rP   r×   r&   r'   rS   r   r   )r   rÜ   rU   rP   rr   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\   rM   Útest)r   rO   r   rP   r´   Ú	TypeErrorrR   )r   rU   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_   )rh   r   é   )r\   zfoo
zIncompleteRead(%d bytes read)zIncompleteRead expected)Úchunked_expectedr   Úchunked_startÚ
last_chunkÚchunked_endr   ru   rv   rS   r.   r   r   ÚIncompleteReadÚpartialrD   r·   r	   rÍ   )r   rr   rP   rx   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_   )rh   r   rê   r   )r\   zfoo
zIncompleteRead(%d bytes read)zIncompleteRead expected)rë   rD   r¼   r   rì   rí   rî   r   ru   rv   r½   rS   r   r   Ú
memoryviewrï   rð   r·   r	   rÍ   )r   rr   Z	nexpectedr¾   rP   rx   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
ra   )rh   r   éÈ   ÚOK)r   rí   rî   r   ru   rv   rS   r.   ÚstatusÚreasonrp   r²   r±   r³   r   )r   rì   rP   rx   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
ra   )rh   rÏ   r   s        r÷   rø   )r   rí   rî   r   ru   rv   r¼   r½   rS   r~   rù   rú   rp   r²   r±   r³   r   )r   rì   rP   rx   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_   )rh   s   Hello
)r   r   ru   rv   rS   r.   rp   r²   )r   rP   rx   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_   )rh   s   Hello
z-IncompleteRead(7 bytes read, 3 more expected)zIncompleteRead expected)r   r   ru   rv   r.   rï   rS   rð   r·   r	   rp   r²   rÍ   )r   rP   rx   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/urlrV   )rR   r   )rU   r   r   Ú<lambda>¾  r   z&BasicTest.test_epipe.<locals>.<lambda>i‘  zBasic realm="example"zwww-authenticate)
r(   r   rO   rP   r´   r*   ÚgetresponserS   rù   rw   )r   rP   rx   r   )rU   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   ru   r   r´   ÚLineTooLongrµ   rv   )r   rV   rx   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   ru   r   r´   r  rv   )r   rV   rx   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   ru   r   rv   r´   r  r.   )r   rV   rx   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   ru   rv   rS   r.   rp   r²   r±   r³   r   )r   rV   rP   rx   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   ru   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_   rN   )r   rO   ru   Zresponse_classr   rP   rR   r´   rµ   r   rp   r³   r   )r   rU   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_   )rh   )rë   r   rì   Úlast_chunk_extendedrî   r   ru   rv   rS   r.   r   )r   Zextrarr   rP   rx   r   r   r   Útest_chunked_extensionü  s    z BasicTest.test_chunked_extensionc             C   sB   t }ttt ƒ}tj|dd}| ¡  |  | ¡ |¡ | 	¡  dS )z9some servers may serve up a short chunked encoding streamr_   )rh   N)
rë   r   rì   rí   r   ru   rv   rS   r.   r   )r   rr   rP   rx   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 )z&See that trailers are read and ignoredr_   )rh   r   N)rë   r   rì   rí   r­   rî   r   ru   rv   rS   r.   r   r   )r   rr   rP   rx   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 )zDCheck that we don't read past the end of the chunked-encoding streamÚ	extradatar_   )rh   r   N)rë   r   rì   rí   r­   rî   r   ru   rv   rS   r.   r   r
   r   )r   rr   r  rP   rx   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 )zBCheck that we don't read past the end of the Content-Length streams	   extradatas
   Hello123
s'   HTTP/1.1 200 OK
Content-Length: 10

r_   )rh   N)r   r   ru   rv   rS   r.   r   r   )r   r  rr   rP   rx   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_   )rh   iÐ  )	r   r   ru   rv   rS   Ú	readlinesr   r.   r   )r   r  rr   rP   rx   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_   )rh   iÐ  )	r   r   ru   rv   rS   Úread1r   r.   r   )r   r  rr   rP   rx   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_   )rh   é
   r   )	r   r   ru   rv   rS   r1   r   r.   r   )r   r  rr   rP   rx   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_   )rh   é   rŒ   )	r   r   ru   rv   rS   r  r.   r   r   )r   r  rr   rP   rx   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.   )rU   Ú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   rO   ÚgetsocknamerR   r   rS   rù   rø   r   r   Údetach)r   r  ZthreadrU   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               @   sx   e Zd 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 )ÚExtendedReadTestz*
    Test peek(), read1(), readline()
    zOHTTP/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_   )rh   )	r   r’   r   ru   rv   r&   ÚBufferedReaderÚfprx   )r   rP   rx   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-   )rx   r/  ÚpeekZassertGreaterrD   ZassertGreaterEqualrp   rq   r.   rS   r±   rI   r   Úlines_expected)r   rx   r2  ÚallrÈ   Zp2rÚ   r   )r1  r   Ú	test_peek¯  s(    


zExtendedReadTest.test_peekc             C   s   | j }|  | j j| j¡ d S )N)rx   Ú_verify_readliner1   r4  )r   rx   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   )rD   rp   r„   rI   rS   r   )r   r1   rr   r5  r©   r   r   r   r7  Ò  s    
z!ExtendedReadTest._verify_readlinec                s0   ˆj ‰ ‡ ‡fdd„}t|ƒ}ˆ |jˆj¡ d S )Nc                 s   ˆ   d¡} ˆ t| ƒd¡ | S )Né   )r  ÚassertLessEqualrD   )Zres)rx   r   r   r   r   á  s    
z&ExtendedReadTest.test_read1.<locals>.r)rx   Ú	Readlinerr7  r1   r4  )r   r   Z	readlinerr   )rx   r   r   Ú
test_read1ß  s    zExtendedReadTest.test_read1c             C   s@   | j }g }x| ¡ }|sP | |¡ qW |  d |¡| j¡ d S )Nr   )rx   r  rI   rS   r   r4  )r   rx   r5  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   )rx   r  r;  rD   rI   rS   r   r4  )r   rx   r5  r   r   r   r   Útest_read1_boundedò  s    
z#ExtendedReadTest.test_read1_boundedc             C   s   |   | j d¡d¡ d S )Nr   r   )rS   rx   r  )r   r   r   r   Útest_read1_0ý  s    zExtendedReadTest.test_read1_0c             C   s    | j  d¡}|  dt|ƒ¡ d S )Nr   )rx   r3  r;  rD   )r   rÈ   r   r   r   Útest_peek_0   s    zExtendedReadTest.test_peek_0)r#   r$   r%   r3   r’   Úfindr
   r4  Zlines_chunkedr0  r6  r8  r7  r=  r>  r?  r@  rA  r   r   r   r   r-  ˆ  s   		
r-  c               @   s   e Zd ZdZdZdS )ÚExtendedReadTestChunkedz:
    Test peek(), read1(), readline() in chunked mode
    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

N)r#   r$   r%   r3   r’   r   r   r   r   rC    s   rC  c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r<  zT
    a simple readline class that uses an arbitrary read function and buffering
    c             C   s   || _ d| _d S )Nr   )ÚreadfuncÚ	remainder)r   rD  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   r9  r-   r   r   )rE  rB  rD   rI   rD  r   )r   Úlimitr   Zdatalenr.   r¨   r   r   r   r1   "  s,    


zReadliner.readlineN)r#   r$   r%   r3   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   rq   ÚgetattrÚaddZassertCountEqualÚ__all__)r   rr   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)rS   r   rH  Ú	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	FORBIDDENrO  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†   rp   Úhasattrr   )r   rr   Úconstr   r   r   Útest_client_constantsN  sr    
z!OfflineTest.test_client_constantsN)r#   r$   r%   rN  rP  rS  r   r   r   r   rG  <  s   rG  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&  rU   )r   r   r   r   r0  Ž  s
    

zSourceAddressTest.setUpc             C   s*   | j r| j  ¡  d | _ | j ¡  d | _d S )N)rU   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   rO   r%  r   rV  rU   r<   rS   rP   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\   )rX  )r   rZ  r%  r   rV  rU   )r   r   r   r   Ú testHTTPSConnectionSourceAddress¢  s    
z2SourceAddressTest.testHTTPSConnectionSourceAddressN)r#   r$   r%   r0  rW  rY  ÚunittestÚskipIfrQ  r   r[  r   r   r   r   rT    s
   rT  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   rU  r^  ÚPORTr&  )r   r   r   r   r0  ¯  s    zTimeoutTest.setUpc             C   s   | j  ¡  d | _ d S )N)r  r   )r   r   r   r   rW  ´  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   rO   r%  r^  r_  r<   rS   rP   Z
gettimeoutr   )r   ZhttpConnr   r   r   ÚtestTimeoutAttribute¸  s*    


z TimeoutTest.testTimeoutAttribute)r#   r$   r%   r_  r0  rW  rc  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)ÚversionrT   z4HTTP/{} 200 OK
{}Content-Length: 12

Dummy body
r_   z/open-connectionr   z/subsequent-requestrŒ   )
r†   rf   r4   rb  rP   rR   r   rS   r.   r5   )r   Útestsre  rT   Zreuser€   rU   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 )z3Return BufferedReader that raises ECONNRESET at EOFc                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   )rj  r   Úmake_reset_readerü  s    
z<PersistenceTest.test_disconnected.<locals>.make_reset_reader)Ú	exceptionr   r_   z/eof-responsez
/reconnectrŒ   )r&   r'   r   ÚRemoteDisconnectedrh  r†   r4   rR   r´   r   rb  rP   rS   r5   )r   rk  rf  Zstream_factoryrl  rU   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_   rN   ZExpectz100-continue)rW   z
/reconnectrŒ   )
r4   rR   r´   r   rm  r   rb  rP   rS   r5   )r   rU   r   r   r   Útest_100_close  s    zPersistenceTest.test_100_closeN)r#   r$   r%   rg  rn  ro  r   r   r   r   rd  Ú  s   rd  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 )NrZ  zssl support required)rQ  r   ÚskipTest)r   r   r   r   r0  $  s    
zHTTPSTest.setUpc             C   s   ddl m} || |dS )Nr   )Úmake_https_server)Úcertfile)Ztest.ssl_serversrr  )r   rs  rr  r   r   r   Úmake_server(  s    zHTTPSTest.make_serverc             C   s$   t jttjdd}|  |jd¡ d S )Nr`  )ra  )r   rZ  r%  r^  r_  rS   ra  )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_   rN   ÚCERTIFICATE_VERIFY_FAILED)Ússlr   ÚrequiresÚtransient_internetr   rZ  r´   ÚSSLErrorrR   rS   rl  rú   )r   rx  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   rv  zself-signed.pythontest.neti»  )Úcontextr_   rN   ÚnginxÚserver)rx  r   ry  rz  Z_create_unverified_contextr   rZ  rR   r   r   rn   rw   )r   rx  r~  rÇ   rx   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 )Nrv  zwww.python.orgi»  r_   rN   zcontent-typez	text/html)
r   ry  rz  r   rZ  rR   r   rw   r   rn   )r   rÇ   rx   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   rv  zself-signed.pythontest.netTi»  )r~  r_   rN   z(?i)key.too.weakzGot z trying to connect to z). See https://bugs.python.org/issue36816.r€  r  )rx  r   ry  rz  Ú
SSLContextÚPROTOCOL_TLS_CLIENTrS   Zverify_modeZCERT_REQUIREDÚcheck_hostnameÚload_verify_locationsÚ CERT_selfsigned_pythontestdotnetr   rZ  rR   r   r{  r	   ÚreÚsearchr\  ZSkipTestrw   r   rn   )	r   rx  Zselfsigned_pythontestdotnetr~  rÇ   rx   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   rv  zself-signed.pythontest.neti»  )r~  r_   rN   rw  )rx  r   ry  rz  rƒ  r„  r†  ÚCERT_localhostr   rZ  r´   r{  rR   rS   rl  rú   )r   rx  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_   rN   rw  )rx  rt  r‹  r   rZ  r   r´   r{  rR   rS   rl  rú   )r   rx  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”  )rx  rt  r‹  rƒ  r„  r†  r   rZ  r   r$  r   rR   r   rS   rù   )r   rx  r€  r~  rÇ   rx   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_   rN   r\   T)r~  r…  Fz/nonexistenti”  )rx  rt  ÚCERT_fakehostnamerƒ  r„  r†  r   rZ  r   r´   ZCertificateErrorrR   r   Zcheck_warningsÚDeprecationWarningr…  r   r   rS   rù   )r   rx  r€  r~  rÇ   rx   r   r   r   Útest_local_bad_hostname™  sB    




z!HTTPSTest.test_local_bad_hostnamerZ  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Å   rZ  rS   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)rx  ZHAS_TLSv1_3rq  r   rZ  rp   Z_contextZpost_handshake_authrƒ  r„  r±   ZassertIsr‹  )r   rx  rÇ   r~  r   r   r   Útest_tls13_pha×  s    

zHTTPSTest.test_tls13_phaN)r#   r$   r%   r0  rt  ru  r}  r  r   Zsystem_must_validate_certr‚  rŠ  rŒ  rŽ  r  r’  r\  r]  rQ  r   rÊ   r“  r   r   r   r   rp  "  s   
!	*rp  c               @   sX   e Z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 )ÚRequestBodyTestz3Test cases where a request includes a message body.c             C   s,   t  d¡| _tdƒ | j_| _| j| j_d S )Nzexample.comr\   )r   rO   rU   r   rP   )r   r   r   r   r0  í  s    zRequestBodyTest.setUpc             C   s(   t  | jj¡}| ¡  t |¡}||fS )N)r&   r'   rP   r   r1   r   rI  )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   rO   rU   r   rP   rR   r—  r    rS   Úgetr.   )r   r‰   rV   rr   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/urlrV   zContent-LengthZ42zcontent-lengthr:  )rU   rR   r—  rS   r˜  rD   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/urlrV   z
text/plainÚ4zcontent-lengths   body)	rU   rR   r—  rS   Úget_content_typerb  Ú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Á)	rU   rR   r—  rS   rœ  rb  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)	rU   rR   r—  rS   rœ  rb  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ÚwrV   r]   z/urlz
text/plainzcontent-lengthrœ   ztransfer-encodings   4
body
0

)r$  r   ÚunlinkÚTESTFNrÔ   ÚwriterU   rR   r—  rS   rœ  rb  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¥  rU   rR   r—  rS   rœ  rb  r  r˜  r    r.   )r   r•  r–  r   r   r   Útest_binary_file_body<  s    z%RequestBodyTest.test_binary_file_bodyN)r#   r$   r%   r3   r0  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   ru   rx   rv   )r   rV   rP   r   r   r   r0  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)rx   rw   rS   )r   rT   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)rx   rw   rS   )r   rT   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­  )rx   rw   rS   )r   rT   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)rx   rw   rS   )r   rT   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-Headerrj   )r¬  )rx   rw   rS   )r   rT   r   r   r   Útest_getting_header_defaultinti  s    z/HTTPResponseTest.test_getting_header_defaultintN)	r#   r$   r%   r0  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   rO   rU   r9   )r   Úresponse_textr   r   r   r0  n  s    zTunnelTests.setUpc             C   s   | j  ¡  d S )N)rU   r   )r   r   r   r   rW  x  s    zTunnelTests.tearDownc                s   d‡ fdd„	}|S )Nc                s   t ˆ | d | d dS )Nr   r   )r   r   )r   )r  ra  rX  )r²  r   r   r8   |  s    z9TunnelTests._create_connection.<locals>.create_connection)NNr   )r   r²  r8   r   )r²  r   r9   {  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   rW   ra   rN   r\   )rU   Ú
set_tunnelrR   rS   rP   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)rU   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.comra   rN   r\   s   CONNECT destination.coms   Host: destination.com:Nones   Host: destination.coms   Host: proxy.com)rU   r³  rR   rS   rP   r   r   r   r´  rn   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]   rN   r\   s   CONNECT destination.coms   Host: destination.com)rU   r³  rR   rS   rP   r   r   r   r´  rn   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]   rN   r\   z
header: {})rf   rU   Zset_debuglevelr9   r³  r   rŽ   rR   r   r   rn   )r   Zexpected_headerr²  r‘   r’   r   r   r   Útest_tunnel_debuglog¨  s    

z TunnelTests.test_tunnel_debuglogN)r#   r$   r%   r0  rW  r9   rµ  r·  r¸  r¹  rº  r   r   r   r   r±  m  s   
r±  Ú__main__rŒ   )Ú	verbosity)4r+   Zhttpr   r&   rd   Ú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,   rO   r4   r@   r”   r°   r-  rC  r<  rG  rT  r^  rd  rp  r”  r©  r±  r#   Úmainr   r   r   r   Ú<module>   sf   ! n      |"Q.H I`#I
