Your IP : 216.73.216.85


Current Path : /opt/alt/python38/lib64/python3.8/test/__pycache__/
Upload File :
Current File : //opt/alt/python38/lib64/python3.8/test/__pycache__/test_httplib.cpython-38.pyc

U

i�f�3�@sddlZddlmZmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZej
Z
ddlmZej�e�Zej�ed�Zej�ed�Zej�ed�ZdZdZd	Zd
ZdedZd
ZdZejZGdd�d�Z Gdd�de �Z!Gdd�dej"�Z#Gdd�dej$�Z%Gdd�de
�Z&Gdd�de
�Z'Gdd�de
�Z(Gdd�de
�Z)Gdd�de
�Z*Gd d!�d!e*�Z+Gd"d#�d#�Z,Gd$d%�d%e
�Z-Gd&d'�d'e
�Z.Gd(d)�d)e
�Z/Gd*d+�d+e
�Z0Gd,d-�d-e
�Z1Gd.d/�d/e
�Z2Gd0d1�d1e
�Z3Gd2d3�d3e
�Z4e5d4k�rej6d5d6�dS)7�N)�client�
HTTPStatus)�supportzkeycert.pemzkeycert2.pemzselfsigned_pythontestdotnet.pemz{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 differentz;foo=barz0
�0�
zX-Dummy: foo
X-Dumm2: bar
c@sHeZdZejddfdd�Zdd�Zddd�Zdd	�Zd
d�Z	dd
�Z
dS)�
FakeSocketNcCsBt|t�r|�d�}||_||_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/python38/lib64/python3.8/test/test_httplib.py�__init__0s

zFakeSocket.__init__cCs |jd7_|j|7_dS)N�)rr�rrrrr�sendall;szFakeSocket.sendallcCs6|dkr|dkrt���|�|j�|_|j|j_|jS)N�r�rb)rZUnimplementedFileModerr
�file�
file_close�close)r�mode�bufsizerrr�makefile?s

zFakeSocket.makefilecCs
d|_dS)NT)r�rrrrrGszFakeSocket.file_closecCsdS�Nrr#rrrrJszFakeSocket.closecCsdSr$r)r�levelZoptname�valuerrr�
setsockoptMszFakeSocket.setsockopt)N)�__name__�
__module__�__qualname__�io�BytesIOrrr"rrr'rrrrr/s
rc@s$eZdZdd�Zdd�Zdd�ZdS)�EPipeSocketcCst�||�||_dSr$)rr�pipe_trigger)rr
r.rrrrRszEPipeSocket.__init__cCs(|j|krttjd��|j|7_dS)NZgotcha)r.�OSError�errnoZEPIPErrrrrrWs
zEPipeSocket.sendallcCsdSr$rr#rrrr\szEPipeSocket.closeN)r(r)r*rrrrrrrr-Psr-c@s$eZdZdZddd�Zd	dd�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.
    ���cCs"tj�||�}|dkrtd��|S�Nr	zcaller tried to read past EOF)r+r,�read�AssertionError)r�nrrrrr4eszNoEOFBytesIO.readNcCs"tj�||�}|dkrtd��|Sr3)r+r,�readliner5)rZlengthrrrrr7kszNoEOFBytesIO.readline)r2)N)r(r)r*�__doc__r4r7rrrrr1_s
r1cs4eZdZdZ�fdd�Z�fdd�Zdd�Z�ZS)�FakeSocketHTTPConnectionz@HTTPConnection subclass using FakeSocket; counts connect() callscs$d|_t��d�||_|j|_dS)Nr�example.com)�connections�superr�fake_socket_args�create_connection�_create_connection)r�args��	__class__rrrtsz!FakeSocketHTTPConnection.__init__cs|jd7_t���S)z.Count the number of times connect() is invokedr)r;r<�connectr#rArrrCzsz FakeSocketHTTPConnection.connectcOs
t|j�Sr$)rr=�r�pos�kwrrrr>sz*FakeSocketHTTPConnection.create_connection)r(r)r*r8rrCr>�
__classcell__rrrArr9qsr9c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�HeaderTestscCs�Gdd�dt�}dD]n}dD]d}t�d�}td�|_|�|_d}i}|rXtt|��||<|�dd	||�|�	|jj
|��d
�qqdS)Nc@seZdZdd�Zdd�ZdS)z;HeaderTests.test_auto_headers.<locals>.HeaderCountingBuffercSs
i|_dSr$)�countr#rrrr�szDHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.__init__cSsX|�d�}t|�dkrH|d�d���}|j�|d�|j|d7<t�||�dS)N�:rrr)�split�len�decode�lowerrI�
setdefault�list�append)r�item�kvZlcKeyrrrrQ�s
zBHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.appendN�r(r)r*rrQrrrr�HeaderCountingBuffer�srU)TF)�Content-lengthZHostzAccept-encodingr:ZblahblahblahZspamspamspam�POST�/r)rPr�HTTPConnectionr�sock�_bufferrrL�request�assertEqualrIrN)rrUZexplicit_header�header�conn�body�headersrrr�test_auto_headers�s

zHeaderTests.test_auto_headerscCsfGdd�dt�}d}d}t�||�D]J\}}t�d�}td�|_|�|_|�|d|�|�	|jj
dd�|��q$d	}|D]F}t�d�}td�|_|�|_|�|dd�|�	|jj
dd
�|��qx|D]F}t�d�}td�|_|�|_|�|dd�|�	|jj
dd�|��q�t�||�D]H}t�d�}td�|_|�|_|�|dd�|�	|jj
d
d�|���qdS)Nc@seZdZdd�Zdd�ZdS)z?HeaderTests.test_content_length_0.<locals>.ContentLengthCheckercSst�|�d|_dSr$)rPr�content_lengthr#rrrr�s
zHHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.__init__cSsF|�dd�}t|�dkr6|d��dkr6|d��|_t�||�dS)NrJrrscontent-length)rKrLrN�striprcrPrQ)rrRrSrrrrQ�szFHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.appendNrTrrrr�ContentLengthChecker�sre)N�)�PUTrWZPATCHr:rX�0z%Header Content-Length incorrect on {})�GET�CONNECTZDELETE�HEADZOPTIONSZTRACEz.Header Content-Length set for empty body on {}rf� �1)
rP�	itertools�productrrYrrZr[r\r]rc�format�chain)rreZbodiesZmethods_with_body�methodr`r_Zmethods_without_bodyrrr�test_content_length_0�sX

�

�

�

�z!HeaderTests.test_content_length_0cCs�t�d�}td�|_|�dd�|�dd�|�d|j�|�dd�|�d	|j�|�d
d�|�d|j�|�d
d�|�d|j�|�ddd�|�d|j�|�dd�|�d|j�|�dd�|�d|j�|�dd�|�d|j�|�dd�|�d|j�|�d d!�|�d"|j�|�d#d$�|�d%|j�|�d&d$�|�d'|j�|�d(d$�|�d)|j�|�d*d$�|�d+|j�dS),Nr:rirXrV�*sContent-length: 42ZFooz bar s
Foo:  bar ZBarz	baz	s
Bar: 	baz	Z
AuthorizationzBearer mytokensAuthorization: Bearer mytokenZ
IterHeaderZIterAZIterBsIterHeader: IterA
	IterBZLatinHeader��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 Spacer&sKey Space: valuez	KeySpace sKeySpace : valuesNonbreak�SpacesNonbreak�Space: values�NonbreakSpaces�NonbreakSpace: value)rrYrrZ�
putrequest�	putheader�assertInr[�rr_rrr�test_putheader�s>

zHeaderTests.test_putheadercCsxd}t�d�}td�}||_|�dd�|�|j�|��d}t�d�}td�}||_|�dd�|�|j�|��dS)NsCGET /foo HTTP/1.1
Host: [2001::]:81
Accept-Encoding: identity

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

z
[2001:102A::])rrYrrZr\�
assertTruer�
startswith�r�expectedr_rZrrr�test_ipv6host_headers

z HeaderTests.test_ipv6host_headercCsFd}t|�}t�|�}|��|�|�d�d�|�|�d�d�dS)Nz4HTTP/1.1 200 OK
First: val
: nval
Second: val

ZFirst�valZSecond)rr�HTTPResponse�beginr]�	getheader�rr`rZ�resprrr�!test_malformed_headers_coped_withs
z-HeaderTests.test_malformed_headers_coped_withcCs,dttdd��dttdd��d}t|�}t�|�}|��|�|�d�d	�|�|jdd	�|�|�d
�d�|�|jd
d�d�	t
ttdd���}|�|�d
�|�|�|jd
|�|�|�d��|�
d|j�|�d�|jdfD]0}|�|�d��|�
d|�|�|�d��q�dS)Ns0HTTP/1.1 200 OK
!#$%&'*+-.^_`|~: value
VCHAR: �!�s
obs-text: ��sM
obs-fold: text
 folded with space
	folded with tab
