HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //usr/local/lib/python3.7/test/__pycache__/test_httplib.cpython-37.opt-2.pyc
B

��g�2�@sddlZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
e
jZddlm
Z
ej�e�Zej�ed�Zej�ed�Zej�ed�ZdZdZd	Zd
ZdedZd
ZdZe
jZGdd�d�ZGdd�de�ZGdd�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�Z.Gd,d-�d-e�Z/Gd.d/�d/e�Z0Gd0d1�d1e�Z1Gd2d3�d3e�Z2e3d4k�re
j4d5d6�dS)7�N)�client)�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
�0z
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)�selfrrrr�r�-/usr/local/lib/python3.7/test/test_httplib.py�__init__/s

zFakeSocket.__init__cCs |jd7_|j|7_dS)N�)rr
)rr
rrr�sendall:szFakeSocket.sendallcCs6|dkr|dkrt���|�|j�|_|j|j_|jS)N�r�rb)rZUnimplementedFileModerr�file�
file_close�close)r�mode�bufsizerrr�makefile>s

zFakeSocket.makefilecCs
d|_dS)NT)r)rrrrrFszFakeSocket.file_closecCsdS)Nr)rrrrrIszFakeSocket.closecCsdS)Nr)r�levelZoptname�valuerrr�
setsockoptLszFakeSocket.setsockopt)N)�__name__�
__module__�__qualname__�io�BytesIOrrrrrr"rrrrr.s
rc@s$eZdZdd�Zdd�Zdd�ZdS)�EPipeSocketcCst�||�||_dS)N)rr�pipe_trigger)rrr)rrrrQszEPipeSocket.__init__cCs(|j|krttjd��|j|7_dS)NZgotcha)r)�OSError�errnoZEPIPEr
)rr
rrrrVs
zEPipeSocket.sendallcCsdS)Nr)rrrrr[szEPipeSocket.closeN)r#r$r%rrrrrrrr(Osr(c@s eZdZddd�Zddd�ZdS)	�NoEOFBytesIO���cCs"tj�||�}|dkrtd��|S)Nrzcaller tried to read past EOF)r&r'�read�AssertionError)r�nr
rrrr.dszNoEOFBytesIO.readNcCs"tj�||�}|dkrtd��|S)Nrzcaller tried to read past EOF)r&r'�readliner/)r�lengthr
rrrr1jszNoEOFBytesIO.readline)r-)N)r#r$r%r.r1rrrrr,^s
r,cs0eZdZ�fdd�Z�fdd�Zdd�Z�ZS)�FakeSocketHTTPConnectioncs$d|_t��d�||_|j|_dS)Nrzexample.com)�connections�superr�fake_socket_args�create_connection�_create_connection)r�args)�	__class__rrrssz!FakeSocketHTTPConnection.__init__cs|jd7_t���S)Nr)r4r5�connect)r)r:rrr;ysz FakeSocketHTTPConnection.connectcOs
t|j�S)N)rr6)r�pos�kwrrrr7~sz*FakeSocketHTTPConnection.create_connection)r#r$r%rr;r7�
__classcell__rr)r:rr3psr3c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�HeaderTestscCs�Gdd�dt�}xzdD]r}xldD]d}t�d�}td�|_|�|_d}i}|r\tt|��||<|�dd	||�|�	|jj
|��d
�q WqWdS)Nc@seZdZdd�Zdd�ZdS)z;HeaderTests.test_auto_headers.<locals>.HeaderCountingBuffercSs
i|_dS)N)�count)rrrrr�szDHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.__init__cSsX|�d�}t|�dkrH|d�d���}|j�|d�|j|d7<t�||�dS)N�:rrr)�split�len�decode�lowerr@�
setdefault�list�append)r�item�kvZlcKeyrrrrH�s
zBHeaderTests.test_auto_headers.<locals>.HeaderCountingBuffer.appendN)r#r$r%rrHrrrr�HeaderCountingBuffer�srK)TF)zContent-lengthZHostzAccept-encodingzexample.comZblahblahblahZspamspamspam�POST�/r)rGr�HTTPConnectionr�sock�_bufferr	rC�request�assertEqualr@rE)rrKZexplicit_header�header�conn�body�headersrrr�test_auto_headers�s



zHeaderTests.test_auto_headerscCsvGdd�dt�}d}d}xZt�||�D]J\}}t�d�}td�|_|�|_|�|d|�|�	|jj
dd�|��q&Wd	}xN|D]F}t�d�}td�|_|�|_|�|dd�|�	|jj
dd
�|��q~WxN|D]F}t�d�}td�|_|�|_|�|dd�|�	|jj
dd�|��q�WxXt�||�D]H}t�d�}td�|_|�|_|�|dd�|�	|jj
d
d�|���q&WdS)Nc@seZdZdd�Zdd�ZdS)z?HeaderTests.test_content_length_0.<locals>.ContentLengthCheckercSst�|�d|_dS)N)rGr�content_length)rrrrr�s
zHHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.__init__cSsF|�dd�}t|�dkr6|d��dkr6|d��|_t�||�dS)NrArrscontent-length)rBrCrE�striprXrGrH)rrIrJrrrrH�szFHeaderTests.test_content_length_0.<locals>.ContentLengthChecker.appendN)r#r$r%rrHrrrr�ContentLengthChecker�srZ)N�)�PUTrLZPATCHzexample.comrM�0z%Header Content-Length incorrect on {})�GET�CONNECTZDELETE�HEADZOPTIONSZTRACEz.Header Content-Length set for empty body on {}r[� �1)
rG�	itertools�productrrNrrOrPrQrRrX�format�chain)rrZZbodiesZmethods_with_body�methodrUrTZmethods_without_bodyrrr�test_content_length_0�sH









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),Nzexample.comr^rMzContent-length�*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)rrNrrO�
putrequest�	putheader�assertInrP)rrTrrr�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::]:81r[r^z/foosEGET /foo HTTP/1.1
Host: [2001:102A::]
Accept-Encoding: identity

z
[2001:102A::])rrNrrOrQ�
assertTruer
�
startswith)r�expectedrTrOrrr�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�beginrR�	getheader)rrUrO�resprrr�!test_malformed_headers_coped_withs
z-HeaderTests.test_malformed_headers_coped_withcCs0dttdd��dttdd��d}t|�}t�|�}|��|�|�d�d	�|�|jdd	�|�|�d
�d�|�|jd
d�d�	t
ttdd���}|�|�d
�|�|�|jd
|�|�|�d��|�
d|j�xH|�d�|jdfD]0}|�|�d��|�
d|�|�|�d��q�WdS)Ns0HTTP/1.1 200 OK
!#$%&'*+-.^_`|~: value
VCHAR: �!�s
obs-text: ��sM
obs-fold: text
 folded with space
	folded with tab
