Your IP : 216.73.216.213


Current Path : /proc/242857/root/opt/alt/python34/lib64/python3.4/test/__pycache__/
Upload File :
Current File : //proc/242857/root/opt/alt/python34/lib64/python3.4/test/__pycache__/test_httplib.cpython-34.pyo

�
f f.��@sddlZddlmZddlZddlZddlZddlZddlZddlZej	Z	ddl
mZejj
e�Zejjed�Zejjed�Zejjed�ZejZGdd�d�ZGd	d
�d
e�ZGdd�dej�ZGd
d�de	�ZGdd�de	�ZGdd�de	�ZGdd�de	�ZGdd�de	�ZGdd�de	�ZGdd�de	�ZGdd�de	�Z Gdd�de	�Z!ej"ddd ��Z#e$d!kr
e#�ndS)"�N)�client)�supportzkeycert.pemzkeycert2.pemzselfsigned_pythontestdotnet.pemc@s[eZdZejdddd�Zdd�Zddd�Zdd	�Zd
d�Z	dS)�
FakeSocketNcCsdt|t�r!|jd�}n||_||_d|_d|_d|_||_||_	dS)N�ascii�rF)
�
isinstance�str�encode�text�	fileclass�data�
sendall_calls�file_closed�host�port)�selfr
rrr�r�6/opt/alt/python34/lib64/python3.4/test/test_httplib.py�__init__s						zFakeSocket.__init__cCs"|jd7_|j|7_dS)N�)r
r)rrrrr�sendall$szFakeSocket.sendallcCsR|dkr'|dkr'tj��n|j|j�|_|j|j_|jS)N�r�rb)rZUnimplementedFileModerr
�file�
file_close�close)r�mode�bufsizerrr�makefile(s
zFakeSocket.makefilecCs
d|_dS)NT)r)rrrrr0szFakeSocket.file_closecCsdS)Nr)rrrrr3szFakeSocket.close)
�__name__�
__module__�__qualname__�io�BytesIOrrrrrrrrrrs
rc@s4eZdZdd�Zdd�Zdd�ZdS)�EPipeSocketcCstj||�||_dS)N)rr�pipe_trigger)rr
r%rrrr8szEPipeSocket.__init__cCs7|j|kr$ttjd��n|j|7_dS)NZgotcha)r%�OSError�errnoZEPIPEr)rrrrrr=szEPipeSocket.sendallcCsdS)Nr)rrrrrBszEPipeSocket.closeN)rr r!rrrrrrrr$6sr$c@s4eZdZdZddd�Zddd�ZdS)	�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.
    rcCs4tjj||�}|dkr0td��n|S)Nrzcaller tried to read past EOF)r"r#�read�AssertionError)r�nrrrrr)KszNoEOFBytesIO.readNcCs4tjj||�}|dkr0td��n|S)Nrzcaller tried to read past EOF)r"r#�readliner*)rZlengthrrrrr,QszNoEOFBytesIO.readline���)rr r!�__doc__r)r,rrrrr(Esr(c@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�HeaderTestscCs�Gdd�dt�}x�dD]�}x�dD]�}tjd�}td	�|_|�|_d
}i}|r�tt|��||<n|jdd||�|j	|jj
|j�d
�q*WqWdS)Nc@s(eZdZdd�Zdd�ZdS)z;HeaderTests.test_auto_headers.<locals>.HeaderCountingBuffercSs
i|_dS)N)�count)rrrrr]szDHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.__init__cSsu|jd�}t|�dkra|djd�j�}|jj|d�|j|d7<ntj||�dS)N�:rrr)�split�len�decode�lowerr0�
setdefault�list�append)r�item�kvZlcKeyrrrr8_szBHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.appendN)rr r!rr8rrrr�HeaderCountingBuffer\sr;TF�Content-length�Host�Accept-encodingzexample.comZblahblahblahZspamspamspam�POST�/r)TF)r<r=r>)r7r�HTTPConnectionr�sock�_bufferrr3�request�assertEqualr0r5)rr;Zexplicit_header�header�conn�body�headersrrr�test_auto_headersXs