Content-Length: 0

�Content-Lengthrz!#$%&'*+-.^_`|~r&rfZVCHARzobs-textzobs-foldr
z folded with spacezfolded with tab)�bytes�rangerrr�r�r]r��msg�join�map�chrZassertIsNotNonerxr|r}�endswith)rr`rZr�ZvcharZfoldedrrr�test_parse_all_octets!s4�����
z!HeaderTests.test_parse_all_octetscCstt�d�}td�|_|�dd�d}|D]F\}}|�||f��*|�td��|�||�W5QRXW5QRXq(dS)Nr:rfrirX))s
Invalid
Name�
ValidValue)sInvalid
Namer�)sInvalid
Namer�)s

InvalidNamer�)s
InvalidNamer�)s
InvalidNamer�)s InvalidNamer�)s	InvalidNamer�)sInvalid:Namer�)s:InvalidNamer�)�	ValidNamesInvalid
Value)r�s
Invalid
Value)r�s
Invalid
Value)r�sInvalidValue
)r�s
InvalidValue
)r�s
InvalidValue
zInvalid header)	rrYrrZrv�subTest�assertRaisesRegex�
ValueErrorrw)rr_�cases�namer&rrr�test_invalid_headers?s

z HeaderTests.test_invalid_headersc	Cs�d}t|�}tj|dd�}t���}|��W5QRX|����}|�|dd�|�|dd�|�|dd�|�|d	d
�dS)Ns9HTTP/1.1 200 OK
First: val
Second: val1
Second: val2
r)Z
debuglevelrzreply: 'HTTP/1.1 200 OK\r\n'zheader: First: val�zheader: Second: val1�zheader: Second: val2)	rrr�r�captured_stdoutr��getvalue�
splitlinesr])rr`rZr��output�linesrrr�test_headers_debuglevel]s�
z#HeaderTests.test_headers_debuglevelN)r(r)r*rbrsrzr�r�r�r�r�rrrrrH�sB"
rHc@seZdZdd�ZdS)�HttpMethodTestsc
CsLd}|D]>}|�td��(t�d�}td�|_|j|dd�W5QRXqdS)N)	zGET
zPOST
zPUT

z
POST
Valuez
POST
HOST:abczGET
rHost:abc
zPOST
Remainder:
z
GET
HOST:
z
PUTz'method can't contain control charactersr:rX)rr�url)r�r�rrYrrZr\)r�methodsrrr_rrr�test_invalid_method_namesps�

z)HttpMethodTests.test_invalid_method_namesN)r(r)r*r�rrrrr�osr�c@sJeZdZdZdd�Zdd�Zdd�Zdd	�Zddd�Zd
d�Z	dd�Z
dS)�TransferEncodingTestsIt's just a flesh woundcCsbt�d�}td�|_|�dd�|j|��dd�|�|jj�\}}}|�	|�}|�
||j�dS)Nr:r	rWrXT)�encode_chunked)rrYrrZrvZ
endheaders�
_make_body�_parse_requestr�_parse_chunkedr]�
expected_body)rr_�_r`rrr�test_endheaders_chunked�s


z,TransferEncodingTest.test_endheaders_chunkedcCsvt�d�}td�|_|�dd|��ddi�|�|jj�\}}}|�ddd	�|�	�D��|�
|dd�|�
||j�t�d�}td�|_|�dd|j�d
�ddi�|�|jj�\}}}|�ddd	�|�	�D��|�
|dd�|�
||j�t�d�}td�|_|jddddid
|��d�|�|jj�\}}}|�ddd	�|D��|�
|dd�|�
|�
|�|j�dS)Nr:r	rWrX�Transfer-Encoding�chunked�content-lengthcSsg|]}|���qSr�rN��.0�krrr�
<listcomp>�sz>TransferEncodingTest.test_explicit_headers.<locals>.<listcomp>�latin-1cSsg|]}|���qSrr�r�rrrr��sz
gzip, chunkedT)rar�r`cSsg|]}|���qSrr�r�rrrr��s)rrYrrZr\r�r�r�assertNotIn�keysr]r�rMr��rr_r�rar`rrr�test_explicit_headers�sF

�


�

�z*TransferEncodingTest.test_explicit_headerscCs�dD]�}t�d�}td�|_|�dd|j|d��|�|jj�\}}}|�|�}|�	||j
�|�	|dd�|�d	d
d�|D��qdS)N)FTr:r	rWrX)�empty_linesr�r�r�cSsg|]}|���qSrr�r�rrrr��sz5TransferEncodingTest.test_request.<locals>.<listcomp>)rrYrrZr\r�r�rr�r]r�r�)rr�r_r�rar`rrr�test_request�s


�
z!TransferEncodingTest.test_requestcCslt�d�}td�|_|�ddd�|�|jj�\}}}|�|dd�|�dd	d
�|D��|�|d�dS)Nr:r	rWrXrr�r�r�cSsg|]}|���qSrr�r�rrrr��sz8TransferEncodingTest.test_empty_body.<locals>.<listcomp>s0

)	rrYrrZr\r�rr]r�r�rrr�test_empty_body�s

z$TransferEncodingTest.test_empty_bodyFccsV|j�d�}t|�D]<\}}|r.|dr.dV|t|�dkrJ|dVq|VqdS)N� r�r	r)r�rK�	enumeraterL)rr�r��idx�linerrrr��szTransferEncodingTest._make_bodycCs�|�d�}|d}i}d}|t|�krrt||�dkrr||�d�\}}|�d���}|�d���||<|d7}q||d�||dd��fS)N�
rrrJr�)rKrLrMrdr�)rrr�r\rar6�keyr�rrrr��s

