AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/python3/dist-packages/twisted/web/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/lib/python3/dist-packages/twisted/web/test/__pycache__/test_newclient.cpython-310.pyc
o

�b6��@s�dZddlmZddlmZddlmZddlmZm	Z	m
Z
mZddlm
Z
mZddlmZmZddlmZdd	lmZdd
lmZddlmZddlmZmZmZmZdd
lm Z ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=ddl>m?Z?ddl@mAZAddlBmCZCmDZDddlEmFZFmGZGGdd�deH�ZIGdd�deH�ZJeAddgi�ZKdd�ZLdd�ZMdd�ZNd d!�ZOd"d#�ZPGd$d%�d%e �ZQGd&d'�d'�ZRGd(d)�d)eRe �ZSGd*d+�d+eRe �ZTGd,d-�d-e �ZUGd.d/�d/�ZVGd0d1�d1�ZWGd2d3�d3e �ZXeeC�Gd4d5�d5��ZYGd6d7�d7e �ZZGd8d9�d9e �Z[Gd:d;�d;e �Z\Gd<d=�d=e �Z]Gd>d?�d?e �Z^d@S)Az&
Tests for L{twisted.web._newclient}.
�)�Optional)�implementer)�verifyObject)�CancelledError�Deferred�fail�succeed)�ConnectionDone�ConnectionLost)�	IConsumer�
IPushProducer)�Protocol)�globalLogPublisher)�LineReceiver)�Failure)�AccumulatingProtocol�EventLoggingObserver�StringTransport� StringTransportWithDisconnection)�TestCase)�BODY�DONE�HEADER�STATUS�UNKNOWN_LENGTH�
BadHeaders�BadResponseVersion�ChunkedEncoder�ConnectionAborted�ExcessWrite�HTTPClientParser�
HTTPParser�LengthEnforcingConsumer�
ParseError�RequestNotSent�TransportProxyProducer�WrongBodyLength�makeStatefulDispatcher)	�HTTP11ClientProtocol�PotentialDataLoss�Request�RequestGenerationFailed�RequestTransmissionFailed�Response�ResponseDone�ResponseFailed�ResponseNeverReceived)�	_DataLoss)�Headers)�
IBodyProducer�	IResponse)�bytesLinearWhitespaceComponents�sanitizedBytesc@�eZdZdZdS)�ArbitraryExceptionze
    A unique, arbitrary exception type which L{twisted.web._newclient} knows
    nothing about.
    N��__name__�
__module__�__qualname__�__doc__�r>r>�A/usr/lib/python3/dist-packages/twisted/web/test/test_newclient.pyr8C�r8c@r7)�AnotherArbitraryExceptionzI
    Similar to L{ArbitraryException} but with a different identity.
    Nr9r>r>r>r?rAJr@rA�host�example.comcs(��fdd�}��||�}|�|�|S)a�
    Assert that the given L{Deferred} fails with the exception given by
    C{mainType} and that the exceptions wrapped by the instance of C{mainType}
    it fails with match the list of exception types given by C{reasonTypes}.

    This is a helper for testing failures of exceptions which subclass
    L{_newclient._WrapperException}.

    @param self: A L{TestCase} instance which will be used to make the
        assertions.

    @param deferred: The L{Deferred} which is expected to fail with
        C{mainType}.

    @param mainType: A L{_newclient._WrapperException} subclass which will be
        trapped on C{deferred}.

    @param reasonTypes: A sequence of exception types which will be trapped on
        the resulting C{mainType} exception instance's C{reasons} sequence.

    @return: A L{Deferred} which fires with the C{mainType} instance
        C{deferred} fails with, or which fails somehow.
    c	sLt|j��D]	\}}|�|�q��t|j�t��d|j�d��d��|S)Nzlen(z	) != len(�))�zip�reasons�trap�assertEqual�len)�err�reason�type��reasonTypes�selfr>r?�cbFailedns�z-assertWrapperExceptionTypes.<locals>.cbFailed)�
assertFailure�addCallback)rO�deferred�mainTyperNrP�dr>rMr?�assertWrapperExceptionTypesUs

rVcC�t||t|�S)zo
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{ResponseFailed}.
    )rVr/�rOrSrNr>r>r?�assertResponseFailed}�rYcCrW)zx
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestGenerationFailed}.
    )rVr+rXr>r>r?�assertRequestGenerationFailed���r[cCrW)zz
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestTransmissionFailed}.
    )rVr,rXr>r>r?�assertRequestTransmissionFailed�r\r]cCstdddt|�S)z�
    Helper function for creating a Response which uses the given transport.
    All of the other parameters to L{Response.__init__} are filled with
    arbitrary values.  Only use this method if you don't care about any of
    them.
    �sHTTP�r_���OK)r-�_boringHeaders)�	transportr>r>r?�justTransportResponse�srdc@�eZdZdZdd�ZdS)�MakeStatefulDispatcherTestsz.
    Tests for L{makeStatefulDispatcher}.
    cCsRGdd�d�}|�}|�|��d�d|_|�|��d�d|_|�t|j�dS)z�
        A method defined with L{makeStatefulDispatcher} invokes a second
        method based on the current state of the object.
        c@s2eZdZdZdd�Zede�Zdd�Zdd�Zd	S)
zCMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo�AcS�dS�Nr>�rOr>r>r?�bar��zGMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo.bar�quuxcS�dS)N�ar>rjr>r>r?�_quux_A�rlzKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_AcSrn)N�br>rjr>r>r?�_quux_B�rlzKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_BN)r:r;r<�_staterkr'rprrr>r>r>r?�Foo�s
rtro�Brq�CN)rHrkrs�assertRaises�RuntimeError)rOrt�statefulr>r>r?�test_functionCalledByState�sz6MakeStatefulDispatcherTests.test_functionCalledByStateN)r:r;r<r=rzr>r>r>r?rf�srfc@sreZdZUdZdZeeed<dd�Zdd�Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�_HTTPParserTestszt
    Base test class for L{HTTPParser} which is responsible for the bulk of
    the task of parsing HTTP bytes.
    N�sepcCs\g}t�}|j|_|�t��|�|jt�|�d|j	�|�|dg�|�|jt
�dS)zj
        L{HTTPParser} calls its C{statusReceived} method when it receives a
        status line.
        �HTTP/1.1 200 OKN)r!�append�statusReceived�makeConnectionrrH�stater�dataReceivedr|r)rO�status�protocolr>r>r?�test_statusCallback�sz$_HTTPParserTests.test_statusCallbackcCs6i}t�}|j|_|�t��|�d|j�||fS)Nr})r!�__setitem__�headerReceivedr�rr�r|�rO�headerr�r>r>r?�_headerTestSetup�sz!_HTTPParserTests._headerTestSetupcCsJ|��\}}|�d|j�|�|j�|�|ddi�|�|jt�dS)ze
        L{HTTPParser} calls its C{headerReceived} method when it receives a
        header.
        s	X-Foo:bar�X-Foo�barN�r�r�r|rHr�rr�r>r>r?�test_headerCallback�s
z$_HTTPParserTests.test_headerCallbackcCsj|��\}}|�d|j�|�d|j�|�d|j�|�|j�|�|ddi�|�|jt�dS)z�
        If a header is split over multiple lines, L{HTTPParser} calls
        C{headerReceived} with the entire value once it is received.
        �
X-Foo: bars bazs	quuxr�sbar baz	quuxNr�r�r>r>r?�test_continuedHeaderCallback�sz-_HTTPParserTests.test_continuedHeaderCallbackcCsZ|��\}}|j�gd��}|�d|�|�d|�|�|j�|�|ddd��dS)z�
        Leading and trailing linear whitespace is stripped from the header
        value passed to the C{headerReceived} callback.
        )s 	 s bar 	s 	�sX-Bar:sX-Foo:r�)r�sX-BarN)r�r|�joinr�rH)rOr�r��valuer>r>r?�test_fieldContentWhitespace�sz,_HTTPParserTests.test_fieldContentWhitespacecsPg�|��\}���fdd�}|�_��|j�|��tg�|��jt�dS)zg
        After the last header is received, L{HTTPParser} calls
        C{allHeadersReceived}.
        cs���j�t�_dSri)r~r�rr>��calledr�r>r?�allHeadersReceiveds
zD_HTTPParserTests.test_allHeadersCallback.<locals>.allHeadersReceivedN)r�r�r�r|rHrr�r)rOr�r�r>r�r?�test_allHeadersCallbacksz(_HTTPParserTests.test_allHeadersCallbackcCs6|��\}}|�|j�|�|i�|�|jt�dS)zp
        If there are no headers in the message, L{HTTPParser} does not call
        C{headerReceived}.
        Nr�r�r>r>r?�test_noHeaderCallbacksz&_HTTPParserTests.test_noHeaderCallbackcCsvt�}|�t��|�d|j�|�d|j�|�d|j�|�|j�dddgfg}|�|t|j����dS)zc
        All headers received by L{HTTPParser} are added to
        L{HTTPParser.headers}.
        r}r��
X-Foo: bazr�r��bazN)	r!r�rr�r|rH�list�headers�getAllRawHeaders)rOr��expectedr>r>r?�test_headersSavedOnResponse#sz,_HTTPParserTests.test_headersSavedOnResponsecCsFt�}gd�}|D]}|�|�|�d|f�q	|�|�d�d�dS)z�
        L{HTTPParser.isConnectionControlHeader} returns C{True} for headers
        which are always connection control headers (similar to "hop-by-hop"
        headers from RFC 2616 section 13.5.1) and C{False} for other headers.
        )�content-length�
connections
keep-alivestestrailersstransfer-encodingsupgradesproxy-connectionz:Expecting %r to be a connection control header, but wasn'tsdatez`Expecting the arbitrarily selected 'date' header to not be a connection control header, but was.N)r!�
assertTrue�isConnectionControlHeader�assertFalse)rOr��connHeaderNamesr�r>r>r?�test_connectionControlHeaders1s���z._HTTPParserTests.test_connectionControlHeaderscCs4t�}|�t��|�t��|�t|jt��dS)zi
        L{HTTPParser.switchToBodyMode} raises L{RuntimeError} if called more
        than once.
        N)r!r�r�switchToBodyMode�objectrwrx�rOr�r>r>r?�test_switchToBodyModeOsz&_HTTPParserTests.test_switchToBodyMode)r:r;r<r=r|r�bytes�__annotations__r�r�r�r�r�r�r�r�r�r�r>r>r>r?r{�s



r{c@�eZdZdZdZdS)�$HTTPParserRFCComplaintDelimeterTestsz<
    L{_HTTPParserTests} using standard CR LF newlines.
    �
N�r:r;r<r=r|r>r>r>r?r�Z�r�c@r�)�'HTTPParserNonRFCComplaintDelimeterTestsz5
    L{_HTTPParserTests} using bare LF newlines.
    �
Nr�r>r>r>r?r�br�r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�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.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z d<S)=�HTTPClientParserTestszd
    Tests for L{HTTPClientParser} which is responsible for parsing HTTP
    response messages.
    cCs tdd�}|�|�d�d�dS)zj
        L{HTTPClientParser.parseVersion} parses a status line into its three
        components.
        Ns	CANDY/7.2)sCANDY��)r rH�parseVersionr�r>r>r?�test_parseVersionps
z'HTTPClientParserTests.test_parseVersioncsXtdd�}t�|j����fdd�}|d�|d�|d�|d�|d�|d	�dS)
zr
        L{HTTPClientParser.parseVersion} raises L{ValueError} when passed an
        unparsable version.
        Ncs ����|�}��|j|�dSri)rwrH�data��s�exc��e�frOr>r?�checkParsing�sz@HTTPClientParserTests.test_parseBadVersion.<locals>.checkParsing�foosfoo/bar/bazsfoo/sfoo/..sfoo/a.bs	foo/-1.-1)r rr�)rOr�r�r>r�r?�test_parseBadVersionxs
z*HTTPClientParserTests.test_parseBadVersioncC�btddtd�}t|d�}|�t��|�d�|�|jjd�|�|jj	d�|�|jj
d�dS)z�
        L{HTTPClientParser.statusReceived} parses the version, code, and phrase
        from the status line and stores them on the response object.
        �GET�/N�HTTP/1.1 200 OK
r^r`ra�r*rbr r�rr�rH�response�version�code�phrase�rO�requestr�r>r>r?�test_responseStatusParsing�s

z0HTTPClientParserTests.test_responseStatusParsingcCr�)z�
        L{HTTPClientParser.statusReceived} can parse a status line without a
        phrase (though such lines are a violation of RFC 7230, section 3.1.2;
        nevertheless some broken servers omit the phrase).
        r�r�NsHTTP/1.1 200
r^r`r�r�r�r>r>r?� test_responseStatusWithoutPhrase�s

z6HTTPClientParserTests.test_responseStatusWithoutPhrasecs,tdd����fdd�}|d�|d�dS)z�
        L{HTTPClientParser.statusReceived} raises L{ParseError} if it is called
        with a status line which cannot be parsed.
        Ncs"��t�j|�}��|j|�dSri)rwr#rrHr�r��r�rOr>r?r���zBHTTPClientParserTests.test_badResponseStatus.<locals>.checkParsingr�sHTTP/1.1 bar OK)r )rOr�r>r�r?�test_badResponseStatus�s
z,HTTPClientParserTests.test_badResponseStatusc	s�i}g}g}g�t||j�}|j|_t�}|�|�|�|�|j|j_�fdd�|j_	|�|�|�
|jd�|�
|jt
�|�
|g�|�
|dg�|�
�dg�|�
|jjd�|S)a0
        Assert that L{HTTPClientParser} parses the given C{response} to
        C{request}, resulting in a response with no body and no extra bytes and
        leaving the transport in the producing state.

        @param request: A L{Request} instance which might have caused a server
            to return the given response.
        @param status: A string giving the status line of the response to be
            parsed.
        @param response: A string giving the response to be parsed.

        @return: A C{dict} of headers from the response.
        c�
��d�S�NT�r~r>��bodyDataFinishedr>r?�<lambda>��
z3HTTPClientParserTests._noBodyTest.<locals>.<lambda>�	producingr�Tr)r r~r�r�rr�r�r��_bodyDataReceived�_bodyDataFinishedrH�
producerStater�r�length)	rOr�r�r�r��finished�bodyr�rcr>r�r?�_noBodyTest�s&



z!HTTPClientParserTests._noBodyTestcCs8tddtd�}d}d}|�|||�}|�|ddi�dS)z�
        If the response is to a HEAD request, no body is expected, the body
        callback is not invoked, and the I{Content-Length} header is passed to
        the header callback.
        �HEADr�Nr�sContent-Length: 10

sContent-Lengths10)r*rbr�rH)rOr�r�r�r�r>r>r?�test_headResponse�s
z'HTTPClientParserTests.test_headResponsecC�(tddtd�}d}d}|�|||�dS)z�
        If the response code is I{NO CONTENT} (204), no body is expected and
        the body callback is not invoked.
        r�r�NsHTTP/1.1 204 NO CONTENT
r��r*rbr��rOr�r�r�r>r>r?�test_noContentResponse��z,HTTPClientParserTests.test_noContentResponsecCr�)z�
        If the response code is I{NOT MODIFIED} (304), no body is expected and
        the body callback is not invoked.
        r�r�NsHTTP/1.1 304 NOT MODIFIED
r�r�r�r>r>r?�test_notModifiedResponse�r�z.HTTPClientParserTests.test_notModifiedResponsecCs�ttddtd�dd��}|�t��|�d�|�d�|�d�|�|jti��|�|j	j
td	d
gi��|�|j	jt
�dS)zq
        The response headers are added to the response object's C{headers}
        L{Headers} instance.
        r�r�NcSrhrir>��restr>r>r?r��z<HTTPClientParserTests.test_responseHeaders.<locals>.<lambda>r�sX-Foo: bar
r��x-foor��r r*rbr�rr�rH�connHeadersr2r�r��assertIdenticalr�rr�r>r>r?�test_responseHeaderss�


z*HTTPClientParserTests.test_responseHeaderscCs�ttddtd�dd��}|�t��|�d�|�d�|�d�|�d	�|�|jti��|�|j	j
td
dgi��|�|j	jt
�dS)z�
        The multi-line response headers are folded and added to the response
        object's C{headers} L{Headers} instance.
        r�r�NcSrhrir>r�r>r>r?r�r�zEHTTPClientParserTests.test_responseHeadersMultiline.<locals>.<lambda>r�sX-Multiline: a
s    b
r�sx-multilinesa    br�r�r>r>r?�test_responseHeadersMultilines�



�z3HTTPClientParserTests.test_responseHeadersMultilinecCs�ttddtd�dd��}|�t��|�d�|�d�|�d�|�d	�|�|jjt	i��|�|j
t	d
gdgd���|�|jjd
�dS)zv
        The connection control headers are added to the parser's C{connHeaders}
        L{Headers} instance.
        r�r�NcSrhrir>r�r>r>r?r�)r�z>HTTPClientParserTests.test_connectionHeaders.<locals>.<lambda>r��Content-Length: 123
sConnection: close
r��123sclose)r�r��{�r r*rbr�rr�rHr�r�r2r�r�r�r>r>r?�test_connectionHeaders#s�



�z,HTTPClientParserTests.test_connectionHeaderscCs�ttddtd�dd��}|�t��|�d�|�d�|�d�|�|jjt	d	d
gi��|�|j
t	i��|�|jjd�dS)z�
        If a HEAD request is made, the I{Content-Length} header in the response
        is added to the response headers, not the connection control headers.
        r�r�NcSrhrir>r�r>r>r?r�=r�zRHTTPClientParserTests.test_headResponseContentLengthEntityHeader.<locals>.<lambda>r�r�r�r�r�rr�r�r>r>r?�*test_headResponseContentLengthEntityHeader7s�


�z@HTTPClientParserTests.test_headResponseContentLengthEntityHeadercCs�g}ttddtd�|j�}t�}|�|�|�d�g}|j|j_|�d�|�d�|�	|j
d�|�	|jt�|�d�|�	|dg�|�	|jt�|�d	�|�	|dd	g�|�	|jt
�|�	|d
g�dS)z�
        If a response includes a body with a length given by the
        I{Content-Length} header, the bytes which make up the body are passed
        to the C{_bodyDataReceived} callback on the L{HTTPParser}.
        r�r�Nr�sContent-Length: 10
r��pausedsxxxxxxsyyyyr�)r r*rbr~rr�r�r�r�rHr�r�rr�rOr�r�rcr�r>r>r?�test_contentLengthIs(�






z(HTTPClientParserTests.test_contentLengthcCs�g}ttddtd�|j�}|�t��|�d�g}|j|j_|�d�|�d�|�	|j
t�|�	|g�|�	|dg�|�	|jjd�dS)	z�
        If a response includes a I{Content-Length} header indicating zero bytes
        in the response, L{Response.length} is set accordingly and no data is
        delivered to L{Response._bodyDataReceived}.
        r�r�Nr��Content-Length: 0
r�r�r)
r r*rbr~r�rr�r�r�rHr�rr��rOr�r�r�r>r>r?�test_zeroContentLengthhs�



z,HTTPClientParserTests.test_zeroContentLengthcCs4ttddtd�d�}|�t��|�t|jd�dS)z�
        If a response includes multiple I{Content-Length} headers,
        L{HTTPClientParser.dataReceived} raises L{ValueError} to indicate that
        the response is invalid and the transport is now unusable.
        r�r�Ns9HTTP/1.1 200 OK
Content-Length: 1
Content-Length: 2

)r r*rbr�rrw�
ValueErrorr�r�r>r>r?�!test_multipleContentLengthHeaders�s�z7HTTPClientParserTests.test_multipleContentLengthHeaderscC�dg}ttddtd�|j�}|�t��|�d�|�d�|�d�|�|jt	�|�|dg�dS)zy
        If extra bytes are received past the end of a response, they are passed
        to the finish callback.
        r�r�Nr�r��
Here is another thing!�Here is another thing!�
r r*rbr~r�rr�rHr�r�rOr�r�r>r>r?�test_extraBytesPassedBack���


z/HTTPClientParserTests.test_extraBytesPassedBackcCr)z�
        If extra bytes are received past the end of the headers of a response
        to a HEAD request, they are passed to the finish callback.
        r�r�Nr�sContent-Length: 12
rrrrr>r>r?�test_extraBytesPassedBackHEAD�rz3HTTPClientParserTests.test_extraBytesPassedBackHEADcCs�g}ttddtd�|j�}|�t��|�d�g}|j|j_|�d�|�d�|�	|g�|�
|jjt�|�d�|�	|dg�|�d	�|�	|dd
g�|�d�|�	|dg�dS)
z�
        If the response headers indicate the response body is encoded with the
        I{chunked} transfer encoding, the body is decoded according to that
        transfer encoding before being passed to L{Response._bodyDataReceived}.
        r�r�Nr�sTransfer-Encoding: chunked
r�s3
a�asbc
sbcs
0

extrasextra)
r r*rbr~r�rr�r�r�rHr�r�rr�r>r>r?�test_chunkedResponseBody�s$�






z.HTTPClientParserTests.test_chunkedResponseBodycCs�g}ttddtd�|j�}t�}|�|�|�d�g}|j|j_|�d�|�d�|�d�|�	|ddg�|�
td��|�	|d	g�dS)
z�
        If a response does not include a I{Transfer-Encoding} or a
        I{Content-Length}, the end of response body is indicated by the
        connection being closed.
        r�r�Nr�r�r�r�zsimulated end of connectionr�)r r*rbr~rr�r�r�r�rH�connectionLostr	r�r>r>r?�test_unknownContentLength�s�





z/HTTPClientParserTests.test_unknownContentLengthcCslg}ttddtd�|j�}t�}|�|�|�d�g}|j|j_|�d�|�	|dg�|�	|dg�dS)z�
        According to RFC 2616, section 4.4, point 3, if I{Content-Length} and
        I{Transfer-Encoding: chunked} are present, I{Content-Length} MUST be
        ignored
        r�r�Nr�s@Content-Length: 102
Transfer-Encoding: chunked

3
abc
0

�abcr�)
r r*rbr~rr�r�r�r�rHr�r>r>r?�%test_contentLengthAndTransferEncoding�s�


�
z;HTTPClientParserTests.test_contentLengthAndTransferEncodingcCsHt�}ttddtd�d�}|�|�|j}|�tt���t	||tg�S)z�
        If L{HTTPClientParser.connectionLost} is called before the headers are
        finished, the C{_responseDeferred} is fired with the L{Failure} passed
        to C{connectionLost}.
        r�r�N)
rr r*rbr��_responseDeferredrrr8rY)rOrcr��responseDeferredr>r>r?�test_connectionLostBeforeBodys
z3HTTPClientParserTests.test_connectionLostBeforeBodycCs�t�|t�}t�}ttddtd�d�}|�|�g}|j�	|j
�|�d�|d}d
dd�}||_|�
d�|�dt|��|d}|d	}|�|jt�|�t�dS)z�
        If one of the L{Response} methods called by
        L{HTTPClientParser.connectionLost} raises an exception, the exception
        is logged and not re-raised.
        r�r�N�&HTTP/1.1 200 OK
Content-Length: 1

rcS�t��ri�r8)rJr>r>r?�fakeBodyDataFinished0�zPHTTPClientParserTests.test_connectionLostWithError.<locals>.fakeBodyDataFinishedr_�log_failureri)r�createWithCleanuprrr r*rbr�rrRr~r�r�r�assertEqualsrI�assertIsInstancer�r8�flushLoggedErrors)rO�logObserverrcr�r�r�eventr�r>r>r?�test_connectionLostWithErrors 



z2HTTPClientParserTests.test_connectionLostWithErrorcCsBttddtd�dd��}|j}|�t��|�t��|�|t	�S)z�
        If no response at all was received and the connection is lost, the
        resulting error is L{ResponseNeverReceived}.
        r�r�NcSrhrir>��ignr>r>r?r�Br�z<HTTPClientParserTests.test_noResponseAtAll.<locals>.<lambda>)
r r*rbrr�rrr
rQr0�rOr�rUr>r>r?�test_noResponseAtAll<s�z*HTTPClientParserTests.test_noResponseAtAllcCsVttddtd�dd��}|j}|�t��|�d�|�t��|�	|t
��|jt
�S)z�
        If a partial response was received and the connection is lost, the
        resulting error is L{ResponseFailed}, but not
        L{ResponseNeverReceived}.
        r�r�NcSrhrir>r r>r>r?r�Qr�zBHTTPClientParserTests.test_someResponseButNotAll.<locals>.<lambda>�2)
r r*rbrr�rr�rr
rQr/rRrr"r>r>r?�test_someResponseButNotAllJs�
�z0HTTPClientParserTests.test_someResponseButNotAllcCs�d}ttddtd�dd��}|�t��|�|�|�t|dd�du�|�|j	t
�|�tt|j
����d�|�tt|j����d�|�|j�dS)	zy
        If a response in the 1XX range is received it just gets swallowed and
        the parser resets itself.
        �HTTP/1.1 103 Early Hints
Server: socketserver/1.0.0
Link: </other/styles.css>; rel=preload; as=style
Link: </other/action.js>; rel=preload; as=script

r�r�NcSrhrir>r r>r>r?r�jr�zCHTTPClientParserTests.test_1XXResponseIsSwallowed.<locals>.<lambda>r�r)r r*rbr�rr�r��getattrrHr�rrIr�r�r�r��_everReceivedData)rO�sample103Responser�r>r>r?�test_1XXResponseIsSwallowed\s��
z1HTTPClientParserTests.test_1XXResponseIsSwallowedcCs�d}d}ttddtd�dd��}|�t��|�||�|�|jjd�|�|jj	t
i��|�|jt
d	d
gi��|�|jjd�dS)z�
        When a 1XX response is swallowed, the final response that follows it is
        the only one that gets sent to the application.
        r&�(HTTP/1.1 200 OK
Content-Length: 123

r�r�NcSrhrir>r r>r>r?r��r�zUHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinal.<locals>.<lambda>r`r�r�r��
r r*rbr�rr�rHr�r�r�r2r�r��rOr)�following200Responser�r>r>r?�-test_1XXFollowedByFinalResponseOnlyEmitsFinalvs��zCHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinalcCs�d}d}ttddtd�dd��}|�t��|�||||�|�|jjd�|�|jj	t
i��|�|jt
d	d
gi��|�|jjd�dS)zp
        It is acceptable for multiple 1XX responses to come through, all of
        which get ignored.
        r&r+r�r�NcSrhrir>r r>r>r?r��r�zKHTTPClientParserTests.test_multiple1XXResponsesAreIgnored.<locals>.<lambda>r`r�r�r�r,r-r>r>r?�#test_multiple1XXResponsesAreIgnored�s(������z9HTTPClientParserTests.test_multiple1XXResponsesAreIgnoredcCszt�|t�}d}ttddtd�dd��}|�t��|�|�|�	dt
|��|d}|�	|d	d
�|�	|dd�dS)
zF
        When a 1XX response is ignored, Twisted emits a log.
        r&r�r�NcSrhrir>r r>r>r?r��r�zHHTTPClientParserTests.test_ignored1XXResponseCausesLog.<locals>.<lambda>r_r�
log_formatz#Ignoring unexpected {code} responser��g)rrrr r*rbr�rr�rrI)rOrr)r�rr>r>r?� test_ignored1XXResponseCausesLog�s��
z6HTTPClientParserTests.test_ignored1XXResponseCausesLogN)!r:r;r<r=r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr	rr
rrrr#r%r*r/r0r3r>r>r>r?r�js>
$

%r�c@s,eZdZdZdZdZdZdd�Zdd�ZdS)	�SlowRequesta�
    L{SlowRequest} is a fake implementation of L{Request} which is easily
    controlled externally (for example, by code in a test method).

    @ivar stopped: A flag indicating whether C{stopWriting} has been called.

    @ivar finished: After C{writeTo} is called, a L{Deferred} which was
        returned by that method.  L{SlowRequest} will never fire this
        L{Deferred}.
    r�FcCst�|_|jSri)rr��rOrcr>r>r?�writeTo�szSlowRequest.writeTocC�
d|_dSr���stoppedrjr>r>r?�stopWriting��
zSlowRequest.stopWritingN)	r:r;r<r=�methodr9�
persistentr6r:r>r>r>r?r4�sr4c@seZdZdZdZdd�ZdS)�
SimpleRequesta
    L{SimpleRequest} is a fake implementation of L{Request} which writes a
    short, fixed string to the transport passed to its C{writeTo} method and
    returns a succeeded L{Deferred}.  This vaguely emulates the behavior of a
    L{Request} with no body producer.
    FcCs|�d�td�S)N�
SOME BYTES)�writerr5r>r>r?r6�s
zSimpleRequest.writeToN)r:r;r<r=r=r6r>r>r>r?r>�sr>c@s*eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dIdd�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&dS)J�HTTP11ClientProtocolTestsz]
    Tests for the HTTP 1.1 client protocol implementation,
    L{HTTP11ClientProtocol}.
    cCs"t�|_t�|_|j�|j�dS)zR
        Create an L{HTTP11ClientProtocol} connected to a fake transport.
        N)rrcr(r�r�rjr>r>r?�setUp�szHTTP11ClientProtocolTests.setUpcCs$|j�t��|�|j��d�dS)z�
        L{HTTP11ClientProtocol.request} accepts a L{Request} and calls its
        C{writeTo} method with its own transport.
        r?N)r�r�r>rHrcr�rjr>r>r?�test_request�sz&HTTP11ClientProtocolTests.test_requestcs>�j�t���fdd�}���j�t��t�}|�|�|S)z�
        The second time L{HTTP11ClientProtocol.request} is called, it returns a
        L{Deferred} which immediately fires with a L{Failure} wrapping a
        L{RequestNotSent} exception.
        c����j��d�dS�Nr��rHrcr���ignoredrjr>r?�	cbNotSent�z?HTTP11ClientProtocolTests.test_secondRequest.<locals>.cbNotSent)r�r�r4rQr>r$rR�rOrIrUr>rjr?�test_secondRequests

z,HTTP11ClientProtocolTests.test_secondRequestcsD�j�ttd����fdd�}���j�t��t�}|�|�|S)z�
        L{HTTP11ClientProtocol.request} returns a L{Deferred} which immediately
        fires with a L{Failure} wrapping a L{RequestNotSent} if called after
        the protocol has been disconnected.
        z
sad transportcrDrErFrGrjr>r?rIrJzLHTTP11ClientProtocolTests.test_requestAfterConnectionLost.<locals>.cbNotSent)	r�rrr	rQr�r>r$rRrKr>rjr?�test_requestAfterConnectionLosts

z9HTTP11ClientProtocolTests.test_requestAfterConnectionLostcsDGdd�d�}�j�|��}�fdd�}t�|tg�}|�|�|S)a%
        If the L{Deferred} returned by L{Request.writeTo} fires with a
        L{Failure}, L{HTTP11ClientProtocol.request} disconnects its transport
        and returns a L{Deferred} which fires with a L{Failure} of
        L{RequestGenerationFailed} wrapping the underlying failure.
        c@re)zCHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequestFcSs
tt��Sri)rr8r5r>r>r?r6,r;zKHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequest.writeToN�r:r;r<r=r6r>r>r>r?�
BrokenRequest)�rOcs&���jj��j�ttd���dS)Nzyou asked for it)r�rc�
disconnectingr�rrr	rGrjr>r?rP1sz>HTTP11ClientProtocolTests.test_failedWriteTo.<locals>.cbFailed)r�r�r[r8rR)rOrOrUrPr>rjr?�test_failedWriteTo!s
z,HTTP11ClientProtocolTests.test_failedWriteTocCs*Gdd�d�}|j�|��}t||tg�S)z�
        If L{Request.writeTo} raises an exception,
        L{HTTP11ClientProtocol.request} returns a L{Deferred} which fires with
        a L{Failure} of L{RequestGenerationFailed} wrapping that exception.
        c@re)zMHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequestFcSrrirr5r>r>r?r6ErzUHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequest.writeToNrNr>r>r>r?rOBrPrO)r�r�r[r8)rOrOrUr>r>r?�test_synchronousWriteToError;sz6HTTP11ClientProtocolTests.test_synchronousWriteToErrorNcCs�t�}|j�|�}t||tg�}|�|j�|j�tt���|�	|j�|dkr1|j
�d�|S|dkrM|j
�tt
���|�t
�}|�t|�d�|S	|S)a2
        If L{HTTP11ClientProtocol}'s transport is disconnected before the
        L{Deferred} returned by L{Request.writeTo} fires, the L{Deferred}
        returned by L{HTTP11ClientProtocol.request} fires with a L{Failure} of
        L{RequestTransmissionFailed} wrapping the underlying failure.
        �callbackN�errbackr_)r4r�r�r]r8r�r9rrr�r�rTrUrArrHrI)rO�moder�rU�errorsr>r>r?�*test_connectionLostDuringRequestGenerationKs �
�zDHTTP11ClientProtocolTests.test_connectionLostDuringRequestGenerationcC�
|�d�S)z�
        If the request passed to L{HTTP11ClientProtocol} finishes generation
        successfully after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        rT�rXrjr>r>r?�+test_connectionLostBeforeGenerationFinishedk�
zEHTTP11ClientProtocolTests.test_connectionLostBeforeGenerationFinishedcCrY)z�
        If the request passed to L{HTTP11ClientProtocol} finished generation
        with an error after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        rUrZrjr>r>r?�)test_connectionLostBeforeGenerationFailedsr\zCHTTP11ClientProtocolTests.test_connectionLostBeforeGenerationFailedcs*t��t����fdd�}��d��|�S)a	
        If the request passed to L{HTTP11ClientProtocol} finished generation
        with an error after the L{HTTP11ClientProtocol}'s connection has been
        lost, an error is logged that gives a non-confusing hint to user on what
        went wrong.
        csH��dt����d}��d|���|dd���|dd�dS)Nr_rrr1zJError writing request, but not in valid state to finalize request: {state}r��CONNECTION_LOST)rrI�assertInrH)�ignorer�rrOr>r?�check�s�znHTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse.<locals>.checkrU)rrrrXrR)rOrbr>rar?�Etest_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse{s

�z_HTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfusec�<�j�tddtd��}�fdd�}|�|��j�d�|S)z�
        When a response is delivered to L{HTTP11ClientProtocol}, the
        L{Deferred} previously returned by the C{request} method is called back
        with a L{Response} instance and the connection is closed.
        r�r�Ncs@��|jd���|jt�����jj����jjd�dS)Nr`�	QUIESCENT)	rHr�r�r2r�rcrQr�r��r�rjr>r?�	cbRequest�szIHTTP11ClientProtocolTests.test_receiveSimplestResponse.<locals>.cbRequests9HTTP/1.1 200 OK
Content-Length: 0
Connection: close

�r�r�r*rbrRr��rOrUrgr>rjr?�test_receiveSimplestResponse�s
�z6HTTP11ClientProtocolTests.test_receiveSimplestResponsecrd)z�
        The headers included in a response delivered to L{HTTP11ClientProtocol}
        are included on the L{Response} instance passed to the callback
        returned by the C{request} method.
        r�r�Ncs"tdddgi�}��|j|�dS)Nr�r�r�)r2rHr�)r�r�rjr>r?rg�r�zHHTTP11ClientProtocolTests.test_receiveResponseHeaders.<locals>.cbRequests+HTTP/1.1 200 OK
X-Foo: bar
X-Foo: baz

rhrir>rjr?�test_receiveResponseHeaders�s
�z5HTTP11ClientProtocolTests.test_receiveResponseHeaderscspg�t��t�j������t������}��d�����fdd�}|�|���fdd�}|�|�|S)a[
        If response bytes are delivered to L{HTTP11ClientProtocol} before the
        L{Deferred} returned by L{Request.writeTo} fires, those response bytes
        are parsed as part of the response.

        The connection is also closed, because we're in a confusing state, and
        therefore the C{quiescentCallback} isn't called.
        s8HTTP/1.1 200 OK
X-Foo: bar
Content-Length: 6

foobarcsVt��t�}�_�������jd����j����g�|���fdd��S)N�%TRANSMITTING_AFTER_RECEIVING_RESPONSEc�
��jfSri�r�r ��pr�r>r?r��r�zoHTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)	rr�closedDeferred�deliverBodyrHr�r�rQrR�r��whenFinished)r��quiescentResultrOrcror?�
cbResponse�s
z]HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponsecsf|\}}��|jd���|jd���|jd���|jtddgi����|d��j�d�dS)Nr^r`rar�r��foobar)rHr�r�r�r�r2r�rT)�resultr�r��r�rOr>r?�
cbAllResponse�sz`HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)rr(r~r�r4r�r�rR�rOrUrvrzr>)r�rur�rOrcr?�/test_receiveResponseBeforeRequestGenerationDone�s	


�
	
zIHTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDonecCsZt�}t�}||_|�|�dtj}|�tddtd��}|�	d|d�t
||tg�S)zy
        The connection is closed when the server respond with a header which
        is above the maximum line.
        r
r�r�NsHTTP/1.1 200 OK
X-Foo: s
X-Ignored: ignored

)rr(r�r�r�
MAX_LENGTHr�r*rbr�rYr	)rOrcr��longLinerUr>r>r?�"test_receiveResponseHeadersTooLong�s

���z<HTTP11ClientProtocolTests.test_receiveResponseHeadersTooLongcsLt���j���}�j�d�dd�}|�|���fdd�}|�|�|S)z�
        If response bytes are delivered to L{HTTP11ClientProtocol} before the
        request completes, calling C{connectionLost} on the protocol will
        result in protocol being moved to C{'CONNECTION_LOST'} state.
        s8HTTP/1.1 400 BAD REQUEST
Content-Length: 9

tisk tiskcs0t��t�}�_����|���fdd��S)Ncrmrirnr ror>r?r�r�z�HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)rrrqrrrRrsr>ror?rvs
zrHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponsecs2�j�d��j�tt������jjd�dS)Nr^)r�rTr�rrr8rHrs)r`ryr>r?rzszuHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)r4r�r�r�rRr{r>ryr?�Dtest_connectionLostAfterReceivingResponseBeforeRequestGenerationDones�

z^HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDonecs�t��t�}�_�j�tddtd��}�j�d�g}|�|j	���
|g��j�d�|d}|����j�d��j�d���fd	d
�}|�|�|S)z�
        The C{deliverBody} method of the response object with which the
        L{Deferred} returned by L{HTTP11ClientProtocol.request} fires can be
        used to get the body of the response.
        r�r�Ns%HTTP/1.1 200 OK
Content-Length: 6

r�rr�r�cs���jd��j�t�dS)Nrw)rHr��closedReasonrGr.rGr�r>r?rzJszIHTTP11ClientProtocolTests.test_receiveResponseBody.<locals>.cbAllResponse)rrrqr�r�r*rbr�rRr~rHrr)rOrt�requestDeferredrxr�rzr>r�r?�test_receiveResponseBody(s&��

z2HTTP11ClientProtocolTests.test_receiveResponseBodycCs�|j�tddtd��}|j�d�g}|�|j�|d}t�}|�|�|j�d�|j�d�|�	|j
d�|j�tt
d	���|j�t�dS)
z�
        If the length of the response body is unknown, the protocol passed to
        the response's C{deliverBody} method has its C{connectionLost}
        method called with a L{Failure} wrapping a L{PotentialDataLoss}
        exception.
        r�r�NsHTTP/1.1 200 OK

rr�r�rwz low-level transport disconnected)r�r�r*rbr�rRr~rrrrHr�rrr	r�rGr)�rOr�rxr�r�r>r>r?�Etest_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknownQs �

�z_HTTP11ClientProtocolTests.test_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknowncCs�|j�tddtd��}|j�d�g}|�|j�|d}t�}|�|�|j�d�|j�d�|�	|j
d�|j�tt
���t|t|j�t
tg�S)	a
        If the final chunk has not been received when the connection is lost
        (for any reason), the protocol passed to C{deliverBody} has its
        C{connectionLost} method called with a L{Failure} wrapping the
        exception for that reason.
        r�r�Ns/HTTP/1.1 200 OK
Transfer-Encoding: chunked

r�3
foo
s3
bar
rw)r�r�r*rbr�rRr~rrrrHr�rrr8rYrr�r1r�r>r>r?�4test_chunkedResponseBodyUnfinishedWhenConnectionLostns$��
�zNHTTP11ClientProtocolTests.test_chunkedResponseBodyUnfinishedWhenConnectionLostcsJ�j�tddtd��}�j�d�t�|tg�}�fdd�}|�|�|S)a!
        If the parser L{HTTP11ClientProtocol} delivers bytes to in
        C{dataReceived} raises an exception, the exception is wrapped in a
        L{Failure} and passed to the parser's C{connectionLost} and then the
        L{HTTP11ClientProtocol}'s transport is disconnected.
        r�r�Nsunparseable garbage goes here