zHeaderTests.test_auto_headerscCs
Gdd�dt�}d}d}xtj||�D]k\}}tjd�}td�|_|�|_|j|d|�|j	|jj
d	d
j|��q5Wd}xm|D]e}tjd�}td�|_|�|_|j|dd�|j	|jj
ddj|��q�Wxm|D]e}tjd�}td�|_|�|_|j|dd�|j	|jj
d	d
j|��q!Wxytj||�D]e}tjd�}td�|_|�|_|j|dd�|j	|jj
dd
j|��q�WdS)Nc@s(eZdZdd�Zdd�ZdS)z?HeaderTests.test_content_length_0.<locals>.ContentLengthCheckercSstj|�d|_dS)N)r7r�content_length)rrrrrxs
zHHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.__init__cSsd|jdd�}t|�dkrP|dj�dkrP|dj�|_ntj||�dS)Nr1rrscontent-length)r2r3r5�striprKr7r8)rr9r:rrrr8{s(zFHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.appendN)rr r!rr8rrrr�ContentLengthCheckerwsrM��PUTr?�PATCHzexample.comr@�0z%Header Content-Length incorrect on {}�GET�CONNECT�DELETE�HEAD�OPTIONS�TRACEz.Header Content-Length set for empty body on {}� �1)NrN)rOzPOSTrP)rRrSrTrUrVrW)
r7�	itertools�productrrArrBrCrDrErK�format�chain)rrMZbodiesZmethods_with_body�methodrHrGZmethods_without_bodyrrr�test_content_length_0usH

z!HeaderTests.test_content_length_0cCstjd�}td�|_|jdd�|jdd�|jd|j�|jdd�|jd	|j�|jd
d�|jd|j�|jd
d�|jd|j�|jddd�|jd|j�|jdd�|jd|j�|jdd�|jd|j�|jdd�|jd|j�|jdd�|jd|j�|jd d!�|jd"|j�|jd#d$�|jd%|j�|jd&d$�|jd'|j�|jd(d$�|jd)|j�|jd*d$�|jd+|j�dS),Nzexample.comrRr@zContent-length�*sContent-length: 42ZFooz bar s
Foo:  bar ZBarz	baz	s
Bar: 	baz	Z
AuthorizationzBearer mytokensAuthorization: Bearer mytokenZ
IterHeaderZIterAZIterBsIterHeader: IterA
	IterBZLatinHeaders�sLatinHeader: �Z
Utf8HeadersÀsUtf8Header: Àz
C1-Controls	next�linesC1-Control: next�linezEmbedded-Fold-Spacezis
 alloweds!Embedded-Fold-Space: is
 allowedzEmbedded-Fold-Tabzis
	allowedsEmbedded-Fold-Tab: is
	allowedz	Key Space�valuesKey Space: valuez	KeySpace sKeySpace : valuesNonbreak�SpacesNonbreak�Space: values�NonbreakSpaces�NonbreakSpace: value)rrArrB�
putrequest�	putheader�assertInrC)rrGrrr�test_putheader�s>zHeaderTests.test_putheadercCs�d}tjd�}td�}||_|jdd�|j|jj|��d}tjd�}td�}||_|jdd�|j|jj|��dS)NsCGET /foo HTTP/1.1
Host: [2001::]:81
Accept-Encoding: identity

z[2001::]:81rNrRz/foosEGET /foo HTTP/1.1
Host: [2001:102A::]
Accept-Encoding: identity

z
[2001:102A::])rrArrBrD�
assertTruer�
startswith)r�expectedrGrBrrr�test_ipv6host_header�s		z HeaderTests.test_ipv6host_headercCsad}t|�}tj|�}|j�|j|jd�d�|j|jd�d�dS)Nz4HTTP/1.1 200 OK
First: val
: nval
Second: val

ZFirst�valZSecond)rr�HTTPResponse�beginrE�	getheader)rrHrB�resprrr�!test_malformed_headers_coped_with�s
z-HeaderTests.test_malformed_headers_coped_withcCs�tjd�}td�|_|jdd�d(}xY|D]Q\}}|j||f��.|jtd��|j||�WdQXWdQXq;WdS))Nzexample.comrNrRr@�
Invalid
Name�
ValidValue�Invalid
Name�Invalid
Name�

InvalidName�
InvalidName�
InvalidName� InvalidName�	InvalidName�Invalid:Name�:InvalidName�	ValidName�Invalid
Value�
Invalid
Value�
Invalid
Value�InvalidValue
�
InvalidValue
�
InvalidValue
zInvalid header�rprq�rrrq�rsrq�rtrq�rurq�rvrq�rwrq�rxrq�ryrq�rzrq�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�)	rrArrBrbZsubTest�assertRaisesRegex�
ValueErrorrc)rrGZcases�namerarrr�test_invalid_headers�s.z HeaderTests.test_invalid_headersN)	rr r!rJr_reriror�rrrrr/WsB"
r/c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Z d=d>�Z!d?S)@�	BasicTestcCs�d}t|�}tj|�}|j�|j|jd�d�|j|j��|j|j�|j|j�d�|j	|j��|j|j�|j
�|j	|j�d}t|�}tj|�}|jtj|j�dS)NzHTTP/1.1 200 Ok

TextrrsTextzHTTP/1.1 400.100 Not Ok

Text)
rrrkrlrEr)�assertFalse�isclosed�closedrfr�assertRaises�
BadStatusLine)rrHrBrnrrr�test_status_liness 

