Your IP : 216.73.216.196


Current Path : /opt/alt/python310/lib64/python3.10/test/__pycache__/
Upload File :
Current File : //opt/alt/python310/lib64/python3.10/test/__pycache__/test_httplib.cpython-310.opt-1.pyc

o

0�i#E�@sPddlZddlmZmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlm
Z
ejZddlmZddlmZddlmZddlmZej�e�Zej�ed�Zej�ed	�Zej�ed
�ZdZdZd
ZdZ dedZ!dZ"dZ#ej$Z$Gdd�d�Z%Gdd�de%�Z&Gdd�dej'�Z(Gdd�dej)�Z*Gdd�de�Z+Gdd�de�Z,Gdd�de�Z-Gd d!�d!e�Z.Gd"d#�d#e�Z/Gd$d%�d%e/�Z0Gd&d'�d'�Z1Gd(d)�d)e�Z2Gd*d+�d+e�Z3Gd,d-�d-e�Z4Gd.d/�d/e�Z5Gd0d1�d1e�Z6Gd2d3�d3e�Z7Gd4d5�d5e�Z8Gd6d7�d7e�Z9e:d8k�r&ej;d9d:�dSdS);�N)�client�
HTTPStatus)�mock)�support)�	os_helper)�
socket_helper)�warnings_helperzkeycert.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)�selfrrrr�r�8/opt/alt/python310/lib64/python3.10/test/test_httplib.py�__init__5s


zFakeSocket.__init__cCs |jd7_|j|7_dS)N�)rr�rrrrr�sendall@szFakeSocket.sendallcCs6|dkr|dkrt���|�|j�|_|j|j_|jS)N�r�rb)rZUnimplementedFileModerr�file�
file_close�close)r�mode�bufsizerrr�makefileDs

zFakeSocket.makefilecCs
d|_dS)NT)r�rrrrr"L�
zFakeSocket.file_closecC�dS�Nrr'rrrr#O�zFakeSocket.closecCr)r*r)r�levelZoptname�valuerrr�
setsockoptRr+zFakeSocket.setsockoptr*)�__name__�
__module__�__qualname__�io�BytesIOrrr&r"r#r.rrrrr4s
rc@�$eZdZdd�Zdd�Zdd�ZdS)�EPipeSocketcCst�||�||_dSr*)rr�pipe_trigger)rrr6rrrrWs
zEPipeSocket.__init__cCs(|j|vrttjd��|j|7_dS)NZgotcha)r6�OSError�errnoZEPIPErrrrrr\s
zEPipeSocket.sendallcCr)r*rr'rrrr#ar+zEPipeSocket.closeN)r/r0r1rrr#rrrrr5Usr5c@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.
    ���cC�"tj�||�}|dkrtd��|S�Nr
zcaller tried to read past EOF)r2r3�read�AssertionError)r�nrrrrr=j�zNoEOFBytesIO.readNcCr;r<)r2r3�readliner>)rZlengthrrrrrApr@zNoEOFBytesIO.readline�r:r*)r/r0r1�__doc__r=rArrrrr9ds
r9cs4eZdZdZ�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__rrrysz!FakeSocketHTTPConnection.__init__cs|jd7_t���S)z.Count the number of times connect() is invokedr)rFrG�connectr'rLrrrNs
z FakeSocketHTTPConnection.connectcOs
t|j�Sr*)rrH�r�pos�kwrrrrI�r(z*FakeSocketHTTPConnection.create_connection)r/r0r1rCrrNrI�
__classcell__rrrLrrDvs
rDc@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�HeaderTestscCs�Gdd�dt�}dD]7}dD]2}t�d�}td�|_|�|_d}i}|r,tt|��||<|�dd	||�|�	|jj
|��d
�qq
dS)Nc@�eZdZdd�Zdd�ZdS)z;HeaderTests.test_auto_headers.<locals>.HeaderCountingBuffercSs
i|_dSr*)�countr'rrrr�r(zDHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.__init__cSsX|�d�}t|�dkr$|d�d���}|j�|d�|j|d7<t�||�dS)N�:rrr)�split�len�decode�lowerrU�
setdefault�list�append)r�item�kvZlcKeyrrrr]�s
zBHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.appendN�r/r0r1rr]rrrr�HeaderCountingBuffer�sra)TF)�Content-lengthZHostzAccept-encodingrEZblahblahblahZspamspamspam�POST�/r)r\r�HTTPConnectionr�sock�_bufferrrX�request�assertEqualrUrZ)rraZexplicit_header�header�conn�body�headersrrr�test_auto_headers�s

��zHeaderTests.test_auto_headerscCsdGdd�dt�}d}d}t�||�D]%\}}t�d�}td�|_|�|_|�|d|�|�	|jj
dd�|��qd	}|D]#}t�d�}td�|_|�|_|�|dd�|�	|jj
dd
�|��q<|D]#}t�d�}td�|_|�|_|�|dd�|�	|jj
dd�|��qbt�||�D]#}t�d�}td�|_|�|_|�|dd�|�	|jj
d
d�|��q�dS)Nc@rT)z?HeaderTests.test_content_length_0.<locals>.ContentLengthCheckercSst�|�d|_dSr*)r\r�content_lengthr'rrrr��

zHHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.__init__cSsF|�dd�}t|�dkr|d��dkr|d��|_t�||�dS)NrVrrscontent-length)rWrXrZ�stripror\r])rr^r_rrrr]�szFHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.appendNr`rrrr�ContentLengthChecker�srr)N�)�PUTrcZPATCHrErd�0z%Header Content-Length incorrect on {})�GET�CONNECTZDELETE�HEADZOPTIONSZTRACEz.Header Content-Length set for empty body on {}rs� �1)
r\�	itertools�productrrerrfrgrhriro�format�chain)rrrZbodiesZmethods_with_body�methodrlrkZmethods_without_bodyrrr�test_content_length_0�sR

�

�

�

��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),NrErvrdrb�*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)rrerrf�
putrequest�	putheader�assertInrg�rrkrrr�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::]:81rsrv�/foosEGET /foo HTTP/1.1
Host: [2001:102A::]
Accept-Encoding: identity

z
[2001:102A::])rrerrfrh�
assertTruer�
startswith�r�expectedrkrfrrr�test_ipv6host_header	s

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�beginri�	getheader�rrlrf�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]}|�|�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-Lengthr	z!#$%&'*+-.^_`|~r-rsZVCHARzobs-textzobs-foldrz folded with spacezfolded with tab)�bytes�rangerrr�r�rir��msg�join�map�chrZassertIsNotNoner�r�r��endswith)rrlrfr�ZvcharZfoldedrrr�test_parse_all_octets&s6�����
�z!HeaderTests.test_parse_all_octetsc
Cs�t�d�}td�|_|�dd�d}|D]7\}}|�||f��$|�td��|�||�Wd�n1s7wYWd�n1sFwYqdS)NrErsrvrd))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)	rrerrfr��subTest�assertRaisesRegex�
ValueErrorr�)rrk�cases�namer-rrr�test_invalid_headersDs