Content-Length: 0

zContent-Lengthrz!#$%&'*+-.^_`|~r!r[ZVCHARzobs-textzobs-foldrz folded with spacezfolded with tab)�bytes�rangerrrtrurRrv�msg�join�map�chrZassertIsNotNonermrorp�endswith)rrUrOrwZvcharZfoldedrrr�test_parse_all_octets s"(
z!HeaderTests.test_parse_all_octetscCsxt�d�}td�|_|�dd�d}xN|D]F\}}|�||f��*|�td��|�||�WdQRXWdQRXq*WdS)Nzexample.comr[r^rM))s
Invalid
Names
ValidValue)sInvalid
Names
ValidValue)sInvalid
Names
ValidValue)s

InvalidNames
ValidValue)s
InvalidNames
ValidValue)s
InvalidNames
ValidValue)s InvalidNames
ValidValue)s	InvalidNames
ValidValue)sInvalid:Names
ValidValue)s:InvalidNames
ValidValue)s	ValidNamesInvalid
Value)s	ValidNames
Invalid
Value)s	ValidNames
Invalid
Value)s	ValidNamesInvalidValue
)s	ValidNames
InvalidValue
)s	ValidNames
InvalidValue
zInvalid header)	rrNrrOrk�subTest�assertRaisesRegex�
ValueErrorrl)rrT�cases�namer!rrr�test_invalid_headers>s

z HeaderTests.test_invalid_headersc	Cs�d}t|�}tj|dd�}t���}|��WdQRX|����}|�|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)	rrrtr�captured_stdoutru�getvalue�
splitlinesrR)rrUrOrw�output�linesrrr�test_headers_debuglevel\s
z#HeaderTests.test_headers_debuglevelN)r#r$r%rWrhrnrrrxr�r�r�rrrrr?�sB"
r?c@seZdZdd�ZdS)�HttpMethodTestsc
CsPd}xF|D]>}|�td��(t�d�}td�|_|j|dd�WdQRXq
WdS)N)	zGET
zPOST
zPUT

z
POST
Valuez
POST
HOST:abczGET
rHost:abc
zPOST
Remainder:
z
GET
HOST:
z
PUTz'method can't contain control characterszexample.comrM)rg�url)r�r�rrNrrOrQ)r�methodsrgrTrrr�test_invalid_method_namesos



z)HttpMethodTests.test_invalid_method_namesN)r#r$r%r�rrrrr�nsr�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)Nzexample.comrrLrMT)�encode_chunked)rrNrrOrkZ
endheaders�
_make_body�_parse_requestr
�_parse_chunkedrR�
expected_body)rrT�_rUrrr�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)Nzexample.comrrLrMzTransfer-Encoding�chunkedzcontent-lengthcSsg|]}|���qSr)rE)�.0�krrr�
<listcomp>�sz>TransferEncodingTest.test_explicit_headers.<locals>.<listcomp>zlatin-1cSsg|]}|���qSr)rE)r�r�rrrr��sz
gzip, chunkedT)rVr�rUcSsg|]}|���qSr)rE)r�r�rrrr��s)rrNrrOrQr�r�r
�assertNotIn�keysrRr�rDr�)rrTr�rVrUrrr�test_explicit_headers�s6






z*TransferEncodingTest.test_explicit_headerscCs�x�dD]�}t�d�}td�|_|�dd|j|d��|�|jj�\}}}|�|�}|�	||j
�|�	|dd�|�d	d
d�|D��qWdS)N)FTzexample.comrrLrM)�empty_lineszTransfer-Encodingr�zcontent-lengthcSsg|]}|���qSr)rE)r�r�rrrr��sz5TransferEncodingTest.test_request.<locals>.<listcomp>)rrNrrOrQr�r�r
r�rRr�r�)rr�rTr�rVrUrrr�test_request�s



z!TransferEncodingTest.test_requestcCslt�d�}td�|_|�ddd�|�|jj�\}}}|�|dd�|�dd	d
�|D��|�|d�dS)Nzexample.comrrLrMrzTransfer-Encodingr�zcontent-lengthcSsg|]}|���qSr)rE)r�r�rrrr��sz8TransferEncodingTest.test_empty_body.<locals>.<listcomp>s0

)	rrNrrOrQr�r
rRr�)rrTr�rVrUrrr�test_empty_body�s

z$TransferEncodingTest.test_empty_bodyFccsZ|j�d�}xHt|�D]<\}}|r0|dr0dV|t|�dkrL|dVq|VqWdS)N� r�rr)r�rB�	enumeraterC)rr�r��idx�linerrrr��szTransferEncodingTest._make_bodycCs�|�d�}|d}i}d}xZ|t|�krtt||�dkrt||�d�\}}|�d���}|�d���||<|d7}qW||d�||dd��fS)Ns
rrrAzlatin-1)rBrCrDrYr�)rr
r�rQrVr0�keyrsrrrr��s
z#TransferEncodingTest._parse_requestcCs�g}i}d}|�d�}xd|||d�\}}t|d�}|dkrH|d7}P|�|t|��|�|�|d7}|t|�krPqWd�|�S)Nrs
r��rr)rB�intrRrCrHr�)rr
rU�trailersr0r��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_S)`�	BasicTestcCs�d}t|�}t�|�}|��|�|�d�d�|�|���|�|j�|�|��d�|�	|���|�|j�|�
�|�	|j�d}t|�}t�|�}|�tj|j�dS)NzHTTP/1.1 200 Ok

TextrrsTextzHTTP/1.1 400.100 Not Ok

Text)
rrrtrurRr.�assertFalse�isclosed�closedror�assertRaises�
BadStatusLine)rrUrOrwrrr�test_status_liness 

zBasicTest.test_status_linescCst�d�}|�t|�d�dS)Nr[zBadStatusLine("''"))rr�rR�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�sTesxt)rrrtrurRr.r�r�ror�r)rrUrOrwrrr�test_partial_readss
zBasicTest.test_partial_readscCs~d}t|�}t�|�}|��|�|��d�|�|���|�|��d�|�	|���|�|j
�|��|�	|j
�dS)Nz4HTTP/1.1 200 Ok
Content-Length: 13

Text
AnothersText
sAnother)rrrtrurRr1r�r�r.ror�r)rrUrOrwrrr�test_mixed_reads-s
zBasicTest.test_mixed_readscCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�
|�	��|�|j�|��|�
|j�dS)Nz*HTTP/1.1 200 Ok
Content-Length: 4

Textr�sTesxt)
rrrtru�	bytearray�readintorRr}r�r�ror�r)rrUrOrw�br0rrr�test_partial_readintos<s 


z BasicTest.test_partial_readintoscCs�d}t|�}t�|�}|��|�|�d�d�|�|���|�|�d�d�|�|�d�d�|�|���|�|j	�|�
�|�|j	�dS)NzHTTP/1.1 200 Ok

Textr�sTesxtrr)rrrtrurRr.r�r�ror�r)rrUrOrwrrr�$test_partial_reads_no_content_lengthPs
z.BasicTest.test_partial_reads_no_content_lengthcCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�|�}|�|d�|�
|�	��dS)NzHTTP/1.1 200 Ok

Textr�sTesxtr)rrrtrur�r�rRr}r�r�ro)rrUrOrwr�r0rrr�(test_partial_readintos_no_content_length`s

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