z#TransferEncodingTest._parse_requestcCs�g}i}d}|�d�}|||d�\}}t|d�}|dkrF|d7}qx|�|t|��|�|�|d7}|t|�krqxqd�|�S)Nrr�r��rr	)rK�intr]rLrQr�)rrr`�trailersr6r��size�chunkrrrr��s


z#TransferEncodingTest._parse_chunkedN)F)r(r)r*r�r�r�r�r�r�r�r�rrrrr��s
&


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?d@�Z"dAdB�Z#dCdD�Z$dEdF�Z%dGdH�Z&dIdJ�Z'dKdL�Z(dMdN�Z)dOdP�Z*dQdR�Z+dSdT�Z,dUdV�Z-dWdX�Z.dYdZ�Z/d[d\�Z0d]d^�Z1d_d`�Z2daS)b�	BasicTestcCs&|�ddddhtttd���k�dS)NZdescriptionr��phraser&�)r|�set�dirrr#rrr�&test_dir_with_added_behavior_on_statussz0BasicTest.test_dir_with_added_behavior_on_statuscCs�d}t|�}t�|�}|��|�|�d�d�|�|���|�|j�|�|��d�|�	|���|�|j�|�
�|�	|j�d}t|�}t�|�}|�tj|j�dS)N�HTTP/1.1 200 Ok

Textrr	sTextzHTTP/1.1 400.100 Not Ok

Text)
rrr�r�r]r4�assertFalse�isclosed�closedr|r�assertRaises�
BadStatusLiner�rrr�test_status_lines
s 

zBasicTest.test_status_linescCst�d�}|�t|�d�dS)NrfzBadStatusLine("''"))rr�r]�repr)r�excrrr�test_bad_status_reprs
zBasicTest.test_bad_status_reprcCs�d}t|�}t�|�}|��|�|�d�d�|�|���|�|�d�d�|�|���|�|j	�|�
�|�|j	�dS�Nz*HTTP/1.1 200 Ok
Content-Length: 4

Textr��Te�xt�rrr�r�r]r4r�r�r|r�rr�rrr�test_partial_reads#s
zBasicTest.test_partial_readscCs~d}t|�}t�|�}|��|�|��d�|�|���|�|��d�|�	|���|�|j
�|��|�	|j
�dS)Nz4HTTP/1.1 200 Ok
Content-Length: 13

Text
AnothersText
sAnother)rrr�r�r]r7r�r�r4r|r�rr�rrr�test_mixed_reads2s
zBasicTest.test_mixed_readscCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�
|�	��|�|j�|��|�
|j�dSr��
rrr�r��	bytearray�readintor]r�r�r�r|r�r�rr`rZr��br6rrr�test_partial_readintosAs 


z BasicTest.test_partial_readintoscCs�d}t|�}t�|�}|��|�|�d�d�|�|���|�|�d�d�|�|�d�d�|�|���|�|j	�|�
�|�|j	�dS)Nr�r�r�r�rr	r�r�rrr�$test_partial_reads_no_content_lengthUs
z.BasicTest.test_partial_reads_no_content_lengthcCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�|�}|�|d�|�
|�	��dS)Nr�r�r�r�r)rrr�r�r�r�r]r�r�r�r|r�rrr�(test_partial_readintos_no_content_lengthes



z2BasicTest.test_partial_readintos_no_content_lengthcCstd}t|�}t�|�}|��|�|�d�d�|�|���|�|�d�d�|�|�d�d�|�|���dS)N�+HTTP/1.1 200 Ok
Content-Length: 10

Textr�r�r�rr	)	rrr�r�r]r4r�r�r|r�rrr�"test_partial_reads_incomplete_bodyxs
z,BasicTest.test_partial_reads_incomplete_bodycCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�|�}|�|d�|�
|�	��|�|j�|��|�
|j�dS)Nr�r�r�r�rr�r�rrr�&test_partial_readintos_incomplete_body�s$



z0BasicTest.test_partial_readintos_incomplete_bodycCsVdD]}|�tjtj|�qdD]0\}}}t�|�}|�||j�|�||j�q dS)N�zwww.python.org:abczuser:password@www.python.org)�z[fe80::207:e9ff:fe9b]:8000�fe80::207:e9ff:fe9bi@)zwww.python.org:80�www.python.org�P)�www.python.org:r�r�)r�r�r�)�[fe80::207:e9ff:fe9b]r�r�)�[fe80::207:e9ff:fe9b]:r�r�)r�r�
InvalidURLrYr]rr�rZhp�h�p�crrr�test_host_port�s
zBasicTest.test_host_portcCs<d}d}t|�}t�|�}|��|�d�}|�||�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)rrr�r�r�r])rr
Zhdr�srZcookiesrrr�test_response_headers�s

zBasicTest.test_response_headerscCs6tdt�}tj|dd�}|��|��r2|�d�dS)N�*HTTP/1.1 200 OK
Content-Length: 14432

rk�rr�)Did not expect response from HEAD request)rr1rr�r�r4�fail�rrZr�rrr�test_read_head�s�zBasicTest.test_read_headcCsTtdt�}tj|dd�}|��td�}|�|�dkr@|�d�|�t	|�d�dS)Nr�rkr��rr��)
rr1rr�r�r�r�rr]r�)rrZr�r�rrr�test_readinto_head�s�
zBasicTest.test_readinto_headcCsRd�dd�ttjd�D��d}d|}t|�}t�|�}|�tjd|j�dS)Nrcss|]}d|VqdS)z
Header%d: fooNr)r��irrr�	<genexpr>�s�z2BasicTest.test_too_many_headers.<locals>.<genexpr>rzHTTP/1.1 200 OK
zgot more than \d+ headers)	r�r�rZ_MAXHEADERSrr�r��
HTTPExceptionr�)rrar
r�rrrr�test_too_many_headers�s
��
�zBasicTest.test_too_many_headersc	Csnd}ttd��V}t�d�}t|�}||_|�dd|�|�|j�	|�d|jdt
|��|f�W5QRXdS)Ns_GET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Transfer-Encoding: chunked

rr:rir{z%r != %r)�open�__file__rrYrrZr\r|rr}rL�rrr`r_rZrrr�test_send_file�s
�zBasicTest.test_send_filecCs�d}t�d�}td�}||_|�|�|�||j�d|_|�t�d|��|�||j�d|_|�t�	|��|�||j�dS)Ns"this is a test this is only a testr:r	r�)
rrYrrZ�sendr]r�arrayr+r,r~rrr�	test_send�s

zBasicTest.test_sendcsXdd��G�fdd�dtj�}d}t�d�}td�}||_|�|��|�|j|�dS)NcssdVdVdVdS)NrZdata_tworrrrrrsz/BasicTest.test_send_updating_file.<locals>.datacs eZdZdZ��Zddd�ZdS)z7BasicTest.test_send_updating_file.<locals>.UpdatingFilerr2cSs
t|j�Sr$)�next�d)r�	blocksizerrrr4sz<BasicTest.test_send_updating_file.<locals>.UpdatingFile.readN)r2)r(r)r*r rr4r�rrr�UpdatingFilesrsdatar:rf)	r+�
TextIOBaserrYrrZrr]r)rrrr_rZrrr�test_send_updating_file�s
z!BasicTest.test_send_updating_filecCsLd}dd�}t�d�}td�}||_|�dd|�dd	i�|�|j|�dS)
NsbGET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Content-Length: 11