�����z HeaderTests.test_invalid_headerscCs�d}t|�}tj|dd�}t���}|��Wd�n1s wY|����}|�|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�
splitlinesri)rrlrfr��output�linesrrr�test_headers_debuglevelbs�

�z#HeaderTests.test_headers_debuglevelN)r/r0r1rnr�r�r�r�r�r�r�rrrrrS�sB"
rSc@�eZdZdd�ZdS)�HttpMethodTestsc	Cs`d}|D])}|�td��t�d�}td�|_|j|dd�Wd�n1s(wYqdS)N)	zGET
zPOST
zPUT

z
POST
Valuez
POST
HOST:abczGET
rHost:abc
zPOST
Remainder:
z
GET
HOST:
z
PUTz'method can't contain control charactersrErd)r�url)r�r�rrerrfrh)r�methodsrrkrrr�test_invalid_method_namesus�

���z)HttpMethodTests.test_invalid_method_namesN)r/r0r1r�rrrrr�t�r�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)NrEr
rcrdT)�encode_chunked)rrerrfr�Z
endheaders�
_make_body�_parse_requestr�_parse_chunkedri�
expected_body)rrk�_rlrrr�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)NrEr
rcrd�Transfer-Encoding�chunked�content-lengthcS�g|]}|���qSr�rZ��.0�krrr�
<listcomp>��z>TransferEncodingTest.test_explicit_headers.<locals>.<listcomp>�latin-1cSr�rr�r�rrrr��r�z
gzip, chunkedT)rmr�rlcSr�rr�r�rrrr��r�)rrerrfrhr�r�r�assertNotIn�keysrir�rYr��rrkr�rmrlrrr�test_explicit_headers�s<

�

�

�z*TransferEncodingTest.test_explicit_headerscCs�dD]@}t�d�}td�|_|�dd|j|d��|�|jj�\}}}|�|�}|�	||j
�|�	|dd�|�d	d
d�|D��qdS)N)FTrEr
rcrd)�empty_linesr�r�r�cSr�rr�r�rrrr��r�z5TransferEncodingTest.test_request.<locals>.<listcomp>)rrerrfrhr�r�rr�rir�r�)rr�rkr�rmrlrrr�test_request�s

�
�z!TransferEncodingTest.test_requestcCslt�d�}td�|_|�ddd�|�|jj�\}}}|�|dd�|�dd	d
�|D��|�|d�dS)NrEr
rcrdrr�r�r�cSr�rr�r�rrrr��r�z8TransferEncodingTest.test_empty_body.<locals>.<listcomp>s0

)	rrerrfrhr�rrir�r�rrr�test_empty_body�s

z$TransferEncodingTest.test_empty_bodyFccsX�|j�d�}t|�D]\}}|r|drdV|t|�dkr&|dVq|VqdS)N� r�r
r)r�rW�	enumeraterX)rr�r��idx�linerrrr��s��zTransferEncodingTest._make_bodycCs�|�d�}|d}i}d}|t|�krFt||�dkrF||�d�\}}|�d���}|�d���||<|d7}|t|�krFt||�dks||d�||dd��fS)N�
rrrVr�)rWrXrYrqr�)rrr�rhrmr?�keyr�rrrr��s
�z#TransferEncodingTest._parse_requestcCs�g}i}d}|�d�}	|||d�\}}t|d�}|dkr$|d7}n|�|t|��|�|�|d7}|t|�kr<nqd�|�S)Nrr�Tr��rr
)rW�intrirXr]r�)rrrl�trailersr?r��size�chunkrrrr��s"


�
z#TransferEncodingTest._parse_chunkedN)F)r/r0r1r�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`�Z2dadb�Z3dcdd�Z4dedf�Z5dgdh�Z6diS)j�	BasicTestcCs"|�hd�tttd���k�dS)N>�descriptionr��phraser-�)r��set�dirrr'rrr�&test_dir_with_added_behavior_on_statuss"z0BasicTest.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
�TextzHTTP/1.1 400.100 Not Ok

Text)
rrr�r�rir=�assertFalse�isclosed�closedr�r#�assertRaises�
BadStatusLiner�rrr�test_status_liness 

zBasicTest.test_status_linescCst�d�}|�t|�d�dS)NrszBadStatusLine("''"))rr�ri�repr)r�excrrr�test_bad_status_repr$s
zBasicTest.test_bad_status_reprcCs�d}t|�}t�|�}|��|�|�d�d�|�|���|�|�d�d�|�|���|�|j	�|�
�|�|j	�dS�N�*HTTP/1.1 200 Ok
Content-Length: 4

Textr��Te�xt�rrr�r�rir=r�r�r�r�r#r�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�rirAr�r�r=r�r�r#r�rrr�test_mixed_reads7s
zBasicTest.test_mixed_readscCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�
|�	��|�|j�|��|�
|j�dSr��
rrr�r��	bytearray�readintorir�r�r�r�r�r#�rrlrfr��br?rrr�test_partial_readintosFs 


z BasicTest.test_partial_readintoscCsbd}t|�}t�|�}|��|�|�d�d�|�|���|�|j	�|�
�|�|j	�dS)Nr��
r��rrr�r�rir=r�r�r�r�r#r�rrr�test_partial_reads_past_endZs
z%BasicTest.test_partial_reads_past_endcCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�dd�d�|�|�	��|�
|j�|��|�|j�dS)Nr�r��r�)
rrr�r�r�r�rir�r�r�r�r�r#r�rrr�test_partial_readintos_past_endfs