zBasicTest.test_status_linescCs)tjd�}|jt|�d�dS)NrNzBadStatusLine("''",))rr�rE�repr)r�excrrr�test_bad_status_repr+szBasicTest.test_bad_status_reprcCs�d}t|�}tj|�}|j�|j|jd�d�|j|j��|j|jd�d�|j|j��|j|j	�|j
�|j|j	�dS)Nz*HTTP/1.1 200 Ok
Content-Length: 4

Text�sTesxt)rrrkrlrEr)r�r�rfr�r)rrHrBrnrrr�test_partial_reads/s

zBasicTest.test_partial_readscCs�d}t|�}tj|�}|j�td�}|j|�}|j|d�|jt|�d�|j|j	��|j|�}|j|d�|jt|�d�|j
|j	��|j|j�|j�|j
|j�dS)Nz*HTTP/1.1 200 Ok
Content-Length: 4

Textr�sTesxt)
rrrkrl�	bytearray�readintorE�bytesr�r�rfr�r)rrHrBrn�br+rrr�test_partial_readintos>s 

z BasicTest.test_partial_readintoscCs�d}t|�}tj|�}|j�|j|jd�d�|j|j��|j|jd�d�|j|jd�d�|j|j��|j|j	�|j
�|j|j	�dS)NzHTTP/1.1 200 Ok

Textr�sTesxtrr)rrrkrlrEr)r�r�rfr�r)rrHrBrnrrr�$test_partial_reads_no_content_lengthRs

z.BasicTest.test_partial_reads_no_content_lengthcCs�d}t|�}tj|�}|j�td�}|j|�}|j|d�|jt|�d�|j|j	��|j|�}|j|d�|jt|�d�|j|�}|j|d�|j
|j	��dS)NzHTTP/1.1 200 Ok

Textr�sTesxtr)rrrkrlr�r�rEr�r�r�rf)rrHrBrnr�r+rrr�(test_partial_readintos_no_content_lengthbs
z2BasicTest.test_partial_readintos_no_content_lengthcCs�d}t|�}tj|�}|j�|j|jd�d�|j|j��|j|jd�d�|j|jd�d�|j|j��dS)Nz+HTTP/1.1 200 Ok
Content-Length: 10

Textr�sTesxtrr)	rrrkrlrEr)r�r�rf)rrHrBrnrrr�"test_partial_reads_incomplete_bodyus
z,BasicTest.test_partial_reads_incomplete_bodycCsd}t|�}tj|�}|j�td�}|j|�}|j|d�|jt|�d�|j|j	��|j|�}|j|d�|jt|�d�|j|�}|j|d�|j
|j	��|j|j�|j�|j
|j�dS)Nz+HTTP/1.1 200 Ok
Content-Length: 10

Textr�sTesxtr)
rrrkrlr�r�rEr�r�r�rfr�r)rrHrBrnr�r+rrr�&test_partial_readintos_incomplete_body�s$

z0BasicTest.test_partial_readintos_incomplete_bodycCs}x'dD]}|jtjtj|�qWxLdD]D\}}}tj|�}|j||j�|j||j�q1WdS)N�www.python.org:abc�user:password@www.python.org�[fe80::207:e9ff:fe9b]:8000�fe80::207:e9ff:fe9b�@�www.python.org:80�www.python.org�P�www.python.org:�[fe80::207:e9ff:fe9b]�[fe80::207:e9ff:fe9b]:)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�
InvalidURLrArErr)r�hp�h�p�crrr�test_host_port�s
zBasicTest.test_host_portcCsTd}d}t|�}tj|�}|j�|jd�}|j||�dS)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)rrrkrlrmrE)rr
Zhdr�srZcookiesrrr�test_response_headers�s
zBasicTest.test_response_headerscCsNtdt�}tj|dd�}|j�|j�rJ|jd�ndS)Nz*HTTP/1.1 200 OK
Content-Length: 14432

r^rUz)Did not expect response from HEAD request)rr(rrkrlr)�fail)rrBrnrrr�test_read_head�s	
zBasicTest.test_read_headcCsytdt�}tj|dd�}|j�td�}|j|�dkr_|jd�n|jt	|�d�dS)	Nz*HTTP/1.1 200 OK
Content-Length: 14432