Textr�sTesxtrr)	rrrtrurRr.r�r�ro)rrUrOrwrrr�"test_partial_reads_incomplete_bodyss
z,BasicTest.test_partial_reads_incomplete_bodycCs�d}t|�}t�|�}|��td�}|�|�}|�|d�|�t|�d�|�|�	��|�|�}|�|d�|�t|�d�|�|�}|�|d�|�
|�	��|�|j�|��|�
|j�dS)Nz+HTTP/1.1 200 Ok
Content-Length: 10

Textr�sTesxtr)
rrrtrur�r�rRr}r�r�ror�r)rrUrOrwr�r0rrr�&test_partial_readintos_incomplete_body�s$



z0BasicTest.test_partial_readintos_incomplete_bodycCs^xdD]}|�tjtj|�qWx8dD]0\}}}t�|�}|�||j�|�||j�q&WdS)N)zwww.python.org:abczuser:password@www.python.org))z[fe80::207:e9ff:fe9b]:8000zfe80::207:e9ff:fe9bi@)zwww.python.org:80zwww.python.org�P)zwww.python.org:zwww.python.orgr�)zwww.python.orgzwww.python.orgr�)z[fe80::207:e9ff:fe9b]zfe80::207:e9ff:fe9br�)z[fe80::207:e9ff:fe9b]:zfe80::207:e9ff:fe9br�)r�r�
InvalidURLrNrRrr)r�hp�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)rrrtrurvrR)rrZhdr�srZcookiesrrr�test_response_headers�s

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

r`)rgz)Did not expect response from HEAD request)rr,rrtrur.�fail)rrOrwrrr�test_read_head�szBasicTest.test_read_headcCsTtdt�}tj|dd�}|��td�}|�|�dkr@|�d�|�t	|�d�dS)Nz*HTTP/1.1 200 OK
Content-Length: 14432

r`)rg�rz)Did not expect response from HEAD requests)
rr,rrtrur�r�r�rRr})rrOrwr�rrr�test_readinto_head�s
zBasicTest.test_readinto_headcCsRd�dd�ttjd�D��d}d|}t|�}t�|�}|�tjd|j�dS)Nz
css|]}d|VqdS)z
Header%d: fooNr)r��irrr�	<genexpr>�sz2BasicTest.test_too_many_headers.<locals>.<genexpr>rzHTTP/1.1 200 OK
zgot more than \d+ headers)	r�r~rZ_MAXHEADERSrrtr��
HTTPExceptionru)rrVrr�rrrr�test_too_many_headers�s

zBasicTest.test_too_many_headersc	Csnd}ttd��V}t�d�}t|�}||_|�dd|�|�|j�	|�d|jdt
|��|f�WdQRXdS)Ns_GET /foo HTTP/1.1
Host: example.com
Accept-Encoding: identity
Transfer-Encoding: chunked

rzexample.comr^z/fooz%r != %r)�open�__file__rrNrrOrQror
rprC)rrqrUrTrOrrr�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 testzexample.comrr�)
rrNrrO�sendrRr
�arrayr&r')rrqrTrOrrr�	test_send�s

zBasicTest.test_sendcsXdd��G�fdd�dtj�}d}t�d�}td�}||_|�|��|�|j|�dS)NcssdVdVdVdS)Nr
Zdata_tworrrrrr
�sz/BasicTest.test_send_updating_file.<locals>.datacs eZdZdZ��Zddd�ZdS)z7BasicTest.test_send_updating_file.<locals>.UpdatingFilerr-cSs
t|j�S)N)�next�d)r�	blocksizerrrr.sz<BasicTest.test_send_updating_file.<locals>.UpdatingFile.readN)r-)r#r$r%rr�r.r)r
rr�UpdatingFilesr�sdatazexample.comr[)	r&�
TextIOBaserrNrrOr�rRr
)rr�rqrTrOr)r
r�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)NsonestwosthreerrrrrrUsz&BasicTest.test_send_iter.<locals>.bodyzexample.comr[r^z/foozContent-LengthZ11)rrNrrOrQrRr
)rrqrUrTrOrrr�test_send_iters
zBasicTest.test_send_itercCsvd}tjd|d�}td�}||_d|d}|�ddt�|�dd	i�|�|jd
�|j	�
dd�d}|�||�dS)
N�zexample.com)r��a�br\rMzContent-Length�9r�s

r)rrNrrOrQr&r'rRrr
rB)rr�rTrOrqrUrrr�test_blocksize_requestsz BasicTest.test_blocksize_requestcCs\d}tjd|d�}td�}||_d|d}|�t�|��|�|jd�|�|j	|�dS)Nr�zexample.com)r�r�r�r�)
rrNrrOr�r&r'rRrr
)rr�rTrOrqrrr�test_blocksize_send+szBasicTest.test_blocksize_sendc	Cs<t�d�}td�|_|�t��|�dd|�WdQRXdS)Nzexample.comr[rL�test)rrNrrOr��	TypeErrorrQ)rrTrrr�test_send_type_error6s

