HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/web/test/__pycache__/test_newclient.cpython-38.pyc
U


W[Ϩ�@s�dZddlmZmZeZddlmZddlm	Z	ddl
mZddlm
Z
mZddlmZmZddlmZmZmZmZdd	lmZdd
lmZddlmZddlmZmZm Z m!Z!dd
l"m#Z#m$Z$m%Z%m&Z&m'Z'ddl"m(Z(m)Z)ddl"m*Z*m+Z+ddl"m,Z,ddl"m-Z-m.Z.ddl"m/Z/ddl"m0Z0m1Z1ddl"m2Z2m3Z3m4Z4ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>ddl?m@Z@ddlAmBZBddlCmDZDmEZEddlFmGZGGdd�deH�ZIGdd�deH�ZJe@ddgi�ZKd d!�ZLd"d#�ZMd$d%�ZNd&d'�ZOd(d)�ZPGd*d+�d+e�ZQGd,d-�d-eR�ZSGd.d/�d/eSe�ZTGd0d1�d1eSe�ZUGd2d3�d3e�ZVGd4d5�d5�ZWGd6d7�d7�ZXGd8d9�d9e�ZYeeD�Gd:d;�d;��ZZGd<d=�d=e�Z[Gd>d?�d?e�Z\Gd@dA�dAe�Z]GdBdC�dCe�Z^GdDdE�dEe�Z_dFS)Gz&
Tests for L{twisted.web._newclient}.
�)�division�absolute_import)�implementer)�verifyObject)�Failure)�	IConsumer�
IPushProducer)�ConnectionDone�ConnectionLost)�Deferred�succeed�fail�CancelledError)�Protocol)�LineReceiver)�TestCase)�AccumulatingProtocol�EventLoggingObserver�StringTransport� StringTransportWithDisconnection)�UNKNOWN_LENGTH�STATUS�HEADER�BODY�DONE)�
HTTPParser�HTTPClientParser)�BadResponseVersion�
ParseError)�ChunkedEncoder)�WrongBodyLength�RequestNotSent)�ConnectionAborted)�
BadHeaders�ExcessWrite)�TransportProxyProducer�LengthEnforcingConsumer�makeStatefulDispatcher)	�HTTP11ClientProtocol�PotentialDataLoss�Request�RequestGenerationFailed�RequestTransmissionFailed�Response�ResponseDone�ResponseFailed�ResponseNeverReceived)�Headers)�	_DataLoss)�
IBodyProducer�	IResponse)�globalLogPublisherc@seZdZdZdS)�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.pyr66sr6c@seZdZdZdS)�AnotherArbitraryExceptionzI
    Similar to L{ArbitraryException} but with a different identity.
    Nr7r<r<r<r=r>=sr>�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.
    csFt|j��D]\}}|�|�q��t|j�t��d|j�f�|S)Nzlen(%s) != len(%s))�zip�reasons�trap�assertEqual�len)�err�reason�type��reasonTypes�selfr<r=�cbFailed`s�z-assertWrapperExceptionTypes.<locals>.cbFailed)�
assertFailure�addCallback)rK�deferredZmainTyperJrL�dr<rIr=�assertWrapperExceptionTypesHs
rQcCst||t|�S)zo
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{ResponseFailed}.
    )rQr/�rKrOrJr<r<r=�assertResponseFailedlsrScCst||t|�S)zx
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestGenerationFailed}.
    )rQr+rRr<r<r=�assertRequestGenerationFailedusrTcCst||t|�S)zz
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestTransmissionFailed}.
    )rQr,rRr<r<r=�assertRequestTransmissionFailed~srUcCstdddt|�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�rW���OK)r-�_boringHeaders)�	transportr<r<r=�justTransportResponse�sr\c@seZdZdZdd�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�AcSsdS�Nr<�rKr<r<r=�bar�szGMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo.barZquuxcSsdS)N�ar<r`r<r<r=�_quux_A�szKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_AcSsdS)N�br<r`r<r<r=�_quux_B�szKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_BN)r8r9r:�_staterar'rcrer<r<r<r=�Foo�s

rgrb�Brd�CN)rDrarf�assertRaises�RuntimeError)rKrgZstatefulr<r<r=�test_functionCalledByState�s
z6MakeStatefulDispatcherTests.test_functionCalledByStateN)r8r9r:r;rlr<r<r<r=r]�sr]c@sdeZdZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�ZdS)�_HTTPParserTestszt
    Base test class for L{HTTPParser} which is responsible for the bulk of
    the task of parsing HTTP bytes.
    NcCs\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�makeConnectionrrD�stater�dataReceived�sepr)rK�status�protocolr<r<r=�test_statusCallback�sz$_HTTPParserTests.test_statusCallbackcCs6i}t�}|j|_|�t��|�d|j�||fS)Nrn)r�__setitem__�headerReceivedrqrrsrt�rK�headerrvr<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|rsrtrDrrrrzr<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	quuxNrrzr<r<r=�test_continuedHeaderCallback�sz-_HTTPParserTests.test_continuedHeaderCallbackcCs^|��\}}|j�ddddg�}|�d|�|�d|�|�|j�|�|ddd��d	S)
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|rt�joinrsrD)rKr{rv�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�_dSr_)rorrrr<�Zcalledrvr<r=�allHeadersReceivedszD_HTTPParserTests.test_allHeadersCallback.<locals>.allHeadersReceivedN)r|r�rsrtrDrrrr)rKr{r�r<r�r=�test_allHeadersCallback�sz(_HTTPParserTests.test_allHeadersCallbackcCs6|��\}}|�|j�|�|i�|�|jt�dS)zp
        If there are no headers in the message, L{HTTPParser} does not call
        C{headerReceived}.
        Nrrzr<r<r=�test_noHeaderCallback
s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}.
        rnr��
X-Foo: bazr}r~�bazN)	rrqrrsrtrD�list�headers�getAllRawHeaders)rKrv�expectedr<r<r=�test_headersSavedOnResponsesz,_HTTPParserTests.test_headersSavedOnResponsecCsRt�}ddddddddg}|D]}|�|�|�d	|f�q|�|�d
�d�dS)
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�
assertTrueZisConnectionControlHeader�assertFalse)rKrvZconnHeaderNamesr{r<r<r=�test_connectionControlHeaders$s*����z._HTTPParserTests.test_connectionControlHeaderscCs4t�}|�t��|�t��|�t|jt��dS)zi
        L{HTTPParser.switchToBodyMode} raises L{RuntimeError} if called more
        than once.
        N)rrqrZswitchToBodyMode�objectrjrk�rKrvr<r<r=�test_switchToBodyMode:sz&_HTTPParserTests.test_switchToBodyMode)r8r9r:r;rtrwr|r�r�r�r�r�r�r�r�r<r<r<r=rm�s	rmc@seZdZdZdZdS)�$HTTPParserRFCComplaintDelimeterTestsz<
    L{_HTTPParserTests} using standard CR LF newlines.
    �
N�r8r9r:r;rtr<r<r<r=r�Fsr�c@seZdZdZdZdS)�'HTTPParserNonRFCComplaintDelimeterTestsz5
    L{_HTTPParserTests} using bare LF newlines.
    �
Nr�r<r<r<r=r�Nsr�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: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��)rrD�parseVersionr�r<r<r=�test_parseVersion[s

�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|�dSr_)rjrD�data��s�exc��e�frKr<r=�checkParsingosz@HTTPClientParserTests.test_parseBadVersion.<locals>.checkParsing�foosfoo/bar/bazsfoo/sfoo/..sfoo/a.bs	foo/-1.-1)rrr�)rKrvr�r<r�r=�test_parseBadVersionfs
z*HTTPClientParserTests.test_parseBadVersioncCsbtddtd�}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
rVrXrY�r*rZrrqrrsrD�response�version�code�phrase�rK�requestrvr<r<r=�test_responseStatusParsing}s

z0HTTPClientParserTests.test_responseStatusParsingcCsbtddtd�}t|d�}|�t��|�d�|�|jjd�|�|jj	d�|�|jj
d�dS)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
rVrXr�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|�dSr_)rjrrprDr�r��rvrKr<r=r��szBHTTPClientParserTests.test_badResponseStatus.<locals>.checkParsingr�sHTTP/1.1 bar OK)r)rKr�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.
        cs
��d�S�NT�ror<�ZbodyDataFinishedr<r=�<lambda>�r�z3HTTPClientParserTests._noBodyTest.<locals>.<lambda>�	producingr�Tr)rrorxryrrqrsr��_bodyDataReceived�_bodyDataFinishedrD�
producerStaterrr�length)	rKr�rur�r{�finished�bodyrvr[r<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*rZr�rD)rKr�rur�r{r<r<r=�test_headResponse�s�z'HTTPClientParserTests.test_headResponsecCs(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*rZr��rKr�rur�r<r<r=�test_noContentResponse�sz,HTTPClientParserTests.test_noContentResponsecCs(tddtd�}d}d}|�|||�dS)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�sz.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�NcSsdSr_r<��restr<r<r=r�r�z<HTTPClientParserTests.test_responseHeaders.<locals>.<lambda>r�sX-Foo: bar
r��x-foor~)rr*rZrqrrsrD�connHeadersr1r�r��assertIdenticalr�rr�r<r<r=�test_responseHeaders�s"�


��z*HTTPClientParserTests.test_responseHeaderscCs�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�NcSsdSr_r<r�r<r<r=r�r�z>HTTPClientParserTests.test_connectionHeaders.<locals>.<lambda>r��Content-Length: 123
sConnection: close
r��123sclose)r�r��{�rr*rZrqrrsrDr�r�r1r�r�r�r<r<r=�test_connectionHeaderss(�



���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�NcSsdSr_r<r�r<r<r=r�,r�zRHTTPClientParserTests.test_headResponseContentLengthEntityHeader.<locals>.<lambda>r�r�r�r�r�rr�r�r<r<r=�*test_headResponseContentLengthEntityHeader%s"�


��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�)rr*rZrorrqrsr�r�rDr�rrrr�rKr�rvr[r�r<r<r=�test_contentLength:s*�






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)
rr*rZrorqrrsr�r�rDrrrr��rKr�rvr�r<r<r=�test_zeroContentLengthZs�



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

)rr*rZrqrrj�
ValueErrorrsr�r<r<r=�!test_multipleContentLengthHeadersts��z7HTTPClientParserTests.test_multipleContentLengthHeaderscCsdg}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!�
rr*rZrorqrrsrDrrr�rKr�rvr<r<r=�test_extraBytesPassedBack�s�


z/HTTPClientParserTests.test_extraBytesPassedBackcCsdg}ttddtd�|j�}|�t��|�d�|�d�|�d�|�|jt	�|�|dg�dS)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
r�r�r�r�r<r<r=�test_extraBytesPassedBackHEAD�s�


z3HTTPClientParserTests.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)
rr*rZrorqrrsr�r�rDr�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�)rr*rZrorrqrsr�r�rD�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�)
rr*rZrorrqrsr�r�rDr�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)
rrr*rZrq�_responseDeferredr�rr6rS)rKr[rvZresponseDeferredr<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

rcSs
t��dSr_�r6)rFr<r<r=�fakeBodyDataFinished1szPHTTPClientParserTests.test_connectionLostWithError.<locals>.fakeBodyDataFinishedrW�log_failure)N)r�createWithCleanupr5rrr*rZrqr�rNrorsr�r��assertEqualsrE�assertIsInstancer�r6�flushLoggedErrors)rK�logObserverr[rvr�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�NcSsdSr_r<�Zignr<r<r=r�Dr�z<HTTPClientParserTests.test_noResponseAtAll.<locals>.<lambda>)
rr*rZr�rqrr�r
rMr0�rKrvrPr<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�NcSsdSr_r<r�r<r<r=r�Tr�zBHTTPClientParserTests.test_someResponseButNotAll.<locals>.<lambda>�2)
rr*rZr�rqrrsr�r
rMr/rNr�r�r<r<r=�test_someResponseButNotAllLs�
�z0HTTPClientParserTests.test_someResponseButNotAllcCs�d}ttddtd�dd��}|�t��|�|�|�t|dd�dk�|�|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�NcSsdSr_r<r�r<r<r=r�mr�zCHTTPClientParserTests.test_1XXResponseIsSwallowed.<locals>.<lambda>r�r)rr*rZrqrrsr��getattrrDrrrrEr�r�r�r�Z_everReceivedData)rK�sample103Responservr<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�NcSsdSr_r<r�r<r<r=r��r�zUHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinal.<locals>.<lambda>rXr�r�r��
rr*rZrqrrsrDr�r�r�r1r�r��rKrZfollowing200Responservr<r<r=�-test_1XXFollowedByFinalResponseOnlyEmitsFinalzs(�����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.
        rrr�r�NcSsdSr_r<r�r<r<r=r��r�zKHTTPClientParserTests.test_multiple1XXResponsesAreIgnored.<locals>.<lambda>rXr�r�r�rrr<r<r=�#test_multiple1XXResponsesAreIgnored�s8���������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.
        rr�r�NcSsdSr_r<r�r<r<r=r��r�zHHTTPClientParserTests.test_ignored1XXResponseCausesLog.<locals>.<lambda>rWr�
log_formatz#Ignoring unexpected {code} responser��g)rr�r5rr*rZrqrrsr�rE)rKr�rrvr�r<r<r=� test_ignored1XXResponseCausesLog�s&���
�z6HTTPClientParserTests.test_ignored1XXResponseCausesLogN) r8r9r: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�rrrrr<r<r<r=r�Vs:& &$#(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�|_|jSr_)rr��rKr[r<r<r=�writeTo�szSlowRequest.writeTocCs
d|_dSr���stoppedr`r<r<r=�stopWriting�szSlowRequest.stopWritingN)	r8r9r:r;�methodr�
persistentrrr<r<r<r=r�s
rc@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)�writerr
r<r<r=rs
zSimpleRequest.writeToN)r8r9r:r;rrr<r<r<r=r�src@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)rr[r(rvrqr`r<r<r=�setUpszHTTP11ClientProtocolTests.setUpcCs$|j�t��|�|j��d�dS)z�
        L{HTTP11ClientProtocol.request} accepts a L{Request} and calls its
        C{writeTo} method with its own transport.
        rN)rvr�rrDr[r�r`r<r<r=�test_requestsz&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.
        cs���j��d�dS�Nr��rDr[r��Zignoredr`r<r=�	cbNotSent,sz?HTTP11ClientProtocolTests.test_secondRequest.<locals>.cbNotSent)rvr�rrMrr!rN�rKrrPr<r`r=�test_secondRequest%s�
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 transportcs���j��d�dSrrrr`r<r=r<szLHTTP11ClientProtocolTests.test_requestAfterConnectionLost.<locals>.cbNotSent)	rvr�rr	rMr�rr!rNrr<r`r=�test_requestAfterConnectionLost4s
��
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@seZdZdZdd�ZdS)zCHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequestFcSs
tt��Sr_)r
r6r
r<r<r=rMszKHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequest.writeToN�r8r9r:rrr<r<r<r=�
BrokenRequestKsr"cs&���jj��j�ttd���dS)Nzyou asked for it)r�r[�
disconnectingrvr�rr	rr`r<r=rLQs
�z>HTTP11ClientProtocolTests.test_failedWriteTo.<locals>.cbFailed)rvr�rTr6rN)rKr"rPrLr<r`r=�test_failedWriteToDs
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@seZdZdZdd�ZdS)zMHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequestFcSs
t��dSr_r�r
r<r<r=rdszUHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequest.writeToNr!r<r<r<r=r"bsr")rvr�rTr6)rKr"rPr<r<r=�test_synchronousWriteToError\sz6HTTP11ClientProtocolTests.test_synchronousWriteToErrorNcCs�t�}|j�|�}t||tg�}|�|j�|j�tt���|�	|j�|dkr`|j
�d�n6|dkr�|j
�tt
���|�t
�}|�t|�d�n|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�errbackrW)rrvr�rUr6r�rr�rr�r�r&r'r>r�rDrE)rK�moder�rP�errorsr<r<r=�*test_connectionLostDuringRequestGenerationks
zDHTTP11ClientProtocolTests.test_connectionLostDuringRequestGenerationcCs
|�d�S)z�
        If the request passed to L{HTTP11ClientProtocol} finishes generation
        successfully after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        r&�r*r`r<r<r=�+test_connectionLostBeforeGenerationFinished�szEHTTP11ClientProtocolTests.test_connectionLostBeforeGenerationFinishedcCs
|�d�S)z�
        If the request passed to L{HTTP11ClientProtocol} finished generation
        with an error after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        r'r+r`r<r<r=�)test_connectionLostBeforeGenerationFailed�szCHTTP11ClientProtocolTests.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)NrWrr�r	zJError writing request, but not in valid state to finalize request: {state}rr�CONNECTION_LOST)r�rE�assertInrD)�ignorer��r�rKr<r=�check�s
�znHTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse.<locals>.checkr')rr�r5r*rN)rKr2r<r1r=�Etest_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse�s�	��z_HTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfusecs<�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)NrX�	QUIESCENT)	rDr�r�r1r�r[r#rvrr�r�r`r<r=�	cbRequest�szIHTTP11ClientProtocolTests.test_receiveSimplestResponse.<locals>.cbRequests9HTTP/1.1 200 OK
Content-Length: 0
Connection: close

�rvr�r*rZrNrs�rKrPr6r<r`r=�test_receiveSimplestResponse�s
�z6HTTP11ClientProtocolTests.test_receiveSimplestResponsecs<�j�tddtd��}�fdd�}|�|��j�d�|S)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�)r1rDr�)r�r�r`r<r=r6�szHHTTP11ClientProtocolTests.test_receiveResponseHeaders.<locals>.cbRequests+HTTP/1.1 200 OK
X-Foo: bar
X-Foo: baz

r7r8r<r`r=�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)NZ%TRANSMITTING_AFTER_RECEIVING_RESPONSEcs
��jfSr_�r�r���pr�r<r=r��r�zoHTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)	rr�closedDeferred�deliverBodyrDrrr�r#rN�r��whenFinished)rv�quiescentResultrKr[r<r=�
cbResponse�s
��z]HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponsecsf|\}}��|jd���|jd���|jd���|jtddgi����|d��j�d�dS)NrVrXrYr�r~�foobar)rDr�r�r�r�r1r�r&)�resultr�r��r�rKr<r=�
cbAllResponsesz`HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)rr(rorqrr�rsrN�rKrPrCrGr<)rvrBr�rKr[r=�/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(rvrqrZ
MAX_LENGTHr�r*rZrsrSr	)rKr[rvZlongLinerPr<r<r=�"test_receiveResponseHeadersTooLongs

���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)Ncs
��jfSr_r;r�r<r<r=r�=r�z�HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)rrr>r?rNr@r<r<r=rC8s
�zrHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponsecs2�j�d��j�tt������jjd�dS)Nr.)r�r&rvr�rr6rDrf)r0rFr<r=rG?szuHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)rrvr�rsrNrHr<rFr=�Dtest_connectionLostAfterReceivingResponseBeforeRequestGenerationDone+s�

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)NrD)rDr��closedReasonrCr.rr�r<r=rGiszIHTTP11ClientProtocolTests.test_receiveResponseBody.<locals>.cbAllResponse)rrr>rvr�r*rZrsrNrorDr?)rKrA�requestDeferredrEr�rGr<r�r=�test_receiveResponseBodyHs"�	

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~rDz low-level transport disconnected)rvr�r*rZrsrNrorr?rDr�r�rr	rLrCr)�rKrMrEr�rvr<r<r=�Etest_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknownps&��

�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
rD)rvr�r*rZrsrNrorr?rDr�r�rr6rSr
rLr2rOr<r<r=�4test_chunkedResponseBodyUnfinishedWhenConnectionLost�s*��
�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�r[r#rDrBr�r�rvr�rr	�r�r`r<r=rL�s�zLHTTP11ClientProtocolTests.test_parserDataReceivedException.<locals>.cbFailed)rvr�r*rZrsrSrrN)rKrMrPrLr<r`r=� 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)rvr�r*rZ�_parserr[r��	_producerZ_disconnectParserrr	rS)rKrMr[r<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(rq�abortrNrorDr�r#r�rr	)rKr[rvZr1Zr2r<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(rqr�rr	rXrNrorDrf�rKr[rvrEr<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(rqr�r*rZrXr�r#r�rr	rSr"rZr<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)Zlenientr�r�Nr�cs(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)rXr`)rvr<r=�connectionMade-sz`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)r'�rKrG)�
testResultr<r=r�3sz`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionLostN)r8r9r:r;r]r�r<)rvr_r<r=�BodyDestination(s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)r?r�r#r�rr	r5)r`rvrKr[r<r=r?;szMHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.deliverBodycs��|jt�dSr_)r�r�r-)�errorr`r<r=�
checkErrorFszLHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.checkError)
rr(rqr�r*rZrsrrrNrSr"r2)rKrEr?rbrOr<)r`rvrKr_r[r=�test_abortAfterResponseHeaderss 

�
�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.
        cs(��|����|jd���|�dS�Nr4�rDrrro�r=�rvrBrKr<r=r&]szHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.callbackr�r�NT�rs&HTTP/1.1 200 OK
Content-length: 3

rcs
��d�S)N�
response doner�r�)rBr<r=r�yr�zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.<lambda>r�ri)rr(rqr�r*rZrsrDrNrorrr>r?rLrCr.rUZ_finishedRequestZ_currentRequestZ_transportProxyr�)rKr&r[rMrEr��bodyProtocolr<rgr=�test_quiescentCallbackCalledPs:
��
�

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.
        cs(��|����|jd���|�dSrdrerfrgr<r=r&�sz]HTTP11ClientProtocolTests.test_transportProducingWhenQuiescentAfterFullBody.<locals>.callbackr�r�NTrhs)HTTP/1.1 200 OK