cs<���jj���|jdjjd��j�t	t
d���dS)Nrsunparseable garbage goes herez
it is done)r�rcrQrHrFr�r�r�rrr	�r�rjr>r?rP�s
�zLHTTP11ClientProtocolTests.test_parserDataReceivedException.<locals>.cbFailed)r�r�r*rbr�rYr#rR)rOr�rUrPr>rjr?� test_parserDataReceivedException�s�
z:HTTP11ClientProtocolTests.test_parserDataReceivedExceptioncCs`|j�tddtd��}|jjj}|�|j|j�|j�t	t
d���|�|jd�t||t
g�S)z�
        When the HTTP response parser is disconnected, the
        L{TransportProxyProducer} which was connected to it as a transport is
        stopped.
        r�r�Nzconnection done)r�r�r*rb�_parserrcr��	_producer�_disconnectParserrr	rY)rOr�rcr>r>r?�test_proxyStopped�s�
z+HTTP11ClientProtocolTests.test_proxyStoppedcCs�t�}t�}|�|�g}g}|���|j�|���|j�|�||fggf�|�|j�|�	t
t���|�|dg�|�|dg�dS)z�
        L{HTTP11ClientProtocol.abort} will tell the transport to close its
        connection when it is invoked, and returns a C{Deferred} that fires
        when the connection is lost.
        N)rr(r��abortrRr~rHr�rQrrr	)rOrcr��r1�r2r>r>r?�test_abortClosesConnection�s
z4HTTP11ClientProtocolTests.test_abortClosesConnectioncCsZt�}t�}|�|�|�tt���g}|���|j�|�	|dg�|�	|j
d�dS)z�
        L{HTTP11ClientProtocol.abort} called after the connection is lost
        returns a C{Deferred} that fires immediately.
        Nr^)rr(r�rrr	r�rRr~rHrs�rOrcr�rxr>r>r?�test_abortAfterConnectionLost�s
z7HTTP11ClientProtocolTests.test_abortAfterConnectionLostcCs\t�}t�}|�|�|�tddtd��}|��|�|j�|�	t
t���t||t
g�S)a
        The Deferred returned by L{HTTP11ClientProtocol.request} will fire
        with a L{ResponseFailed} failure containing a L{ConnectionAborted}
        exception, if the connection was aborted before all response headers
        have been received.
        r�r�N)rr(r�r�r*rbr�r�rQrrr	rYrr�r>r>r?�test_abortBeforeResponseBody�s
z6HTTP11ClientProtocolTests.test_abortBeforeResponseBodycs�tdd��t��������tddtd��}��d�t��G��fdd�dt������fd	d
�}�fdd�}|�	|�t
��ttg�}|�	|�S)
aS
        When the connection is aborted after the response headers have
        been received and the L{Response} has been made available to
        application code, the response body protocol's C{connectionLost}
        method will be invoked with a L{ResponseFailed} failure containing a
        L{ConnectionAborted} exception.
        T)�lenientr�r�Nrcs(eZdZdZ�fdd�Z�fdd�ZdS)zQHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestinationzl
            A body response protocol which immediately aborts the HTTP
            connection.
            cs���dS)z<
                Abort the HTTP connection.
                N)r�rj)r�r>r?�connectionMadesz`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionMadecs��|�dS)z�
                Make the reason for the losing of the connection available to
                the unit test via C{testResult}.
                N)rU�rOrK)�
testResultr>r?r
rZz`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionLostN)r:r;r<r=r�rr>)r�r�r>r?�BodyDestinationsr�cs,|�������j���tt���dS)z�
            Connect the L{BodyDestination} response body protocol to the
            response, and then simulate connection loss after ensuring that
            the HTTP connection has been aborted.
            N)rrr�rQrrr	rf)r�r�rOrcr>r?rrszMHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.deliverBodycs��|jt�dSri)rr�r-)�errorrjr>r?�
checkError�zLHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.checkError)
rr(r�r�r*rbr�rr
rRrYrr1)rOrxrrr�rSr>)r�r�rOr�rcr?�test_abortAfterResponseHeaders�s





�
z8HTTP11ClientProtocolTests.test_abortAfterResponseHeadersc	sg����fdd�}t�}t|����|���tddtddd��}��d����g�g}|�|j	�|d	}t
�}t�|_|j��fd
d��|�
|���d�|j�t�����d
g����jd����jd����jd����jd����jd�dS)a
        If after a response is done the {HTTP11ClientProtocol} stays open and
        returns to QUIESCENT state, all per-request state is reset and the
        C{quiescentCallback} is called with the protocol instance.

        This is useful for implementing a persistent connection pool.

        The C{quiescentCallback} is called *before* the response-receiving
        protocol's C{connectionLost}, so that new requests triggered by end of
        first request can re-use a persistent connection.
        c�(��|����|jd���|�dS�Nre�rHr�r~�rp�r�rurOr>r?rT5�zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.callbackr�r�NT�r=s&HTTP/1.1 200 OK
Content-length: 3

rcr�)N�
response doner�r )rur>r?r�Or�zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.<lambda>rr�)rr(r�r�r*rbr�rHrRr~rrrqrrr�rGr.r��_finishedRequest�_currentRequest�_transportProxyr)rOrTrcr�rxr��bodyProtocolr>r�r?�test_quiescentCallbackCalled's6
�

�

z6HTTP11ClientProtocolTests.test_quiescentCallbackCalledc	s�g����fdd�}t�}t|����|���tddtddd��}��d���|�}��|j	d	���t
��d
���|jd�dS)a�
        The C{quiescentCallback} passed to L{HTTP11ClientProtocol} will only be
        invoked once that protocol is in a state similar to its initial state.
        One of the aspects of this initial state is the producer-state of its
        transport; an L{HTTP11ClientProtocol} begins with a transport that is
        producing, i.e. not C{pauseProducing}'d.

        Therefore, when C{quiescentCallback} is invoked the protocol will still
        be producing.
        cr�r�r�r�r�r>r?rTmr�z]HTTP11ClientProtocolTests.test_transportProducingWhenQuiescentAfterFullBody.<locals>.callbackr�r�NTr�s)HTTP/1.1 200 OK
Content-length: 3

BBB�DEFERRED_CLOSEr_r�)rr(r�r�r*rbr��successResultOfrHrsrIr�)rOrTrcr�r�r>r�r?�1test_transportProducingWhenQuiescentAfterFullBody`s
��
zKHTTP11ClientProtocolTests.test_transportProducingWhenQuiescentAfterFullBodyc	s�g����fdd�}t�}t|����|���tddtddd��}|��j���d���	t
��d	����d
�����dt
�dS)z�
        The quiescentCallback is called before the request C{Deferred} fires,
        in cases where the response has no body.
        cr�r�r�r�r�r>r?rT�r�zUHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponse.<locals>.callbackr�r�NTr��&HTTP/1.1 200 OK
Content-length: 0

r�rr_)rr(r�r�r*rbrRr~r�rHrIr�rr-)rOrTrcr�r>r�r?�)test_quiescentCallbackCalledEmptyResponse�s
�
zCHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponsec	C�g}t�}t|j�}|�|�|�tddtddd��}|�d�g}|�|j�|d}t	�}|�
|�|j�t
�|�|g�|�|j�dS)z�
        If after a response is done the {HTTP11ClientProtocol} returns a
        C{Connection: close} header in the response, the C{quiescentCallback}
        is not called and the connection is lost.
        r�r�NTr�s9HTTP/1.1 200 OK
Content-length: 0
Connection: close

r�rr(r~r�r�r*rbr�rRrrrr�rGr.rHr�rQ�rOrurcr�r�rxr�r�r>r>r?�test_quiescentCallbackNotCalled�s$

��
z9HTTP11ClientProtocolTests.test_quiescentCallbackNotCalledc	Cr�)z�
        If the request was non-persistent (i.e. sent C{Connection: close}),
        the C{quiescentCallback} is not called and the connection is lost.
        r�r�NFr�r�rr�r�r>r>r?�1test_quiescentCallbackNotCalledNonPersistentQuery�s 

�

zKHTTP11ClientProtocolTests.test_quiescentCallbackNotCalledNonPersistentQueryc	Cs�dd�}t�|t�}t�}t|�}|�|�|�tddtddd��}|�	d�g}|�
|j�|d	}t�}|�
|�|j�t�|�d
t|��|d	}	|	d}
|�|
jt�|�t�|�|j�dS)zx
        If C{quiescentCallback} throws an exception, the error is logged and
        protocol is disconnected.
        cSrri)�ZeroDivisionErrorr�r>r>r?rT�rzHHTTP11ClientProtocolTests.test_quiescentCallbackThrows.<locals>.callbackr�r�NTr�r�rr_r)rrrrr(r�r�r*rbr�rRr~rrrr�rGr.rrIrr�r�rr�rQ)rOrTrrcr�r�rxr�r�rr�r>r>r?�test_quiescentCallbackThrows�s*
�


z6HTTP11ClientProtocolTests.test_quiescentCallbackThrowscCsNt�}t�}|�|�|�tddtd��}|��|�|j�t	||t
tg�S)a
        The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
        with a L{ResponseNeverReceived} failure containing a L{CancelledError}
        exception if the request was cancelled before any response headers were
        received.
        r�r�N)rr(r�r�r*rb�cancelr��disconnectedrVr0rr�r>r>r?�test_cancelBeforeResponses

�z3HTTP11ClientProtocolTests.test_cancelBeforeResponsecCsVt�}t�}|�|�|�tddtd��}|�d�|��|�|j	�t
||tg�S)a
        The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
        with a L{ResponseFailed} failure containing a L{CancelledError}
        exception if the request was cancelled before all response headers were
        received.
        r�r�Nr�)rr(r�r�r*rbr�r�r�r�rYrr�r>r>r?�test_cancelDuringResponses

z3HTTP11ClientProtocolTests.test_cancelDuringResponsecs�t�}t�}|�|�t|��ddi��fdd����fdd�}|�_|�tddt���}�j�	d	�|�
�|�|j�|��d�t
||tg�S)
a
        The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
        with a L{RequestGenerationFailed} failure containing a
        L{CancelledError} exception if the request was cancelled before a
        C{bodyProducer} has finished producing.
        �	cancelledFcsd�d<dS)NTr�r>r )�nonLocalr>r?r�2�zJHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.cancelcs|�_t���_�jSri��consumerrr�)r�)r��producerr>r?�startProducing5s
zRHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.startProducing�POST�/barsxxxxx)rr(r��StringProducerr�r�r*rbr�r@r�r�r�r[r)rO�producerLengthrcr�r�rxr>)r�r�r�r?� assertCancelDuringBodyProduction$s
z:HTTP11ClientProtocolTests.assertCancelDuringBodyProductioncCrY)a(
        The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
        with a L{RequestGenerationFailed} failure containing a
        L{CancelledError} exception if the request was cancelled before a
        C{bodyProducer} with an explicit length has finished producing.
        �
)r�rjr>r>r?�test_cancelDuringBodyProductionC�
z9HTTP11ClientProtocolTests.test_cancelDuringBodyProductioncCs
|�t�S)a'
        The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
        with a L{RequestGenerationFailed} failure containing a
        L{CancelledError} exception if the request was cancelled before a
        C{bodyProducer} with C{UNKNOWN_LENGTH} has finished producing.
        )r�rrjr>r>r?�&test_cancelDuringChunkedBodyProductionLr�z@HTTP11ClientProtocolTests.test_cancelDuringChunkedBodyProductionri)'r:r;r<r=rBrCrLrMrRrSrXr[r]rcrjrkr|rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r>r>r>r?rA�sJ
 2 ) :9.#	rAc@s<eZdZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
S)r�a�
    L{StringProducer} is a dummy body producer.

    @ivar stopped: A flag which indicates whether or not C{stopProducing} has
        been called.
    @ivar consumer: After C{startProducing} is called, the value of the
        C{consumer} argument to that method.
    @ivar finished: After C{startProducing} is called, a L{Deferred} which was
        returned by that method.  L{StringProducer} will never fire this
        L{Deferred}.
    FcCs