r^rU�rz)Did not expect response from HEAD requestss)
rr(rrkrlr�r�r�rEr�)rrBrnr�rrr�test_readinto_head�s	
zBasicTest.test_readinto_headcCsodjdd�ttjd�D��d}d|}t|�}tj|�}|jtjd|j�dS)Nz
css|]}d|VqdS)z
Header%d: fooNr)�.0�irrr�	<genexpr>�sz2BasicTest.test_too_many_headers.<locals>.<genexpr>rzHTTP/1.1 200 OK
zgot more than \d+ headers)	�join�rangerZ_MAXHEADERSrrkr�Z
HTTPExceptionrl)rrIr
r�rrrr�test_too_many_headers�s
zBasicTest.test_too_many_headerscCs�d}ttd��w}tjd�}t|�}||_|jdd|�|j|jj	|�d|jdt
|��|f�WdQXdS)NsPGET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Content-Length:rzexample.comrRz/fooz%r != %r)�open�__file__rrArrBrDrfrrgr3)rrhrHrGrBrrr�test_send_file�s	zBasicTest.test_send_filecCs�d}tjd�}td�}||_|j|�|j||j�d|_|jtjd|��|j||j�d|_|jtj	|��|j||j�dS)Ns"this is a test this is only a testzexample.comrr�)
rrArrB�sendrEr�arrayr"r#)rrhrGrBrrr�	test_send�s	
		zBasicTest.test_sendcsvdd��G�fdd�d�}d}tjd�}td�}||_|j|��|j|j|�dS)NcssdVdVdVdS)NrZdata_tworrrrrr�sz/BasicTest.test_send_updating_file.<locals>.datacs.eZdZdZ��Zddd�ZdS)z7BasicTest.test_send_updating_file.<locals>.UpdatingFilerrcSs
|jj�S)N)�d�__next__)rZ	blocksizerrrr)sz<BasicTest.test_send_updating_file.<locals>.UpdatingFile.readNr-)rr r!rr�r)r)rrr�UpdatingFiles	r�sdatazexample.comrN)rrArrBr�rEr)rr�rhrGrBr)rr�test_send_updating_file�s	z!BasicTest.test_send_updating_filecCsmd}dd�}tjd�}td�}||_|jdd|�idd	6�|j|j|�dS)
NsbGET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Content-Length: 11

onetwothreecssdVdVdVdS)NsonestwosthreerrrrrrHsz&BasicTest.test_send_iter.<locals>.bodyzexample.comrNrRz/fooZ11zContent-Length)rrArrBrDrEr)rrhrHrGrBrrr�test_send_iters	 zBasicTest.test_send_itercCsKtjd�}td�|_|jt��|jdd|�WdQXdS)Nzexample.comrNr?�test)rrArrBr��	TypeErrorrD)rrGrrr�test_send_type_errorszBasicTest.test_send_type_errorc	Cs�d}d}t|d�}tj|dd�}|j�|j|j�|�|j�x�tdd�D]o}t|d�}tj|dd�}|j�|j|j|�|j|�|j�|�|j�qkWx�dD]�}t||�}tj|dd�}|j�z�y|j�Wnwtjk
r�}zT|j|j	|�d
t
|�}|jt|�|�|jt|�|�WYdd}~XnX|j
d�Wd|j�Xq�WdS)
Nz{HTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello worl
3
d! 
8
and now 
22
for something completely different
s7hello world! and now for something completely differentz0
r^rRr�rN�foo
zIncompleteRead(%d bytes read)zIncompleteRead expected)rNr�)rrrkrlrEr)rr��IncompleteRead�partialr3r�rr�)	r�
chunked_startrhrBrnr+�xr��expected_messagerrr�test_chunked&s4


0

)zBasicTest.test_chunkedcCsfd}d}t|�}td�}t|d�}tj|dd�}|j�|j|�}|j|d|�|�|j||�|j�x�t	dd�D]�}t|d�}tj|dd�}|j�t
|�}|j|d	|��}	|	|j||	||	��7}	|	|j||	d��7}	|j|d|�|�|j|	|�|j�q�Wx�dD]�}
t||
�}tj|dd�}|j�z�y|j|�}Wnwtjk
rA}	zT|j|	j|�dt|�}|jt
|	�|�|jt|	�|�WYdd}	~	XnX|jd
�Wd|j�Xq}WdS)Nz{HTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello worl
3
d! 
8
and now 
22
for something completely different
s7hello world! and now for something completely different�z0
r^rRrr�rrN�foo
zIncompleteRead(%d bytes read)zIncompleteRead expected)rNr�)r3r�rrrkrlr�rErr��
memoryviewr�r�r�rr�)rr�rhZ	nexpectedr�rBrnr+�mr�r�r�rrr�test_readinto_chunkedRsF


!

)zBasicTest.test_readinto_chunkedcCs�d}t|d�}tj|dd�}|j�|j|j�d�|j|jd�|j|jd�|j|j	��|j
|j�|j�|j|j�dS)NzEHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello world
1
d
z0
r^rUr���OK)
rrrkrlrEr)�status�reasonrfr�r�r�r)rr�rBrnrrr�test_chunked_head�s