z)BasicTest.test_partial_readintos_past_endcCs�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�rir�r�r�r�r�rrr�(test_partial_readintos_no_content_length�s



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�rir=r�r�r�r�rrr�"test_partial_reads_incomplete_body�s
z,BasicTest.test_partial_reads_incomplete_bodycCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�|�}|�|d�|�
|�	��|�|j�|��|�
|j�dS)Nrr�r�r�rr�r�rrr�&test_partial_readintos_incomplete_body�s$



z0BasicTest.test_partial_readintos_incomplete_bodycC�VdD]}|�tjtj|�qdD]\}}}t�|�}|�||j�|�||j�qdS)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�
InvalidURLrerirr�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�ri)rrZhdr�srZcookiesrrr�test_response_headers�s

zBasicTest.test_response_headerscCs:tdt�}tj|dd�}|��|��r|�d�dSdS)N�*HTTP/1.1 200 OK
Content-Length: 14432

rx�r�)Did not expect response from HEAD request)rr9rr�r�r=�fail�rrfr�rrr�test_read_head�s��zBasicTest.test_read_headcCsTtdt�}tj|dd�}|��td�}|�|�dkr |�d�|�t	|�d�dS)Nrrxr�rr�)
rr9rr�r�r�r�rrir�)rrfr�r�rrr�test_readinto_head�s�
zBasicTest.test_readinto_headcCsRd�dd�ttjd�D��d}d|}t|�}t�|�}|�tjd|j�dS)Nr
css�|]}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�)rrmrrrrrr�test_too_many_headers�s
��
�zBasicTest.test_too_many_headersc	Cs�d}ttd��1}t�d�}t|�}||_|�dd|�|�|j�	|�d|jdt
|��|f�Wd�dS1s;wYdS)Ns_GET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Transfer-Encoding: chunked

r rErvr�z%r != %r)�open�__file__rrerrfrhr�rr�rX�rr�rlrkrfrrr�test_send_files
�"�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 testrEr
r�)
rrerrf�sendrir�arrayr2r3r�rrr�	test_sends

zBasicTest.test_sendcsXdd��G�fdd�dtj�}d}t�d�}td�}||_|�|��|�|j|�dS)Ncss�dVdVdVdS)NrZdata_tworrrrrr ��
z/BasicTest.test_send_updating_file.<locals>.datacs eZdZdZ��Zddd�ZdS)z7BasicTest.test_send_updating_file.<locals>.UpdatingFilerr:cSs
t|j�Sr*)�next�d)r�	blocksizerrrr=(r(z<BasicTest.test_send_updating_file.<locals>.UpdatingFile.readNrB)r/r0r1r$r-r=r�rrr�UpdatingFile%sr0sdatarErs)	r2�
TextIOBaserrerrfr(rir)rr0r�rkrfrr/r�test_send_updating_files
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

onetwothreecss�dVdVdVdS)Nsonestwosthreerrrrrrl9r+z&BasicTest.test_send_iter.<locals>.bodyrErsrvr�r�Z11)rrerrfrhrirr&rrr�test_send_iter4s
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.�rE�r.N�a�brtrdr��9r��

r)rrerrfrhr2r3rirrrW)rr.rkrfr�rlrrr�test_blocksize_requestDsz BasicTest.test_blocksize_requestcCs\d}tjd|d�}td�}||_d|d}|�t�|��|�|jd�|�|j	|�dS)z5Check that send() respects the configured block size.r4rEr5Nr6r7r�)
rrerrfr(r2r3rirr)rr.rkrfr�rrr�test_blocksize_sendPszBasicTest.test_blocksize_sendcCsRt�d�}td�|_|�t��|�dd|�Wd�dS1s"wYdS)NrErsrc�test)rrerrfr��	TypeErrorrhr�rrr�test_send_type_error[s


"�zBasicTest.test_send_type_errorcCsbt}tttt�}tj|dd�}|��|�|�	�|�|�
�tdd�D]+}tttt�}tj|dd�}|��|�|�	|�|�	|�|�	�|�|�
�q&dD]Z}tt|�}tj|dd�}|��zBz|�	�Wn0tjy�}z#|�|j
|�dt|�}|�t|�|�|�t|�|�WYd}~n
d}~ww|�d�W|�
�qT|�
�wdS)Nrvrr��rszfoo
�IncompleteRead(%d bytes read)�IncompleteRead expected)�chunked_expectedr�
chunked_start�
last_chunk�chunked_endrr�r�rir=r#r��IncompleteRead�partialrXr�rr)rr�rfr�r?�xr �expected_messagerrr�test_chunkedbs:$
��
��zBasicTest.test_chunkedcCs�t}t|�}td�}tttt�}tj|dd�}|�	�|�
|�}|�|d|�|�|�||�|��t
dd�D]N}tttt�}tj|dd�}|�	�t|�}|�
|d|��}||�
||||��7}||�
||d��7}|�|d|�|�|�||�|��q;dD][}	tt|	�}tj|dd�}|�	�zCz|�
|�}Wn0tjy�}z#|�|j|�dt|�}
|�t|�|
�|�t|�|
�WYd}~n
d}~ww|�d	�W|��q�|��wdS)
Nr�rvrrr?rr@rArB)rCrXr�rrDrErFrr�r�r�rir#r��
memoryviewrGrHr�rr)rr�Z	nexpectedr�rfr�r?�mr rIrJrrr�test_readinto_chunked�sL

��
��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
rxrr
���OK)rrErFrr�r�rir=�status�reasonr�r�r�r�r#)rrDrfr�rrr�test_chunked_head�s�zBasicTest.test_chunked_headcCs�d}t|tt�}tj|dd�}|��td�}|�|�}|�|d�|�t	|�d�|�|j
d�|�|jd�|�|�
��|�|j�|��|�|j�dS)	NrOrxrrrrrPrQ)rrErFrr�r�r�r�rir�rRrSr�r�r�r�r#)rrDrfr�r�r?rrr�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
rvr�Hello
)rrr�r�rir=r�r�rrrr�test_negative_content_length�s�z&BasicTest.test_negative_content_lengthc
Cs�td�}tj|dd�}|��z|��Wn2tjyG}z%|�|jd�|�t|�d�|�t	|�d�|�
|���WYd}~dSd}~ww|�d�dS)Nz.HTTP/1.1 200 OK
Content-Length: 10