zBasicTest.test_send_type_errorcCsdt}tttt�}tj|dd�}|��|�|�	�|�|�
�xdtdd�D]V}tttt�}tj|dd�}|��|�|�	|�|�	|�|�	�|�|�
�qNWx�dD]�}tt|�}tj|dd�}|��z|y|�	�Wn`tjk
�rB}z>|�|j
|�dt|�}|�t|�|�|�t|�|�Wdd}~XYnX|�d�Wd|�
�Xq�WdS)Nr^)rgr�)r[zfoo
zIncompleteRead(%d bytes read)zIncompleteRead expected)�chunked_expectedr�
chunked_start�
last_chunk�chunked_endrrtrurRr.rr~�IncompleteRead�partialrCr�r	r�)rrqrOrwr0�xr��expected_messagerrr�test_chunked=s2$
"zBasicTest.test_chunkedcCs�t}t|�}td�}tttt�}tj|dd�}|�	�|�
|�}|�|d|�|�|�||�|��x�t
dd�D]�}tttt�}tj|dd�}|�	�t|�}|�
|d|��}||�
||||��7}||�
||d��7}|�|d|�|�|�||�|��qxWx�dD]�}	tt|	�}tj|dd�}|�	�z~y|�
|�}Wn`tjk
�r�}z>|�|j|�dt|�}
|�t|�|
�|�t|�|
�Wdd}~XYnX|�d	�Wd|��X�qWdS)
Nr{r^)rgrr�r)r[zfoo
zIncompleteRead(%d bytes read)zIncompleteRead expected)r�rCr�rr�r�r�rrtrur�rRrr~�
memoryviewr�r�r�r	r�)rrqZ	nexpectedr�rOrwr0�mr�r�r�rrr�test_readinto_chunked]sD

"zBasicTest.test_readinto_chunkedcCs�d}t|tt�}tj|dd�}|��|�|��d�|�|jd�|�|j	d�|�
|���|�|j
�|��|�
|j
�dS)NzEHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello world
1
d
r`)rgr���OK)rr�r�rrtrurRr.�status�reasonror�r�r�r)rr�rOrwrrr�test_chunked_head�szBasicTest.test_chunked_headcCs�d}t|tt�}tj|dd�}|��td�}|�|�}|�|d�|�t	|�d�|�|j
d�|�|jd�|�|�
��|�|j�|��|�|j�dS)	NzEHTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello world
1
d
r`)rgr�rsr�r�)rr�r�rrtrur�r�rRr}r�r�ror�r�r�r)rr�rOrwr�r0rrr�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
r^)rgsHello
)rrrtrurRr.ror�)rrOrwrrr�test_negative_content_length�sz&BasicTest.test_negative_content_lengthc
Cs�td�}tj|dd�}|��y|��Wn`tjk
r�}z@|�|jd�|�t|�d�|�t	|�d�|�
|���Wdd}~XYnX|�d�dS)Nz.HTTP/1.1 200 OK
Content-Length: 10

Hello
r^)rgsHello
z-IncompleteRead(7 bytes read, 3 more expected)zIncompleteRead expected)
rrrtrur.r�rRr�r�r	ror�r�)rrOrwr�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-Lengthzexample.comcs��ddd�S)Nr\z/urlrU)rQr)rTrr�<lambda>�rz&BasicTest.test_epipe.<locals>.<lambda>i�zBasic realm="example"zwww-authenticate)
r(rrNrOr�r*�getresponserRr�rv)rrOrwr)rTr�
test_epipe�s
zBasicTest.test_epipecCs8dddd}t�t|��}|�tjtjf|j�dS)NzHTTP/1.1 200 Okr�iz
)rrtrr��LineTooLongr�ru)rrUrwrrr�test_overflowing_status_line�sz&BasicTest.test_overflowing_status_linecCs2dddd}t�t|��}|�tj|j�dS)NzHTTP/1.1 200 OK
X-Foo: barriz

)rrtrr�rru)rrUrwrrr�test_overflowing_header_line�sz&BasicTest.test_overflowing_header_linec	Cs^dd}t�t|��}|�tj��}|��WdQRX|�dt|j��|�dt|j��dS)NzHTTP/1.1 100 OK
r
i�zgot more than rV)	rrtrr�r�rurmr	�	exception)rrUrw�cmrrr�'test_overflowing_header_limit_after_100�sz1BasicTest.test_overflowing_header_limit_after_100cCs:dddd}t�t|��}|��|�tj|j�dS)Nz/HTTP/1.1 200 OK
Transfer-Encoding: chunked

riza
hello world
0

)rrtrrur�rr.)rrUrwrrr�test_overflowing_chunked_line�sz'BasicTest.test_overflowing_chunked_linecCs`d}t|�}t�|�}|��|�|��d�|�|���|�|j	�|�
�|�|j	�dS)NzHTTP/1.1 200 Okr)rrrtrurRr.ror�r�r�r)rrUrOrwrrr�test_early_eofs
zBasicTest.test_early_eofcsnt�d�}d�G�fdd�dtj�}||_td�|_|�dd�|�tj|j	�|�
�j�|�
|jj�dS)Nzexample.comcseZdZ�fdd�ZdS)z+BasicTest.test_error_leak.<locals>.Responsecs|�tjj|f|�|�dS)N)rrtr)rr<r=)�responserrrsz4BasicTest.test_error_leak.<locals>.Response.__init__N)r#r$r%rr)rrr�ResponsesrzInvalid status liner^rM)
rrNrtZresponse_classrrOrQr�r�rror�r)rrTrr)rr�test_error_leaks

zBasicTest.test_error_leakcCsRd}td}tt|tt�}tj|dd�}|��|�|�	�|�|�
�dS)Nz3;foo=bar
abc
sabcr^)rg)r�rr��last_chunk_extendedr�rrtrurRr.r)rZextrarqrOrwrrr�test_chunked_extensionsz BasicTest.test_chunked_extensioncCsBt}ttt�}tj|dd�}|��|�|��|�|�	�dS)Nr^)rg)
r�rr�r�rrtrurRr.r)rrqrOrwrrr�test_chunked_missing_end)sz"BasicTest.test_chunked_missing_endcCs\t}ttttt�}tj|dd�}|��|�	|�
�|�|�	|j�
�d�|��dS)Nr^)rgr)
r�rr�r�r�r�rrtrurRr.rr)rrqrOrwrrr�test_chunked_trailers2szBasicTest.test_chunked_trailerscCsjt}d}ttttt|�}tj|dd�}|��|�	|�
�|�|�	|j�
�|�d��|�
�dS)N�	extradatar^)rgr)r�rr�r�r�r�rrtrurRr.rr
r)rrqrrOrwrrr�test_chunked_sync=szBasicTest.test_chunked_synccCs\d}d}td||�}tj|dd�}|��|�|��|�|�|j��|�|��dS)Ns	extradatas
Hello123
s'HTTP/1.1 200 OK
Content-Length: 10