zBasicTest.test_chunked_headcCs�d}t|d�}tj|dd�}|j�td�}|j|�}|j|d�|jt|�d
�|j|jd�|j|j	d	�|j
|j��|j|j
�|j�|j
|j
�dS)NzEHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello world
1
d
z0
r^rUr�rsr�r�s)rrrkrlr�r�rEr�r�r�rfr�r�r�r)rr�rBrnr�r+rrr�test_readinto_chunked_head�s

z$BasicTest.test_readinto_chunked_headcCsXtd�}tj|dd�}|j�|j|j�d�|j|j��dS)Nz.HTTP/1.1 200 OK
Content-Length: -1

Hello
r^rRsHello
)rrrkrlrEr)rfr�)rrBrnrrr�test_negative_content_length�s	
z&BasicTest.test_negative_content_lengthcCs�td�}tj|dd�}|j�y|j�Wnztjk
r�}zW|j|jd�|jt|�d�|jt	|�d�|j
|j��WYdd}~XnX|jd�dS)Nz.HTTP/1.1 200 OK
Content-Length: 10

Hello
r^rRsHello
z-IncompleteRead(7 bytes read, 3 more expected)zIncompleteRead expected)
rrrkrlr)r�rEr�r�rrfr�r�)rrBrnr�rrr�test_incomplete_read�s
&zBasicTest.test_incomplete_readcstdd�}tjd��|�_|jt�fdd���j�}|jd|j�|jd|j	d��dS)	NzgHTTP/1.0 401 Authorization Required
Content-type: text/html
WWW-Authenticate: Basic realm="example"
sContent-Lengthzexample.comcs�jddd�S)NrOz/urlrH)rDr)rGrr�<lambda>�sz&BasicTest.test_epipe.<locals>.<lambda>i�zBasic realm="example"zwww-authenticate)
r$rrArBr�r&�getresponserEr�rm)rrBrnr)rGr�
test_epipe�s				zBasicTest.test_epipecCsJdddd}tjt|��}|jtjtjf|j�dS)NzHTTP/1.1 200 Ok�kiz
)rrkrr��LineTooLongr�rl)rrHrnrrr�test_overflowing_status_line�sz&BasicTest.test_overflowing_status_linecCsAdddd}tjt|��}|jtj|j�dS)NzHTTP/1.1 200 OK
X-Foo: barriz

)rrkrr�r�rl)rrHrnrrr�test_overflowing_header_line�sz&BasicTest.test_overflowing_header_linecCsKdddd}tjt|��}|j�|jtj|j�dS)Nz/HTTP/1.1 200 OK
Transfer-Encoding: chunked

�0iza
hello world
0
)rrkrrlr�r�r))rrHrnrrr�test_overflowing_chunked_line�s
z'BasicTest.test_overflowing_chunked_linecCs�d}t|�}tj|�}|j�|j|j�d�|j|j��|j|j	�|j
�|j|j	�dS)NzHTTP/1.1 200 Okr)rrrkrlrEr)rfr�r�r�r)rrHrBrnrrr�test_early_eof�s

zBasicTest.test_early_eofcCs�tjd�}td�}||_d|jd}|jdd|�|j|jd�tjd�}td�}||_d|j}|jdd|�|j|jd�dS)Nzexample.com�xrr?r@)	rrArrBZmssrDrEr
Z
assertGreater)rrGrBrHrrr�test_delayed_ack_opts		
zBasicTest.test_delayed_ack_optcs�tjd�}d�G�fdd�dtj�}||_td�|_|jdd�|jtj|j	�|j
�j�|j
|jj�dS)Nzexample.comcs"eZdZ�fdd�ZdS)z+BasicTest.test_error_leak.<locals>.Responsecs |�tjj|||�dS)N)rrkr)r�pos�kw)�responserrrsz4BasicTest.test_error_leak.<locals>.Response.__init__N)rr r!rr)r�rr�ResponsesrrNrRr@)
rrArkZresponse_classrrBrDr�r�r�rfr�r)rrGrr)r�r�test_error_leaks	zBasicTest.test_error_leakN)"rr 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�rrrrrr�s>

	,6r�c@s(eZdZdd�Zdd�ZdS)�OfflineTestcCs�dh}ddh}x]tt�D]O}||kr:q"ntt|�}t|dd�dkr"|j|�q"q"W|jtj|�dS)N�	responsesZHTTPMessage�
parse_headersr zhttp.client)�dirr�getattr�addZassertCountEqual�__all__)rrhZ	blacklistr�Z
module_objectrrr�test_all)s	zOfflineTest.test_allcCs|jtjtjd�dS)Nz	Not Found)rErrZ	NOT_FOUND)rrrr�test_responses7szOfflineTest.test_responsesN)rr r!r	r
rrrrr(src@s\eZdZdd�Zdd�Zdd�Zejee	d�d�d	d
��Z
dS)�SourceAddressTestcCs\tjtjtj�|_tj|j�|_tj�|_|jj	d�d|_
dS)Nr�)�socket�AF_INET�SOCK_STREAM�servr�	bind_portrZfind_unused_port�source_port�listenrG)rrrr�setUp<s
zSourceAddressTest.setUpcCs<|jr"|jj�d|_n|jj�d|_dS)N)rGrr)rrrr�tearDownCs
	