||_dSri)r�)rOr�r>r>r?�__init__fr;zStringProducer.__init__cCs||_t�|_|jSrir�)rOr�r>r>r?r�iszStringProducer.startProducingcCr7r�r8rjr>r>r?�
stopProducingnr;zStringProducer.stopProducingcCrhrir>rjr>r>r?�pauseProducingq�zStringProducer.pauseProducingcCrhrir>rjr>r>r?�resumeProducingur�zStringProducer.resumeProducingN)
r:r;r<r=r9r�r�r�r�r�r>r>r>r?r�Vsr�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd5d)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd(S)6�RequestTestsz
    Tests for L{Request}.
    cCst�|_dSri)rrcrjr>r>r?rBr�zRequestTests.setUpcCs,tddtd��|j�|�|j��d�dS)zk
        L{Request.writeTo} formats the request data and writes it to the given
        transport.
        r�r�Ns8GET / HTTP/1.1
Connection: close
Host: example.com

�r*rbr6rcrHr�rjr>r>r?�test_sendSimplestRequest�s
�z%RequestTests.test_sendSimplestRequestcCs4tddtddd�}|�|j�|�|j��d�dS)zO
        A pesistent request does not send 'Connection: close' header.
        r�r�NTr�s%GET / HTTP/1.1
Host: example.com

r�)rO�reqr>r>r?�"test_sendSimplestPersistentRequest�s�z/RequestTests.test_sendSimplestPersistentRequestcCs�tddgdgd��}tdd|d��|j�|j���d�}|�|d	d
�|�|dd�ddg�|d	=|dd�=|��|�|gd
��dS)zf
        L{Request.writeTo} formats header data and writes it to the given
        transport.
        r�r�rC)r�rBr��/fooNr�r�GET /foo HTTP/1.1���r�)�Connection: closesHost: example.comr�r�)r2r*r6rcr��splitrH�sort)rOr��linesr>r>r?�test_sendRequestHeaders�s�z$RequestTests.test_sendRequestHeaderscCs�tD]T}t||gddgi�}t�}tdd|d��|�|���d�}|�|dd�|�|d	d�d
d
g�|d=|d	d�=|�d�|�d�d
�	t
t
g�}|�||g�qdS)z_
        Linear whitespace in request headers is replaced with a single
        space.
        rBsexample.invalidr�r�Nr�rr�r�r�r�sHost: example.invalids: )r5r2rr*r6r�r�rH�remover�r6)rO�	componentr�rcr��sanitizedHeaderLiner>r>r?�-test_sanitizeLinearWhitespaceInRequestHeaders�s

�z:RequestTests.test_sanitizeLinearWhitespaceInRequestHeaderscCs�tt�}tddt|�}|�|j�|�|jd�|�|jj	|�|�
|jj�|�|j�
�d�|j��|j�d�|j�d�|j�d�|�|jj	d�|�|j�
�d�dS)z�
        L{Request.writeTo} uses chunked encoding to write data from the request
        body producer to the given transport.  It registers the request body
        producer with the transport.
        r�r�NsXPOST /bar HTTP/1.1
Connection: close
Transfer-Encoding: chunked
Host: example.com

sxxxsyyyyyyyyyyyyyyys!3
xxx
f
yyyyyyyyyyyyyyy
0

)r�rr*rbr6rc�assertNotIdenticalr�r�r�r��	streamingrHr��clearr@r�rT�rOr�r�r>r>r?�test_sendChunkedRequestBody�s&�
�z(RequestTests.test_sendChunkedRequestBodycs`tt�}tddt|�}|��j�}�j��|j�t	���fdd�}��
|t	�}|�|�|S)a�
        If L{Request} is created with a C{bodyProducer} without a known length
        and the L{Deferred} returned from its C{startProducing} method fires
        with a L{Failure}, the L{Deferred} returned by L{Request.writeTo} fires
        with that L{Failure} and the body producer is unregistered from the
        transport.  The final zero-length chunk is not written to the
        transport.
        r�r�cs&���j��d����jjd�dSrE)rHrcr�r�r�rGrjr>r?rP�szCRequestTests.test_sendChunkedRequestBodyWithError.<locals>.cbFailed)r�rr*rbr6rcr�r�rUr8rQrR)rOr�r��
writeDeferredrPrUr>rjr?�$test_sendChunkedRequestBodyWithError�s	

z1RequestTests.test_sendChunkedRequestBodyWithErrorcCs�td�}tddt|�}|�|j�|�|jd�|�|jj|�|�	|jj
�|�|j��d�|j�
�|j�d�|j�d�|�|jjd�|�|j��d�dS)z�
        If L{Request} is created with a C{bodyProducer} with a known length,
        that length is sent as the value for the I{Content-Length} header and
        chunked encoding is not used.
        �r�r�NsOPOST /bar HTTP/1.1
Connection: close
Content-Length: 3
Host: example.com

r)r�r*rbr6rcr�r�r�r�r�r�rHr�r�r@r�rTr�r>r>r?�test_sendRequestBodyWithLength�s�
z+RequestTests.test_sendRequestBodyWithLengthcCs4t|dtd�}|�|j�|�|j��|d�dS)a"
        Verify that the message generated by a L{Request} initialized with
        the given method and C{None} as the C{bodyProducer} includes
        I{Content-Length: 0} in the header.

        @param method: The HTTP method issue in the request.
        @type method: L{bytes}
        r�NsK /foo HTTP/1.1
Connection: close
Content-Length: 0
Host: example.com

r�)rOr<r�r>r>r?�_sendRequestEmptyBodyWithLengths	�z,RequestTests._sendRequestEmptyBodyWithLengthcC�|�d�dS)z�
        If I{PUT} L{Request} is created without a C{bodyProducer},
        I{Content-Length: 0} is included in the header and chunked
        encoding is not used.
        sPUTN�r�rjr>r>r?�test_sendPUTRequestEmptyBody(�z)RequestTests.test_sendPUTRequestEmptyBodycCr�)z�
        If I{POST} L{Request} is created without a C{bodyProducer},
        I{Content-Length: 0} is included in the header and chunked
        encoding is not used.
        r�Nr�rjr>r>r?�test_sendPOSTRequestEmptyBody0r�z*RequestTests.test_sendPOSTRequestEmptyBodycCsVtd�}tddt|�}|�|j�}|j�d�|j�d�|�	|jj
d�|�|t�S)a
        If L{Request} is created with a C{bodyProducer} with a known length and
        the producer does not produce that many bytes, the L{Deferred} returned
        by L{Request.writeTo} fires with a L{Failure} wrapping a
        L{WrongBodyLength} exception.
        r�r�r��abN)
r�r*rbr6rcr�r@r�rTr�r�rQr&�rOr�r�r�r>r>r?�#test_sendRequestBodyWithTooFewBytes8sz0RequestTests.test_sendRequestBodyWithTooFewBytescs�td��tddt��}|��j�}�j�d����j��j�d���	�j���
�jjd����fdd�}��|t
�}|�|�|S)	a7
        Verify that when too many bytes have been written by a body producer
        and then the body producer's C{startProducing} L{Deferred} fires that
        the producer is unregistered from the transport and that the
        L{Deferred} returned from L{Request.writeTo} is fired with a L{Failure}
        wrapping a L{WrongBodyLength}.

        @param finisher: A callable which will be invoked with the body
            producer after too many bytes have been written to the transport.
            It should fire the startProducing Deferred somehow.
        r�r�r�r��cdNcsL���j��d��j����t�jjd�������j��d�dS)NsQPOST /bar HTTP/1.1
Connection: close
Content-Length: 3
Host: example.com

absefr�)rHrcr�r�rwrr�r@r���finisherr�rOr>r?rPgs�
	zCRequestTests._sendRequestBodyWithTooManyBytesTest.<locals>.cbFailed)r�r*rbr6rcr�r@r�r9r�r�r�rQr&rR)rOr�r�r�rPrUr>r�r?�$_sendRequestBodyWithTooManyBytesTestGs
z1RequestTests._sendRequestBodyWithTooManyBytesTestcCsdd�}|�|�S)�
        If L{Request} is created with a C{bodyProducer} with a known length and
        the producer tries to produce more than than many bytes, the
        L{Deferred} returned by L{Request.writeTo} fires with a L{Failure}
        wrapping a L{WrongBodyLength} exception.
        cS�|j�d�dSri�r�rT�r�r>r>r?r���zCRequestTests.test_sendRequestBodyWithTooManyBytes.<locals>.finisher)r��rOr�r>r>r?�$test_sendRequestBodyWithTooManyBytes�s
z1RequestTests.test_sendRequestBodyWithTooManyBytescs$t��t����fdd�}��|�S)r�csV|j�t���d}��d|�|d}��|jt���t�}��t|�d�dS)Nrrr_)	r�rUr8r_rr�rrHrI)r�rr�rWrar>r?r��s
zHRequestTests.test_sendRequestBodyErrorWithTooManyBytes.<locals>.finisher)rrrr�r�r>rar?�)test_sendRequestBodyErrorWithTooManyBytes�s
	z6RequestTests.test_sendRequestBodyErrorWithTooManyBytescCs�t�|t�}td�}tddt|�}|�|j�|jj	}|j�
d�|j�d�|�
t��|d}|�d|�|d}|�|jt�|�t|�t��d�dS)	a�
        Though there should be no way for the internal C{finishedConsuming}
        L{Deferred} in L{Request._writeToBodyProducerContentLength} to fire a
        L{Failure} after the C{finishedProducing} L{Deferred} has fired, in
        case this does happen, the error should be logged with a message about
        how there's probably a bug in L{Request}.

        This is a whitebox test.
        r�r�r�rNrrr_)rrrr�r*rbr6rcr��	_finishedr@r�rTrUr8r_rr�rHrIr)rOrr�r��finishedConsumingrr�r>r>r?�*test_sendRequestBodyErrorWithConsumerError�s
z7RequestTests.test_sendRequestBodyErrorWithConsumerErrorcCsxtd�}tddt|�}|�|j�}|j�d�||�|�|jjd�|j�	�|�
t|jjd�|�|j�
�d�|S)a
        Verify that if the body producer fires its Deferred and then keeps
        writing to the consumer that the extra writes are ignored and the
        L{Deferred} returned by L{Request.writeTo} fires with a L{Failure}
        wrapping the most appropriate exception type.
        r�r�r�r�Nr�r�)r�r*rbr6rcr�r@r�r�r�rwrrHr�)rOr�r�r�r�r>r>r?�-_sendRequestBodyFinishedEarlyThenTooManyBytes�s
z:RequestTests._sendRequestBodyFinishedEarlyThenTooManyBytescC�dd�}|�|�|�t�S)a-
        If the request body producer indicates it is done by firing the
        L{Deferred} returned from its C{startProducing} method but then goes on
        to write too many bytes, the L{Deferred} returned by {Request.writeTo}
        fires with a L{Failure} wrapping L{WrongBodyLength}.
        cSr�rir�r�r>r>r?r��r�zPRequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytes.<locals>.finisher)rQrr&r�r>r>r?�1test_sendRequestBodyFinishedEarlyThenTooManyBytes��
�z>RequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytescCr)a3
        If the request body producer indicates an error by firing the
        L{Deferred} returned from its C{startProducing} method but then goes on
        to write too many bytes, the L{Deferred} returned by {Request.writeTo}
        fires with that L{Failure} and L{WrongBodyLength} is logged.
        cSs|j�t��dSri)r�rUr8r�r>r>r?r��r�zORequestTests.test_sendRequestBodyErroredEarlyThenTooManyBytes.<locals>.finisher)rQrr8r�r>r>r?�0test_sendRequestBodyErroredEarlyThenTooManyBytes�rz=RequestTests.test_sendRequestBodyErroredEarlyThenTooManyBytesNcCs`tt�}tddt|�}|�|j�}|j�|�|j��|�	t