r^)rg)rrrtrurRr.rr)rrrqrOrwrrr�test_content_length_syncIsz"BasicTest.test_content_length_synccCs`d}d}td||�}tj|dd�}|��|�|�d�|g�|�|j��|�|��dS)Ns	extradatas
Hello123
s'HTTP/1.1 200 OK
Content-Length: 10

r^)rgi�)	rrrtrurR�	readlinesrr.r)rrrqrOrwrrr�test_readlines_content_lengthUsz'BasicTest.test_readlines_content_lengthcCs^d}d}td||�}tj|dd�}|��|�|�d�|�|�|j��|�|��dS)Ns	extradatas
Hello123
s'HTTP/1.1 200 OK
Content-Length: 10

r^)rgi�)	rrrtrurR�read1rr.r)rrrqrOrwrrr�test_read1_content_length`sz#BasicTest.test_read1_content_lengthcCspd}d}td||�}tj|dd�}|��|�|�d�|�|�|�d�d�|�|j��|�|��dS)Ns	extradatas
Hello123
s'HTTP/1.1 200 OK
Content-Length: 10

r^)rg�
r)	rrrtrurRr1rr.r)rrrqrOrwrrr�"test_readline_bound_content_lengthksz,BasicTest.test_readline_bound_content_lengthcCsvd}d}td|d|�}tj|dd�}|��|�|�d�|d�|�|��|�|�|j��|�|��dS)	Ns	extradatas
Hello123
s'HTTP/1.1 200 OK
Content-Length: 30

r�r^)rg�r�)	rrrtrurRrr.rr)rrrqrOrwrrr�test_read1_bound_content_lengthwsz)BasicTest.test_read1_bound_content_lengthcs�t�tjtjtj��|��j���tdf����d���fdd�}t	j
|d�}|��|�|jt
d��tj����}|�dd�|��}z>|�|jtj�tj|��d�}z|�d	�Wd|��XWd|��|��X|��|��d	�dS)
Nrc
sb���\}}|�H|�d��2}x|��}|�d�s Pq W|�d�|���WdQRXWdQRXdS)Nrs
s'HTTP/1.1 200 Connection established

)Zacceptrr1�rstriprr.)rT�address�readerr�)�result�servrr�
run_server�s

z2BasicTest.test_response_fileno.<locals>.run_server)�targetrr_z
dummy:1234)�filenos
proxied data
)�socket�AF_INET�SOCK_STREAMZIPPROTO_TCP�
addCleanuprZbind�HOST�listen�	threadingZThread�startr��floatrrN�getsocknamerQrrRr�r�r'r�detach)rr%ZthreadrTrr�r)r#r$r�test_response_fileno�s.
zBasicTest.test_response_filenocCs4Gdd�dtj�}|d�}td�|_|�dd�dS)Nc@seZdZdd�ZdS)zRBasicTest.test_putrequest_override_domain_validation.<locals>.UnsafeHTTPConnectioncSsdS)Nr)rr�rrr�_validate_path�szaBasicTest.test_putrequest_override_domain_validation.<locals>.UnsafeHTTPConnection._validate_pathN)r#r$r%r4rrrr�UnsafeHTTPConnection�sr5zexample.comr[r^z/)rrNrrOrk)rr5rTrrr�*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>.UnsafeHTTPConnectioncSsdS)Nr)rr�rrr�_validate_host�sz_BasicTest.test_putrequest_override_host_validation.<locals>.UnsafeHTTPConnection._validate_hostN)r#r$r%r7rrrrr5�sr5z
example.com
r[r^rMr)Z	skip_host)rrNrrOrk)rr5rTrrr�(test_putrequest_override_host_validation�s
z2BasicTest.test_putrequest_override_host_validationcCs4Gdd�dtj�}|d�}td�|_|�dd�dS)Nc@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%r9rrrrr5�sr5zexample.comr[r^u/☃)rrNrrOrk)rr5rTrrr�!test_putrequest_override_encoding�s
z+BasicTest.test_putrequest_override_encodingN)2r#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�rrrrrrrrr
rrrrrrrrrrr3r6r8r:rrrrr�s^

	 +


	(
r�c@steZdZdZee�d�d��d�ZdZdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�ExtendedReadTestzOHTTP/1.1 200 OK

hello world!
and now 
for something completely different
fooZhelloNrz�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)Nr^)rg)	rr�rrtrur&�BufferedReader�fprw)rrOrwrrr�setUp�s

zExtendedReadTest.setUpcs�|j}|jj�d�fdd�	}||j_g}x�|�d�}|r�|�t|�d�|��}|�t|�t|��|�|�|��|�t|��}|�	||�n|��}|�
|�|�|�|s*Pq*W|�	d�|�|j
�dS)Nr-cs(�|�}|dkr|d|�S|dd�S)Nrrr)r0r�)�oldpeekrr�mypeek�sz*ExtendedReadTest.test_peek.<locals>.mypeekr�rr)r-)rwr=�peekZ
assertGreaterrCZassertGreaterEqualrorpr.rRr�rHr��lines_expected)rrwr@�allr�Zp2r�r)r?r�	test_peek�s(


zExtendedReadTest.test_peekcCs|j}|�|jj|j�dS)N)rw�_verify_readliner1rB)rrwrrr�
test_readlineszExtendedReadTest.test_readlinecCs`g}xD|d�}|r6|dkr6t|�dkr6|�|�d��|�|�|sPqW|�d�|�|�dS)Nr�sfoo�
r)rCror�rHrRr�)rr1rqrCr�rrrrEs
z!ExtendedReadTest._verify_readlinecs0�j���fdd�}t|�}��|j�j�dS)Ncs��d�}��t|�d�|S)N�)r�assertLessEqualrC)Zres)rwrrrr+s
z&ExtendedReadTest.test_read1.<locals>.r)rw�	ReadlinerrEr1rB)rrZ	readlinerr)rwrr�
test_read1)szExtendedReadTest.test_read1cCs@|j}g}x|��}|sP|�|�qW|�d�|�|j�dS)Nr)rwrrHrRr�rB)rrwrCr
rrr�test_read1_unbounded2sz%ExtendedReadTest.test_read1_unboundedcCsR|j}g}x.|�d�}|sP|�t|�d�|�|�qW|�d�|�|j�dS)Nrr)rwrrIrCrHrRr�rB)rrwrCr
rrr�test_read1_bounded<s
z#ExtendedReadTest.test_read1_boundedcCs|�|j�d�d�dS)Nrr)rRrwr)rrrr�test_read1_0GszExtendedReadTest.test_read1_0cCs |j�d�}|�dt|��dS)Nr)rwrArIrC)rr�rrr�test_peek_0JszExtendedReadTest.test_peek_0)r#r$r%r��findr
rBZ
lines_chunkedr>rDrFrErKrLrMrNrOrrrrr;�s	
	