Hello
rvrrVz-IncompleteRead(7 bytes read, 3 more expected)rB)
rrr�r�r=rGrirHr�rr�r�r)rrfr�r rrr�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-LengthrEcs��ddd�S)Nrt�/urlrl)rhr�rkrr�<lambda>�sz&BasicTest.test_epipe.<locals>.<lambda>i�zBasic realm="example"zwww-authenticate)
r5rrerfr�r7�getresponserirRr�rrrZr�
test_epipe�s�

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

)rr�rr�r`r�rarrr�test_overflowing_header_lines���z&BasicTest.test_overflowing_header_linecCsrdd}t�t|��}|�tj��}|��Wd�n1s wY|�dt|j��|�dt|j��dS)NzHTTP/1.1 100 OK
r
i�zgot more than rm)	rr�rr�r"r�r�r�	exception)rrlr��cmrrr�'test_overflowing_header_limit_after_100s��
�z1BasicTest.test_overflowing_header_limit_after_100cCs:dddd}t�t|��}|��|�tj|j�dS)Nz/HTTP/1.1 200 OK
Transfer-Encoding: chunked

r	r_za
hello world
0

)rr�rr�r�r`r=rarrr�test_overflowing_chunked_lines���z'BasicTest.test_overflowing_chunked_linecCs`d}t|�}t�|�}|��|�|��d�|�|���|�|j	�|�
�|�|j	�dS)Nr^r
r�r�rrr�test_early_eof(s
zBasicTest.test_early_eofcsnt�d�}d�G�fdd�dtj�}||_td�|_|�dd�|�tj|j	�|�
�j�|�
|jj�dS)NrEcseZdZ�fdd�ZdS)z+BasicTest.test_error_leak.<locals>.Responsecs"|�tjj|g|�Ri|��dSr*)rr�rrO��responserrr9sz4BasicTest.test_error_leak.<locals>.Response.__init__N)r/r0r1rrrirr�Response8srkzInvalid status linervrd)
rrer�Zresponse_classrrfrhr�r�r\r�r�r)rrkrkrrir�test_error_leak4s

zBasicTest.test_error_leakcCsRd}td}tt|tt�}tj|dd�}|��|�|�	�|�|�
�dS)Nz3;foo=bar
abc
sabcrvr)rCrrD�last_chunk_extendedrFrr�r�rir=r#)rZextrar�rfr�rrr�test_chunked_extensionDsz BasicTest.test_chunked_extensioncCsBt}ttt�}tj|dd�}|��|�|��|�|�	�dS)z9some servers may serve up a short chunked encoding streamrvrN)
rCrrDrErr�r�rir=r#�rr�rfr�rrr�test_chunked_missing_endNsz"BasicTest.test_chunked_missing_endcCs\t}ttttt�}tj|dd�}|��|�	|�
�|�|�	|j�
�d�|��dS)z&See that trailers are read and ignoredrvrr
N)
rCrrDrEr�rFrr�r�rir=r!r#rorrr�test_chunked_trailersWszBasicTest.test_chunked_trailerscCsjt}d}ttttt|�}tj|dd�}|��|�	|�
�|�|�	|j�
�|�d��|�
�dS)zDCheck that we don't read past the end of the chunked-encoding stream�	extradatarvrrN)rCrrDrEr�rFrr�r�rir=r!rr#)rr�rrrfr�rrr�test_chunked_syncbszBasicTest.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

rvrN)rrr�r�rir=r!r#�rrrr�rfr�rrr�test_content_length_syncnsz"BasicTest.test_content_length_synccCs`d}d}td||�}tj|dd�}|��|�|�d�|g�|�|j��|�|��dS�Nrtrurvrvri�)	rrr�r�ri�	readlinesr!r=r#rwrrr�test_readlines_content_lengthzsz'BasicTest.test_readlines_content_lengthcCs^d}d}td||�}tj|dd�}|��|�|�d�|�|�|j��|�|��dSry)	rrr�r�ri�read1r!r=r#rwrrr�test_read1_content_length�sz#BasicTest.test_read1_content_lengthcCspd}d}td||�}tj|dd�}|��|�|�d�|�|�|�d�d�|�|j��|�|��dS)Nrtrurvrvrr�r
)	rrr�r�rirAr!r=r#rwrrr�"test_readline_bound_content_length�sz,BasicTest.test_readline_bound_content_lengthcCsvd}d}td|d|�}tj|dd�}|��|�|�d�|d�|�|��|�|�|j��|�|��dS)	Nrtrus'HTTP/1.1 200 OK
Content-Length: 30

r�rvr�r�)	rrr�r�rir|r=r!r#rwrrr�test_read1_bound_content_length�sz)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	�W|��n|��wW|��|��n	|��|��w|��|��d	�dS)
Nrc	s����\}}|�;|�d��}	|��}|�d�snq|�d�|���Wd�n1s.wYWd�dSWd�dS1sFwYdS)Nr Tr�s'HTTP/1.1 200 Connection established

)�acceptr&rA�rstriprr=)rk�address�readerr���result�servrr�
run_server�s
�

P�z2BasicTest.test_response_fileno.<locals>.run_server��targetrrwz
dummy:1234)�filenos
proxied data
)�socket�
create_server�HOST�
addCleanupr#�	threading�Thread�startr��floatrre�getsocknamerhr\rirRrQr�r�detach)rr��threadrkrjrrr�r�test_response_fileno�s,
�
zBasicTest.test_response_filenoc	s�t�tdf��|��j���fdd�}tj|d�}|��|�|jd�t	j
����}z<tdd�D]*}d|>�|�
d	d
�|���}|�t|�����Wd�n1sWwYq2W|��|�d�dS|��|�d�w)Nrcs����\}}|�6|�d�r6|�d��|�d�d�|�d��d�|�d�sWd�dSWd�dS1sAwYdS)N�s'HTTP/1.1 200 Ok
Content-Length: %d