zSourceAddressTest.tearDowncCs[tjt|jdd|jf�|_|jj�|j|jjj	�d|j�dS)N�source_addressrNr)
rrA�HOSTrrrG�connectrErBZgetsockname)rrrr�testHTTPConnectionSourceAddressJs
z1SourceAddressTest.testHTTPConnectionSourceAddress�HTTPSConnectionz'http.client.HTTPSConnection not definedcCs+tjt|jdd|jf�|_dS)NrrN)rrrrrrG)rrrr� testHTTPSConnectionSourceAddressPsz2SourceAddressTest.testHTTPSConnectionSourceAddressN)rr r!rrr�unittest�skipIf�hasattrrrrrrrr;s
rc@s:eZdZdZdd�Zdd�Zdd�ZdS)�TimeoutTestNcCsDtjtjtj�|_tj|j�t_|jjd�dS)Nr�)	rr
rrrrr�PORTr)rrrrr]szTimeoutTest.setUpcCs|jj�d|_dS)N)rr)rrrrrbs
zTimeoutTest.tearDowncCs@|jtj��tjd�z#tjttj�}|j	�Wdtjd�X|j
|jj�d�|j
�|jtj��tjd�z)tjttjdd�}|j	�Wdtjd�X|j
|jj�d�|j
�tjttjdd�}|j	�|j
|jj�d�|j
�dS)N��timeout)�assertIsNonerZgetdefaulttimeoutZsetdefaulttimeoutrrArrrrrErBZ
gettimeoutr)rZhttpConnrrr�testTimeoutAttributefs*


	

z TimeoutTest.testTimeoutAttribute)rr r!rrrr#rrrrrZsrc@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zej	dd��Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zejeed�d�dd��ZdS)�	HTTPSTestcCs#ttd�s|jd�ndS)Nrzssl support required)rrZskipTest)rrrrr�szHTTPSTest.setUpcCs ddlm}||d|�S)Nr)�make_https_server�certfile)Ztest.ssl_serversr%)rr&r%rrr�make_server�szHTTPSTest.make_servercCs2tjttjdd�}|j|jd�dS)Nr!r )rrrrrrEr!)rr�rrr�test_attributes�szHTTPSTest.test_attributescCs�ddl}tjd�tjd��Xtjdd�}|j|j��}|jdd�WdQX|j	|j
jd�WdQXdS)Nr�networkzself-signed.pythontest.neti�rRr@�CERTIFICATE_VERIFY_FAILED)�sslr�requires�transient_internetrrr��SSLErrorrDrE�	exceptionr�)rr+r��exc_inforrr�test_networked�s
zHTTPSTest.test_networkedcCs�ddl}tjd�tjd��h|j�}tjddd|�}|jdd�|j�}|j	�|j
d|jd	��WdQXdS)
Nrr)zself-signed.pythontest.neti��contextrRr@�nginx�server)r+rr,r-Z_create_unverified_contextrrrDr�rrdrm)rr+r2r�rnrrr�test_networked_noverification�s
	
z'HTTPSTest.test_networked_noverificationc
Cs~tjd�tjd��\tjdd�}|jdd�|j�}|jd�}|j�|j	d|�WdQXdS)Nr)zwww.python.orgi�rRr@zcontent-typez	text/html)
rr,r-rrrDr�rmrrd)rr�rnZcontent_typerrr�&test_networked_trusted_by_default_cert�s

z0HTTPSTest.test_networked_trusted_by_default_certcCs�ddl}tjd�tjd���|j|j�}|j|_|jt	�t
jddd|�}|jdd�|j
�}|jd�}|j�|jd	|�WdQXdS)
Nrr)zself-signed.pythontest.neti�r2rRr@r4r3)r+rr,r-�
SSLContext�PROTOCOL_TLSv1�
CERT_REQUIRED�verify_mode�load_verify_locations� CERT_selfsigned_pythontestdotnetrrrDr�rmrrd)rr+r2r�rnZ
server_stringrrr�test_networked_good_cert�s


z"HTTPSTest.test_networked_good_certcCs�ddl}tjd�tjd���|j|j�}|j|_|jt	�t
jddd|�}|j|j
��}|jdd�WdQX|j|jjd�WdQXdS)	Nrr)zself-signed.pythontest.neti�r2rRr@r*)r+rr,r-r7r8r9r:r;�CERT_localhostrrr�r.rDrEr/r�)rr+r2r�r0rrr�test_networked_bad_cert�s