Content-length: 3

BBBZDEFERRED_CLOSErWr�)rr(rqr�r*rZrsZsuccessResultOfrDrfrEr�)rKr&r[rMr�r<rgr=�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.
        cs(��|����|jd���|�dSrdrerfrgr<r=r&�szUHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponse.<locals>.callbackr�r�NTrh�&HTTP/1.1 200 OK
Content-length: 0

r�rrW)rr(rqr�r*rZrNrorsrDrEr�r�r-)rKr&r[rMr<rgr=�)test_quiescentCallbackCalledEmptyResponse�s
��zCHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponsec	Cs�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�NTrhs9HTTP/1.1 200 OK
Content-length: 0
Connection: close

r�rr(rorqr�r*rZrsrNrr?rLrCr.rDr�r#�rKrBr[rvrMrEr�rjr<r<r=�test_quiescentCallbackNotCalled�s$

��
z9HTTP11ClientProtocolTests.test_quiescentCallbackNotCalledc	Cs�g}t�}t|j�}|�|�|�tddtddd��}|�d�g}|�|j�|d}t	�}|�
|�|j�t
�|�|g�|�|j�dS)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�NFrhrmrrorpr<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.
        cSs
t��dSr_)�ZeroDivisionErrorrfr<r<r=r&szHHTTP11ClientProtocolTests.test_quiescentCallbackThrows.<locals>.callbackr�r�NTrhrmrrWr�)rr�r5rr(rqr�r*rZrsrNrorr?rLrCr.r�rEr�r�rsr�r�r#)rKr&r�r[rvrMrEr�rjr�r�r<r<r=�test_quiescentCallbackThrows
s4�
��

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(rqr�r*rZ�cancelr��disconnectedrQr0rrZr<r<r=�test_cancelBeforeResponse5s
�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(rqr�r*rZrsrur�rvrSrrZr<r<r=�test_cancelDuringResponseFs

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)NTryr<r�)�nonLocalr<r=rudszJHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.cancelcs|�_t���_�jSr_��consumerrr�)r|)ru�producerr<r=�startProducingfs
zRHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.startProducing�POST�/barsxxxxx)rr(rq�StringProducerr~r�r*rZr|rrur�rvrTr)rKZproducerLengthr[rvr~rEr<)rurzr}r=� assertCancelDuringBodyProductionWs 
�z:HTTP11ClientProtocolTests.assertCancelDuringBodyProductioncCs
|�d�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 an explicit length has finished producing.
        �
)r�r`r<r<r=�test_cancelDuringBodyProductionusz9HTTP11ClientProtocolTests.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�rr`r<r<r=�&test_cancelDuringChunkedBodyProductionsz@HTTP11ClientProtocolTests.test_cancelDuringChunkedBodyProduction)N)'r8r9r:r;rrrr r$r%r*r,r-r3r9r:rIrJrKrNrPrRrTrWrYr[r\rcrkrlrnrqrrrtrwrxr�r�r�r<r<r<r=rsH		
!		1( @:-(
rc@s,eZdZdZdZdd�Zdd�Zdd�Zd	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
||_dSr_)r�)rKr�r<r<r=�__init__�szStringProducer.__init__cCs||_t�|_|jSr_r{)rKr|r<r<r=r~�szStringProducer.startProducingcCs
d|_dSr�rr`r<r<r=�
stopProducing�szStringProducer.stopProducingN)r8r9r:r;rr�r~r�r<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 d!�Zd"d#�Zd$d%�Zd3d'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd&S)4�RequestTestsz
    Tests for L{Request}.
    cCst�|_dSr_)rr[r`r<r<r=r�szRequestTests.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*rZrr[rDr�r`r<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�NTrhs%GET / HTTP/1.1
Host: example.com

r�)rKZreqr<r<r=�"test_sendSimplestPersistentRequest�s�z/RequestTests.test_sendSimplestPersistentRequestcCs�tddgdgd��}tdd|d��|j�|j���d�}|�|d	d
�|�|dd�ddg�|d	=|dd�=|��|�|d
dddg�dS)zf
        L{Request.writeTo} formats header data and writes it to the given
        transport.
        r~r�r@)r�r?r��/fooNr�rsGET /foo HTTP/1.1���r�sConnection: closesHost: example.comr�r�)r1r*rr[r��splitrD�sort)rKr��linesr<r<r=�test_sendRequestHeaders�s"���z$RequestTests.test_sendRequestHeaderscCs�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.
        rr�NsXPOST /bar HTTP/1.1
Connection: close
Transfer-Encoding: chunked
Host: example.com

sxxxsyyyyyyyyyyyyyyys!3
xxx
f
yyyyyyyyyyyyyyy
0

)r�rr*rZrr[�assertNotIdenticalr|r�r}r��	streamingrDr��clearrr�r&�rKr}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.
        rr�cs&���j��d����jjd�dSr)rDr[r�r�r}rr`r<r=rLszCRequestTests.test_sendChunkedRequestBodyWithError.<locals>.cbFailed)r�rr*rZrr[r�r�r'r6rMrN)rKr}r��
writeDeferredrLrPr<r`r=�$test_sendChunkedRequestBodyWithErrors	

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.
        �rr�NsOPOST /bar HTTP/1.1
Connection: close
Content-Length: 3
Host: example.com

r�)r�r*rZrr[r�r|r�r}r�r�rDr�r�rr�r&r�r<r<r=�test_sendRequestBodyWithLengths�
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�)rKrr�r<r<r=�_sendRequestEmptyBodyWithLength9s	�z,RequestTests._sendRequestEmptyBodyWithLengthcCs|�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�r`r<r<r=�test_sendPUTRequestEmptyBodyNsz)RequestTests.test_sendPUTRequestEmptyBodycCs|�d�dS)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.
        rNr�r`r<r<r=�test_sendPOSTRequestEmptyBodyWsz*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�rr��abN)
r�r*rZrr[r|rr�r&r�r}rMr �rKr}r�r�r<r<r=�#test_sendRequestBodyWithTooFewBytes`s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�rr�r��cdNcsL���j��d��j����t�jjd�������j��d�dS)NsQPOST /bar HTTP/1.1
Connection: close
Content-Length: 3
Host: example.com