r;c@seZdZdZdS)�ExtendedReadTestChunkedz�HTTP/1.1 200 OK
Transfer-Encoding: chunked

a
hello worl
3
d!

9
and now 

23
for something completely different

3
foo
0

N)r#r$r%r�rrrrrQOsrQc@seZdZdd�Zdd�ZdS)rJcCs||_d|_dS)Nr)�readfunc�	remainder)rrRrrrriszReadliner.__init__cCs�g}d}|j}y�xP|�d�}|dkr&P|t|�|krB||d}|�|�|��}|sd}PqW|d7}|�|d|��||d�|_d�|�Sd�|�|_�YnXdS)NrrGr-rr)rSrPrCrHrRr�)r�limitr
Zdatalenr.r�rrrr1ms,


zReadliner.readlineN)r#r$r%rr1rrrrrJesrJc@s$eZdZdd�Zdd�Zdd�ZdS)�OfflineTestcCsjdh}ddh}xHtt�D]<}|�d�s||kr0qtt|�}t|dd�dkr|�|�qW|�tj|�dS)N�	responsesZHTTPMessage�
parse_headersr�r$zhttp.client)�dirrrp�getattr�addZassertCountEqual�__all__)rrqZ	blacklistr�Z
module_objectrrr�test_all�s
zOfflineTest.test_allcCs|�tjtjd�dS)Nz	Not Found)rRrrV�	NOT_FOUND)rrrr�test_responses�szOfflineTest.test_responsesc6Cs�ddddddddd	d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6g6}x4|D],}|j|d7��|�tt|��WdQRXqvWdS)8NZCONTINUEZSWITCHING_PROTOCOLSZ
PROCESSINGr�ZCREATEDZACCEPTEDZNON_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_LARGEZINTERNAL_SERVER_ERRORZNOT_IMPLEMENTEDZBAD_GATEWAYZSERVICE_UNAVAILABLEZGATEWAY_TIMEOUTZHTTP_VERSION_NOT_SUPPORTEDZINSUFFICIENT_STORAGEZNOT_EXTENDEDZNETWORK_AUTHENTICATION_REQUIRED)Zconstant)r�ro�hasattrr)rrq�constrrr�test_client_constants�sr
z!OfflineTest.test_client_constantsN)r#r$r%r\r^rarrrrrU�srUc@s@eZdZdd�Zdd�Zdd�Ze�ee	d�d�d	d
��Z
dS)�SourceAddressTestcCs>t�tjtj�|_t�|j�|_t��|_|j�	�d|_
dS)N)r(r)r*r$r�	bind_portrZfind_unused_port�source_portr-rT)rrrrr>�s


zSourceAddressTest.setUpcCs*|jr|j��d|_|j��d|_dS)N)rTrr$)rrrr�tearDown�s


zSourceAddressTest.tearDowncCsBtjt|jd|jfd�|_|j��|�|jj�	�d|j�dS)Nr[)�source_addressr)
rrNr,rrdrTr;rRrOr1)rrrr�testHTTPConnectionSourceAddress�s

z1SourceAddressTest.testHTTPConnectionSourceAddress�HTTPSConnectionz'http.client.HTTPSConnection not definedcCstjt|jd|jfd�|_dS)Nr[)rf)rrhr,rrdrT)rrrr� testHTTPSConnectionSourceAddress�s
z2SourceAddressTest.testHTTPSConnectionSourceAddressN)r#r$r%r>rerg�unittest�skipIfr_rrirrrrrb�s
rbc@s(eZdZdZdd�Zdd�Zdd�ZdS)�TimeoutTestNcCs.t�tjtj�|_t�|j�t_|j��dS)N)	r(r)r*r$rrcrl�PORTr-)rrrrr>�szTimeoutTest.setUpcCs|j��d|_dS)N)r$r)rrrrre�s
zTimeoutTest.tearDownc	Cs�|�t���t�d�zt�ttj�}|�	�Wdt�d�X|�
|j��d�|�
�|�t���t�d�ztjttjdd�}|�	�Wdt�d�X|�
|j��d�|�
�tjttjdd�}|�	�|�
|j��d�|�
�dS)N�)�timeout)�assertIsNoner(ZgetdefaulttimeoutZsetdefaulttimeoutrrNr,rlrmr;rRrOZ
gettimeoutr)rZhttpConnrrr�testTimeoutAttributes*


z TimeoutTest.testTimeoutAttribute)r#r$r%rmr>rerqrrrrrl�srlc@s$eZdZdd�Zdd�Zdd�ZdS)�PersistenceTestcCs�d}x�|D]�\}}}|j||d���d�||�}t|�}|�|j�|�dd�|���"}|�|jdk|�|��WdQRX|�|jdk|�|�|j	d�|�dd�|�|j	|r�dnd�WdQRXq
WdS)	N))z1.0r[F)z1.0zConnection: keep-alive
T)z1.1r[T)z1.1zConnection: close
F)z1.0zConnection: keep-ALIVE
T)z1.1zConnection: cloSE
F)�versionrSz4HTTP/{} 200 OK
{}Content-Length: 12

Dummy body
r^z/open-connectionrz/subsequent-requestr�)
r�rer3rprOrQrrRr.r4)r�testsrsrSZreuserrTrrrr�test_reuse_reconnect's
z$PersistenceTest.test_reuse_reconnectc
Cs�dd�}tjtjf|tff}xr|D]j\}}|j|d��Ptd|�}|�dd�|�||j	�|�
|j�|�dd�|�|j
d�WdQRXq"WdS)	Ncs&t�|���fdd�}|�_t���S)Ncs tj��|�}|dkrt��|S)Nr)r&r'r��ConnectionResetError)�bufferr�)�streamrrr�JszNPersistenceTest.test_disconnected.<locals>.make_reset_reader.<locals>.readinto)r&r'r�r<)rr�r)rxr�make_reset_readerGs
z<PersistenceTest.test_disconnected.<locals>.make_reset_reader)r	rr^z
/eof-responsez
/reconnectr�)r&r'r�RemoteDisconnectedrvr�r3rQr�rrprOrRr4)rryrtZstream_factoryr	rTrrr�test_disconnectedEs