z!HTTPSTest.test_networked_bad_certc
Csuddl}|jt�}tjd|j�}|j|j��}|jdd�WdQX|j	|j
jd�dS)Nr�	localhostrRr@r*)r+r'r>rrrr�r.rDrEr/r�)rr+r4r�r0rrr�test_local_unknown_cert�sz!HTTPSTest.test_local_unknown_certcCs�ddl}|jt�}|j|j�}|j|_|jt�tj	d|j
d|�}|jdd�|j�}|j
|jd�dS)Nrr@r2rRz/nonexistenti�)r+r'r>r7r8r9r:r;rrrrDr�rEr�)rr+r4r2r�rnrrr�test_local_good_hostname�s
z"HTTPSTest.test_local_good_hostnamecCs�ddl}|jt�}|j|j�}|j|_d|_|jt�t	j
d|jd|�}|j|j
��|jdd�WdQXt	j
d|jd|dd�}|j|j
��|jdd�WdQXd|_t	j
d|jd|dd�}|jdd	�|j�}|j|jd
�d|_t	j
d|jd|�}|jdd	�|j|j�jd
�t	j
d|jd|dd�}|j|j
��|jdd�WdQXdS)NrTr@r2rRr@�check_hostnameFz/nonexistenti�)r+r'�CERT_fakehostnamer7r8r9r:rCr;rrrr�ZCertificateErrorrDr�rEr�)rr+r4r2r�rnrrr�test_local_bad_hostname�s6	
					z!HTTPSTest.test_local_bad_hostnamerz)http.client.HTTPSConnection not availablecCs}x'dD]}|jtjtj|�qWxLdD]D\}}}tj|�}|j||j�|j||j�q1WdS)N�www.python.org:abc�user:password@www.python.org�[fe80::207:e9ff:fe9b]:8000�fe80::207:e9ff:fe9b�@�www.python.org:443�www.python.org��www.python.org:�[fe80::207:e9ff:fe9b]�[fe80::207:e9ff:fe9b]:)rFrG�rHrIrJ�rKrLrM�rNrLrM�rLrLrM�rOrIrM�rPrIrM)rQrRrSrTrUrV)r�rr�rrErr)rr�r�r�r�rrrr�s
zHTTPSTest.test_host_portN)rr r!rr'r(r1r5rZsystem_must_validate_certr6r=r?rArBrErrrrr�rrrrr$�s


	$r$c@sveZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�ZdS)�RequestBodyTestz3Test cases where a request includes a message body.cCs>tjd�|_td�|j_|_|j|j_dS)Nzexample.comrN)rrArGrrB)rrrrr'szRequestBodyTest.setUpcCs8tj|jj�}|j�tj|�}||fS)N)r"r#rBrr,rr)r�f�messagerrr�get_headers_and_fp,s
z"RequestBodyTest.get_headers_and_fpcCsk|jjdddidd6�|j�\}}|jd|jd��|jdt|j���dS)NrOz/urlrHZ42zContent-Lengthzcontent-length�)rGrDrZrE�getr3r))rrYrXrrr�test_manual_content_length2s
z*RequestBodyTest.test_manual_content_lengthcCs�|jjddd�|j�\}}|jd|j��|j|j��|jd|jd��|jd|j��dS)NrOz/urlrHz
text/plain�4zcontent-lengthsbody)	rGrDrZrE�get_content_typer"�get_charsetr\r))rrYrXrrr�test_ascii_body;szRequestBodyTest.test_ascii_bodycCs�|jjddd�|j�\}}|jd|j��|j|j��|jd|jd��|jd|j��dS)NrOz/urlubodyÁz
text/plain�5zcontent-lengthsbody�)	rGrDrZrEr_r"r`r\r))rrYrXrrr�test_latin1_bodyCsz RequestBodyTest.test_latin1_bodycCs�|jjddd�|j�\}}|jd|j��|j|j��|jd|jd��|jd|j��dS)NrOz/urlsbody�z
text/plainrbzcontent-length)	rGrDrZrEr_r"r`r\r))rrYrXrrr�test_bytes_bodyKszRequestBodyTest.test_bytes_bodycCs�|jtjtj�ttjd��}|jd�WdQXttj���}|jjdd|�|j�\}}|j	d|j
��|j|j��|j	d|j
d��|j	d|j��WdQXdS)	N�wrHrOz/urlz
text/plainr^zcontent-lengthsbody)�
addCleanupr�unlink�TESTFNr��writerGrDrZrEr_r"r`r\r))rrXrYrrr�test_file_bodySszRequestBodyTest.test_file_bodycCs�|jtjtj�ttjd��}|jd�WdQXttjd���}|jjdd|�|j�\}}|j	d|j
��|j|j��|j	d|j
d��|j	d|j��WdQXdS)	N�wbsbody�rrOz/urlz
text/plainrbzcontent-length)rfrrgrhr�rirGrDrZrEr_r"r`r\r))rrXrYrrr�test_binary_file_body_sz%RequestBodyTest.test_binary_file_bodyN)rr r!r.rrZr]rarcrdrjrlrrrrrW$s	rWc@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�HTTPResponseTestcCs5d}t|�}tj|�|_|jj�dS)NzXHTTP/1.1 200 Ok
My-Header: first-value
My-Header:                 second-value