absefr�)rDr[r�r�rjr$r|rrS��finisherr}rKr<r=rL�s�
zCRequestTests._sendRequestBodyWithTooManyBytesTest.<locals>.cbFailed)r�r*rZrr[r|rr�rr�r�r}rMr rN)rKr�r�r�rLrPr<r�r=�$_sendRequestBodyWithTooManyBytesTestps
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.
        cSs|j�d�dSr_�r�r&�r}r<r<r=r��szCRequestTests.test_sendRequestBodyWithTooManyBytes.<locals>.finisher)r��rKr�r<r<r=�$test_sendRequestBodyWithTooManyBytes�sz1RequestTests.test_sendRequestBodyWithTooManyBytescs$t��t����fdd�}��|�S)r�csV|j�t���d}��d|�|d}��|jt���t�}��t|�d�dS)Nrr�rW)	r�r'r6r/r�r�r�rDrE)r}r�r�r)r1r<r=r��s
zHRequestTests.test_sendRequestBodyErrorWithTooManyBytes.<locals>.finisher)rr�r5r�r�r<r1r=�)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�rr�r�Nrr�rW)rr�r5r�r*rZrr[r|Z	_finishedrr�r&r'r6r/r�r�rDrEr�)rKr�r}r�ZfinishedConsumingr�r�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�rr�r�Nr�r�)r�r*rZrr[r|rr�r}r�rjr$rDr�)rKr�r}r�r�r<r<r=�-_sendRequestBodyFinishedEarlyThenTooManyBytes�s
z:RequestTests._sendRequestBodyFinishedEarlyThenTooManyBytescCsdd�}|�|�|�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}.
        cSs|j�d�dSr_r�r�r<r<r=r�	szPRequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytes.<locals>.finisher)rMr�r r�r<r<r=�1test_sendRequestBodyFinishedEarlyThenTooManyBytes	s