onetwothreecssdVdVdVdS)Nsonestwosthreerrrrrr`sz&BasicTest.test_send_iter.<locals>.bodyr:rfrir{r�Z11)rrYrrZr\r]rrrrr�test_send_iters
zBasicTest.test_send_itercCsvd}tjd|d�}td�}||_d|d}|�ddt�|�d	d
i�|�|jd�|j	�
dd
�d
}|�||�dS)z8Check that request() respects the configured block size.�r:�rN�a�brgrXr��9r�s

r)rrYrrZr\r+r,r]rrrK)rrr_rZrr`rrr�test_blocksize_request$sz BasicTest.test_blocksize_requestcCs\d}tjd|d�}td�}||_d|d}|�t�|��|�|jd�|�|j	|�dS)z5Check that send() respects the configured block size.rr:rNrrr�)
rrYrrZrr+r,r]rr)rrr_rZrrrr�test_blocksize_send0szBasicTest.test_blocksize_sendc	Cs<t�d�}td�|_|�t��|�dd|�W5QRXdS)Nr:rfrW�test)rrYrrZr��	TypeErrorr\ryrrr�test_send_type_error;s

zBasicTest.test_send_type_errorcCs\t}tttt�}tj|dd�}|��|�|�	�|�|�
�tdd�D]V}tttt�}tj|dd�}|��|�|�	|�|�	|�|�	�|�|�
�qLdD]�}tt|�}tj|dd�}|��z|z|�	�Wn`tjk
�r<}z>|�|j
|�dt|�}|�t|�|�|�t|�|�W5d}~XYnX|�d�W5|�
�Xq�dS)Nrir�r��rfzfoo
�IncompleteRead(%d bytes read)�IncompleteRead expected)�chunked_expectedr�
chunked_start�
last_chunk�chunked_endrr�r�r]r4rr��IncompleteRead�partialrLr�rr)rrrZr�r6�xr�expected_messagerrr�test_chunkedBs2$
"zBasicTest.test_chunkedcCs�t}t|�}td�}tttt�}tj|dd�}|�	�|�
|�}|�|d|�|�|�||�|��t
dd�D]�}tttt�}tj|dd�}|�	�t|�}|�
|d|��}||�
||||��7}||�
||d��7}|�|d|�|�|�||�|��qvdD]�}	tt|	�}tj|dd�}|�	�z~z|�
|�}Wn`tjk
�r�}z>|�|j|�dt|�}
|�t|�|
�|�t|�|
�W5d}~XYnX|�d	�W5|��X�qdS)
Nr�rir�rr#rr$r%r&)r'rLr�rr(r)r*rr�r�r�r]rr��
memoryviewr+r,r�rr)rrZ	nexpectedr�rZr�r6�mrr-r.rrr�test_readinto_chunkedbsD

"zBasicTest.test_readinto_chunkedcCs�d}t|tt�}tj|dd�}|��|�|��d�|�|jd�|�|j	d�|�
|���|�|j
�|��|�
|j
�dS)N�EHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello world
1
d
rkr�r	���OK)rr)r*rr�r�r]r4�status�reasonr|r�r�r�r)rr(rZr�rrr�test_chunked_head�s�zBasicTest.test_chunked_headcCs�d}t|tt�}tj|dd�}|��td�}|�|�}|�|d�|�t	|�d�|�|j
d�|�|jd�|�|�
��|�|j�|��|�|j�dS)	Nr3rkr�rrrr4r5)rr)r*rr�r�r�r�r]r�r6r7r|r�r�r�r)rr(rZr�r�r6rrr�test_readinto_chunked_head�s�
z$BasicTest.test_readinto_chunked_headcCs@td�}tj|dd�}|��|�|��d�|�|���dS)Nz.HTTP/1.1 200 OK
Content-Length: -1

Hello
rir��Hello
)rrr�r�r]r4r|r�rrrr�test_negative_content_length�s�z&BasicTest.test_negative_content_lengthc
Cs�td�}tj|dd�}|��z|��Wn`tjk
r�}z@|�|jd�|�t|�d�|�t	|�d�|�
|���W5d}~XYnX|�d�dS)Nz.HTTP/1.1 200 OK
Content-Length: 10

Hello
rir�r:z-IncompleteRead(7 bytes read, 3 more expected)r&)
rrr�r�r4r+r]r,r�rr|r�r)rrZr�rrrr�test_incomplete_read�s
�
� zBasicTest.test_incomplete_readcsZtdd�}t�d��|�_|�t�fdd�����}|�d|j�|�d|�	d��dS)	NzgHTTP/1.0 401 Authorization Required
Content-type: text/html
WWW-Authenticate: Basic realm="example"
sContent-Lengthr:cs��ddd�S)Nrg�/urlr`)r\r�r_rr�<lambda>�r	z&BasicTest.test_epipe.<locals>.<lambda>i�zBasic realm="example"zwww-authenticate)
r-rrYrZr�r/�getresponser]r6r�rrr>r�
test_epipe�s�

��zBasicTest.test_epipecCs8dddd}t�t|��}|�tjtjf|j�dS)N�HTTP/1.1 200 Okr��r)rr�rr��LineTooLongr�r��rr`r�rrr�test_overflowing_status_line�sz&BasicTest.test_overflowing_status_linecCs2dddd}t�t|��}|�tj|j�dS)NzHTTP/1.1 200 OK
X-Foo: barrrCz

)rr�rr�rDr�rErrr�test_overflowing_header_line�s���z&BasicTest.test_overflowing_header_linec	Cs^dd}t�t|��}|�tj��}|��W5QRX|�dt|j��|�dt|j��dS)NzHTTP/1.1 100 OK
r
i�zgot more than ra)	rr�rr�rr�rxr�	exception)rr`r��cmrrr�'test_overflowing_header_limit_after_100�s��z1BasicTest.test_overflowing_header_limit_after_100cCs:dddd}t�t|��}|��|�tj|j�dS)Nz/HTTP/1.1 200 OK
Transfer-Encoding: chunked

rrCza
hello world
0

)rr�rr�r�rDr4rErrr�test_overflowing_chunked_line�s���z'BasicTest.test_overflowing_chunked_linecCs`d}t|�}t�|�}|��|�|��d�|�|���|�|j	�|�
�|�|j	�dS)NrBr	)rrr�r�r]r4r|r�r�r�rr�rrr�test_early_eofs
zBasicTest.test_early_eofcsnt�d�}d�G�fdd�dtj�}||_td�|_|�dd�|�tj|j	�|�
�j�|�
|jj�dS)Nr:cseZdZ�fdd�ZdS)z+BasicTest.test_error_leak.<locals>.Responsecs|�tjj|f|�|�dSr$)rr�rrD��responserrrsz4BasicTest.test_error_leak.<locals>.Response.__init__N)r(r)r*rrrMrr�ResponsesrOzInvalid status linerirX)
rrYr�Zresponse_classrrZr\r�r�r@r|r�r)rr_rOrrMr�test_error_leaks