z!PersistenceTest.test_disconnectedcCsVtd�}|jddddid�|�tj|j�|�|j�|�dd�|�|j	d�dS)	NsHTTP/1.1 100 Continue

r^rMZExpectz100-continue)rVz
/reconnectr�)
r3rQr�rrzrrprOrRr4)rrTrrr�test_100_close`szPersistenceTest.test_100_closeN)r#r$r%rur{r|rrrrrr%srrc@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)Nrhzssl support required)r_r�skipTest)rrrrr>os
zHTTPSTest.setUpcCsddlm}|||d�S)Nr)�make_https_server)�certfile)Ztest.ssl_serversr)rr�rrrr�make_serversszHTTPSTest.make_servercCs$tjttjdd�}|�|jd�dS)Nrn)ro)rrhr,rlrmrRro)rr�rrr�test_attributeswszHTTPSTest.test_attributesc
Cslddl}t�d�t�d��Ft�dd�}|�|j��}|�dd�WdQRX|�	|j
jd�WdQRXdS)Nr�networkzself-signed.pythontest.neti�r^rM�CERTIFICATE_VERIFY_FAILED)�sslr�requires�transient_internetrrhr��SSLErrorrQrRr	r�)rr�r��exc_inforrr�test_networked|s
zHTTPSTest.test_networkedc	Cszddl}t�d�t�d��T|��}tjdd|d�}|�dd�|��}|�	�|�
d|�d	��|�	�WdQRXdS)
Nrr�zself-signed.pythontest.neti�)�contextr^rM�nginx�server)r�rr�r�Z_create_unverified_contextrrhrQrrrmrv)rr�r�r�rwrrr�test_networked_noverification�s
z'HTTPSTest.test_networked_noverificationc	Csjt�d�t�d��Lt�dd�}|�dd�|��}|�d�}|��|��|�	d|�WdQRXdS)Nr�zwww.python.orgi�r^rMzcontent-typez	text/html)
rr�r�rrhrQrrvrrm)rr�rwZ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�y(tj
|d|d�}|�dd�|��}WnR|jk
r�}z2t|�}t�d	|�r�t�d
|�d|�d����Wdd}~XYnX|�d
�}|��|��|�d|�WdQRXdS)Nrr�zself-signed.pythontest.netTi�)r�r^rMz(?i)key.too.weakzGot z trying to connect to z). See https://bugs.python.org/issue36816.r�r�)r�rr�r��
SSLContext�PROTOCOL_TLS_CLIENTrRZverify_modeZ
CERT_REQUIRED�check_hostname�load_verify_locations� CERT_selfsigned_pythontestdotnetrrhrQrr�r	�re�searchrjZSkipTestrvrrm)	rr�Zselfsigned_pythontestdotnetr�r�rwZssl_errZssl_err_strZ
server_stringrrr�test_networked_good_cert�s.


z"HTTPSTest.test_networked_good_certc
Cs�ddl}t�d�t�d��`|�|j�}|�t�tj	dd|d�}|�
|j��}|�dd�WdQRX|�
|jjd�WdQRXdS)	Nrr�zself-signed.pythontest.neti�)r�r^rMr�)r�rr�r�r�r�r��CERT_localhostrrhr�r�rQrRr	r�)rr�r�r�r�rrr�test_networked_bad_cert�s

z!HTTPSTest.test_networked_bad_certc	CsXddl}|�t�}t�d|j�}|�|j��}|�dd�WdQRX|�	|j
jd�dS)Nr�	localhostr^rMr�)r�r�r�rrhrr�r�rQrRr	r�)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�r^z/nonexistenti�)r�r�r�r�r�r�rrhrr+rrQrrRr�)rr�r�r�r�rwrrr�test_local_good_hostname�s

z"HTTPSTest.test_local_good_hostnamec	Cs�ddl}|�t�}|�|j�}|�t�tjd|j|d�}|�	|j
��|�dd�WdQRXt�
dtf��tjd|j|dd�}WdQRX|�	|j
��|�dd�WdQRXd	|_t�
dtf��tjd|j|d	d�}WdQRX|�dd
�|��}|��|��|�|jd�d	|_tjd|j|d�}|�dd
�|��}|�|jd�|��|��t�
dtf��tjd|j|dd�}WdQRX|�	|j
��|�dd�WdQRXdS)Nrr�)r�r^rMr[T)r�r�Fz/nonexistenti�)r�r��CERT_fakehostnamer�r�r�rrhrr�ZCertificateErrorrQrZcheck_warnings�DeprecationWarningr�rrrRr�)rr�r�r�r�rwrrr�test_local_bad_hostname�sB




z!HTTPSTest.test_local_bad_hostnamerhz)http.client.HTTPSConnection not availablecCs^xdD]}|�tjtj|�qWx8dD]0\}}}t�|�}|�||j�|�||j�q&WdS)N)zwww.python.org:abczuser:password@www.python.org))z[fe80::207:e9ff:fe9b]:8000zfe80::207:e9ff:fe9bi@)zwww.python.org:443zwww.python.orgi�)zwww.python.org:zwww.python.orgi�)zwww.python.orgzwww.python.orgi�)z[fe80::207:e9ff:fe9b]zfe80::207:e9ff:fe9bi�)z[fe80::207:e9ff:fe9b]:zfe80::207:e9ff:fe9bi�)r�rr�rhrRrr)rr�r�r�r�rrrr�s

zHTTPSTest.test_host_portcCs�ddl}|js|�d�t�dd�}|�|jj�|�|j	�}|�
|j�tjdd|d�}|�|j|�|�
|jj�tjdd|td�}|�|jj�dS)NrzTLS 1.3 support requiredr�i�)r�)r�Z	cert_file)
r�ZHAS_TLSv1_3r~rrhroZ_contextZpost_handshake_authr�r�r�ZassertIsr�)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�rjrkr_rr�r�rrrrr}ms

!	
*r}c@sTeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�RequestBodyTestcCs,t�d�|_td�|j_|_|j|j_dS)Nzexample.comr[)rrNrTrrO)rrrrr>8szRequestBodyTest.setUpcCs(t�|jj�}|��t�|�}||fS)N)r&r'rOr
r1rrW)r�f�messagerrr�get_headers_and_fp=s
z"RequestBodyTest.get_headers_and_fpc
Cs�ddgdfdf}x�|D]�\}}|�|��zt�d�|_td�|j_|_|j�dd|�|��\}}|�d	|�|�d
|�|�	|�
d�d�|�	||���WdQRXqWdS)
Nsfoosbars3
foo
3
bar
0

))sfoosbars3
foo
3
bar
0