�z>RequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytescCsdd�}|�|�|�t�S)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��dSr_)r�r'r6r�r<r<r=r�	szORequestTests.test_sendRequestBodyErroredEarlyThenTooManyBytes.<locals>.finisher)rMr�r6r�r<r<r=�0test_sendRequestBodyErroredEarlyThenTooManyBytes	s
�z=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.
        rr�r�r�)r�rr*rZrr[r�r&r�rjr$r|rrDr�)rKZ_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.
        )r�rr6rM)rKrPr<r<r=�9test_sendChunkedRequestBodyFinishedWithErrorThenWriteMore5	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}.
        �rr�r�sQPOST /bar HTTP/1.1
Connection: close
Content-Length: 5
Host: example.com

abN)r�r*rZrr[r�r}r�r�r|rrDr�r�r#r�r'rr6rMr�r<r<r=�test_sendRequestBodyWithErrorA	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�sHostr@sexample.org)r*r1rjr#rr[rDr�)rKr�r<r<r=�test_hostHeaderRequiredh	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*rZrr[r�rrr�r�r<r<r=�test_stopWritingw	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�cSstd��dS�NzstopProducing is bustedr�r<r<r<r=�brokenStopProducing�	szBRequestTests.test_brokenStopProducing.<locals>.brokenStopProducingr�r�rWrr�N)rr�r5r�r�r*rZrr[rrDrEr�r6r�r/r�r�)rKr�r}r�r�r�r�r<r<r=�test_brokenStopProducing�	s&��z%RequestTests.test_brokenStopProducing)N)r8r9r:r;rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r<r<r<r=r��s2
%		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�)rrEr�r}rr[r&�enforcerr`r<r<r=r�	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.
        r�sdefN)r�rrDr[r�r�r`r<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)r�rrjr �_noMoreWritesExpectedr`r<r<r=�test_finishedEarly�	sz/LengthEnforcingConsumerTests.test_finishedEarlyFcCsP|j�d�|�|jj�|j�d�|�|jj�|rB|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)
r�rr�r}rr�r�rMrEr )rKZ_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}.
        r�r�N)	r�rr�r�r}rrjr$r�r`r<r<r=�test_writeAfterNoMoreExpected�	s