zBasicTest.test_error_leakcCsRd}td}tt|tt�}tj|dd�}|��|�|�	�|�|�
�dS)Nz3;foo=bar
abc
sabcrir�)r'rr(�last_chunk_extendedr*rr�r�r]r4r)rZextrarrZr�rrr�test_chunked_extension$sz BasicTest.test_chunked_extensioncCsBt}ttt�}tj|dd�}|��|�|��|�|�	�dS)z9some servers may serve up a short chunked encoding streamrir�N)
r'rr(r)rr�r�r]r4r�rrrZr�rrr�test_chunked_missing_end.sz"BasicTest.test_chunked_missing_endcCs\t}ttttt�}tj|dd�}|��|�	|�
�|�|�	|j�
�d�|��dS)z&See that trailers are read and ignoredrir�r	N)
r'rr(r)r�r*rr�r�r]r4rrrSrrr�test_chunked_trailers7szBasicTest.test_chunked_trailerscCsjt}d}ttttt|�}tj|dd�}|��|�	|�
�|�|�	|j�
�|�d��|�
�dS)zDCheck that we don't read past the end of the chunked-encoding stream�	extradatarir�rN)r'rr(r)r�r*rr�r�r]r4rrr)rrrVrZr�rrr�test_chunked_syncBszBasicTest.test_chunked_synccCs\d}d}td||�}tj|dd�}|��|�|��|�|�|j��|�|��dS)zBCheck that we don't read past the end of the Content-Length stream�	extradata�
Hello123
�'HTTP/1.1 200 OK
Content-Length: 10

rir�N)rrr�r�r]r4rr�rrVrrZr�rrr�test_content_length_syncNsz"BasicTest.test_content_length_synccCs`d}d}td||�}tj|dd�}|��|�|�d�|g�|�|j��|�|��dS�NrXrYrZrir�i�)	rrr�r�r]�	readlinesrr4rr[rrr�test_readlines_content_lengthZsz'BasicTest.test_readlines_content_lengthcCs^d}d}td||�}tj|dd�}|��|�|�d�|�|�|j��|�|��dSr])	rrr�r�r]�read1rr4rr[rrr�test_read1_content_lengthesz#BasicTest.test_read1_content_lengthcCspd}d}td||�}tj|dd�}|��|�|�d�|�|�|�d�d�|�|j��|�|��dS)NrXrYrZrir��
r	)	rrr�r�r]r7rr4rr[rrr�"test_readline_bound_content_lengthpsz,BasicTest.test_readline_bound_content_lengthcCsvd}d}td|d|�}tj|dd�}|��|�|�d�|d�|�|��|�|�|j��|�|��dS)	NrXrYs'HTTP/1.1 200 OK
Content-Length: 30

r�rir��r�)	rrr�r�r]r`r4rrr[rrr�test_read1_bound_content_length|sz)BasicTest.test_read1_bound_content_lengthcs�t�tdf��|��j�d���fdd�}tj|d�}|��|�|jt	d��t
j����}|�
dd�|��}z>|�|jt
j�tj|��d�}z|�d	�W5|��XW5|��|��X|��|��d	�dS)
Nrc
s^���\}}|�D|�d��.}|��}|�d�sq4q|�d�|���W5QRXW5QRXdS)Nrr�s'HTTP/1.1 200 Connection established

)Zacceptr"r7�rstriprr4)r_�address�readerr���result�servrr�
run_server�s

z2BasicTest.test_response_fileno.<locals>.run_server)�targetrrjz
dummy:1234)�filenos
proxied data
)�socketZ
create_server�HOST�
addCleanupr�	threadingZThread�startr��floatrrY�getsocknamer\r@r]r6r5rn�detachr)rrl�threadr_rNr�rrir�test_response_fileno�s(
zBasicTest.test_response_filenocCs4Gdd�dtj�}|d�}td�|_|�dd�dS)zv
        It should be possible to override the default validation
        behavior in putrequest (bpo-38216).
        c@seZdZdd�ZdS)zRBasicTest.test_putrequest_override_domain_validation.<locals>.UnsafeHTTPConnectioncSsdSr$r�rr�rrr�_validate_path�szaBasicTest.test_putrequest_override_domain_validation.<locals>.UnsafeHTTPConnection._validate_pathN)r(r)r*rzrrrr�UnsafeHTTPConnection�sr{r:rfriz/N�rrYrrZrv�rr{r_rrr�*test_putrequest_override_domain_validation�s
z4BasicTest.test_putrequest_override_domain_validationcCs8Gdd�dtj�}|d�}td�|_|jdddd�dS)	Nc@seZdZdd�ZdS)zPBasicTest.test_putrequest_override_host_validation.<locals>.UnsafeHTTPConnectioncSsdSr$rryrrr�_validate_host�sz_BasicTest.test_putrequest_override_host_validation.<locals>.UnsafeHTTPConnection._validate_hostN)r(r)r*rrrrrr{�sr{z
example.com
rfrirXr)Z	skip_hostr|r}rrr�(test_putrequest_override_host_validation�s
z2BasicTest.test_putrequest_override_host_validationcCs4Gdd�dtj�}|d�}td�|_|�dd�dS)z�
        It should be possible to override the default encoding
        to transmit bytes in another encoding even if invalid
        (bpo-36274).
        c@seZdZdd�ZdS)zIBasicTest.test_putrequest_override_encoding.<locals>.UnsafeHTTPConnectioncSs
|�d�S)Nzutf-8)r)rZstr_urlrrr�_encode_request�szYBasicTest.test_putrequest_override_encoding.<locals>.UnsafeHTTPConnection._encode_requestN)r(r)r*r�rrrrr{�sr{r:rfriu/☃Nr|r}rrr�!test_putrequest_override_encoding�s
z+BasicTest.test_putrequest_override_encodingN)3r(r)r*r�r�r�r�r�r�r�r�r�r�r�r�rrr	r
rrrrrr"r/r2r8r9r;r<rArFrGrJrKrLrPrRrTrUrWr\r_rarcrerxr~r�r�rrrrr�s`

	 +


	%
r�c@sxeZdZdZdZee�d�d��d�ZdZdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�ExtendedReadTestz*
    Test peek(), read1(), readline()
    zOHTTP/1.1 200 OK

hello world!
and now 
for something completely different
fooZhelloNr��HTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello worl
3
d!

9
and now 

23
for something completely different

3
foo
0

cCs8t|j�}tj|dd�}|��t�|j�|_||_dS)Nrir�)	rr�rr�r�r+�BufferedReader�fpr�rrrr�setUp�s

zExtendedReadTest.setUpcs�|j}|jj�d�fdd�	}||j_g}|�d�}|r�|�t|�d�|��}|�t|�t|��|�|�|��|�t|��}|�	||�n|��}|�
|�|�|�|s(q�q(|�	d�|�|j
�dS)Nr2cs(�|�}|dkr|d|�S|dd�S)Nrrbr)r6r��Zoldpeekrr�mypeek�sz*ExtendedReadTest.test_peek.<locals>.mypeekr�rr	)r2)r�r��peekZ
assertGreaterrLZassertGreaterEqualr|r}r4r]r�rQr��lines_expected)rr�r��allr�Zp2rrr�r�	test_peek�s&