|jjd�|�
|j��d�|S)a
        If the request body producer with an unknown length tries to write
        after firing the L{Deferred} returned by its C{startProducing} method,
        the C{write} call raises an exception and does not write anything to
        the underlying transport.
        r�r�r�r�)r�rr*rbr6rcr�rTr�rwrr�r@rHr�)rO�_withr�r�r�r>r>r?�0test_sendChunkedRequestBodyFinishedThenWriteMore�s
z=RequestTests.test_sendChunkedRequestBodyFinishedThenWriteMorecCs|�tt���}|�|t�S)a$
        If the request body producer with an unknown length tries to write
        after firing the L{Deferred} returned by its C{startProducing} method
        with a L{Failure}, the C{write} call raises an exception and does not
        write anything to the underlying transport.
        )rrr8rQ)rOrUr>r>r?�9test_sendChunkedRequestBodyFinishedWithErrorThenWriteMore	s�zFRequestTests.test_sendChunkedRequestBodyFinishedWithErrorThenWriteMorecCs�td�}tddt|�}|�|j�}|�|jj|�|�|jj�|j	�
d�|�|j��d�|�
|jj�|j�tt���|�
|jj�|�|jjd�|�|t�S)z�
        If the L{Deferred} returned from the C{startProducing} method of the
        L{IBodyProducer} passed to L{Request} fires with a L{Failure}, the
        L{Deferred} returned from L{Request.writeTo} fails with that
        L{Failure}.
        �r�r�r�sQPOST /bar HTTP/1.1
Connection: close
Content-Length: 5
Host: example.com

abN)r�r*rbr6rcr�r�r�r�r�r@rHr�r�rQr�rUrr8rQr�r>r>r?�test_sendRequestBodyWithError	s�
z*RequestTests.test_sendRequestBodyWithErrorcCsxtddti�d�}|�t|j|j�|�|j��d�tddtdddgi�d�}|�t|j|j�|�|j��d�dS)z�
        L{Request.writeTo} raises L{BadHeaders} if there is not exactly one
        I{Host} header and writes nothing to the given transport.
        r�r�Nr�sHostrCsexample.org)r*r2rwrr6rcrHr�)rOr�r>r>r?�test_hostHeaderRequired;	s�z$RequestTests.test_hostHeaderRequiredcCsFtd�}tddt|�}|�|j�|�|j�|��|�|j�dS)zc
        L{Request.stopWriting} calls its body producer's C{stopProducing}
        method.
        r�r�r�N)	r�r*rbr6rcr�r9r:r�r�r>r>r?�test_stopWritingJ	szRequestTests.test_stopWritingcCs�t�|t�}td�}dd�}||_tddt|�}|�|j�|�	�|�
t|�t
��d�|�dt|��|d}|�d|�|d}|�|jt
�d	S)
z�
        If the body producer's C{stopProducing} method raises an exception,
        L{Request.stopWriting} logs it and does not re-raise it.
        r�cSstd���NzstopProducing is bustedrr>r>r>r?�brokenStopProducing^	szBRequestTests.test_brokenStopProducing.<locals>.brokenStopProducingr�r�r_rrN)rrrr�r�r*rbr6rcr:rHrIrr8rr_rr�)rOrr�rr�rr�r>r>r?�test_brokenStopProducingV	sz%RequestTests.test_brokenStopProducingri)r:r;r<r=rBr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrr	rrr
rr>r>r>r?r�zs6!A

'r�c@sReZdZdZdd�Zdd�Zdd�Zdd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�LengthEnforcingConsumerTestsz/
    Tests for L{LengthEnforcingConsumer}.
    cCs2t�|_td�|_t�|_t|j|j|j�|_dS)Nr�)rrxr�r�rrcr"�enforcerrjr>r>r?rBs	s

�z"LengthEnforcingConsumerTests.setUpcCsJ|j�d�|�|j��d�|j��|j�d�|�|j��d�dS)z�
        L{LengthEnforcingConsumer.write} calls the wrapped consumer's C{write}
        method with the bytes it is passed as long as there are fewer of them
        than the C{length} attribute indicates remain to be received.
        rsdefN)rr@rHrcr�r�rjr>r>r?�
test_write{	s

z'LengthEnforcingConsumerTests.test_writecCs |j�d�|�t|jj�dS)z�
        L{LengthEnforcingConsumer._noMoreWritesExpected} raises
        L{WrongBodyLength} if it is called before the indicated number of bytes
        have been written.
        s	xxxxxxxxxN)rr@rwr&�_noMoreWritesExpectedrjr>r>r?�test_finishedEarly�	sz/LengthEnforcingConsumerTests.test_finishedEarlyFcCsP|j�d�|�|jj�|j�d�|�|jj�|r!|j��|�|jt	�S)aN
        If it is called with a total number of bytes exceeding the indicated
        limit passed to L{LengthEnforcingConsumer.__init__},
        L{LengthEnforcingConsumer.write} fires the L{Deferred} with a
        L{Failure} wrapping a L{WrongBodyLength} and also calls the
        C{stopProducing} method of the producer.
        �
xxxxxxxxxx�x)
rr@r�r�r9r�rrQrxr&)rO�_unregisterAfterr>r>r?�test_writeTooMany�	s
z.LengthEnforcingConsumerTests.test_writeTooManycCsH|j�d�|j��|�|jj�|�t|jjd�|�|jj�dS)z�
        If L{LengthEnforcingConsumer.write} is called after
        L{LengthEnforcingConsumer._noMoreWritesExpected}, it calls the
        producer's C{stopProducing} method and raises L{ExcessWrite}.
        rrN)	rr@rr�r�r9rwrr�rjr>r>r?�test_writeAfterNoMoreExpected�	s

z:LengthEnforcingConsumerTests.test_writeAfterNoMoreExpectedcCrY)z�
        L{LengthEnforcingConsumer._noMoreWritesExpected} does nothing (in
        particular, it does not raise any exception) if called after too many
        bytes have been passed to C{write}.
        T)rrjr>r>r?�test_finishedLate�	r\z.LengthEnforcingConsumerTests.test_finishedLatecCs"|j�d�|�|j��d�dS)z�
        If L{LengthEnforcingConsumer._noMoreWritesExpected} is called after
        the correct number of bytes have been written it returns L{None}.
        rN)rr@r�rrjr>r>r?�
test_finished�	sz*LengthEnforcingConsumerTests.test_finishedcs6�fdd�}|�j_�fdd�}���}|�|�|S)a8
        If L{LengthEnforcingConsumer.write} calls the producer's
        C{stopProducing} because too many bytes were written and the
        C{stopProducing} method raises an exception, the exception is logged
        and the L{LengthEnforcingConsumer} still errbacks the finished
        L{Deferred}.
        cst��j�td��r)r�r�r�r8r>rjr>r?r�	szRLengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.brokenStopProducingcs��t��t��d�dS)Nr_)rHrIrr8rGrjr>r?�
cbFinished�	szILengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.cbFinished)r�r�rrR)rOrrrUr>rjr?�test_stopProducingRaises�	s	
z5LengthEnforcingConsumerTests.test_stopProducingRaisesN)F)r:r;r<r=rBrrrrrrrr>r>r>r?rn	s
	rc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�RequestBodyConsumerTestsz�
    Tests for L{ChunkedEncoder} which sits between an L{ITransport} and a
    request/response body producer and chunked encodes everything written to
    it.
    cCs|�tttt����dS)zC
        L{ChunkedEncoder} instances provide L{IConsumer}.
        N)r�rrrrrjr>r>r?�test_interface�	sz'RequestBodyConsumerTests.test_interfacecCsNt�}t|�}|�d�|�|��d�|��|�d�|�|��d�dS)z}
        L{ChunkedEncoder.write} writes to the transport the chunked encoded
        form of the bytes passed to it.
        r�r�sxxxxxxxxxxxxxxxxs10
xxxxxxxxxxxxxxxx
N)rrr@rHr�r�)rOrc�encoderr>r>r?r�	s

z#RequestBodyConsumerTests.test_writecCsdt�}t�}t|�}|�|d�|�|j|�|�|j�|��|�|jd�|�	|�
�d�dS)a
        L{ChunkedEncoder.registerProducer} registers the given streaming
        producer with its transport and L{ChunkedEncoder.unregisterProducer}
        writes a zero-length chunk to its transport and unregisters the
        transport's producer.
        TNs0

)rr�r�registerProducerr�r�r�r��unregisterProducerrHr�)rOrcr�r!r>r>r?�test_producerRegistration�	sz2RequestBodyConsumerTests.test_producerRegistrationN)r:r;r<r=r rr$r>r>r>r?r�	s

rc@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�TransportProxyProducerTestszn
    Tests for L{TransportProxyProducer} which proxies the L{IPushProducer}
    interface of a transport.
    cCs|�tttd���dS)zO
        L{TransportProxyProducer} instances provide L{IPushProducer}.
        N)r�rrr%rjr>r>r?r 
sz*TransportProxyProducerTests.test_interfacecCs6t�}t|�}|�|j|�|��|�|jd�dS)z~
        L{TransportProxyProducer.stopProxying} drops the reference to the
        wrapped L{IPushProducer} provider.
        N)rr%r�r��stopProxying�rOrc�proxyr>r>r?�%test_stopProxyingUnreferencesProducer