�Ar��B�r�Zrecvr�rkr��r�r�rrr��s
����"�z7BasicTest.test_large_content_length.<locals>.run_serverr���?��rrvrd)r�r�r�r�r#r�r�r�r�rrer�r�rhr\rirXr=�rr�r�rk�wrjrr�r�test_large_content_length�s(
����z#BasicTest.test_large_content_lengthc
st�tdf��|��j���fdd�}tj|d�}|��|�|jd�t	j
����}zJtdd�D],}d|>�|�
d	d
�|���}|�t	j|j�Wd�n1sUwY|��q2W|��d�|�
d	d
�|��|�d�dS|��d�|�
d	d
�|��|�d�w)Nrcsb	���\}}|�|�d��s	Wd�dS|�d��Wd�n1s+wYq)NTr�s+HTTP/1.1 200 Ok
Content-Length: %d

Textr�r�r�rrr��s
�����zABasicTest.test_large_content_length_truncated.<locals>.run_serverr�r���Arrvrd)r�r�r�r�r#r�r�r�r�rrer�r�rhr\r�rGr=r�rr�r�#test_large_content_length_truncated�s4

�
��z-BasicTest.test_large_content_length_truncatedcC�4Gdd�dtj�}|d�}td�|_|�dd�dS)zv
        It should be possible to override the default validation
        behavior in putrequest (bpo-38216).
        c@r�)zRBasicTest.test_putrequest_override_domain_validation.<locals>.UnsafeHTTPConnectioncSr)r*r�rr�rrr�_validate_pathr+zaBasicTest.test_putrequest_override_domain_validation.<locals>.UnsafeHTTPConnection._validate_pathN)r/r0r1r�rrrr�UnsafeHTTPConnectionr�r�rErsrvz/N�rrerrfr��rr�rkrrr�*test_putrequest_override_domain_validations
z4BasicTest.test_putrequest_override_domain_validationcCs8Gdd�dtj�}|d�}td�|_|jdddd�dS)	Nc@r�)zPBasicTest.test_putrequest_override_host_validation.<locals>.UnsafeHTTPConnectioncSr)r*rr�rrr�_validate_hostr+z_BasicTest.test_putrequest_override_host_validation.<locals>.UnsafeHTTPConnection._validate_hostN)r/r0r1r�rrrrr�r�r�z
example.com
rsrvrdr)Z	skip_hostr�r�rrr�(test_putrequest_override_host_validations
z2BasicTest.test_putrequest_override_host_validationcCr�)z�
        It should be possible to override the default encoding
        to transmit bytes in another encoding even if invalid
        (bpo-36274).
        c@r�)zIBasicTest.test_putrequest_override_encoding.<locals>.UnsafeHTTPConnectioncSs
|�d�S)N�utf-8)r)rZstr_urlrrr�_encode_request.r(zYBasicTest.test_putrequest_override_encoding.<locals>.UnsafeHTTPConnection._encode_requestN)r/r0r1r�rrrrr�-r�r�rErsrvu/☃Nr�r�rrr�!test_putrequest_override_encoding's
z+BasicTest.test_putrequest_override_encodingN)7r/r0r1r�r�r�r�r�r�r�rrrrrrrrrr#r'r*r2r3r:r;r>rKrNrTrUrWrXr]rbrcrfrgrhrlrnrprqrsrxr{r}r~r�r�r�r�r�r�r�rrrrr�
sj

	 +


	%$
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)Nrvr)	rr�rr�r�r2�BufferedReader�fpr�rrrr�setUpTs


zExtendedReadTest.setUpcs�|j}|jj�d�fdd�	}||j_g}	|�d�}|rH|�t|�d�|��}|�t|�t|��|�|�|��|�t|��}|�	||�n	|��}|�
|�|�|�|sYnq|�	d�|�|j
�dS)	Nr:cs(�|�}|dkr|d|�S|dd�S)Nrr�r)r?r�Zoldpeekrr�mypeekasz*ExtendedReadTest.test_peek.<locals>.mypeekTr�rr
rB)r�r��peek�
assertGreaterrXZassertGreaterEqualr�r�r=rir�r]r��lines_expected)rr�r��allrZp2r,rr�r�	test_peek]s*


�zExtendedReadTest.test_peekcCs|j}|�|jj|j�dSr*)r��_verify_readlinerAr�)rr�rrr�
test_readline|szExtendedReadTest.test_readlinecCs^g}	|d�}|r|dkrt|�dkr|�|�d��|�|�|s#nq|�d�|�|�dS)NTr�foo�
r
)rXr�r�r]rir�)rrAr�r�r�rrrr��s
�	z!ExtendedReadTest._verify_readlinecs0�j���fdd�}t|�}��|j�j�dS)Ncs��d�}��t|�d�|S)Nr�)r|�assertLessEqualrX)�res�r�rrrr�s
z&ExtendedReadTest.test_read1.<locals>.r)r��	Readlinerr�rAr�)rrZ	readlinerrr�r�
test_read1�szExtendedReadTest.test_read1cCs>|j}g}	|��}|s
n|�|�q|�d�|�|j�dS)NTr
)r�r|r]rir�r��rr�r�rrrr�test_read1_unbounded�s
�z%ExtendedReadTest.test_read1_unboundedcCsP|j}g}	|�d�}|sn|�t|�d�|�|�q|�d�|�|j�dS)NTr�r
)r�r|r�rXr]rir�r�r�rrr�test_read1_bounded�s

�z#ExtendedReadTest.test_read1_boundedcCs|�|j�d�d�dS)Nrr
)rir�r|r'rrr�test_read1_0��zExtendedReadTest.test_read1_0cCs |j�d�}|�dt|��dS�Nr)r�r�r�rX)rrrrr�test_peek_0�szExtendedReadTest.test_peek_0)r/r0r1rCr��findrr�Z
lines_chunkedr�r�r�r�r�r�r�r�r�rrrrr�6s ��	
	
r�c@seZdZdZdZdS)�ExtendedReadTestChunkedz:
    Test peek(), read1(), readline() in chunked mode
    r�N)r/r0r1rCr�rrrrr��s�r�c@s eZdZdZdd�Zdd�ZdS)r�zT
    a simple readline class that uses an arbitrary read function and buffering
    cCs||_d|_dS)Nr
)�readfunc�	remainder)rr�rrrr�s
zReadliner.__init__cCs�g}d}|j}zB	|�d�}|dkrn|t|�|kr!||d}|�|�|��}|s/d}nq	|d7}|�|d|��||d�|_d�|�WSd�|�|_�)NrTr�r:rr
)r�r�rXr]r�r�)r�limitrZdatalenr=r�rrrrA�s.