zExtendedReadTest.test_peekcCs|j}|�|jj|j�dSr$)r��_verify_readliner7r�)rr�rrr�
test_readlineszExtendedReadTest.test_readlinecCs\g}|d�}|r4|dkr4t|�dkr4|�|�d��|�|�|sqFq|�d�|�|�dS)Nr�foo�
r	)rLr|r�rQr]r�)rr7rr�r�rrrr�s
z!ExtendedReadTest._verify_readlinecs0�j���fdd�}t|�}��|j�j�dS)Ncs��d�}��t|�d�|S)N�)r`�assertLessEqualrL)�res�r�rrrr-s
z&ExtendedReadTest.test_read1.<locals>.r)r��	Readlinerr�r7r�)rrZ	readlinerrr�r�
test_read1+szExtendedReadTest.test_read1cCs<|j}g}|��}|sq$|�|�q
|�d�|�|j�dS�Nr	)r�r`rQr]r�r��rr�r�rrrr�test_read1_unbounded4sz%ExtendedReadTest.test_read1_unboundedcCsN|j}g}|�d�}|sq6|�t|�d�|�|�q
|�d�|�|j�dS)Nrbr	)r�r`r�rLrQr]r�r�r�rrr�test_read1_bounded>s
z#ExtendedReadTest.test_read1_boundedcCs|�|j�d�d�dS)Nrr	)r]r�r`r#rrr�test_read1_0IszExtendedReadTest.test_read1_0cCs |j�d�}|�dt|��dS�Nr)r�r�r�rL)rr�rrr�test_peek_0LszExtendedReadTest.test_peek_0)r(r)r*r8r��findrr�Z
lines_chunkedr�r�r�r�r�r�r�r�r�rrrrr��s��	
	
r�c@seZdZdZdZdS)�ExtendedReadTestChunkedz:
    Test peek(), read1(), readline() in chunked mode
    r�N)r(r)r*r8r�rrrrr�Qs�r�c@s eZdZdZdd�Zdd�ZdS)r�zT
    a simple readline class that uses an arbitrary read function and buffering
    cCs||_d|_dSr�)�readfunc�	remainder)rr�rrrrkszReadliner.__init__cCs�g}d}|j}z�|�d�}|dkr$q^|t|�|kr@||d}|�|�|��}|sd}q^q|d7}|�|d|��||d�|_d�|�WSd�|�|_�YnXdS)Nrr�r2rr	)r�r�rLrQr�r�)r�limitrZdatalenr4r�rrrr7os*

zReadliner.readlineN)r(r)r*r8rr7rrrrr�gsr�c@s$eZdZdd�Zdd�Zdd�ZdS)�OfflineTestcCsfdh}ddh}tt�D]<}|�d�s||kr.qtt|�}t|dd�dkr|�|�q|�tj|�dS)N�	responsesZHTTPMessage�
parse_headersr�r)zhttp.client)r�rr}�getattr�addZassertCountEqual�__all__)rrZ	blacklistr�Z
module_objectrrr�test_all�s
zOfflineTest.test_allcCs|�tjtjd�dS)Nz	Not Found)r]rr��	NOT_FOUNDr#rrr�test_responses�szOfflineTest.test_responsesc7Cs�ddddddddd	d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7g7}|D],}|j|d8��|�tt|��W5QRXqvdS)9NZCONTINUEZSWITCHING_PROTOCOLSZ
PROCESSINGr5ZCREATEDZACCEPTEDZNON_AUTHORITATIVE_INFORMATIONZ
NO_CONTENTZ
RESET_CONTENTZPARTIAL_CONTENTZMULTI_STATUSZIM_USEDZMULTIPLE_CHOICESZMOVED_PERMANENTLYZFOUNDZ	SEE_OTHERZNOT_MODIFIEDZ	USE_PROXYZTEMPORARY_REDIRECTZBAD_REQUESTZUNAUTHORIZEDZPAYMENT_REQUIREDZ	FORBIDDENr�ZMETHOD_NOT_ALLOWEDZNOT_ACCEPTABLEZPROXY_AUTHENTICATION_REQUIREDZREQUEST_TIMEOUTZCONFLICTZGONEZLENGTH_REQUIREDZPRECONDITION_FAILEDZREQUEST_ENTITY_TOO_LARGEZREQUEST_URI_TOO_LONGZUNSUPPORTED_MEDIA_TYPEZREQUESTED_RANGE_NOT_SATISFIABLEZEXPECTATION_FAILEDZMISDIRECTED_REQUESTZUNPROCESSABLE_ENTITYZLOCKEDZFAILED_DEPENDENCYZUPGRADE_REQUIREDZPRECONDITION_REQUIREDZTOO_MANY_REQUESTSZREQUEST_HEADER_FIELDS_TOO_LARGEZUNAVAILABLE_FOR_LEGAL_REASONSZINTERNAL_SERVER_ERRORZNOT_IMPLEMENTEDZBAD_GATEWAYZSERVICE_UNAVAILABLEZGATEWAY_TIMEOUTZHTTP_VERSION_NOT_SUPPORTEDZINSUFFICIENT_STORAGEZNOT_EXTENDEDZNETWORK_AUTHENTICATION_REQUIRED)Zconstant)r�r|�hasattrr)rrZconstrrr�test_client_constants�sv�9z!OfflineTest.test_client_constantsN)r(r)r*r�r�r�rrrrr��sr�c@s@eZdZdd�Zdd�Zdd�Ze�ee	d�d�d	d
��Z
dS)�SourceAddressTestcCs>t�tjtj�|_t�|j�|_t��|_|j�	�d|_
dSr$)ro�AF_INET�SOCK_STREAMrkr�	bind_portrZfind_unused_port�source_port�listenr_r#rrrr��s


zSourceAddressTest.setUpcCs*|jr|j��d|_|j��d|_dSr$)r_rrkr#rrr�tearDown�s


zSourceAddressTest.tearDowncCsBtjt|jd|jfd�|_|j��|�|jj�	�d|j�dS)Nrf��source_addressr)
rrYrprr�r_rCr]rZrur#rrr�testHTTPConnectionSourceAddress�s

�
z1SourceAddressTest.testHTTPConnectionSourceAddress�HTTPSConnectionz'http.client.HTTPSConnection not definedcCstjt|jd|jfd�|_dS)Nrfr�)rr�rprr�r_r#rrr� testHTTPSConnectionSourceAddress�s
�z2SourceAddressTest.testHTTPSConnectionSourceAddressN)r(r)r*r�r�r��unittest�skipIfr�rr�rrrrr��s�r�c@s(eZdZdZdd�Zdd�Zdd�ZdS)�TimeoutTestNcCs.t�tjtj�|_t�|j�t_|j��dSr$)	ror�r�rkrr�r��PORTr�r#rrrr��szTimeoutTest.setUpcCs|j��d|_dSr$)rkrr#rrrr�s
zTimeoutTest.tearDownc	Cs�|�t���t�d�zt�ttj�}|�	�W5t�d�X|�
|j��d�|�
�|�t���t�d�ztjttjdd�}|�	�W5t�d�X|�
|j��d�|�
�tjttjdd�}|�	�|�
|j��d�|�
�dS�N�)�timeout)�assertIsNoneroZgetdefaulttimeoutZsetdefaulttimeoutrrYrpr�r�rCr]rZZ
gettimeoutr)rZhttpConnrrr�testTimeoutAttributes,


�z TimeoutTest.testTimeoutAttribute)r(r)r*r�r�r�r�rrrrr��sr�c@s$eZdZdd�Zdd�Zdd�ZdS)�PersistenceTestcCs�d}|D]�\}}}|j||d���d�||�}t|�}|�|j�|�dd�|���"}|�|jdk|�|��W5QRX|�|jdk|�|�|j	d�|�dd�|�|j	|r�dnd�W5QRXqdS)	N))�1.0rfF)r�zConnection: keep-alive
T)�1.1rfT)r�zConnection: close
F)r�zConnection: keep-ALIVE
T)r�zConnection: cloSE
F)�versionr^z4HTTP/{} 200 OK
{}Content-Length: 12