zexample.comr[r\z/urlzContent-TypezContent-LengthzTransfer-Encodingr�)r�rrNrTrrOrQr�r�rR�getr.)rr�rUrqrr�rrr�test_list_bodyCs
zRequestBodyTest.test_list_bodycCsL|j�dddddi�|��\}}|�d|�d��|�dt|����dS)Nr\z/urlrUzContent-LengthZ42zcontent-lengthrH)rTrQr�rRr�rCr.)rr�r�rrr�test_manual_content_lengthWs

z*RequestBodyTest.test_manual_content_lengthcCs`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)Nr\z/urlrUz
text/plain�4zcontent-lengthsbody)	rTrQr�rR�get_content_typerp�get_charsetr�r.)rr�r�rrr�test_ascii_body`szRequestBodyTest.test_ascii_bodycCs`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)Nr\z/urlubodyÁz
text/plain�5zcontent-lengthsbody�)	rTrQr�rRr�rpr�r�r.)rr�r�rrr�test_latin1_bodyhsz RequestBodyTest.test_latin1_bodycCs`|j�ddd�|��\}}|�d|���|�|���|�d|�d��|�d|���dS)Nr\z/urlsbody�z
text/plainr�zcontent-length)	rTrQr�rRr�rpr�r�r.)rr�r�rrr�test_bytes_bodypszRequestBodyTest.test_bytes_bodyc	Cs�|�tjtj�ttjd��}|�d�WdQRXttj��r}|j�dd|�|��\}}|�	d|�
��|�|���|�|�
d��|�	d|�
d��|�	d	|���WdQRXdS)
N�wrUr\z/urlz
text/plainzcontent-lengthr�ztransfer-encodings4
body
0

)r+r�unlink�TESTFNr��writerTrQr�rRr�rpr�r�r.)rr�r�rrr�test_text_file_bodyxsz#RequestBodyTest.test_text_file_bodyc	Cs�|�tjtj�ttjd��}|�d�WdQRXttjd��n}|j�dd|�|��\}}|�	d|�
��|�|���|�	d|�
d��|�d	|�|�	d
|���WdQRXdS)N�wbsbody�rr\z/urlz
text/plainr�zTransfer-EncodingzContent-Lengths5
body�
0

)r+rr�r�r�r�rTrQr�rRr�rpr�r�r�r.)rr�r�rrr�test_binary_file_body�sz%RequestBodyTest.test_binary_file_bodyN)r#r$r%r>r�r�r�r�r�r�r�r�rrrrr�5s	r�c@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)rrrtrwru)rrUrOrrrr>�szHTTPResponseTest.setUpcCs6|j�d�}|�|d�|j�dd�}|�|d�dS)Nz	My-Headerzfirst-value, second-valuezsome default)rwrvrR)rrSrrr�test_getting_header�sz$HTTPResponseTest.test_getting_headercCs|j�dd�}|�|d�dS)NzNo-Such-Headerz
default-value)rwrvrR)rrSrrr�3test_getting_nonexistent_header_with_string_default�szDHTTPResponseTest.test_getting_nonexistent_header_with_string_defaultcCs<|j�dddg�}|�|d�|j�dd�}|�|d�dS)NzNo-Such-Header�default�valueszdefault, values)r�r�)rwrvrR)rrSrrr�5test_getting_nonexistent_header_with_iterable_default�szFHTTPResponseTest.test_getting_nonexistent_header_with_iterable_defaultcCs|j�d�}|�|d�dS)NzNo-Such-Header)rwrvrR)rrSrrr�/test_getting_nonexistent_header_without_default�sz@HTTPResponseTest.test_getting_nonexistent_header_without_defaultcCs |jjddd�}|�|d�dS)NzNo-Such-Headerri)r�)rwrvrR)rrSrrr�test_getting_header_defaultint�sz/HTTPResponseTest.test_getting_header_defaultintN)	r#r$r%r>r�r�r�r�r�rrrrr��sr�c@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)rrrNrTr8)r�
response_textrrrr>�szTunnelTests.setUpcCs|j��dS)N)rTr)rrrrre�szTunnelTests.tearDowncsd�fdd�	}|S)Ncst�|d|dd�S)Nrr)rr)r)r!rorf)r�rrr7�sz9TunnelTests._create_connection.<locals>.create_connection)NNr)rr�r7r)r�rr8�szTunnelTests._create_connectioncCs�d}d}ddi}|jj|||d�|j�ddd�|�|jjj|j�|�|jjjtj�|�|jj	|�|�|jj
|�|�|jj|�dS)	Nzdestination.comi�"z
User-Agentz!Mozilla/5.0 (compatible, MSIE 11))rrVr`rMr[)rT�
set_tunnelrQrRrOrrr�	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)Nzdestination.com)rTr;r��RuntimeErrorr�)rrrr�&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)	Nzdestination.comr`rMr[sCONNECT destination.comsHost: destination.com:NonesHost: destination.comsHost: proxy.com)rTr�rQrRrOrrrr�rmr
r�)rrrr�test_connect_with_tunnel�sz$TunnelTests.test_connect_with_tunnelcCsl|j�d�|j�ddd�|�|jjj|j�|�|jjjtj�|�	d|jjj
�|�	d|jjj
�dS)Nzdestination.comr\rMr[sCONNECT destination.comsHost: destination.com)rTr�rQrRrOrrrr�rmr
)rrrr�test_connect_put_request�sz$TunnelTests.test_connect_put_requestc	Cszd}d�|�}|j�d�|�|�|j_|j�d�t���}|j�ddd�WdQRX|���	�}|�
d�|�|�dS)	Nz
X-Dummy: 1zHTTP/1.0 200 OK
{}

rzdestination.comr\rMr[z
header: {})rerTZset_debuglevelr8r�rr�rQr�r�rm)rZexpected_headerr�r�r�rrr�test_tunnel_debuglog�s

z TunnelTests.test_tunnel_debuglogN)r#r$r%r>rer8r�r�r�r�r�rrrrr��s


r��__main__r�)�	verbosity)5r+Zhttprr&rc�osr�r�r(r.rjZTestCaser�r�path�dirnamer��herer�r�r�r�r�r�Zchunk_extensionr�rr�r�r,rr(r'r,rNr3r?r�r�r�r;rQrJrUrbrlrrr}r�r�r�r#�mainrrrr�<module>sh!nS}"Q.HI`#I