�zReadliner.readlineN)r/r0r1rCrrArrrrr��sr�c@r4)�OfflineTestcCsfdh}ddh}tt�D]}|�d�s||vrqtt|�}t|dd�dkr)|�|�q|�tj|�dS)N�	responsesZHTTPMessage�
parse_headersr�r0zhttp.client)r�rr��getattr�addZassertCountEqual�__all__)rr�Zdenylistr�Z
module_objectrrr�test_all�s

�zOfflineTest.test_allcCs|�tjtjd�dS)Nz	Not Found)rirr��	NOT_FOUNDr'rrr�test_responses�r�zOfflineTest.test_responsesc	CsRgd�}|D] }|j|d��|�tt|��Wd�n1s!wYqdS)N):ZCONTINUEZSWITCHING_PROTOCOLSZ
PROCESSINGrQZCREATEDZACCEPTEDZNON_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_FAILEDZIM_A_TEAPOTZMISDIRECTED_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_REQUIREDZEARLY_HINTSZ	TOO_EARLY)Zconstant)r�r��hasattrr)rr�Zconstrrr�test_client_constants�s<���z!OfflineTest.test_client_constantsN)r/r0r1r�r�r�rrrrr��sr�c@s@eZdZdd�Zdd�Zdd�Ze�ee	d�d�d	d
��Z
dS)�SourceAddressTestcCs>t�tjtj�|_t�|j�|_t��|_|j�	�d|_
dSr*)r��AF_INET�SOCK_STREAMr�r�	bind_portrZfind_unused_port�source_port�listenrkr'rrrr�As



zSourceAddressTest.setUpcCs*|jr|j��d|_|j��d|_dSr*)rkr#r�r'rrr�tearDownHs



zSourceAddressTest.tearDowncCsBtjt|jd|jfd�|_|j��|�|jj�	�d|j�dS)Nrs��source_addressr)
rrer�rr�rkrNrirfr�r'rrr�testHTTPConnectionSourceAddressOs

�
z1SourceAddressTest.testHTTPConnectionSourceAddress�HTTPSConnectionz'http.client.HTTPSConnection not definedcCstjt|jd|jfd�|_dS)Nrsr�)rr�r�rr�rkr'rrr� testHTTPSConnectionSourceAddressUs
�z2SourceAddressTest.testHTTPSConnectionSourceAddressN)r/r0r1r�r�r��unittest�skipIfr�rr�rrrrr�@s�r�c@s(eZdZdZdd�Zdd�Zdd�ZdS)�TimeoutTestNcCs.t�tjtj�|_t�|j�t_|j��dSr*)	r�r�r�r�rr�r��PORTr�r'rrrr�bszTimeoutTest.setUpcCs|j��d|_dSr*)r�r#r'rrrr�grpzTimeoutTest.tearDownc	Cs|�t���t�d�zt�ttj�}|�	�Wt�d�nt�d�w|�
|j��d�|�
�|�t���t�d�ztjttjdd�}|�	�Wt�d�nt�d�w|�
|j��d�|�
�tjttjdd�}|�	�|�
|j��d�|�
�dS�N�)�timeout)�assertIsNoner�ZgetdefaulttimeoutZsetdefaulttimeoutrrer�r�r�rNrirfZ
gettimeoutr#)rZhttpConnrrr�testTimeoutAttributeks,



�
z TimeoutTest.testTimeoutAttribute)r/r0r1r�r�r�r�rrrrr�_s
r�c@r4)�PersistenceTestc
Cs�d}|D]v\}}}|j||d��bd�||�}t|�}|�|j�|�dd�|���}|�|jdu|�|��Wd�n1sDwY|�|jdu|�|�|j	d�|�dd�|�|j	|rhdnd�Wd�n1suwYqdS)	N))�1.0rsF)r�zConnection: keep-alive
T)�1.1rsT)r�zConnection: close
F)r�zConnection: keep-ALIVE
T)r�zConnection: cloSE
F)�versionrjz4HTTP/{} 200 OK
{}Content-Length: 12

Dummy body
rvz/open-connectionrz/subsequent-requestr�)
r�r}rDr�rfrhr\rir=rF)r�testsr�rjZreuser�rkrjrrr�test_reuse_reconnect�s(�

����z$PersistenceTest.test_reuse_reconnectc	Cs�dd�}tjtjf|tff}|D]?\}}|j|d��-td|�}|�dd�|�||j	�|�
|j�|�dd�|�|j
d�Wd�n1sJwYqdS)	Ncs&t�|���fdd�}|�_t���S)z3Return BufferedReader that raises ECONNRESET at EOFcs tj��|�}|dkrt��|Sr�)r2r3r��ConnectionResetError)�bufferr���streamrrr��szNPersistenceTest.test_disconnected.<locals>.make_reset_reader.<locals>.readinto)r2r3r�r�)rr�rr�r�make_reset_reader�s

z<PersistenceTest.test_disconnected.<locals>.make_reset_reader)rdr
rvz
/eof-response�
/reconnectr�)r2r3r�RemoteDisconnectedr�r�rDrhr�r\r�rfrirF)rr�r�Zstream_factoryrdrkrrr�test_disconnected�s
�
���z!PersistenceTest.test_disconnectedcCsVtd�}|jddddid�|�tj|j�|�|j�|�dd�|�|j	d�dS)	NsHTTP/1.1 100 Continue

rvrdZExpectz100-continue)rmr�r�)
rDrhr�rrr\r�rfrirFr�rrr�test_100_close�s�zPersistenceTest.test_100_closeN)r/r0r1r�rrrrrrr��sr�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�dSdS)Nr�zssl support required)r�r�skipTestr'rrrr��s
�zHTTPSTest.setUpcCsddlm}|||d�S)Nr)�make_https_server)�certfile)Ztest.ssl_serversr)rrrrrr�make_server�szHTTPSTest.make_servercCs$tjttjdd�}|�|jd�dSr�)rr�r�r�r�rir�)rrrrr�test_attributes�szHTTPSTest.test_attributesc	Cs�ddl}t�d�t�d��3t�dd�}|�|j��}|�	dd�Wd�n1s,wY|�
|jjd�Wd�dS1sDwYdS)Nr�network�self-signed.pythontest.net�rvrd�CERTIFICATE_VERIFY_FAILED)
�sslr�requiresr�transient_internetrr�r��SSLErrorrhrirdrS)rr
r�exc_inforrr�test_networked�s
�"�zHTTPSTest.test_networkedcCs�ddl}t�d�t�d��0|��}tjdd|d�}|�dd�|�	�}|�
�|�d|�d	��|�
�Wd�dS1sAwYdS)
Nrr	r
r��contextrvrd�nginx�server)
r
rrrrZ_create_unverified_contextrr�rhr\r#r�r�)rr
rrr�rrr�test_networked_noverification�s
�
"�z'HTTPSTest.test_networked_noverificationcCs�t�d�t�d��,t�dd�}|�dd�|��}|�d�}|�	�|�	�|�
d|�Wd�dS1s9wYdS)Nr	r
rrvrdzcontent-typez	text/html)rrrrrr�rhr\r�r#r�)rrr�Zcontent_typerrr�&test_networked_trusted_by_default_cert�s