z:LengthEnforcingConsumerTests.test_writeAfterNoMoreExpectedcCs
|�d�S)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)r�r`r<r<r=�test_finishedLate�	sz.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}.
        r�N)r�rr�r�r`r<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��dSr�)r�r�r}r6r<r`r<r=r��	szRLengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.brokenStopProducingcs��t��t��d�dS)NrW)rDrEr�r6rr`r<r=�
cbFinished�	s�zILengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.cbFinished)r}r�r�rN)rKr�r�rPr<r`r=�test_stopProducingRaises�	s
z5LengthEnforcingConsumerTests.test_stopProducingRaisesN)F)r8r9r:r;rr�r�r�r�r�r�r�r<r<r<r=r��	s



		r�c@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�rrrrr`r<r<r=�test_interface
s�z'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�rQsxxxxxxxxxxxxxxxxs10
xxxxxxxxxxxxxxxx
N)rrrrDr�r�)rKr[�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�rZregisterProducerr�r}r�r�ZunregisterProducerrDr�)rKr[r}r�r<r<r=�test_producerRegistration$
sz2RequestBodyConsumerTests.test_producerRegistrationN)r8r9r:r;r�r�r�r<r<r<r=r�
sr�c@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�rrr%r`r<r<r=r�<
s�z*TransportProxyProducerTests.test_interfacecCs6t�}t|�}|�|j|�|��|�|jd�dS)z~
        L{TransportProxyProducer.stopProxying} drops the reference to the
        wrapped L{IPushProducer} provider.
        N)rr%r�rV�stopProxying�rKr[�proxyr<r<r=�%test_stopProxyingUnreferencesProducerD
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)r�pauseProducingr%rDr��resumeProducingr�r�r<r<r=�test_resumeProducingP
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%rDr�r�r�r�r�r<r<r=�test_pauseProducingg
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�rN)rr%rDr�r�r�r�r<r<r=�test_stopProducing}
sz.TransportProxyProducerTests.test_stopProducingcCsdt�}t�}|�|�||_t|�}|�|j�|�|jd�|�	�|�|jd�|�
|j�dS)zs
        L{TransportProxyProducer.loseConnection} calls the wrapped transport's
        C{loseConnection}.
        r�N)rrrqrvr%r��	connectedrDr��loseConnectionr��rKr[rvr�r<r<r=� test_loseConnectionWhileProxying�
s
z<TransportProxyProducerTests.test_loseConnectionWhileProxyingcCsPt�}t�}|�|�||_t|�}|��|�|j�|��|�|j�dS)zm
        L{TransportProxyProducer.loseConnection} does nothing when the
        proxy is not active.
        N)	rrrqrvr%r�r�r�r�r�r<r<r=�test_loseConnectionNotProxying�
s
z:TransportProxyProducerTests.test_loseConnectionNotProxyingN)r8r9r:r;r�r�r�r�r�r�r�r<r<r<r=r�7
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)r\rr�rr4�rKr�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.
        cseZdZ�fdd�ZdS)z7ResponseTests.test_makeConnection.<locals>.SomeProtocolcs��|�dSr_r�)rKr}�Z	producersr<r=rq�
szFResponseTests.test_makeConnection.<locals>.SomeProtocol.makeConnectionN�r8r9r:rqr<r�r<r=�SomeProtocol�
sr�r�r�N)rrr\r?r�rDr�r�)rKr[r�r|r�ZtheProducerr<r�r=�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.
        cseZdZ�fdd�ZdS)z5ResponseTests.test_dataReceived.<locals>.ListConsumercs��|�dSr_r��rKr���bytesr<r=rs�
szBResponseTests.test_dataReceived.<locals>.ListConsumer.dataReceivedN�r8r9r:rsr<r�r<r=�ListConsumer�
sr�r�N)rr\rr?r�rD�rKr�r|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.
        cseZdZ�fdd�ZdS)z7ResponseTests.test_connectionLost.<locals>.ListConsumercs��|�dSr_r�r^�Zlostr<r=r��
szFResponseTests.test_connectionLost.<locals>.ListConsumer.connectionLostN)r8r9r:r�r<r�r<r=r��
sr�rrWN)rr\rr?r�rCr.rDrEr�Z
_bodyProtocolr�r<r�r=�test_connectionLost�
s

z!ResponseTests.test_connectionLostcstg�G�fdd�dt�}|�}tt��}|�d�|�d�|�|�|�d�|��dddg�|�|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.
        cseZdZ�fdd�ZdS)z8ResponseTests.test_bufferEarlyData.<locals>.ListConsumercs��|�dSr_r�r�r�r<r=rsszEResponseTests.test_bufferEarlyData.<locals>.ListConsumer.dataReceivedNr�r<r�r<r=r�sr�r�r~r�N)rr\rr�r?rDr�Z_bodyBuffer)rKr�rvr�r<r�r=�test_bufferEarlyDatas




z"ResponseTests.test_bufferEarlyDatacCs,tt��}|�t��|�t|jt��dS)zb
        L{Response.deliverBody} raises L{RuntimeError} if called more than
        once.
        N)r\rr?rrjrkr�r<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)r\rr?rr�rjrkr�r<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)r\rr�rjrkr�r�r<r<r=�'test_bodyDataReceivedAfterFinishedFails7s
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)r\rr�r?rrjrkr�r�r<r<r=�'test_bodyDataReceivedAfterDeliveryFailsAs
z5ResponseTests.test_bodyDataReceivedAfterDeliveryFailscCs$tt��}|��|�t|j�dS)zh
        L{Response._bodyDataFinished} raises L{RuntimeError} if called more
        than once.
        N)r\rr�rjrkr�r<r<r=�'test_bodyDataFinishedAfterFinishedFailsLs
z5ResponseTests.test_bodyDataFinishedAfterFinishedFailscCs0tt��}|��|�t��|�t|j�dS)z{
        L{Response._bodyDataFinished} raises L{RuntimeError} if called after
        the body has been delivered.
        N)r\rr�r?rrjrkr�r<r<r=�'test_bodyDataFinishedAfterDeliveryFailsVs
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.
        cseZdZ�fdd�ZdS)z9ResponseTests.test_transportResumed.<locals>.ListConsumercs��|j�dSr_)ror�r
�ZtransportStater<r=rqhszHResponseTests.test_transportResumed.<locals>.ListConsumer.makeConnectionNr�r<r�r<r=r�gsr�r�r�N)rrr�r\rDr�r?)rKr�r[rvr�r<r�r=�test_transportResumedas
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~rDN)rr\r�r�rr?rDr�rLrCr.�rKr[r�rvr<r<r=�)test_bodyDataFinishedBeforeStartProducingus


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.
        Z	CONNECTEDN)rr\rr?rDrfr�rr6rLrCr�r<r<r=�#test_finishedWithErrorWhenConnected�s
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.
        ZINITIALN)rr\rDrfr�rr6rr?rLrCr�r<r<r=�!test_finishedWithErrorWhenInitial�s
z/ResponseTests.test_finishedWithErrorWhenInitialN)r8r9r:r;r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r<r<r<r=r��
s 


r�N)`r;Z
__future__rrrHZ
__metaclass__Zzope.interfacerZzope.interface.verifyrZtwisted.python.failurerZtwisted.internet.interfacesrrZtwisted.internet.errorr	r
Ztwisted.internet.deferrrr
rZtwisted.internet.protocolrZtwisted.protocols.basicrZtwisted.trial.unittestrZtwisted.test.proto_helpersrrrrZtwisted.web._newclientrrrrrrrrrrr r!r"r#r$r%r&r'Ztwisted.web.clientr(r)r*r+r,r-r.r/r0Ztwisted.web.http_headersr1Ztwisted.web.httpr2Ztwisted.web.iwebr3r4Ztwisted.loggerr5�	Exceptionr6r>rZrQrSrTrUr\r]r�rmr�r�r�rrrr�r�r�r�r�r�r<r<r<r=�<module>s�,$			yi/