Dummy body
riz/open-connectionrz/subsequent-requestr�)
r�rpr9r�rZr\r@r]r4r;)r�testsr�r^Zreuser�r_rNrrr�test_reuse_reconnect*s$��
z$PersistenceTest.test_reuse_reconnectc
Cs�dd�}tjtjf|tff}|D]j\}}|j|d��Ptd|�}|�dd�|�||j	�|�
|j�|�dd�|�|j
d�W5QRXq dS)	Ncs&t�|���fdd�}|�_t���S)z3Return BufferedReader that raises ECONNRESET at EOFcs tj��|�}|dkrt��|Sr�)r+r,r��ConnectionResetError)�bufferr���streamrrr�MszNPersistenceTest.test_disconnected.<locals>.make_reset_reader.<locals>.readinto)r+r,r�r�)r
r�rr�r�make_reset_readerJs
z<PersistenceTest.test_disconnected.<locals>.make_reset_reader)rHr	riz
/eof-response�
/reconnectr�)r+r,r�RemoteDisconnectedr�r�r9r\r�r@r�rZr]r;)rr�r�Zstream_factoryrHr_rrr�test_disconnectedHs
�
z!PersistenceTest.test_disconnectedcCsVtd�}|jddddid�|�tj|j�|�|j�|�dd�|�|j	d�dS)	NsHTTP/1.1 100 Continue

rirXZExpectz100-continue)rar�r�)
r9r\r�rr�r@r�rZr]r;ryrrr�test_100_closecs�zPersistenceTest.test_100_closeN)r(r)r*r�r�r�rrrrr�(sr�c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zej	dd��Z
d
d�Zdd�Zdd�Z
dd�Zdd�Ze�eed�d�dd��Zdd�ZdS)�	HTTPSTestcCsttd�s|�d�dS)Nr�zssl support required)r�r�skipTestr#rrrr�rs
zHTTPSTest.setUpcCsddlm}|||d�S)Nr)�make_https_server)�certfile)Ztest.ssl_serversr�)rr�r�rrr�make_servervszHTTPSTest.make_servercCs$tjttjdd�}|�|jd�dSr�)rr�rpr�r�r]r�)rr�rrr�test_attributeszszHTTPSTest.test_attributesc
Cslddl}t�d�t�d��Ft�dd�}|�|j��}|�dd�W5QRX|�	|j
jd�W5QRXdS)Nr�network�self-signed.pythontest.net�rirX�CERTIFICATE_VERIFY_FAILED)�sslr�requires�transient_internetrr�r��SSLErrorr\r]rHr7)rr�r��exc_inforrr�test_networkeds
zHTTPSTest.test_networkedc	Cszddl}t�d�t�d��T|��}tjdd|d�}|�dd�|��}|�	�|�
d|�d	��|�	�W5QRXdS)
Nrr�r�r���contextrirX�nginx�server)r�rr�r�Z_create_unverified_contextrr�r\r@rrxr�)rr�r�r�r�rrr�test_networked_noverification�s
�z'HTTPSTest.test_networked_noverificationc	Csjt�d�t�d��Lt�dd�}|�dd�|��}|�d�}|��|��|�	d|�W5QRXdS)Nr�r�r�rirXzcontent-typez	text/html)
rr�r�rr�r\r@r�rrx)rr�r�Zcontent_typerrr�&test_networked_trusted_by_default_cert�s

z0HTTPSTest.test_networked_trusted_by_default_certc	Csddl}t�d�d}t�|���|�|j�}|�|j|j�|�|j	d�|�
t�z(tj
|d|d�}|�dd�|��}WnR|jk
r�}z2t|�}t�d	|�r�t�d
|�d|�d����W5d}~XYnX|�d
�}|��|��|�d|�W5QRXdS)Nrr�r�Tr�r�rirXz(?i)key.too.weakzGot z trying to connect to z). See https://bugs.python.org/issue36816.r�r�)r�rr�r��
SSLContext�PROTOCOL_TLS_CLIENTr]Zverify_modeZ
CERT_REQUIRED�check_hostname�load_verify_locations� CERT_selfsigned_pythontestdotnetrr�r\r@r�r�re�searchr�ZSkipTestr�rrx)	rr�Zselfsigned_pythontestdotnetr�r�r�Zssl_errZssl_err_strZ
server_stringrrr�test_networked_good_cert�s2