"�z0HTTPSTest.test_networked_trusted_by_default_certc	Csddl}t�d�d}t�|��o|�|j�}|�|j|j	�|�|j
d�|�t�zt
j|d|d�}|�dd�|��}Wn$|jyc}zt|�}t�d	|�r^t�d
|�d|�d����d}~ww|�d
�}|��|��|�d|�Wd�dS1s�wYdS)Nrr	r
Trrrvrdz(?i)key.too.weakzGot z trying to connect to z). See https://bugs.python.org/issue36816.rr)r
rrrr�
SSLContext�PROTOCOL_TLS_CLIENTriZverify_modeZ
CERT_REQUIRED�check_hostname�load_verify_locations� CERT_selfsigned_pythontestdotnetrr�rhr\rr�re�searchr�ZSkipTestr�r#r�)	rr
Zselfsigned_pythontestdotnetrrr�Zssl_errZssl_err_strZ
server_stringrrr�test_networked_good_cert	s<

�����

"�z"HTTPSTest.test_networked_good_certc	Cs�ddl}t�d�t�d��@|�|j�}|�t�t	j
dd|d�}|�|j��}|�
dd�Wd�n1s9wY|�|jjd�Wd�dS1sQwYdS)	Nrr	r
rrrvrdr)r
rrrrrrr�CERT_localhostrr�r�rrhrirdrS)rr
rrrrrr�test_networked_bad_cert*s

�"�z!HTTPSTest.test_networked_bad_certcCslddl}|�t�}t�d|j�}|�|j��}|�dd�Wd�n1s'wY|�	|j
jd�dS)Nr�	localhostrvrdr)r
rr!rr�rr�rrhrirdrS)rr
rrrrrr�test_local_unknown_cert6s
�z!HTTPSTest.test_local_unknown_certcCsxddl}|�t�}|�|j�}|�t�tjd|j|d�}|�	|j
�|�dd�|��}|�	|j
�|�
|jd�dS)Nrr#rrv�/nonexistentr�)r
rr!rrrrr�rr�r#rhr\rirR�rr
rrrr�rrr�test_local_good_hostname?s

z"HTTPSTest.test_local_good_hostnamecCs2ddl}|�t�}|�|j�}|�t�tjd|j|d�}|�	|j
��|�dd�Wd�n1s4wYt�
dtf��tjd|j|dd�}Wd�n1sUwY|�	|j
��|�dd�Wd�n1sqwYd	|_t�
dtf��tjd|j|d	d�}Wd�n1s�wY|�dd
�|��}|��|��|�|jd�d	|_tjd|j|d�}|�dd
�|��}|�|jd�|��|��t�
dtf��tjd|j|dd�}Wd�n1s�wY|�	|j
��|�dd�Wd�dS1�swYdS)Nrr#rrvrdrsT)rrFr%r�)r
r�CERT_fakehostnamerrrrr�rr�ZCertificateErrorrhrZcheck_warnings�DeprecationWarningrr\r#rirRr&rrr�test_local_bad_hostnameLsT

�
���
��
��$�z!HTTPSTest.test_local_bad_hostnamer�z)http.client.HTTPSConnection not availablecCr)Nr)r)zwww.python.org:443r
r)rr
r)r
r
r)r
r	r)rr	r)r�rrr�rirrrrrrrvs
�zHTTPSTest.test_host_portcCs�ddl}|js|�d�t�dd�}|�|jj�|�|j	�}|�
|j�tjdd|d�}|�|j|�|�
|jj�t�
��t�ddt�tjdd|td�}Wd�n1sZwY|�|jj�dS)	NrzTLS 1.3 support requiredr#rr�ignorez5key_file, cert_file and check_hostname are deprecated)rZ	cert_file)r
ZHAS_TLSv1_3rrr�r�Z_contextZpost_handshake_authrrr�ZassertIs�warnings�catch_warnings�filterwarningsr)r!)rr
rrrrr�test_tls13_pha�s&

�
��zHTTPSTest.test_tls13_phaN)r/r0r1r�rrrrrZsystem_must_validate_certrr r"r$r'r*r�r�r�rrr/rrrrr�s$