s
zATransportProxyProducerTests.test_stopProxyingUnreferencesProducercCsdt�}|��t|�}|�|jd�|��|�|jd�|��|��|��|�|jd�dS)z�
        L{TransportProxyProducer.resumeProducing} calls the wrapped
        transport's C{resumeProducing} method unless told to stop proxying.
        r�r�N)rr�r%rHr�r�r&r'r>r>r?�test_resumeProducing
sz0TransportProxyProducerTests.test_resumeProducingcCs\t�}t|�}|�|jd�|��|�|jd�|��|��|��|�|jd�dS)z�
        L{TransportProxyProducer.pauseProducing} calls the wrapped transport's
        C{pauseProducing} method unless told to stop proxying.
        r�r�N)rr%rHr�r�r�r&r'r>r>r?�test_pauseProducing,
sz/TransportProxyProducerTests.test_pauseProducingcCsbt�}t|�}|�|jd�|��|�|jd�t�}t|�}|��|��|�|jd�dS)z�
        L{TransportProxyProducer.stopProducing} calls the wrapped transport's
        C{stopProducing} method unless told to stop proxying.
        r�r9N)rr%rHr�r�r&r'r>r>r?�test_stopProducingA
sz.TransportProxyProducerTests.test_stopProducingcCsdt�}t�}|�|�||_t|�}|�|j�|�|jd�|�	�|�|jd�|�
|j�dS)zs
        L{TransportProxyProducer.loseConnection} calls the wrapped transport's
        C{loseConnection}.
        r�N)rrr�r�r%r��	connectedrHr��loseConnectionr��rOrcr�r(r>r>r?� test_loseConnectionWhileProxyingU
s
z<TransportProxyProducerTests.test_loseConnectionWhileProxyingcCsPt�}t�}|�|�||_t|�}|��|�|j�|��|�|j�dS)zm
        L{TransportProxyProducer.loseConnection} does nothing when the
        proxy is not active.
        N)	rrr�r�r%r&r�r-r.r/r>r>r?�test_loseConnectionNotProxyingj
s
z:TransportProxyProducerTests.test_loseConnectionNotProxyingN)r:r;r<r=r r)r*r+r,r0r1r>r>r>r?r%�	sr%c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd S)!�
ResponseTestsz 
    Tests for L{Response}.
    cCstt��}|�tt|��dS)z=
        L{Response} instances provide L{IResponse}.
        N)rdrr�rr4�rOr�r>r>r?�test_verifyInterface�
s
z"ResponseTests.test_verifyInterfacecslg�t�}G�fdd�dt�}|�}t|�}|�|��\}|��|�|jd�|��|�|jd�dS)z�
        The L{IProtocol} provider passed to L{Response.deliverBody} has its
        C{makeConnection} method called with an L{IPushProducer} provider
        hooked up to the response as an argument.
        c�eZdZ�fdd�ZdS)z7ResponseTests.test_makeConnection.<locals>.SomeProtocolc���|�dSrir�)rOr���	producersr>r?r��
�zFResponseTests.test_makeConnection.<locals>.SomeProtocol.makeConnectionN�r:r;r<r�r>r7r>r?�SomeProtocol�
�r;r�r�N)rr
rdrrr�rHr�r�)rOrcr;r�r��theProducerr>r7r?�test_makeConnection�
s
z!ResponseTests.test_makeConnectioncsNg�G�fdd�dt�}|�}tt��}|�|�|�d�|��dg�dS)z�
        The L{IProtocol} provider passed to L{Response.deliverBody} has its
        C{dataReceived} method called with bytes received as part of the
        response body.
        cr5)z5ResponseTests.test_dataReceived.<locals>.ListConsumercr6rir��rOr��r�r>r?r��
r9zBResponseTests.test_dataReceived.<locals>.ListConsumer.dataReceivedN�r:r;r<r�r>r@r>r?�ListConsumer�
r<rBr�N)r
rdrrrr�rH�rOrBr�r�r>r@r?�test_dataReceived�
s


zResponseTests.test_dataReceivedcsjg�G�fdd�dt�}|�}tt��}|�|�|���d�t�|�t��d�|�	|j
d�dS)z�
        The L{IProtocol} provider passed to L{Response.deliverBody} has its
        C{connectionLost} method called with a L{Failure} wrapping
        L{ResponseDone} when the response's C{_bodyDataFinished} method is
        called.
        cr5)z7ResponseTests.test_connectionLost.<locals>.ListConsumercr6rir�r���lostr>r?r�
r9zFResponseTests.test_connectionLost.<locals>.ListConsumer.connectionLostN)r:r;r<rr>rEr>r?rB�
r<rBrr_N)r
rdrrrr�rGr.rHrIr��
_bodyProtocolrCr>rEr?�test_connectionLost�
s

z!ResponseTests.test_connectionLostcsrg�G�fdd�dt�}|�}tt��}|�d�|�d�|�|�|�d�|��gd��|�|jd�dS)z�
        If data is delivered to the L{Response} before a protocol is registered
        with C{deliverBody}, that data is buffered until the protocol is
        registered and then is delivered.
        cr5)z8ResponseTests.test_bufferEarlyData.<locals>.ListConsumercr6rir�r?r@r>r?r��
r9zEResponseTests.test_bufferEarlyData.<locals>.ListConsumer.dataReceivedNrAr>r@r>r?rB�
r<rBr�r�r�)r�r�r�N)r
rdrr�rrrHr��_bodyBuffer)rOrBr�r�r>r@r?�test_bufferEarlyData�
s




z"ResponseTests.test_bufferEarlyDatacCs,tt��}|�t��|�t|jt��dS)zb
        L{Response.deliverBody} raises L{RuntimeError} if called more than
        once.
        N)rdrrrr
rwrxr3r>r>r?� test_multipleStartProducingFails�
s
z.ResponseTests.test_multipleStartProducingFailscCs4tt��}|�t��|��|�t|jt��dS)zw
        L{Response.deliverBody} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished}.
        N)rdrrrr
r�rwrxr3r>r>r?�%test_startProducingAfterFinishedFails�
s
z3ResponseTests.test_startProducingAfterFinishedFailscCs&tt��}|��|�t|jd�dS)z�
        L{Response._bodyDataReceived} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished} but before L{Response.deliverBody}.
        r�N)rdrr�rwrxr�r3r>r>r?�'test_bodyDataReceivedAfterFinishedFails�
s
z5ResponseTests.test_bodyDataReceivedAfterFinishedFailscCs2tt��}|��|�t��|�t|jd�dS)z�
        L{Response._bodyDataReceived} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished} and after L{Response.deliverBody}.
        r�N)rdrr�rrr
rwrxr�r3r>r>r?�'test_bodyDataReceivedAfterDeliveryFails�
s
z5ResponseTests.test_bodyDataReceivedAfterDeliveryFailscCs$tt��}|��|�t|j�dS)zh
        L{Response._bodyDataFinished} raises L{RuntimeError} if called more
        than once.
        N)rdrr�rwrxr3r>r>r?�'test_bodyDataFinishedAfterFinishedFailss
z5ResponseTests.test_bodyDataFinishedAfterFinishedFailscCs0tt��}|��|�t��|�t|j�dS)z{
        L{Response._bodyDataFinished} raises L{RuntimeError} if called after
        the body has been delivered.
        N)rdrr�rrr
rwrxr3r>r>r?�'test_bodyDataFinishedAfterDeliveryFailss
z5ResponseTests.test_bodyDataFinishedAfterDeliveryFailscslg�G�fdd�dt�}t�}|��|�}t|�}|�|jd�|�|�|��dg�|�|jd�dS)z�
        L{Response.deliverBody} resumes the HTTP connection's transport
        after passing it to the consumer's C{makeConnection} method.
        cr5)z9ResponseTests.test_transportResumed.<locals>.ListConsumercs��|j�dSri)r~r�r5��transportStater>r?r�#r�zHResponseTests.test_transportResumed.<locals>.ListConsumer.makeConnectionNr:r>rQr>r?rB"r<rBr�r�N)r
rr�rdrHr�rr)rOrBrcr�r�r>rQr?�test_transportResumeds
z#ResponseTests.test_transportResumedcCsXt�}t|�}|�d�|�d�|��t�}|�|�|�|jd�|j�	t
�dS)z�
        If the entire body is delivered to the L{Response} before the
        response's C{deliverBody} method is called, the protocol passed to
        C{deliverBody} is immediately given the body data and then
        disconnected.
        r�r�rwN)rrdr�r�rrrrHr�r�rGr.�rOrcr�r�r>r>r?�)test_bodyDataFinishedBeforeStartProducing/s


z7ResponseTests.test_bodyDataFinishedBeforeStartProducingcCsLt�}t|�}t�}|�|�|�|jd�|�tt���|j	�
t�dS)a	
        The L{Failure} passed to L{Response._bodyDataFinished} when the response
        is in the I{connected} state is passed to the C{connectionLost} method
        of the L{IProtocol} provider passed to the L{Response}'s
        C{deliverBody} method.
        �	CONNECTEDN)rrdrrrrHrsr�rr8r�rGrTr>r>r?�#test_finishedWithErrorWhenConnectedAs
z1ResponseTests.test_finishedWithErrorWhenConnectedcCsLt�}t|�}|�|jd�|�tt���t�}|�|�|j	�
t�dS)a
        The L{Failure} passed to L{Response._bodyDataFinished} when the response
        is in the I{initial} state is passed to the C{connectionLost} method of
        the L{IProtocol} provider passed to the L{Response}'s C{deliverBody}
        method.
        �INITIALN)rrdrHrsr�rr8rrrr�rGrTr>r>r?�!test_finishedWithErrorWhenInitialTs
z/ResponseTests.test_finishedWithErrorWhenInitialN)r:r;r<r=r4r>rDrHrJrKrLrMrNrOrPrSrUrWrYr>r>r>r?r2}
s"	
	
	
r2N)_r=�typingr�zope.interfacer�zope.interface.verifyr�twisted.internet.deferrrrr�twisted.internet.errorr	r
�twisted.internet.interfacesrr�twisted.internet.protocolr
�twisted.loggerr�twisted.protocols.basicr�twisted.python.failurer�twisted.test.proto_helpersrrrr�twisted.trial.unittestr�twisted.web._newclientrrrrrrrrrrr r!r"r#r$r%r&r'�twisted.web.clientr(r)r*r+r,r-r.r/r0�twisted.web.httpr1�twisted.web.http_headersr2�twisted.web.iwebr3r4�twisted.web.test.requesthelperr5r6�	Exceptionr8rArbrVrYr[r]rdrfr{r�r�r�r4r>rAr�r�rrr%r2r>r>r>r?�<module>spP,(


!`o#we,~

Anon7 - 2022
AnonSec Team