Text)rrrkrnrl)rrHrBrrrrnszHTTPResponseTest.setUpcCsK|jjd�}|j|d�|jjdd�}|j|d�dS)Nz	My-Headerzfirst-value, second-valuezsome default)rnrmrE)rrFrrr�test_getting_headerusz$HTTPResponseTest.test_getting_headercCs)|jjdd�}|j|d�dS)NzNo-Such-Headerz
default-value)rnrmrE)rrFrrr�3test_getting_nonexistent_header_with_string_default|szDHTTPResponseTest.test_getting_nonexistent_header_with_string_defaultcCsT|jjdddg�}|j|d�|jjdd�}|j|d�dS)NzNo-Such-Header�default�valueszdefault, values)zdefaultzvalues)rnrmrE)rrFrrr�5test_getting_nonexistent_header_with_iterable_default�szFHTTPResponseTest.test_getting_nonexistent_header_with_iterable_defaultcCs&|jjd�}|j|d�dS)NzNo-Such-Header)rnrmrE)rrFrrr�/test_getting_nonexistent_header_without_default�sz@HTTPResponseTest.test_getting_nonexistent_header_without_defaultcCs,|jjddd�}|j|d�dS)NzNo-Such-Headerrpr`)rnrmrE)rrFrrr�test_getting_header_defaultint�sz/HTTPResponseTest.test_getting_header_defaultintN)	rr r!rrnrorrrsrtrrrrrmlsrmc@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�TunnelTestscsLd�dd�fdd�}d|_tj|j�|_||j_dS)Nz:HTTP/1.0 200 OK

HTTP/1.1 200 OK
Content-Length: 42

cst�d|dd|d�S)Nrrrr)r)Zaddressr!r)�
response_textrr�create_connection�sz,TunnelTests.setUp.<locals>.create_connectionz	proxy.com)rrrArGZ_create_connection)rrwr)rvrr�s
	zTunnelTests.setUpcCs|jj�dS)N)rGr)rrrrr�szTunnelTests.tearDowncCs�d}d}idd6}|jj|d|d|�|jjddd	�|j|jjj|j�|j|jjjtj�|j|jj	|�|j|jj
|�|j|jj|�dS)
Nzdestination.comi�"z!Mozilla/5.0 (compatible, MSIE 11)z
User-AgentrrIrUr@rN)rG�
set_tunnelrDrErBrrr�	HTTP_PORTZ_tunnel_hostZ_tunnel_portZ_tunnel_headers)rZtunnel_hostZtunnel_portZtunnel_headersrrr�!test_set_tunnel_host_port_headers�s
z-TunnelTests.test_set_tunnel_host_port_headerscCs*|jj�|jt|jjd�dS)Nzdestination.com)rGrr��RuntimeErrorrx)rrrr�&test_disallow_set_tunnel_after_connect�s
z2TunnelTests.test_disallow_set_tunnel_after_connectcCs�|jjd�|jjddd�|j|jjj|j�|j|jjjtj�|j	d|jjj
�|jd|jjj
�|j	d|jjj
�|jd|jjj
�dS)	Nzdestination.comrUr@rNsCONNECT destination.comsHost: destination.com:NonesHost: destination.comsHost: proxy.com)rGrxrDrErBrrrryrdrZassertNotIn)rrrr�test_connect_with_tunnel�sz$TunnelTests.test_connect_with_tunnelcCs�|jjd�|jjddd�|j|jjj|j�|j|jjjtj�|j	d|jjj
�|j	d|jjj
�dS)Nzdestination.comrOr@rNsCONNECT destination.comsHost: destination.com)rGrxrDrErBrrrryrdr)rrrr�test_connect_put_request�sz$TunnelTests.test_connect_put_requestN)	rr r!rrrzr|r}r~rrrrru�s

ruc
Cs)tjtttttttt	t
�	dS)N)rZrun_unittestr/rr�rr$rWrrmru)�verboserrr�	test_main�s	r��__main__)%r'Zhttprr"rZ�osr�rrZTestCaser�r�path�dirnamer��herer�r>rDr<rrr$r#r(r/r�rrrr$rWrmruZreap_threadsr�rrrrr�<module>s@		���.�H#<