!	
*�
rc@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)NrErs)rrerkrrfr'rrrr��szRequestBodyTest.setUpcCs(t�|jj�}|��t�|�}||fSr*)r2r3rfrrArr��r�f�messagerrr�get_headers_and_fp�s
z"RequestBodyTest.get_headers_and_fpc	Cs�ddgdfdf}|D]S\}}|�|��Bt�d�|_td�|j_|_|j�dd|�|��\}}|�d	|�|�d
|�|�	|�
d�d�|�	||���Wd�n1sXwYq
dS)
Nr��bar�3
foo
3
bar
0

))r�r5r6rErsrtrYzContent-Typer�r�r�)r�rrerkrrfrhr4r�ri�getr=)rr�rlr�r�r2rrr�test_list_body�s 
����zRequestBodyTest.test_list_bodycCsL|j�dddddi�|��\}}|�d|�d��|�dt|����dS)NrtrYrlr�Z42r�r�)rkrhr4rir7rXr=�rr3r2rrr�test_manual_content_length�s�z*RequestBodyTest.test_manual_content_lengthcC�`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)NrtrYrl�
text/plain�4r�sbody�	rkrhr4ri�get_content_typer��get_charsetr7r=r9rrr�test_ascii_body��zRequestBodyTest.test_ascii_bodycCr;)NrtrYubodyÁr<�5r��body�r>r9rrr�test_latin1_body�rBz RequestBodyTest.test_latin1_bodycCs`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)NrtrYrDr<rCr�r>r9rrr�test_bytes_body�rBzRequestBodyTest.test_bytes_bodycCs�|�tjtj�ttjddd��
}|�d�Wd�n1s wYttjdd��?}|j�dd|�|��\}}|�	d|�
��|�|���|�|�
d��|�	d	|�
d
��|�	d|���Wd�dS1snwYdS)Nr�r�)�encodingrlrtrYr<r�r�ztransfer-encodings4
body
0

)r�r�unlink�TESTFNr$�writerkrhr4rir?r�r@r7r=r1rrr�test_text_file_body�s�"�z#RequestBodyTest.test_text_file_bodycCs�|�tjtj�ttjd��
}|�d�Wd�n1swYttjd��=}|j�dd|�|��\}}|�	d|�
��|�|���|�	d|�
d��|�d	|�|�	d
|���Wd�dS1siwYdS)N�wbrDr rtrYr<r�r�r�s5
body�
0

)r�rrHrIr$rJrkrhr4rir?r�r@r7r�r=r1rrr�test_binary_file_body�s�"�z%RequestBodyTest.test_binary_file_bodyN)
r/r0r1rCr�r4r8r:rArErFrKrMrrrrr0�s	r0c@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�)rrlrfrrrr�szHTTPResponseTest.setUpcCs6|j�d�}|�|d�|j�dd�}|�|d�dS)Nz	My-Headerzfirst-value, second-valuezsome default�r�r�ri�rrjrrr�test_getting_header	sz$HTTPResponseTest.test_getting_headercCs|j�dd�}|�|d�dS)N�No-Such-Headerz
default-valuerOrPrrr�3test_getting_nonexistent_header_with_string_defaultszDHTTPResponseTest.test_getting_nonexistent_header_with_string_defaultcCs<|j�dddg�}|�|d�|j�dd�}|�|d�dS)NrR�default�valueszdefault, values)rTrUrOrPrrr�5test_getting_nonexistent_header_with_iterable_defaultszFHTTPResponseTest.test_getting_nonexistent_header_with_iterable_defaultcCs|j�d�}|�|d�dS)NrRrOrPrrr�/test_getting_nonexistent_header_without_defaultsz@HTTPResponseTest.test_getting_nonexistent_header_without_defaultcCs |jjddd�}|�|d�dS)NrRr�)rTrOrPrrr�test_getting_header_defaultintsz/HTTPResponseTest.test_getting_header_defaultintN)	r/r0r1r�rQrSrVrWrXrrrrrNsrNc@sTeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�TunnelTestscCs*d}d|_t�|j�|_|�|�|j_dS)Nz:HTTP/1.0 200 OK

HTTP/1.1 200 OK
Content-Length: 42

z	proxy.com)rrrerkrJ)r�
response_textrrrr�$s
�zTunnelTests.setUpcCs|j��dSr*)rkr#r'rrrr�.szTunnelTests.tearDowncsd�fdd�	}|S)Ncst�|d|dd�S)Nrr)rr)r)r�r�r��rZrrrI2sz9TunnelTests._create_connection.<locals>.create_connection)NNr)rrZrIrr[rrJ1szTunnelTests._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))rrmrxrdrs)rk�
set_tunnelrhrirfrrr�	HTTP_PORTZ_tunnel_hostZ_tunnel_portZ_tunnel_headers)rZtunnel_hostZtunnel_portZtunnel_headersrrr�!test_set_tunnel_host_port_headers6s
�z-TunnelTests.test_set_tunnel_host_port_headerscCs |j��|�t|jjd�dS)Nr\)rkrNr��RuntimeErrorr]r'rrr�&test_disallow_set_tunnel_after_connectCs
�z2TunnelTests.test_disallow_set_tunnel_after_connectcCs�|j�d�|j�ddd�|�|jjj|j�|�|jjjtj�|�	d|jjj
�|�d|jjj
�|�	d|jjj
�|�d|jjj
�dS)	Nr\rxrdrs�CONNECT destination.comsHost: destination.com:None�Host: destination.comsHost: proxy.com)rkr]rhrirfrrrr^r�rr�r'rrr�test_connect_with_tunnelIsz$TunnelTests.test_connect_with_tunnelcCs�tj�|jd��}|j�d�|j��|j�dd�Wd�n1s%wY|��|jt	|j
�dd|j
��d�|j
d	dd	}|�d
|�|j|�
d�d|��d�dS)
z8Regresstion test for https://bugs.python.org/issue43332.r(r\rvrdNrz!unexpected number of send calls: )r�rrbr9zunexpected proxy data sent )rZpatch�objectrkr]rNrhZ
assert_calledr�rXZ
mock_callsr�r�r�)rZ	mock_sendZproxy_setup_data_sentrrr�0test_tunnel_connect_single_send_connection_setupVs 
�

�
�z<TunnelTests.test_tunnel_connect_single_send_connection_setupcCsl|j�d�|j�ddd�|�|jjj|j�|�|jjjtj�|�	d|jjj
�|�	d|jjj
�dS)Nr\rtrdrsrbrc)rkr]rhrirfrrrr^r�rr'rrr�test_connect_put_requestgsz$TunnelTests.test_connect_put_requestcCs�d}d�|�}|j�d�|�|�|j_|j�d�t���}|j�ddd�Wd�n1s1wY|���	�}|�
d�|�|�dS)	Nz
X-Dummy: 1zHTTP/1.0 200 OK
{}

rr\rtrdrsz
header: {})r}rkZset_debuglevelrJr]rr�rhr�r�r�)rZexpected_headerrZr�r�rrr�test_tunnel_debuglogos

�z TunnelTests.test_tunnel_debuglogN)r/r0r1r�r�rJr_rardrfrgrhrrrrrY#s


rY�__main__r�)�	verbosity)<r8Zhttprrr2r{�osr)rr�r�r,r�rZTestCaser<rZtest.supportrrr�path�dirnamer%�herer�r!r(rrDrCZchunk_extensionrErmr�rFr�rr5r3r9rerDrSr�r�r�r�r�r�r�r�r�r�rr0rNrYr/�mainrrrr�<module>sz�!n2}"U.HL`#
Z