��
z"HTTPSTest.test_networked_good_certc
Cs�ddl}t�d�t�d��`|�|j�}|�t�tj	dd|d�}|�
|j��}|�dd�W5QRX|�
|jjd�W5QRXdS)	Nrr�r�r�r�rirXr�)r�rr�r�r�r�r��CERT_localhostrr�r�r�r\r]rHr7)rr�r�r�r�rrr�test_networked_bad_cert�s

z!HTTPSTest.test_networked_bad_certc	CsXddl}|�t�}t�d|j�}|�|j��}|�dd�W5QRX|�	|j
jd�dS)Nr�	localhostrirXr�)r�r�r�rr�rr�r�r\r]rHr7)rr�r�r�r�rrr�test_local_unknown_cert�s
z!HTTPSTest.test_local_unknown_certcCsxddl}|�t�}|�|j�}|�t�tjd|j|d�}|�	|j
�|�dd�|��}|�	|j
�|�
|jd�dS)Nrr�r�ri�/nonexistentr�)r�r�r�r�r�r�rr�rrqrr\r@r]r6�rr�r�r�r�r�rrr�test_local_good_hostname�s

z"HTTPSTest.test_local_good_hostnamec	Cs�ddl}|�t�}|�|j�}|�t�tjd|j|d�}|�	|j
��|�dd�W5QRXt�
dtf��tjd|j|dd�}W5QRX|�	|j
��|�dd�W5QRXd	|_t�
dtf��tjd|j|d	d�}W5QRX|�dd
�|��}|��|��|�|jd�d	|_tjd|j|d�}|�dd
�|��}|�|jd�|��|��t�
dtf��tjd|j|dd�}W5QRX|�	|j
��|�dd�W5QRXdS)Nrr�r�rirXrfT)r�r�Fr�r�)r�r��CERT_fakehostnamer�r�r�rr�rr�ZCertificateErrorr\rZcheck_warnings�DeprecationWarningr�r@rr]r6r�rrr�test_local_bad_hostname�sN


�
�
�z!HTTPSTest.test_local_bad_hostnamer�z)http.client.HTTPSConnection not availablecCsVdD]}|�tjtj|�qdD]0\}}}t�|�}|�||j�|�||j�q dS)Nr�)r�)zwww.python.org:443r�r�)r�r�r�)r�r�r�)r�r�r�)r�r�r�)r�rr�r�r]rrr�rrrr�s
zHTTPSTest.test_host_portc	Cs�ddl}|js|�d�t�dd�}|�|jj�|�|j	�}|�
|j�tjdd|d�}|�|j|�|�
|jj�t�
��&t�ddt�tjdd|td�}W5QRX|�|jj�dS)	NrzTLS 1.3 support requiredr�r�r��ignorez5key_file, cert_file and check_hostname are deprecated)r�Z	cert_file)r�ZHAS_TLSv1_3r�rr�r|Z_contextZpost_handshake_authr�r�r�ZassertIs�warnings�catch_warnings�filterwarningsr�r�)rr�r�r�rrr�test_tls13_pha%s$

�
�zHTTPSTest.test_tls13_phaN)r(r)r*r�r�r�r�r�rZsystem_must_validate_certr�r�r�r�r�r�r�r�r�rr�r�rrrrr�ps"

!	
*�
r�c@sXeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)�RequestBodyTestz3Test cases where a request includes a message body.cCs,t�d�|_td�|j_|_|j|j_dS)Nr:rf)rrYr_rrZr#rrrr�>szRequestBodyTest.setUpcCs(t�|jj�}|��t�|�}||fSr$)r+r,rZrr7rr��r�f�messagerrr�get_headers_and_fpCs
z"RequestBodyTest.get_headers_and_fpc
Cs�ddgdfdf}|D]�\}}|�|��zt�d�|_td�|j_|_|j�dd|�|��\}}|�d	|�|�d
|�|�	|�
d�d�|�	||���W5QRXqdS)
Nr��bar�3
foo
3
bar
0

))r�rrr:rfrgr=zContent-Typer�r�r�)r�rrYr_rrZr\rr�r]�getr4)rr�r`rr�rrrr�test_list_bodyIs
�zRequestBodyTest.test_list_bodycCsL|j�dddddi�|��\}}|�d|�d��|�dt|����dS)Nrgr=r`r�Z42r�r�)r_r\rr]rrLr4�rrrrrr�test_manual_content_length]s�z*RequestBodyTest.test_manual_content_lengthcCs`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)Nrgr=r`�
text/plain�4r�sbody�	r_r\rr]�get_content_typer��get_charsetrr4r	rrr�test_ascii_bodyfszRequestBodyTest.test_ascii_bodycCs`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)Nrgr=ubodyÁr�5r��body�r
r	rrr�test_latin1_bodynsz RequestBodyTest.test_latin1_bodycCs`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)Nrgr=rrrr�r
r	rrr�test_bytes_bodyvszRequestBodyTest.test_bytes_bodyc	Cs�|�tjtj�ttjd��}|�d�W5QRXttj��r}|j�dd|�|��\}}|�	d|�
��|�|���|�|�
d��|�	d|�
d��|�	d	|���W5QRXdS)
N�wr`rgr=rr�r�ztransfer-encodings4
body
0

)rqr�unlink�TESTFNr
�writer_r\rr]rr�rrr4rrrr�test_text_file_body~sz#RequestBodyTest.test_text_file_bodyc	Cs�|�tjtj�ttjd��}|�d�W5QRXttjd��n}|j�dd|�|��\}}|�	d|�
��|�|���|�	d|�
d��|�d	|�|�	d
|���W5QRXdS)N�wbrrrgr=rr�r�r�s5
body�
0

)rqrrrr
rr_r\rr]rr�rrr�r4rrrr�test_binary_file_body�sz%RequestBodyTest.test_binary_file_bodyN)
r(r)r*r8r�rrr
rrrrrrrrrr;s	rc@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�HTTPResponseTestcCs&d}t|�}t�|�|_|j��dS)NzXHTTP/1.1 200 Ok
My-Header: first-value
My-Header:                 second-value

Text)rrr�r�r�)rr`rZrrrr��szHTTPResponseTest.setUpcCs6|j�d�}|�|d�|j�dd�}|�|d�dS)Nz	My-Headerzfirst-value, second-valuezsome default�r�r�r]�rr^rrr�test_getting_header�sz$HTTPResponseTest.test_getting_headercCs|j�dd�}|�|d�dS)N�No-Such-Headerz
default-valuerrrrr�3test_getting_nonexistent_header_with_string_default�szDHTTPResponseTest.test_getting_nonexistent_header_with_string_defaultcCs<|j�dddg�}|�|d�|j�dd�}|�|d�dS)Nr �default�valueszdefault, values)r"r#rrrrr�5test_getting_nonexistent_header_with_iterable_default�szFHTTPResponseTest.test_getting_nonexistent_header_with_iterable_defaultcCs|j�d�}|�|d�dS)Nr rrrrr�/test_getting_nonexistent_header_without_default�sz@HTTPResponseTest.test_getting_nonexistent_header_without_defaultcCs |jjddd�}|�|d�dS)Nr rt)r"rrrrr�test_getting_header_defaultint�sz/HTTPResponseTest.test_getting_header_defaultintN)	r(r)r*r�rr!r$r%r&rrrrr�src@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�TunnelTestscCs*d}d|_t�|j�|_|�|�|j_dS)Nz:HTTP/1.0 200 OK

HTTP/1.1 200 OK
Content-Length: 42

z	proxy.com)rrrYr_r?)r�
response_textrrrr��s
�zTunnelTests.setUpcCs|j��dSr$)r_rr#rrrr��szTunnelTests.tearDowncsd�fdd�	}|S)Ncst�|d|dd�S)Nrr)rr)r)rgr�r��r(rrr>�sz9TunnelTests._create_connection.<locals>.create_connection)NNr)rr(r>rr)rr?�szTunnelTests._create_connectioncCs�d}d}ddi}|jj|||d�|j�ddd�|�|jjj|j�|�|jjjtj�|�|jj	|�|�|jj
|�|�|jj|�dS)	N�destination.comi�"z
User-Agentz!Mozilla/5.0 (compatible, MSIE 11))rrarkrXrf)r_�
set_tunnelr\r]rZrrr�	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 |j��|�t|jjd�dS)Nr*)r_rCr��RuntimeErrorr+r#rrr�&test_disallow_set_tunnel_after_connect�s
�z2TunnelTests.test_disallow_set_tunnel_after_connectcCs�|j�d�|j�ddd�|�|jjj|j�|�|jjjtj�|�	d|jjj
�|�d|jjj
�|�	d|jjj
�|�d|jjj
�dS)	Nr*rkrXrf�CONNECT destination.comsHost: destination.com:None�Host: destination.comsHost: proxy.com)r_r+r\r]rZrrrr,rxrr�r#rrr�test_connect_with_tunnel�sz$TunnelTests.test_connect_with_tunnelcCsl|j�d�|j�ddd�|�|jjj|j�|�|jjjtj�|�	d|jjj
�|�	d|jjj
�dS)Nr*rgrXrfr0r1)r_r+r\r]rZrrrr,rxrr#rrr�test_connect_put_request�sz$TunnelTests.test_connect_put_requestc	Cszd}d�|�}|j�d�|�|�|j_|j�d�t���}|j�ddd�W5QRX|���	�}|�
d�|�|�dS)	Nz
X-Dummy: 1zHTTP/1.0 200 OK
{}

rr*rgrXrfz
header: {})rpr_Zset_debuglevelr?r+rr�r\r�r�rx)rZexpected_headerr(r�r�rrr�test_tunnel_debuglog�s

z TunnelTests.test_tunnel_debuglogN)r(r)r*r�r�r?r-r/r2r3r4rrrrr'�s


r'�__main__r�)�	verbosity)7r0Zhttprrr+rn�osrr�rorrr�r�ZTestCaser r�path�dirnamer�herer�r�r�r�r(r'Zchunk_extensionr)rQr�r*rprr-r,r1rYr9rHr�r�r�r�r�r�r�r�r�r�r�rrr'r(�mainrrrr�<module>sl�!nT}"R.HL`#I