U
    2ÜŸjˆ· ã                   @   s¦  d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZ ddlmZ ddlm Z  ddl!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/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZD ddlEmFZFmGZG G dd„ deHƒZIG dd„ deHƒZJeAddgiƒZKdd„ ZLdd„ ZMdd„ ZNd d!„ ZOd"d#„ ZPG d$d%„ d%e ƒZQG d&d'„ d'ƒZRG d(d)„ d)eRe ƒZSG d*d+„ d+eRe ƒZTG d,d-„ d-e ƒZUG d.d/„ d/ƒZVG d0d1„ d1ƒZWG d2d3„ d3e ƒZXeeCƒG d4d5„ d5ƒƒZYG d6d7„ d7e ƒZZG d8d9„ d9e ƒZ[G d:d;„ d;e ƒZ\G d<d=„ d=e ƒZ]G d>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)ÚAccumulatingProtocolÚEventLoggingObserverÚStringTransportÚ StringTransportWithDisconnection)ÚglobalLogPublisher)ÚLineReceiver)ÚFailure)Ú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                   @   s   e Zd ZdZdS )ÚArbitraryExceptionze
    A unique, arbitrary exception type which L{twisted.web._newclient} knows
    nothing about.
    N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r=   r=   úC/tmp/pip-unpacked-wheel-4b529toj/twisted/web/test/test_newclient.pyr7   C   s   r7   c                   @   s   e Zd ZdZdS )ÚAnotherArbitraryExceptionzI
    Similar to L{ArbitraryException} but with a different identity.
    Nr8   r=   r=   r=   r>   r?   J   s   r?   ó   hostó   example.comc                    s(   ‡ ‡fdd„}ˆ  ||¡}| |¡ |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              	      sL   t | jˆ ƒD ]\}}| |¡ qˆ t| jƒtˆ ƒd| j› dˆ › d�¡ | S )Nzlen(z	) != len(ú))ÚzipÚreasonsÚtrapÚassertEqualÚlen)ÚerrÚreasonÚtype©ÚreasonTypesÚselfr=   r>   ÚcbFailedn   s    ýz-assertWrapperExceptionTypes.<locals>.cbFailed)ÚassertFailureÚaddCallback)rM   ÚdeferredZmainTyperL   rN   Údr=   rK   r>   ÚassertWrapperExceptionTypesU   s    

rS   c                 C   s   t | |t|ƒS )zo
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{ResponseFailed}.
    )rS   r/   ©rM   rQ   rL   r=   r=   r>   ÚassertResponseFailed}   s    rU   c                 C   s   t | |t|ƒS )zx
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestGenerationFailed}.
    )rS   r+   rT   r=   r=   r>   ÚassertRequestGenerationFailed…   s       ÿrV   c                 C   s   t | |t|ƒS )zz
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestTransmissionFailed}.
    )rS   r,   rT   r=   r=   r>   ÚassertRequestTransmissionFailed�   s       ÿrW   c                 C   s   t dddt| ƒ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.
    ©s   HTTPé   rY   éÈ   ó   OK)r-   Ú_boringHeaders)Ú	transportr=   r=   r>   ÚjustTransportResponse™   s    r^   c                   @   s   e Zd ZdZdd„ ZdS )ÚMakeStatefulDispatcherTestsz.
    Tests for L{makeStatefulDispatcher}.
    c                 C   sR   G dd„ dƒ}|ƒ }|   | ¡ d¡ d|_|   | ¡ d¡ d|_|  t|j¡ dS )z‹
        A method defined with L{makeStatefulDispatcher} invokes a second
        method based on the current state of the object.
        c                   @   s2   e Zd ZdZdd„ ZedeƒZdd„ Zdd„ Zd	S )
zCMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.FooÚAc                 S   s   d S ©Nr=   ©rM   r=   r=   r>   Úbar±   s    zGMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo.barZquuxc                 S   s   dS )NÚar=   rb   r=   r=   r>   Ú_quux_A¶   s    zKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_Ac                 S   s   dS )NÚbr=   rb   r=   r=   r>   Ú_quux_B¹   s    zKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_BN)r9   r:   r;   Ú_staterc   r'   re   rg   r=   r=   r=   r>   ÚFoo®   s
   
ri   rd   ÚBrf   ÚCN)rF   rc   rh   ÚassertRaisesÚRuntimeError)rM   ri   Zstatefulr=   r=   r>   Útest_functionCalledByState¨   s    z6MakeStatefulDispatcherTests.test_functionCalledByStateN)r9   r:   r;   r<   rn   r=   r=   r=   r>   r_   £   s   r_   c                   @   sr   e Zd ZU dZdZee ed< dd„ Zdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú_HTTPParserTestszt
    Base test class for L{HTTPParser} which is responsible for the bulk of
    the task of parsing HTTP bytes.
    NÚsepc                 C   s\   g }t ƒ }|j|_| tƒ ¡ |  |jt¡ | d| j	 ¡ |  |dg¡ |  |jt
¡ dS )zj
        L{HTTPParser} calls its C{statusReceived} method when it receives a
        status line.
        ó   HTTP/1.1 200 OKN)r!   ÚappendÚstatusReceivedÚmakeConnectionr   rF   Ústater   ÚdataReceivedrp   r   )rM   ÚstatusÚprotocolr=   r=   r>   Útest_statusCallbackÌ   s    z$_HTTPParserTests.test_statusCallbackc                 C   s6   i }t ƒ }|j|_| tƒ ¡ | d| j ¡ ||fS )Nrq   )r!   Ú__setitem__ÚheaderReceivedrt   r   rv   rp   ©rM   Úheaderrx   r=   r=   r>   Ú_headerTestSetupÚ   s    z!_HTTPParserTests._headerTestSetupc                 C   sJ   |   ¡ \}}| d| j ¡ | | j¡ |  |ddi¡ |  |jt¡ dS )ze
        L{HTTPParser} calls its C{headerReceived} method when it receives a
        header.
        s	   X-Foo:baró   X-Fooó   barN©r~   rv   rp   rF   ru   r   r|   r=   r=   r>   Útest_headerCallbackâ   s
    z$_HTTPParserTests.test_headerCallbackc                 C   sj   |   ¡ \}}| d| j ¡ | d| j ¡ | d| j ¡ | | j¡ |  |ddi¡ |  |jt¡ dS )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_continuedHeaderCallbackc                 C   s^   |   ¡ \}}| j ddddg¡}| d| ¡ | d| ¡ | | j¡ |  |dddœ¡ d	S )
zŒ
        Leading and trailing linear whitespace is stripped from the header
        value passed to the C{headerReceived} callback.
        s    	 s    bar 	s    	ó    s   X-Bar:s   X-Foo:r€   )r   s   X-BarN)r~   rp   Újoinrv   rF   )rM   r}   rx   Úvaluer=   r=   r>   Útest_fieldContentWhitespaceü   s    z,_HTTPParserTests.test_fieldContentWhitespacec                    sP   g ‰ |   ¡ \}‰‡ ‡fdd„}|ˆ_ˆ | j¡ |  ˆ tg¡ |  ˆjt¡ dS )zg
        After the last header is received, L{HTTPParser} calls
        C{allHeadersReceived}.
        c                      s   ˆ   ˆj¡ tˆ_d S ra   )rr   ru   r   r=   ©Úcalledrx   r=   r>   ÚallHeadersReceived  s    zD_HTTPParserTests.test_allHeadersCallback.<locals>.allHeadersReceivedN)r~   r‹   rv   rp   rF   r   ru   r   )rM   r}   r‹   r=   r‰   r>   Útest_allHeadersCallback  s    z(_HTTPParserTests.test_allHeadersCallbackc                 C   s6   |   ¡ \}}| | j¡ |  |i ¡ |  |jt¡ dS )zp
        If there are no headers in the message, L{HTTPParser} does not call
        C{headerReceived}.
        Nr�   r|   r=   r=   r>   Útest_noHeaderCallback  s    z&_HTTPParserTests.test_noHeaderCallbackc                 C   sv   t ƒ }| tƒ ¡ | d| j ¡ | d| j ¡ | d| j ¡ | | j¡ dddgfg}|  |t|j ¡ ƒ¡ dS )zc
        All headers received by L{HTTPParser} are added to
        L{HTTPParser.headers}.
        rq   rƒ   ó
   X-Foo: bazr   r€   ó   bazN)	r!   rt   r   rv   rp   rF   ÚlistÚheadersÚgetAllRawHeaders)rM   rx   Úexpectedr=   r=   r>   Útest_headersSavedOnResponse#  s    z,_HTTPParserTests.test_headersSavedOnResponsec                 C   sR   t ƒ }ddddddddg}|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-alives   tes   trailerss   transfer-encodings   upgrades   proxy-connectionz:Expecting %r to be a connection control header, but wasn'ts   datez`Expecting the arbitrarily selected 'date' header to not be a connection control header, but was.N)r!   Ú
assertTrueZisConnectionControlHeaderÚassertFalse)rM   rx   ZconnHeaderNamesr}   r=   r=   r>   Útest_connectionControlHeaders1  s*    øÿþþz._HTTPParserTests.test_connectionControlHeadersc                 C   s4   t ƒ }| tƒ ¡ | tƒ ¡ |  t|jtƒ ¡ dS )zi
        L{HTTPParser.switchToBodyMode} raises L{RuntimeError} if called more
        than once.
        N)r!   rt   r   ZswitchToBodyModeÚobjectrl   rm   ©rM   rx   r=   r=   r>   Útest_switchToBodyModeO  s    z&_HTTPParserTests.test_switchToBodyMode)r9   r:   r;   r<   rp   r   ÚbytesÚ__annotations__ry   r~   r‚   r„   rˆ   rŒ   r�   r”   r™   rœ   r=   r=   r=   r>   ro   Ä   s   

ro   c                   @   s   e Zd ZdZdZdS )Ú$HTTPParserRFCComplaintDelimeterTestsz<
    L{_HTTPParserTests} using standard CR LF newlines.
    ó   
N©r9   r:   r;   r<   rp   r=   r=   r=   r>   rŸ   Z  s   rŸ   c                   @   s   e Zd ZdZdZdS )Ú'HTTPParserNonRFCComplaintDelimeterTestsz5
    L{_HTTPParserTests} using bare LF newlines.
    ó   
Nr¡   r=   r=   r=   r>   r¢   b  s   r¢   c                   @   s   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Z d<d=„ Z!d>d?„ Z"d@dA„ Z#dBdC„ Z$dDdE„ Z%dFS )GÚHTTPClientParserTestszd
    Tests for L{HTTPClientParser} which is responsible for parsing HTTP
    response messages.
    c                 C   s    t ddƒ}|  | d¡d¡ dS )zj
        L{HTTPClientParser.parseVersion} parses a status line into its three
        components.
        Ns	   CANDY/7.2)s   CANDYé   é   )r    rF   ÚparseVersionr›   r=   r=   r>   Útest_parseVersionp  s    
z'HTTPClientParserTests.test_parseVersionc                    sX   t ddƒ}t‰ |j‰‡ ‡‡fdd„}|dƒ |dƒ |dƒ |dƒ |dƒ |d	ƒ dS )
zr
        L{HTTPClientParser.parseVersion} raises L{ValueError} when passed an
        unparsable version.
        Nc                    s    ˆ  ˆ ˆ| ¡}ˆ |j| ¡ d S ra   )rl   rF   Údata©ÚsÚexc©ÚeÚfrM   r=   r>   ÚcheckParsing�  s    z@HTTPClientParserTests.test_parseBadVersion.<locals>.checkParsingó   foos   foo/bar/bazs   foo/s   foo/..s   foo/a.bs	   foo/-1.-1)r    r   r§   )rM   rx   r°   r=   r­   r>   Útest_parseBadVersionx  s    
z*HTTPClientParserTests.test_parseBadVersionc                 C   sb   t ddtdƒ}t|dƒ}| tƒ ¡ | d¡ |  |jjd¡ |  |jj	d¡ |  |jj
d¡ dS )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
rX   rZ   r[   ©r*   r\   r    rt   r   rv   rF   ÚresponseÚversionÚcodeÚphrase©rM   Úrequestrx   r=   r=   r>   Útest_responseStatusParsingŽ  s    

z0HTTPClientParserTests.test_responseStatusParsingc                 C   sb   t ddtdƒ}t|dƒ}| tƒ ¡ | d¡ |  |jjd¡ |  |jj	d¡ |  |jj
d¡ dS )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´   Ns   HTTP/1.1 200
rX   rZ   r…   r¶   r»   r=   r=   r>   Ú test_responseStatusWithoutPhrase›  s    

z6HTTPClientParserTests.test_responseStatusWithoutPhrasec                    s,   t ddƒ‰ ‡ ‡fdd„}|dƒ |dƒ dS )zŒ
        L{HTTPClientParser.statusReceived} raises L{ParseError} if it is called
        with a status line which cannot be parsed.
        Nc                    s"   ˆ  tˆ j| ¡}ˆ |j| ¡ d S ra   )rl   r#   rs   rF   r©   rª   ©rx   rM   r=   r>   r°   °  s    zBHTTPClientParserTests.test_badResponseStatus.<locals>.checkParsingr±   s   HTTP/1.1 bar OK)r    )rM   r°   r=   r¿   r>   Útest_badResponseStatus©  s    
z,HTTPClientParserTests.test_badResponseStatusc           	         sº   i }g }g }g ‰ t ||jƒ}|j|_tƒ }| |¡ | |¡ |j|j_‡ fdd„|j_	| |¡ |  
|jd¡ |  
|jt¡ |  
|g ¡ |  
|dg¡ |  
ˆ dg¡ |  
|jjd¡ |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                      s
   ˆ   d¡S ©NT©rr   r=   ©ZbodyDataFinishedr=   r>   Ú<lambda>Ö  r…   z3HTTPClientParserTests._noBodyTest.<locals>.<lambda>Ú	producingr…   Tr   )r    rr   rz   r{   r   rt   rv   r·   Ú_bodyDataReceivedÚ_bodyDataFinishedrF   ÚproducerStateru   r   Úlength)	rM   r¼   rw   r·   r}   ÚfinishedÚbodyrx   r]   r=   rÃ   r>   Ú_noBodyTest¼  s&    



z!HTTPClientParserTests._noBodyTestc                 C   s8   t ddtdƒ}d}d}|  |||¡}|  |ddi¡ dS )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µ   s   Content-Length: 10

s   Content-Lengths   10)r*   r\   rÌ   rF   )rM   r¼   rw   r·   r}   r=   r=   r>   Útest_headResponseà  s
    z'HTTPClientParserTests.test_headResponsec                 C   s(   t ddtdƒ}d}d}|  |||¡ dS )z€
        If the response code is I{NO CONTENT} (204), no body is expected and
        the body callback is not invoked.
        r³   r´   Ns   HTTP/1.1 204 NO CONTENT
r    ©r*   r\   rÌ   ©rM   r¼   rw   r·   r=   r=   r>   Útest_noContentResponseì  s    z,HTTPClientParserTests.test_noContentResponsec                 C   s(   t ddtdƒ}d}d}|  |||¡ dS )z‚
        If the response code is I{NOT MODIFIED} (304), no body is expected and
        the body callback is not invoked.
        r³   r´   Ns   HTTP/1.1 304 NOT MODIFIED
r    rÏ   rÐ   r=   r=   r>   Útest_notModifiedResponseö  s    z.HTTPClientParserTests.test_notModifiedResponsec                 C   s‚   t tddtdƒdd„ ƒ}| tƒ ¡ | d¡ | d¡ | d¡ |  |jti ƒ¡ |  |j	j
td	d
giƒ¡ |  |j	jt¡ dS )zq
        The response headers are added to the response object's C{headers}
        L{Headers} instance.
        r³   r´   Nc                 S   s   d S ra   r=   ©Úrestr=   r=   r>   rÄ     r…   z<HTTPClientParserTests.test_responseHeaders.<locals>.<lambda>rµ   s   X-Foo: bar
r    ó   x-foor€   ©r    r*   r\   rt   r   rv   rF   ÚconnHeadersr2   r·   r‘   ÚassertIdenticalrÉ   r   r›   r=   r=   r>   Útest_responseHeaders   s     ÿ


z*HTTPClientParserTests.test_responseHeadersc                 C   sŒ   t tddtdƒdd„ ƒ}| tƒ ¡ | d¡ | d¡ | d¡ | d	¡ |  |jti ƒ¡ |  |j	j
td
dgiƒ¡ |  |j	jt¡ dS )z‡
        The multi-line response headers are folded and added to the response
        object's C{headers} L{Headers} instance.
        r³   r´   Nc                 S   s   d S ra   r=   rÓ   r=   r=   r>   rÄ     r…   zEHTTPClientParserTests.test_responseHeadersMultiline.<locals>.<lambda>rµ   s   X-Multiline: a
s       b
r    s   x-multilines   a    brÖ   r›   r=   r=   r>   Útest_responseHeadersMultiline  s     ÿ



 ÿz3HTTPClientParserTests.test_responseHeadersMultilinec                 C   s�   t tddtdƒdd„ ƒ}| tƒ ¡ | d¡ | d¡ | d¡ | d	¡ |  |jjt	i ƒ¡ |  |j
t	d
gdgdœƒ¡ |  |jjd¡ dS )zv
        The connection control headers are added to the parser's C{connHeaders}
        L{Headers} instance.
        r³   r´   Nc                 S   s   d S ra   r=   rÓ   r=   r=   r>   rÄ   )  r…   z>HTTPClientParserTests.test_connectionHeaders.<locals>.<lambda>rµ   ó   Content-Length: 123
s   Connection: close
r    ó   123s   close)r•   r–   é{   ©r    r*   r\   rt   r   rv   rF   r·   r‘   r2   r×   rÉ   r›   r=   r=   r>   Útest_connectionHeaders#  s     ÿ



þz,HTTPClientParserTests.test_connectionHeadersc                 C   s‚   t tddtdƒdd„ ƒ}| tƒ ¡ | d¡ | d¡ | d¡ |  |jjt	d	d
giƒ¡ |  |j
t	i ƒ¡ |  |jjd¡ dS )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´   Nc                 S   s   d S ra   r=   rÓ   r=   r=   r>   rÄ   =  r…   zRHTTPClientParserTests.test_headResponseContentLengthEntityHeader.<locals>.<lambda>rµ   rÛ   r    r•   rÜ   r   rÞ   r›   r=   r=   r>   Ú*test_headResponseContentLengthEntityHeader7  s     ÿ


 ÿz@HTTPClientParserTests.test_headResponseContentLengthEntityHeaderc                 C   sÒ   g }t tddtdƒ|jƒ}tƒ }| |¡ | d¡ g }|j|j_| d¡ | d¡ |  	|j
d¡ |  	|jt¡ | d¡ |  	|dg¡ |  	|jt¡ | d	¡ |  	|dd	g¡ |  	|jt¡ |  	|d
g¡ dS )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µ   s   Content-Length: 10
r    Úpauseds   xxxxxxs   yyyyr…   )r    r*   r\   rr   r   rt   rv   r·   rÆ   rF   rÈ   ru   r   r   ©rM   rÊ   rx   r]   rË   r=   r=   r>   Útest_contentLengthI  s*     ÿ






z(HTTPClientParserTests.test_contentLengthc                 C   sŽ   g }t tddtdƒ|jƒ}| tƒ ¡ | d¡ g }|j|j_| d¡ | d¡ |  	|j
t¡ |  	|g ¡ |  	|dg¡ |  	|jjd¡ dS )	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*   r\   rr   rt   r   rv   r·   rÆ   rF   ru   r   rÉ   ©rM   rÊ   rx   rË   r=   r=   r>   Útest_zeroContentLengthh  s     ÿ



z,HTTPClientParserTests.test_zeroContentLengthc                 C   s4   t tddtdƒdƒ}| tƒ ¡ |  t|jd¡ dS )zè
        If a response includes multiple, distinct 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´   Ns9   HTTP/1.1 200 OK
Content-Length: 1
Content-Length: 2

©r    r*   r\   rt   r   rl   Ú
ValueErrorrv   r›   r=   r=   r>   Ú)test_multipleDistinctContentLengthHeaders�  s    ýz?HTTPClientParserTests.test_multipleDistinctContentLengthHeadersc                 C   sL   t tddtdƒdƒ}| tƒ ¡ | d¡ |  |jjd¡ |  |j	t
¡ dS )zË
        If a response includes multiple, yet equal, I{Content-Length} headers,
        L{HTTPClientParser.dataReceived} successfully handles and passes single
        length for body processing
        r³   r´   Ns9   HTTP/1.1 200 OK
Content-Length: 1
Content-Length: 1

rY   ©r    r*   r\   rt   r   rv   rF   r·   rÉ   ru   r   r›   r=   r=   r>   Ú&test_multipleEqualContentLengthHeaders“  s    ÿz<HTTPClientParserTests.test_multipleEqualContentLengthHeadersc                 C   s4   t tddtdƒdƒ}| tƒ ¡ |  t|jd¡ dS )zô
        If a response includes multiple, distinct I{Content-Length} header
        field-values, L{HTTPClientParser.dataReceived} raises L{ValueError} to
        indicate that the response is invalid and the transport is now unusable.
        r³   r´   Ns)   HTTP/1.1 200 OK
Content-Length: 1, 2

rç   r›   r=   r=   r>   Ú3test_multipleDistinctContentLengthHeaderFieldValues¥  s    ýzIHTTPClientParserTests.test_multipleDistinctContentLengthHeaderFieldValuesc                 C   sL   t tddtdƒdƒ}| tƒ ¡ | d¡ |  |jjd¡ |  |j	t
¡ dS )a%  
        If a response includes multiple, equal I{Content-Length} header
        field-values, L{HTTPClientParser.dataReceived} successfully handles and
        passes single content-length header for body processing. Field-values
        are considered equal by parsed decimal value.
        r³   r´   Ns+   HTTP/1.1 200 OK
Content-Length: 1, 001

rY   rê   r›   r=   r=   r>   Ú0test_multipleEqualContentLengthHeaderFieldValues´  s    ÿzFHTTPClientParserTests.test_multipleEqualContentLengthHeaderFieldValuesc                 C   s4   t tddtdƒdƒ}| tƒ ¡ |  t|jd¡ dS )zç
        If the I{Content-Length} header contains anything other than digits
        L{HTTPClientParser.dataReceived} raises L{ValueError} to
        indicate that the response is invalid and the transport is now unusable.
        r³   r´   Ns'   HTTP/1.1 200 OK
Content-Length: +1

rç   r›   r=   r=   r>   Útest_contentLengthTooPositiveÄ  s    ýz3HTTPClientParserTests.test_contentLengthTooPositivec                 C   s4   t tddtdƒdƒ}| tƒ ¡ |  t|jd¡ dS )zØ
        If the I{Content-Length} header has a negative value
        L{HTTPClientParser.dataReceived} raises L{ValueError} to
        indicate that the response is invalid and the transport is now unusable.
        r³   r´   Ns'   HTTP/1.1 200 OK
Content-Length: -1

rç   r›   r=   r=   r>   Útest_contentLengthNegativeÓ  s    ýz0HTTPClientParserTests.test_contentLengthNegativec                 C   sd   g }t tddtdƒ|jƒ}| tƒ ¡ | d¡ | d¡ | d¡ |  |jt	¡ |  |dg¡ dS )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*   r\   rr   rt   r   rv   rF   ru   r   ©rM   rÊ   rx   r=   r=   r>   Útest_extraBytesPassedBackâ  s     ÿ


z/HTTPClientParserTests.test_extraBytesPassedBackc                 C   sd   g }t tddtdƒ|jƒ}| tƒ ¡ | d¡ | d¡ | d¡ |  |jt	¡ |  |dg¡ dS )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µ   s   Content-Length: 12
rð   rñ   rò   ró   r=   r=   r>   Útest_extraBytesPassedBackHEADó  s     ÿ


z3HTTPClientParserTests.test_extraBytesPassedBackHEADc                 C   s¼   g }t tddtdƒ|jƒ}| tƒ ¡ | d¡ g }|j|j_| d¡ | d¡ |  	|g ¡ |  
|jjt¡ | d¡ |  	|dg¡ | d	¡ |  	|dd
g¡ | d¡ |  	|dg¡ dS )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µ   s   Transfer-Encoding: chunked
r    s   3
aó   as   bc
s   bcs
   0

extras   extra)r    r*   r\   rr   rt   r   rv   r·   rÆ   rF   rØ   rÉ   r   rå   r=   r=   r>   Útest_chunkedResponseBody  s&     ÿ






z.HTTPClientParserTests.test_chunkedResponseBodyc                 C   s�   g }t tddtdƒ|jƒ}tƒ }| |¡ | d¡ g }|j|j_| d¡ | d¡ | d¡ |  	|ddg¡ | 
tdƒ¡ |  	|d	g¡ dS )
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€   zsimulated end of connectionr…   )r    r*   r\   rr   r   rt   rv   r·   rÆ   rF   ÚconnectionLostr	   râ   r=   r=   r>   Útest_unknownContentLength)  s      ÿ





z/HTTPClientParserTests.test_unknownContentLengthc                 C   sl   g }t tddtdƒ|jƒ}tƒ }| |¡ | d¡ g }|j|j_| d¡ |  	|dg¡ |  	|dg¡ dS )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*   r\   rr   r   rt   rv   r·   rÆ   rF   râ   r=   r=   r>   Ú%test_contentLengthAndTransferEncodingA  s     ÿ


ÿ
z;HTTPClientParserTests.test_contentLengthAndTransferEncodingc                 C   sH   t ƒ }ttddtdƒdƒ}| |¡ |j}| ttƒ ƒ¡ t	| |tgƒ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)
r   r    r*   r\   rt   Ú_responseDeferredrø   r   r7   rU   )rM   r]   rx   ZresponseDeferredr=   r=   r>   Útest_connectionLostBeforeBody_  s    
z3HTTPClientParserTests.test_connectionLostBeforeBodyc                 C   sª   t  | t¡}tƒ }ttddtdƒdƒ}| |¡ g }|j 	|j
¡ | d¡ |d }d
dd„}||_| d¡ |  dt|ƒ¡ |d }|d	 }|  |jt¡ |  t¡ dS )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

r   c                 S   s
   t ƒ ‚d S ra   ©r7   )rH   r=   r=   r>   ÚfakeBodyDataFinished  s    zPHTTPClientParserTests.test_connectionLostWithError.<locals>.fakeBodyDataFinishedrY   Úlog_failure)N)r   ÚcreateWithCleanupr   r   r    r*   r\   rt   rü   rP   rr   rv   rÇ   rø   ÚassertEqualsrG   ÚassertIsInstancer‡   r7   ÚflushLoggedErrors)rM   ÚlogObserverr]   rx   r·   r   Úeventr¯   r=   r=   r>   Útest_connectionLostWithErrorn  s     



z2HTTPClientParserTests.test_connectionLostWithErrorc                 C   sB   t tddtdƒdd„ ƒ}|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´   Nc                 S   s   d S ra   r=   ©Zignr=   r=   r>   rÄ   ‘  r…   z<HTTPClientParserTests.test_noResponseAtAll.<locals>.<lambda>)
r    r*   r\   rü   rt   r   rø   r
   rO   r0   ©rM   rx   rR   r=   r=   r>   Útest_noResponseAtAll‹  s     ÿz*HTTPClientParserTests.test_noResponseAtAllc                 C   sV   t tddtdƒdd„ ƒ}|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´   Nc                 S   s   d S ra   r=   r	  r=   r=   r>   rÄ      r…   zBHTTPClientParserTests.test_someResponseButNotAll.<locals>.<lambda>ó   2)r    r*   r\   rü   rt   r   rv   rø   r
   rO   r/   rP   r  r
  r=   r=   r>   Útest_someResponseButNotAll™  s     ÿ
 ÿz0HTTPClientParserTests.test_someResponseButNotAllc                 C   sš   d}t tddtdƒdd„ ƒ}| tƒ ¡ | |¡ |  t|ddƒdk¡ |  |j	t
¡ |  tt|j ¡ ƒƒd¡ |  tt|j ¡ ƒƒd¡ |  |j¡ dS )	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´   Nc                 S   s   d S ra   r=   r	  r=   r=   r>   rÄ   ¹  r…   zCHTTPClientParserTests.test_1XXResponseIsSwallowed.<locals>.<lambda>r·   r   )r    r*   r\   rt   r   rv   r—   ÚgetattrrF   ru   r   rG   r�   r‘   r’   r×   Z_everReceivedData)rM   Úsample103Responserx   r=   r=   r>   Útest_1XXResponseIsSwallowed«  s    ÿ ÿ
z1HTTPClientParserTests.test_1XXResponseIsSwallowedc                 C   sŠ   d}d}t tddtdƒdd„ ƒ}| tƒ ¡ | || ¡ |  |jjd¡ |  |jj	t
i ƒ¡ |  |jt
d	d
giƒ¡ |  |jjd¡ dS )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´   Nc                 S   s   d S ra   r=   r	  r=   r=   r>   rÄ   Ô  r…   zUHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinal.<locals>.<lambda>rZ   r•   rÜ   rÝ   ©r    r*   r\   rt   r   rv   rF   r·   r¹   r‘   r2   r×   rÉ   ©rM   r  Zfollowing200Responserx   r=   r=   r>   Ú-test_1XXFollowedByFinalResponseOnlyEmitsFinalÅ  s    ÿ ÿzCHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinalc                 C   s’   d}d}t tddtdƒdd„ ƒ}| tƒ ¡ | || | | ¡ |  |jjd¡ |  |jj	t
i ƒ¡ |  |jt
d	d
giƒ¡ |  |jjd¡ dS )zp
        It is acceptable for multiple 1XX responses to come through, all of
        which get ignored.
        r  r  r³   r´   Nc                 S   s   d S ra   r=   r	  r=   r=   r>   rÄ   í  r…   zKHTTPClientParserTests.test_multiple1XXResponsesAreIgnored.<locals>.<lambda>rZ   r•   rÜ   rÝ   r  r  r=   r=   r>   Ú#test_multiple1XXResponsesAreIgnoredÞ  s*    ÿ ÿÿþýÿz9HTTPClientParserTests.test_multiple1XXResponsesAreIgnoredc                 C   sz   t  | t¡}d}ttddtdƒdd„ ƒ}| tƒ ¡ | |¡ |  	dt
|ƒ¡ |d }|  	|d	 d
¡ |  	|d d¡ dS )zF
        When a 1XX response is ignored, Twisted emits a log.
        r  r³   r´   Nc                 S   s   d S ra   r=   r	  r=   r=   r>   rÄ   
  r…   zHHTTPClientParserTests.test_ignored1XXResponseCausesLog.<locals>.<lambda>rY   r   Ú
log_formatz#Ignoring unexpected {code} responser¹   ég   )r   r  r   r    r*   r\   rt   r   rv   r  rG   )rM   r  r  rx   r  r=   r=   r>   Ú test_ignored1XXResponseCausesLogü  s    ÿ ÿ
z6HTTPClientParserTests.test_ignored1XXResponseCausesLogN)&r9   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÷   rù   rû   rý   r  r  r  r  r  r  r  r=   r=   r=   r>   r¤   j  sF   $

%r¤   c                   @   s,   e Zd ZdZdZdZdZdd„ Zdd„ ZdS )	Ú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³   Fc                 C   s   t ƒ | _| jS ra   )r   rÊ   ©rM   r]   r=   r=   r>   ÚwriteTo%  s    zSlowRequest.writeToc                 C   s
   d| _ d S rÁ   ©Ústoppedrb   r=   r=   r>   ÚstopWriting)  s    zSlowRequest.stopWritingN)	r9   r:   r;   r<   Úmethodr  Ú
persistentr  r  r=   r=   r=   r>   r    s   r  c                   @   s   e Zd ZdZdZdd„ ZdS )Ú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.
    Fc                 C   s   |  d¡ td ƒS )Nó
   SOME BYTES)Úwriter   r  r=   r=   r>   r  7  s    
zSimpleRequest.writeToN)r9   r:   r;   r<   r!  r  r=   r=   r=   r>   r"  -  s   r"  c                   @   s*  e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dIdd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAdB„ Z#dCdD„ Z$dEdF„ Z%dGdH„ Z&dS )JÚHTTP11ClientProtocolTestsz]
    Tests for the HTTP 1.1 client protocol implementation,
    L{HTTP11ClientProtocol}.
    c                 C   s"   t ƒ | _tƒ | _| j | j¡ dS )zR
        Create an L{HTTP11ClientProtocol} connected to a fake transport.
        N)r   r]   r(   rx   rt   rb   r=   r=   r>   ÚsetUpB  s    zHTTP11ClientProtocolTests.setUpc                 C   s$   | j  tƒ ¡ |  | j ¡ d¡ dS )z†
        L{HTTP11ClientProtocol.request} accepts a L{Request} and calls its
        C{writeTo} method with its own transport.
        r#  N)rx   r¼   r"  rF   r]   r‡   rb   r=   r=   r>   Útest_requestJ  s    z&HTTP11ClientProtocolTests.test_requestc                    s>   ˆ j  tƒ ¡ ‡ fdd„}ˆ  ˆ 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                    s   ˆ   ˆ j ¡ d¡ d S ©Nr…   ©rF   r]   r‡   ©Zignoredrb   r=   r>   Ú	cbNotSentZ  s    z?HTTP11ClientProtocolTests.test_secondRequest.<locals>.cbNotSent)rx   r¼   r  rO   r"  r$   rP   ©rM   r+  rR   r=   rb   r>   Útest_secondRequestR  s
    
z,HTTP11ClientProtocolTests.test_secondRequestc                    sD   ˆ j  ttdƒƒ¡ ‡ fdd„}ˆ  ˆ 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 transportc                    s   ˆ   ˆ j ¡ d¡ d S r(  r)  r*  rb   r=   r>   r+  i  s    zLHTTP11ClientProtocolTests.test_requestAfterConnectionLost.<locals>.cbNotSent)	rx   rø   r   r	   rO   r¼   r"  r$   rP   r,  r=   rb   r>   Útest_requestAfterConnectionLosta  s
    
z9HTTP11ClientProtocolTests.test_requestAfterConnectionLostc                    sD   G dd„ dƒ}ˆ j  |ƒ ¡}‡ fdd„}tˆ |tgƒ}| |¡ |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                   @   s   e Zd ZdZdd„ ZdS )zCHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequestFc                 S   s
   t tƒ ƒS ra   )r   r7   r  r=   r=   r>   r  {  s    zKHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequest.writeToN©r9   r:   r;   r!  r  r=   r=   r=   r>   ÚBrokenRequestx  s   r0  c                    s&   ˆ   ˆ jj¡ ˆ j ttdƒƒ¡ d S )Nzyou asked for it)r—   r]   Údisconnectingrx   rø   r   r	   r*  rb   r=   r>   rN   €  s    z>HTTP11ClientProtocolTests.test_failedWriteTo.<locals>.cbFailed)rx   r¼   rV   r7   rP   )rM   r0  rR   rN   r=   rb   r>   Útest_failedWriteTop  s    
z,HTTP11ClientProtocolTests.test_failedWriteToc                 C   s*   G dd„ dƒ}| j  |ƒ ¡}t| |tgƒ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                   @   s   e Zd ZdZdd„ ZdS )zMHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequestFc                 S   s
   t ƒ ‚d S ra   rÿ   r  r=   r=   r>   r  ”  s    zUHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequest.writeToNr/  r=   r=   r=   r>   r0  ‘  s   r0  )rx   r¼   rV   r7   )rM   r0  rR   r=   r=   r>   Útest_synchronousWriteToErrorŠ  s    z6HTTP11ClientProtocolTests.test_synchronousWriteToErrorNc                 C   sš   t ƒ }| j |¡}t| |tgƒ}|  |j¡ | j ttƒ ƒ¡ |  	|j¡ |dkr`|j
 d¡ n6|dkr–|j
 ttƒ ƒ¡ |  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ÚerrbackrY   )r  rx   r¼   rW   r7   r˜   r  rø   r   r—   rÊ   r4  r5  r?   r  rF   rG   )rM   Úmoder¼   rR   Úerrorsr=   r=   r>   Ú*test_connectionLostDuringRequestGenerationš  s    
zDHTTP11ClientProtocolTests.test_connectionLostDuringRequestGenerationc                 C   s
   |   d¡S )zÂ
        If the request passed to L{HTTP11ClientProtocol} finishes generation
        successfully after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        r4  ©r8  rb   r=   r=   r>   Ú+test_connectionLostBeforeGenerationFinishedº  s    zEHTTP11ClientProtocolTests.test_connectionLostBeforeGenerationFinishedc                 C   s
   |   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.
        r5  r9  rb   r=   r=   r>   Ú)test_connectionLostBeforeGenerationFailedÂ  s    zCHTTP11ClientProtocolTests.test_connectionLostBeforeGenerationFailedc                    s*   t  ˆt¡‰ ‡ ‡fdd„}ˆ 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.
        c                    sH   ˆ  dtˆ ƒ¡ ˆ d }ˆ d|¡ ˆ |d d¡ ˆ |d d¡ d S )NrY   r   r  r  zJError writing request, but not in valid state to finalize request: {state}ru   ÚCONNECTION_LOST)r  rG   ÚassertInrF   )Úignorer  ©r  rM   r=   r>   ÚcheckÓ  s    þznHTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse.<locals>.checkr5  )r   r  r   r8  rP   )rM   r@  r=   r?  r>   ÚEtest_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuseÊ  s
    
ÿz_HTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfusec                    s<   ˆ j  tddtdƒ¡}‡ fdd„}| |¡ ˆ 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´   Nc                    s@   ˆ   | jd¡ ˆ   | jtƒ ¡ ˆ  ˆ jj¡ ˆ   ˆ jjd¡ d S )NrZ   Ú	QUIESCENT)	rF   r¹   r‘   r2   r—   r]   r1  rx   ru   ©r·   rb   r=   r>   Ú	cbRequestê  s    zIHTTP11ClientProtocolTests.test_receiveSimplestResponse.<locals>.cbRequests9   HTTP/1.1 200 OK
Content-Length: 0
Connection: close

©rx   r¼   r*   r\   rP   rv   ©rM   rR   rD  r=   rb   r>   Útest_receiveSimplestResponseâ  s    
ÿz6HTTP11ClientProtocolTests.test_receiveSimplestResponsec                    s<   ˆ j  tddtdƒ¡}‡ fdd„}| |¡ ˆ 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´   Nc                    s"   t dddgiƒ}ˆ  | j|¡ d S )NrÕ   r€   r�   )r2   rF   r‘   )r·   r“   rb   r=   r>   rD    s    zHHTTP11ClientProtocolTests.test_receiveResponseHeaders.<locals>.cbRequests+   HTTP/1.1 200 OK
X-Foo: bar
X-Foo: baz

rE  rF  r=   rb   r>   Útest_receiveResponseHeadersù  s    
ÿz5HTTP11ClientProtocolTests.test_receiveResponseHeadersc                    sp   g ‰t ƒ ‰tˆjƒ‰ ˆ  ˆ¡ tƒ ‰ˆ  ˆ¡}ˆ  d¡ ‡ ‡‡‡fdd„}| |¡ ‡‡fdd„}| |¡ |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.
        s8   HTTP/1.1 200 OK
X-Foo: bar
Content-Length: 6

foobarc                    sV   t ƒ ‰ tƒ  }ˆ _ˆ ˆ ¡ ˆ ˆjd¡ ˆ ˆj¡ ˆ ˆg ¡ | ‡ ‡fdd„¡S )NZ%TRANSMITTING_AFTER_RECEIVING_RESPONSEc                    s
   ˆˆ j fS ra   ©r©   r	  ©Úpr·   r=   r>   rÄ   *  r…   zoHTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)	r   r   ÚclosedDeferredÚdeliverBodyrF   ru   r—   r1  rP   ©r·   ÚwhenFinished)rx   ÚquiescentResultrM   r]   rJ  r>   Ú
cbResponse#  s    
z]HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponsec                    sf   | \}}ˆ  |jd¡ ˆ  |jd¡ ˆ  |jd¡ ˆ  |jtddgiƒ¡ ˆ  |d¡ ˆ j d ¡ d S )NrX   rZ   r[   rÕ   r€   ó   foobar)rF   r¸   r¹   rº   r‘   r2   rÊ   r4  )Úresultr·   rË   ©r¼   rM   r=   r>   ÚcbAllResponse.  s    z`HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)r   r(   rr   rt   r  r¼   rv   rP   ©rM   rR   rQ  rU  r=   )rx   rP  r¼   rM   r]   r>   Ú/test_receiveResponseBeforeRequestGenerationDone  s    	


ÿ	

zIHTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDonec                 C   sZ   t ƒ }tƒ }||_| |¡ dtj }| tddtdƒ¡}| 	d| d ¡ t
| |tgƒS )zy
        The connection is closed when the server respond with a header which
        is above the maximum line.
        rö   r³   r´   Ns   HTTP/1.1 200 OK
X-Foo: s   
X-Ignored: ignored

)r   r(   rx   rt   r   Z
MAX_LENGTHr¼   r*   r\   rv   rU   r	   )rM   r]   rx   ZlongLinerR   r=   r=   r>   Ú"test_receiveResponseHeadersTooLong=  s    

ÿÿÿz<HTTP11ClientProtocolTests.test_receiveResponseHeadersTooLongc                    sL   t ƒ ‰ ˆj ˆ ¡}ˆj d¡ dd„ }| |¡ ‡ ‡fdd„}| |¡ |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.
        s8   HTTP/1.1 400 BAD REQUEST
Content-Length: 9

tisk tiskc                    s0   t ƒ ‰ tƒ  }ˆ _ˆ ˆ ¡ | ‡ ‡fdd„¡S )Nc                    s
   ˆˆ j fS ra   rI  r	  rJ  r=   r>   rÄ   j  r…   z„HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)r   r   rL  rM  rP   rN  r=   rJ  r>   rQ  f  s    
zrHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponsec                    s2   ˆ j  d ¡ ˆj ttƒ ƒ¡ ˆ ˆjjd¡ d S )Nr<  )rÊ   r4  rx   rø   r   r7   rF   rh   )r>  rT  r=   r>   rU  n  s    zuHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)r  rx   r¼   rv   rP   rV  r=   rT  r>   ÚDtest_connectionLostAfterReceivingResponseBeforeRequestGenerationDoneW  s    ÿ

z^HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDonec                    s¢   t ƒ ‰ tƒ  }ˆ _ˆj tddtdƒ¡}ˆj d¡ g }| |j	¡ ˆ 
|g ¡ ˆj d¡ |d }| ˆ ¡ ˆj d¡ ˆj d¡ ‡ ‡fd	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£   r   r±   r€   c                    s   ˆ  ˆ jd¡ ˆ j t¡ d S )NrR  )rF   r©   ÚclosedReasonrE   r.   r*  r¿   r=   r>   rU  ™  s    zIHTTP11ClientProtocolTests.test_receiveResponseBody.<locals>.cbAllResponse)r   r   rL  rx   r¼   r*   r\   rv   rP   rr   rF   rM  )rM   rO  ÚrequestDeferredrS  r·   rU  r=   r¿   r>   Útest_receiveResponseBodyw  s&    ÿÿ

z2HTTP11ClientProtocolTests.test_receiveResponseBodyc                 C   s”   | j  tddtdƒ¡}| j  d¡ g }| |j¡ |d }tƒ }| |¡ | j  d¡ | j  d¡ |  	|j
d¡ | j  ttd	ƒƒ¡ |j t¡ dS )
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´   Ns   HTTP/1.1 200 OK

r   r±   r€   rR  z low-level transport disconnected)rx   r¼   r*   r\   rv   rP   rr   r   rM  rF   r©   rø   r   r	   rZ  rE   r)   ©rM   r[  rS  r·   rx   r=   r=   r>   ÚEtest_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknown   s     ÿ

ÿz_HTTP11ClientProtocolTests.test_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknownc                 C   s˜   | j  tddtdƒ¡}| j  d¡ g }| |j¡ |d }tƒ }| |¡ | j  d¡ | j  d¡ |  	|j
d¡ | j  ttƒ ƒ¡ t| t|jƒttgƒ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
s   3
bar
rR  )rx   r¼   r*   r\   rv   rP   rr   r   rM  rF   r©   rø   r   r7   rU   r   rZ  r1   r]  r=   r=   r>   Ú4test_chunkedResponseBodyUnfinishedWhenConnectionLost½  s(    ÿÿ
  ÿzNHTTP11ClientProtocolTests.test_chunkedResponseBodyUnfinishedWhenConnectionLostc                    sJ   ˆ j  tddtdƒ¡}ˆ j  d¡ tˆ |tgƒ}‡ fdd„}| |¡ |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´   Ns   unparseable garbage goes here
c                    s<   ˆ   ˆ jj¡ ˆ  | jd jjd¡ ˆ j t	t
dƒƒ¡ d S )Nr   s   unparseable garbage goes herez
it is done)r—   r]   r1  rF   rD   r‡   r©   rx   rø   r   r	   ©r¬   rb   r=   r>   rN   ê  s     ÿzLHTTP11ClientProtocolTests.test_parserDataReceivedException.<locals>.cbFailed)rx   r¼   r*   r\   rv   rU   r#   rP   )rM   r[  rR   rN   r=   rb   r>   Ú test_parserDataReceivedExceptionÝ  s    ÿ
z:HTTP11ClientProtocolTests.test_parserDataReceivedExceptionc                 C   s`   | j  tddtdƒ¡}| j jj}|  |j| j¡ | j  t	t
dƒƒ¡ |  |jd¡ 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´   Nzconnection done)rx   r¼   r*   r\   Ú_parserr]   rØ   Ú	_producerZ_disconnectParserr   r	   rU   )rM   r[  r]   r=   r=   r>   Útest_proxyStoppedù  s    ÿ
z+HTTP11ClientProtocolTests.test_proxyStoppedc                 C   sŽ   t ƒ }tƒ }| |¡ g }g }| ¡  |j¡ | ¡  |j¡ |  ||fg g f¡ |  |j¡ | 	t
tƒ ƒ¡ |  |dg¡ |  |dg¡ dS )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)r   r(   rt   ÚabortrP   rr   rF   r—   r1  rø   r   r	   )rM   r]   rx   Zr1Zr2r=   r=   r>   Útest_abortClosesConnection  s    
z4HTTP11ClientProtocolTests.test_abortClosesConnectionc                 C   sZ   t ƒ }tƒ }| |¡ | ttƒ ƒ¡ g }| ¡  |j¡ |  	|dg¡ |  	|j
d¡ dS )z‰
        L{HTTP11ClientProtocol.abort} called after the connection is lost
        returns a C{Deferred} that fires immediately.
        Nr<  )r   r(   rt   rø   r   r	   rf  rP   rr   rF   rh   ©rM   r]   rx   rS  r=   r=   r>   Útest_abortAfterConnectionLost  s    
z7HTTP11ClientProtocolTests.test_abortAfterConnectionLostc                 C   s\   t ƒ }tƒ }| |¡ | tddtdƒ¡}| ¡  |  |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)r   r(   rt   r¼   r*   r\   rf  r—   r1  rø   r   r	   rU   r   rh  r=   r=   r>   Útest_abortBeforeResponseBody,  s    
z6HTTP11ClientProtocolTests.test_abortBeforeResponseBodyc                    s–   t dd�‰tƒ ‰ˆ ˆ¡ ˆ tddtdƒ¡}ˆ d¡ tƒ ‰G ‡‡fdd„dtƒ‰ ‡ ‡‡‡fd	d
„}‡fdd„}| 	|¡ t
ˆˆtt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´   Nrþ   c                       s(   e Zd ZdZ‡ fdd„Z‡fdd„ZdS )zQHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestinationzl
            A body response protocol which immediately aborts the HTTP
            connection.
            c                    s   ˆ   ¡  dS )z<
                Abort the HTTP connection.
                N)rf  rb   )rx   r=   r>   ÚconnectionMadeV  s    z`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionMadec                    s   ˆ   |¡ dS )z�
                Make the reason for the losing of the connection available to
                the unit test via C{testResult}.
                N)r5  ©rM   rI   )Ú
testResultr=   r>   rø   \  s    z`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionLostN)r9   r:   r;   r<   rl  rø   r=   )rx   rn  r=   r>   ÚBodyDestinationP  s   ro  c                    s,   |   ˆ ƒ ¡ ˆ ˆj¡ ˆ ttƒ ƒ¡ dS )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)rM  r—   r1  rø   r   r	   rC  )ro  rx   rM   r]   r=   r>   rM  c  s    zMHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.deliverBodyc                    s   ˆ   | jt¡ d S ra   )r  r·   r-   )Úerrorrb   r=   r>   Ú
checkErrorm  s    zLHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.checkError)r   r(   rt   r¼   r*   r\   rv   r   r   rP   rU   r   r1   )rM   rS  rM  rq  rQ   r=   )ro  rx   rM   rn  r]   r>   Útest_abortAfterResponseHeaders<  s     




  ÿz8HTTP11ClientProtocolTests.test_abortAfterResponseHeadersc              	      s  g ‰‡ ‡‡fdd„}t ƒ }t|ƒ‰ ˆ  |¡ ˆ  tddtddd�¡}ˆ  d¡ ˆ ˆg ¡ g }| |j	¡ |d	 }t
ƒ }tƒ |_|j ‡fd
d„¡ | |¡ ˆ  d¡ |j t¡ ˆ ˆˆ dg¡ ˆ ˆ jd¡ ˆ ˆ jd¡ ˆ ˆ jd¡ ˆ ˆ jd¡ ˆ ˆ jd¡ dS )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                    s(   ˆ  | ˆ ¡ ˆ  | jd¡ ˆ | ¡ d S ©NrB  ©rF   ru   rr   ©rK  ©rx   rP  rM   r=   r>   r4  „  s    zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.callbackr³   r´   NT©r!  s&   HTTP/1.1 200 OK
Content-length: 3

r   c                    s
   ˆ   d¡S )Núresponse donerÂ   r	  )rP  r=   r>   rÄ   ž  r…   zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.<lambda>rú   rx  )r   r(   rt   r¼   r*   r\   rv   rF   rP   rr   r   r   rL  rM  rZ  rE   r.   rc  Z_finishedRequestZ_currentRequestZ_transportProxyrü   )rM   r4  r]   r[  rS  r·   ÚbodyProtocolr=   rv  r>   Útest_quiescentCallbackCalledv  s6    
ÿ

ÿ

z6HTTP11ClientProtocolTests.test_quiescentCallbackCalledc              	      sˆ   g ‰‡ ‡‡fdd„}t ƒ }t|ƒ‰ ˆ  |¡ ˆ  tddtddd�¡}ˆ  d¡ ˆ |¡}ˆ |j	d	¡ ˆ t
ˆƒd
¡ ˆ |jd¡ dS )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.
        c                    s(   ˆ  | ˆ ¡ ˆ  | jd¡ ˆ | ¡ d S rs  rt  ru  rv  r=   r>   r4  ¼  s    z]HTTP11ClientProtocolTests.test_transportProducingWhenQuiescentAfterFullBody.<locals>.callbackr³   r´   NTrw  s)   HTTP/1.1 200 OK
Content-length: 3

BBBZDEFERRED_CLOSErY   rÅ   )r   r(   rt   r¼   r*   r\   rv   ZsuccessResultOfrF   rh   rG   rÈ   )rM   r4  r]   r[  r·   r=   rv  r>   Ú1test_transportProducingWhenQuiescentAfterFullBody¯  s    
ÿÿ
zKHTTP11ClientProtocolTests.test_transportProducingWhenQuiescentAfterFullBodyc              	      sŽ   g ‰‡ ‡‡fdd„}t ƒ }t|ƒ‰ ˆ  |¡ ˆ  tddtddd�¡}| ˆj¡ ˆ  d¡ ˆ 	t
ˆƒd	¡ ˆ ˆd
 ˆ ¡ ˆ ˆd t¡ dS )zˆ
        The quiescentCallback is called before the request C{Deferred} fires,
        in cases where the response has no body.
        c                    s(   ˆ  | ˆ ¡ ˆ  | jd¡ ˆ | ¡ d S rs  rt  ru  rv  r=   r>   r4  ä  s    zUHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponse.<locals>.callbackr³   r´   NTrw  ó&   HTTP/1.1 200 OK
Content-length: 0

r¦   r   rY   )r   r(   rt   r¼   r*   r\   rP   rr   rv   rF   rG   rØ   r  r-   )rM   r4  r]   r[  r=   rv  r>   Ú)test_quiescentCallbackCalledEmptyResponseÝ  s    
ÿ
zCHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponsec              	   C   s�   g }t ƒ }t|jƒ}| |¡ | tddtddd�¡}| d¡ g }| |j¡ |d }t	ƒ }| 
|¡ |j t¡ |  |g ¡ |  |j¡ dS )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´   NTrw  s9   HTTP/1.1 200 OK
Content-length: 0
Connection: close

r   ©r   r(   rr   rt   r¼   r*   r\   rv   rP   r   rM  rZ  rE   r.   rF   r—   r1  ©rM   rP  r]   rx   r[  rS  r·   ry  r=   r=   r>   Útest_quiescentCallbackNotCalled÷  s$    

ÿÿ
z9HTTP11ClientProtocolTests.test_quiescentCallbackNotCalledc              	   C   s�   g }t ƒ }t|jƒ}| |¡ | tddtddd�¡}| d¡ g }| |j¡ |d }t	ƒ }| 
|¡ |j t¡ |  |g ¡ |  |j¡ dS )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´   NFrw  r|  r   r~  r  r=   r=   r>   Ú1test_quiescentCallbackNotCalledNonPersistentQuery  s     

ÿ

zKHTTP11ClientProtocolTests.test_quiescentCallbackNotCalledNonPersistentQueryc              	   C   sÊ   dd„ }t  | t¡}tƒ }t|ƒ}| |¡ | tddtddd�¡}| 	d¡ g }| 
|j¡ |d	 }tƒ }| |¡ |j t¡ |  d
t|ƒ¡ |d	 }	|	d }
|  |
jt¡ |  t¡ |  |j¡ dS )zx
        If C{quiescentCallback} throws an exception, the error is logged and
        protocol is disconnected.
        c                 S   s
   t ƒ ‚d S ra   )ÚZeroDivisionErrorru  r=   r=   r>   r4  5  s    zHHTTP11ClientProtocolTests.test_quiescentCallbackThrows.<locals>.callbackr³   r´   NTrw  r|  r   rY   r  )r   r  r   r   r(   rt   r¼   r*   r\   rv   rP   rr   r   rM  rZ  rE   r.   r  rG   r  r‡   r‚  r  r—   r1  )rM   r4  r  r]   rx   r[  rS  r·   ry  r  r¯   r=   r=   r>   Útest_quiescentCallbackThrows/  s*    
ÿ


z6HTTP11ClientProtocolTests.test_quiescentCallbackThrowsc                 C   sN   t ƒ }tƒ }| |¡ | tddtdƒ¡}| ¡  |  |j¡ t	| |t
tgƒ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)r   r(   rt   r¼   r*   r\   Úcancelr—   ÚdisconnectedrS   r0   r   rh  r=   r=   r>   Útest_cancelBeforeResponseR  s    
   ÿz3HTTP11ClientProtocolTests.test_cancelBeforeResponsec                 C   sV   t ƒ }tƒ }| |¡ | tddtdƒ¡}| d¡ | ¡  |  |j	¡ t
| |tgƒ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µ   )r   r(   rt   r¼   r*   r\   rv   r„  r—   r…  rU   r   rh  r=   r=   r>   Útest_cancelDuringResponsec  s    

z3HTTP11ClientProtocolTests.test_cancelDuringResponsec                    s–   t ƒ }tƒ }| |¡ t|ƒ‰ddi‰‡fdd„‰ ‡ ‡fdd„}|ˆ_| tddtˆƒ¡}ˆj 	d	¡ | 
¡  |  |j¡ |  ˆd ¡ t| |tgƒ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.
        Ú	cancelledFc                    s   dˆ d< d S )NTrˆ  r=   r	  )ÚnonLocalr=   r>   r„  �  s    zJHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.cancelc                    s   | ˆ_ tˆ ƒˆ_ˆjS ra   ©Úconsumerr   rÊ   )r‹  )r„  Úproducerr=   r>   ÚstartProducing„  s    
zRHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.startProducingó   POSTó   /bars   xxxxx)r   r(   rt   ÚStringProducerr�  r¼   r*   r\   r‹  r$  r„  r—   r…  rV   r   )rM   ZproducerLengthr]   rx   r�  rS  r=   )r„  r‰  rŒ  r>   Ú assertCancelDuringBodyProductions  s    
z:HTTP11ClientProtocolTests.assertCancelDuringBodyProductionc                 C   s
   |   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‘  rb   r=   r=   r>   Útest_cancelDuringBodyProduction’  s    z9HTTP11ClientProtocolTests.test_cancelDuringBodyProductionc                 C   s
   |   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‘  r   rb   r=   r=   r>   Ú&test_cancelDuringChunkedBodyProduction›  s    z@HTTP11ClientProtocolTests.test_cancelDuringChunkedBodyProduction)N)'r9   r:   r;   r<   r&  r'  r-  r.  r2  r3  r8  r:  r;  rA  rG  rH  rW  rX  rY  r\  r^  r`  rb  re  rg  ri  rj  rr  rz  r{  r}  r€  r�  rƒ  r†  r‡  r‘  r“  r”  r=   r=   r=   r>   r%  <  sH   
 2 ) :9.#	r%  c                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ 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}.
    Fc                 C   s
   || _ d S ra   )rÉ   )rM   rÉ   r=   r=   r>   Ú__init__µ  s    zStringProducer.__init__c                 C   s   || _ tƒ | _| jS ra   rŠ  )rM   r‹  r=   r=   r>   r�  ¸  s    zStringProducer.startProducingc                 C   s
   d| _ d S rÁ   r  rb   r=   r=   r>   ÚstopProducing½  s    zStringProducer.stopProducingc                 C   s   d S ra   r=   rb   r=   r=   r>   ÚpauseProducingÀ  s    zStringProducer.pauseProducingc                 C   s   d S ra   r=   rb   r=   r=   r>   ÚresumeProducingÄ  s    zStringProducer.resumeProducingN)
r9   r:   r;   r<   r  r•  r�  r–  r—  r˜  r=   r=   r=   r>   r�  ¥  s   r�  c                   @   sÚ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd5d)d*„Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd(S )6ÚRequestTestsz
    Tests for L{Request}.
    c                 C   s   t ƒ | _d S ra   )r   r]   rb   r=   r=   r>   r&  Î  s    zRequestTests.setUpc                 C   s,   t ddtdƒ | j¡ |  | j ¡ d¡ dS )zk
        L{Request.writeTo} formats the request data and writes it to the given
        transport.
        r³   r´   Ns8   GET / HTTP/1.1
Connection: close
Host: example.com

©r*   r\   r  r]   rF   r‡   rb   r=   r=   r>   Útest_sendSimplestRequestÑ  s
    þz%RequestTests.test_sendSimplestRequestc                 C   s4   t ddtddd�}| | j¡ |  | j ¡ d¡ dS )zO
        A pesistent request does not send 'Connection: close' header.
        r³   r´   NTrw  s%   GET / HTTP/1.1
Host: example.com

rš  )rM   Úreqr=   r=   r>   Ú"test_sendSimplestPersistentRequestß  s    þz/RequestTests.test_sendSimplestPersistentRequestc                 C   s’   t ddgdgdœƒ}tdd|dƒ | j¡ | j ¡  d¡}|  |d	 d
¡ |  |dd… ddg¡ |d	= |dd…= | ¡  |  |ddddg¡ dS )zf
        L{Request.writeTo} formats header data and writes it to the given
        transport.
        r€   r�   rA   ©rÕ   r@   r³   ó   /fooNr    r   ó   GET /foo HTTP/1.1éþÿÿÿr…   ó   Connection: closes   Host: example.comrƒ   rŽ   )r2   r*   r  r]   r‡   ÚsplitrF   Úsort)rM   r‘   Úlinesr=   r=   r>   Útest_sendRequestHeadersê  s    
þz$RequestTests.test_sendRequestHeadersc                 C   s¦   t D ]œ}t|gdgdœƒ}tƒ }tdd|dƒ |¡ | ¡  d¡}|  |d d¡ |  |d	d… d
d
g¡ |d= |d	d…= | d¡ | d¡ |  |dt	 g¡ qdS )ze
        Linear whitespace in request header values is replaced with a
        single space.
        s   example.invalidrž  r³   rŸ  Nr    r   r   r¡  r…   r¢  s   Host: example.invalids   X-Foo: )
r5   r2   r   r*   r  r‡   r£  rF   Úremover6   )rM   Ú	componentr‘   r]   r¥  r=   r=   r>   Ú-test_sanitizeLinearWhitespaceInRequestHeadersû  s    

z:RequestTests.test_sanitizeLinearWhitespaceInRequestHeadersc                 C   s´   t tƒ}tddt|ƒ}| | j¡ |  |jd¡ |  | jj	|¡ |  
| jj¡ |  | j ¡ d¡ | j ¡  |j d¡ |j d¡ |j d¡ |  | jj	d¡ |  | j ¡ d¡ dS )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�  NsX   POST /bar HTTP/1.1
Connection: close
Transfer-Encoding: chunked
Host: example.com

s   xxxs   yyyyyyyyyyyyyyys!   3
xxx
f
yyyyyyyyyyyyyyy
0

)r�  r   r*   r\   r  r]   ÚassertNotIdenticalr‹  rØ   rŒ  r—   Ú	streamingrF   r‡   Úclearr$  rÊ   r4  ©rM   rŒ  r¼   r=   r=   r>   Útest_sendChunkedRequestBody  s&    þ
þz(RequestTests.test_sendChunkedRequestBodyc                    s`   t tƒ}tddt|ƒ}| ˆ j¡}ˆ j ¡  |j t	ƒ ¡ ‡ fdd„}ˆ  
|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�  c                    s&   ˆ   ˆ j ¡ d¡ ˆ  ˆ jjd ¡ d S r(  )rF   r]   r‡   rØ   rŒ  r*  rb   r=   r>   rN   <  s    zCRequestTests.test_sendChunkedRequestBodyWithError.<locals>.cbFailed)r�  r   r*   r\   r  r]   r¬  rÊ   r5  r7   rO   rP   )rM   rŒ  r¼   ÚwriteDeferredrN   rR   r=   rb   r>   Ú$test_sendChunkedRequestBodyWithError-  s    	

z1RequestTests.test_sendChunkedRequestBodyWithErrorc                 C   s¨   t dƒ}tddt|ƒ}| | j¡ |  |jd¡ |  | jj|¡ |  	| jj
¡ |  | j ¡ d¡ | j ¡  |j d¡ |j d¡ |  | jjd¡ |  | j ¡ d¡ dS )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�  NsO   POST /bar HTTP/1.1
Connection: close
Content-Length: 3
Host: example.com

rú   )r�  r*   r\   r  r]   rª  r‹  rØ   rŒ  r—   r«  rF   r‡   r¬  r$  rÊ   r4  r­  r=   r=   r>   Útest_sendRequestBodyWithLengthD  s    þ
z+RequestTests.test_sendRequestBodyWithLengthc                 C   s4   t |dtdƒ}| | j¡ |  | j ¡ |d ¡ dS )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š  )rM   r   r¼   r=   r=   r>   Ú_sendRequestEmptyBodyWithLengtha  s    	þz,RequestTests._sendRequestEmptyBodyWithLengthc                 C   s   |   d¡ dS )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.
        s   PUTN©r³  rb   r=   r=   r>   Útest_sendPUTRequestEmptyBodyv  s    z)RequestTests.test_sendPUTRequestEmptyBodyc                 C   s   |   d¡ dS )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´  rb   r=   r=   r>   Útest_sendPOSTRequestEmptyBody~  s    z*RequestTests.test_sendPOSTRequestEmptyBodyc                 C   sV   t dƒ}tddt|ƒ}| | j¡}|j d¡ |j d¡ |  	| jj
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*   r\   r  r]   r‹  r$  rÊ   r4  rØ   rŒ  rO   r&   ©rM   rŒ  r¼   r¯  r=   r=   r>   Ú#test_sendRequestBodyWithTooFewBytes†  s    z0RequestTests.test_sendRequestBodyWithTooFewBytesc                    sŒ   t dƒ‰tddtˆƒ}| ˆj¡}ˆj d¡ ˆ ˆj¡ ˆj d¡ ˆ 	ˆj¡ ˆ 
ˆjjd¡ ‡ ‡‡fdd„}ˆ |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·  ó   cdNc                    sL   ˆ  ˆj ¡ d¡ ˆj ¡  ˆ tˆjjd¡ ˆ ˆƒ ˆ  ˆj ¡ d¡ d S )NsQ   POST /bar HTTP/1.1
Connection: close
Content-Length: 3
Host: example.com

abs   efr…   )rF   r]   r‡   r¬  rl   r   r‹  r$  ra  ©ÚfinisherrŒ  rM   r=   r>   rN   µ  s    þ	
zCRequestTests._sendRequestBodyWithTooManyBytesTest.<locals>.cbFailed)r�  r*   r\   r  r]   r‹  r$  r˜   r  r—   rØ   rŒ  rO   r&   rP   )rM   r¼  r¼   r¯  rN   rR   r=   r»  r>   Ú$_sendRequestBodyWithTooManyBytesTest•  s    
z1RequestTests._sendRequestBodyWithTooManyBytesTestc                 C   s   dd„ }|   |¡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.
        c                 S   s   | j  d ¡ d S ra   ©rÊ   r4  ©rŒ  r=   r=   r>   r¼  Þ  s    zCRequestTests.test_sendRequestBodyWithTooManyBytes.<locals>.finisher)r½  ©rM   r¼  r=   r=   r>   Ú$test_sendRequestBodyWithTooManyBytesÖ  s    z1RequestTests.test_sendRequestBodyWithTooManyBytesc                    s$   t  ˆt¡‰ ‡ ‡fdd„}ˆ |¡S )r¾  c                    sV   | j  tƒ ¡ ˆ d }ˆ d|¡ |d }ˆ |jt¡ ˆ t¡}ˆ t|ƒd¡ d S )Nr   r  rY   )	rÊ   r5  r7   r=  r  r‡   r  rF   rG   )rŒ  r  r¯   r7  r?  r=   r>   r¼  ì  s    
zHRequestTests.test_sendRequestBodyErrorWithTooManyBytes.<locals>.finisher)r   r  r   r½  rÁ  r=   r?  r>   Ú)test_sendRequestBodyErrorWithTooManyBytesã  s    	z6RequestTests.test_sendRequestBodyErrorWithTooManyBytesc                 C   sž   t  | t¡}tdƒ}tddt|ƒ}| | j¡ |jj	}|j 
d¡ |j d¡ | tƒ ¡ |d }|  d|¡ |d }|  |jt¡ |  t|  t¡ƒd¡ dS )	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�  rú   Nr   r  rY   )r   r  r   r�  r*   r\   r  r]   r‹  Ú	_finishedr$  rÊ   r4  r5  r7   r=  r  r‡   rF   rG   r  )rM   r  rŒ  r¼   ZfinishedConsumingr  r¯   r=   r=   r>   Ú*test_sendRequestBodyErrorWithConsumerError÷  s    
z7RequestTests.test_sendRequestBodyErrorWithConsumerErrorc                 C   sx   t dƒ}tddt|ƒ}| | j¡}|j d¡ ||ƒ |  | jjd¡ | j 	¡  |  
t|jjd¡ |  | 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*   r\   r  r]   r‹  r$  rØ   rŒ  r¬  rl   r   rF   r‡   )rM   r¼  rŒ  r¼   r¯  r=   r=   r>   Ú-_sendRequestBodyFinishedEarlyThenTooManyBytes	  s    
z:RequestTests._sendRequestBodyFinishedEarlyThenTooManyBytesc                 C   s   dd„ }|   |  |¡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}.
        c                 S   s   | j  d ¡ d S ra   r¿  rÀ  r=   r=   r>   r¼  -	  s    zPRequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytes.<locals>.finisher)rO   rÆ  r&   rÁ  r=   r=   r>   Ú1test_sendRequestBodyFinishedEarlyThenTooManyBytes%	  s
    þz>RequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytesc                 C   s   dd„ }|   |  |¡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.
        c                 S   s   | j  tƒ ¡ d S ra   )rÊ   r5  r7   rÀ  r=   r=   r>   r¼  =	  s    zORequestTests.test_sendRequestBodyErroredEarlyThenTooManyBytes.<locals>.finisher)rO   rÆ  r7   rÁ  r=   r=   r>   Ú0test_sendRequestBodyErroredEarlyThenTooManyBytes5	  s
    þz=RequestTests.test_sendRequestBodyErroredEarlyThenTooManyBytesNc                 C   s`   t tƒ}tddt|ƒ}| | j¡}|j |¡ | j ¡  |  	t
|jj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�  r   r*   r\   r  r]   rÊ   r4  r¬  rl   r   r‹  r$  rF   r‡   )rM   Z_withrŒ  r¼   r¯  r=   r=   r>   Ú0test_sendChunkedRequestBodyFinishedThenWriteMoreE	  s    
z=RequestTests.test_sendChunkedRequestBodyFinishedThenWriteMorec                 C   s   |   ttƒ ƒ¡}|  |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É  r   r7   rO   )rM   rR   r=   r=   r>   Ú9test_sendChunkedRequestBodyFinishedWithErrorThenWriteMoreV	  s    ÿzFRequestTests.test_sendChunkedRequestBodyFinishedWithErrorThenWriteMorec                 C   s¨   t dƒ}tddt|ƒ}| | j¡}|  | jj|¡ |  | jj¡ |j	 
d¡ |  | j ¡ d¡ |  | jj¡ |j ttƒ ƒ¡ |  | jj¡ |  | jjd¡ |  |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·  sQ   POST /bar HTTP/1.1
Connection: close
Content-Length: 5
Host: example.com

abN)r�  r*   r\   r  r]   rØ   rŒ  r—   r«  r‹  r$  rF   r‡   r˜   r1  rÊ   r5  r   r7   rO   r¸  r=   r=   r>   Útest_sendRequestBodyWithErrorb	  s    þ
z*RequestTests.test_sendRequestBodyWithErrorc                 C   sx   t ddti ƒdƒ}|  t|j| j¡ |  | j ¡ d¡ t ddtdddgiƒdƒ}|  t|j| j¡ |  | j ¡ d¡ dS )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…   s   HostrA   s   example.org)r*   r2   rl   r   r  r]   rF   r‡   )rM   r¼   r=   r=   r>   Útest_hostHeaderRequired‰	  s       ÿz$RequestTests.test_hostHeaderRequiredc                 C   sF   t dƒ}tddt|ƒ}| | j¡ |  |j¡ | ¡  |  |j¡ dS )zc
        L{Request.stopWriting} calls its body producer's C{stopProducing}
        method.
        r±  r³   r´   N)	r�  r*   r\   r  r]   r˜   r  r  r—   r­  r=   r=   r>   Útest_stopWriting˜	  s    zRequestTests.test_stopWritingc                 C   s˜   t  | t¡}tdƒ}dd„ }||_tddt|ƒ}| | j¡ | 	¡  |  
t|  t¡ƒd¡ |  dt|ƒ¡ |d }|  d|¡ |d }|  |jt¡ 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±  c                   S   s   t dƒ‚d S ©NzstopProducing is bustedrÿ   r=   r=   r=   r>   ÚbrokenStopProducing¬	  s    zBRequestTests.test_brokenStopProducing.<locals>.brokenStopProducingr³   r´   rY   r   r  N)r   r  r   r�  r–  r*   r\   r  r]   r  rF   rG   r  r7   r  r=  r  r‡   )rM   r  rŒ  rÐ  r¼   r  r¯   r=   r=   r>   Útest_brokenStopProducing¤	  s    z%RequestTests.test_brokenStopProducing)N)r9   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Î  rÑ  r=   r=   r=   r>   r™  É  s4   !A
'r™  c                   @   sR   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚLengthEnforcingConsumerTestsz/
    Tests for L{LengthEnforcingConsumer}.
    c                 C   s2   t ƒ | _tdƒ| _tƒ | _t| j| j| jƒ| _d S )Nr’  )r   rS  r�  rŒ  r   r]   r"   Úenforcerrb   r=   r=   r>   r&  Á	  s    
  ÿz"LengthEnforcingConsumerTests.setUpc                 C   sJ   | j  d¡ |  | j ¡ d¡ | j ¡  | j  d¡ |  | j ¡ d¡ dS )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ú   s   defN)rÓ  r$  rF   r]   r‡   r¬  rb   r=   r=   r>   Ú
test_writeÉ	  s
    
z'LengthEnforcingConsumerTests.test_writec                 C   s    | j  d¡ |  t| j j¡ dS )z´
        L{LengthEnforcingConsumer._noMoreWritesExpected} raises
        L{WrongBodyLength} if it is called before the indicated number of bytes
        have been written.
        s	   xxxxxxxxxN)rÓ  r$  rl   r&   Ú_noMoreWritesExpectedrb   r=   r=   r>   Útest_finishedEarlyÕ	  s    z/LengthEnforcingConsumerTests.test_finishedEarlyFc                 C   sP   | j  d¡ |  | jj¡ | j  d¡ |  | jj¡ |rB| j  ¡  |  | jt	¡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Ó  r$  r˜   rŒ  r  r—   rÕ  rO   rS  r&   )rM   Z_unregisterAfterr=   r=   r>   Útest_writeTooManyÞ	  s    
z.LengthEnforcingConsumerTests.test_writeTooManyc                 C   sH   | j  d¡ | j  ¡  |  | jj¡ |  t| j jd¡ |  | jj¡ dS )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Ó  r$  rÕ  r˜   rŒ  r  rl   r   r—   rb   r=   r=   r>   Útest_writeAfterNoMoreExpectedî	  s
    
z:LengthEnforcingConsumerTests.test_writeAfterNoMoreExpectedc                 C   s
   |   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Ù  rb   r=   r=   r>   Útest_finishedLateú	  s    z.LengthEnforcingConsumerTests.test_finishedLatec                 C   s"   | j  d¡ |  | j  ¡ d¡ dS )zŸ
        If L{LengthEnforcingConsumer._noMoreWritesExpected} is called after
        the correct number of bytes have been written it returns L{None}.
        r×  N)rÓ  r$  rØ   rÕ  rb   r=   r=   r>   Útest_finished
  s    z*LengthEnforcingConsumerTests.test_finishedc                    s6   ‡ fdd„}|ˆ j _‡ fdd„}ˆ  ¡ }| |¡ |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}.
        c                      s   t  ˆ j¡ tdƒ‚d S rÏ  )r�  r–  rŒ  r7   r=   rb   r=   r>   rÐ  
  s    zRLengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.brokenStopProducingc                    s   ˆ   tˆ  t¡ƒd¡ d S )NrY   )rF   rG   r  r7   r*  rb   r=   r>   Ú
cbFinished
  s    zILengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.cbFinished)rŒ  r–  rÙ  rP   )rM   rÐ  rÝ  rR   r=   rb   r>   Útest_stopProducingRaises

  s    	
z5LengthEnforcingConsumerTests.test_stopProducingRaisesN)F)r9   r:   r;   r<   r&  rÔ  rÖ  rÙ  rÚ  rÛ  rÜ  rÞ  r=   r=   r=   r>   rÒ  ¼	  s   	
rÒ  c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚRequestBodyConsumerTestsz¤
    Tests for L{ChunkedEncoder} which sits between an L{ITransport} and a
    request/response body producer and chunked encodes everything written to
    it.
    c                 C   s   |   ttttƒ ƒƒ¡ dS )zC
        L{ChunkedEncoder} instances provide L{IConsumer}.
        N)r—   r   r   r   r   rb   r=   r=   r>   Útest_interface(
  s    z'RequestBodyConsumerTests.test_interfacec                 C   sN   t ƒ }t|ƒ}| d¡ |  | ¡ d¡ | ¡  | d¡ |  | ¡ d¡ dS )z}
        L{ChunkedEncoder.write} writes to the transport the chunked encoded
        form of the bytes passed to it.
        r±   r_  s   xxxxxxxxxxxxxxxxs   10
xxxxxxxxxxxxxxxx
N)r   r   r$  rF   r‡   r¬  )rM   r]   Úencoderr=   r=   r>   rÔ  .
  s    

z#RequestBodyConsumerTests.test_writec                 C   sd   t ƒ }tƒ }t|ƒ}| |d¡ |  |j|¡ |  |j¡ | ¡  |  |jd¡ |  	| 
¡ d¡ dS )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.
        TNs   0

)r   rš   r   ZregisterProducerrØ   rŒ  r—   r«  ZunregisterProducerrF   r‡   )rM   r]   rŒ  rá  r=   r=   r>   Útest_producerRegistration;
  s    z2RequestBodyConsumerTests.test_producerRegistrationN)r9   r:   r;   r<   rà  rÔ  râ  r=   r=   r=   r>   rß  !
  s   rß  c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚTransportProxyProducerTestszn
    Tests for L{TransportProxyProducer} which proxies the L{IPushProducer}
    interface of a transport.
    c                 C   s   |   tttdƒƒ¡ dS )zO
        L{TransportProxyProducer} instances provide L{IPushProducer}.
        N)r—   r   r   r%   rb   r=   r=   r>   rà  S
  s    z*TransportProxyProducerTests.test_interfacec                 C   s6   t ƒ }t|ƒ}|  |j|¡ | ¡  |  |jd¡ dS )z~
        L{TransportProxyProducer.stopProxying} drops the reference to the
        wrapped L{IPushProducer} provider.
        N)r   r%   rØ   rd  ÚstopProxying©rM   r]   Úproxyr=   r=   r>   Ú%test_stopProxyingUnreferencesProducerY
  s
    zATransportProxyProducerTests.test_stopProxyingUnreferencesProducerc                 C   sd   t ƒ }| ¡  t|ƒ}|  |jd¡ | ¡  |  |jd¡ | ¡  | ¡  | ¡  |  |jd¡ dS )z™
        L{TransportProxyProducer.resumeProducing} calls the wrapped
        transport's C{resumeProducing} method unless told to stop proxying.
        rá   rÅ   N)r   r—  r%   rF   rÈ   r˜  rä  rå  r=   r=   r>   Útest_resumeProducingd
  s    z0TransportProxyProducerTests.test_resumeProducingc                 C   s\   t ƒ }t|ƒ}|  |jd¡ | ¡  |  |jd¡ | ¡  | ¡  | ¡  |  |jd¡ dS )z—
        L{TransportProxyProducer.pauseProducing} calls the wrapped transport's
        C{pauseProducing} method unless told to stop proxying.
        rÅ   rá   N)r   r%   rF   rÈ   r—  r˜  rä  rå  r=   r=   r>   Útest_pauseProducingz
  s    z/TransportProxyProducerTests.test_pauseProducingc                 C   sb   t ƒ }t|ƒ}|  |jd¡ | ¡  |  |jd¡ t ƒ }t|ƒ}| ¡  | ¡  |  |jd¡ dS )z•
        L{TransportProxyProducer.stopProducing} calls the wrapped transport's
        C{stopProducing} method unless told to stop proxying.
        rÅ   r  N)r   r%   rF   rÈ   r–  rä  rå  r=   r=   r>   Útest_stopProducing�
  s    z.TransportProxyProducerTests.test_stopProducingc                 C   sd   t ƒ }tƒ }| |¡ ||_t|ƒ}|  |j¡ |  |jd¡ | 	¡  |  |jd¡ |  
|j¡ dS )zs
        L{TransportProxyProducer.loseConnection} calls the wrapped transport's
        C{loseConnection}.
        rÅ   N)r   r   rt   rx   r%   r—   Ú	connectedrF   rÈ   ÚloseConnectionr˜   ©rM   r]   rx   ræ  r=   r=   r>   Ú test_loseConnectionWhileProxying£
  s    
z<TransportProxyProducerTests.test_loseConnectionWhileProxyingc                 C   sP   t ƒ }tƒ }| |¡ ||_t|ƒ}| ¡  |  |j¡ | ¡  |  |j¡ dS )zm
        L{TransportProxyProducer.loseConnection} does nothing when the
        proxy is not active.
        N)	r   r   rt   rx   r%   rä  r—   rë  rì  rí  r=   r=   r>   Útest_loseConnectionNotProxying¸
  s    
z:TransportProxyProducerTests.test_loseConnectionNotProxyingN)r9   r:   r;   r<   rà  rç  rè  ré  rê  rî  rï  r=   r=   r=   r>   rã  M
  s   rã  c                   @   sˆ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd S )!ÚResponseTestsz 
    Tests for L{Response}.
    c                 C   s   t tƒ ƒ}|  tt|ƒ¡ dS )z=
        L{Response} instances provide L{IResponse}.
        N)r^   r   r—   r   r4   ©rM   r·   r=   r=   r>   Útest_verifyInterfaceÐ
  s    
z"ResponseTests.test_verifyInterfacec                    sl   g ‰ t ƒ }G ‡ fdd„dtƒ}|ƒ }t|ƒ}| |¡ ˆ \}| ¡  |  |jd¡ | ¡  |  |jd¡ dS )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                       s   e Zd Z‡ fdd„ZdS )z7ResponseTests.test_makeConnection.<locals>.SomeProtocolc                    s   ˆ   |¡ d S ra   rÂ   )rM   rŒ  ©Z	producersr=   r>   rt   á
  s    zFResponseTests.test_makeConnection.<locals>.SomeProtocol.makeConnectionN©r9   r:   r;   rt   r=   ró  r=   r>   ÚSomeProtocolà
  s   rõ  rá   rÅ   N)r   r   r^   rM  r—  rF   rÈ   r˜  )rM   r]   rõ  r‹  r·   ZtheProducerr=   ró  r>   Útest_makeConnection×
  s    
z!ResponseTests.test_makeConnectionc                    sN   g ‰ G ‡ fdd„dt ƒ}|ƒ }ttƒ ƒ}| |¡ | d¡ |  ˆ dg¡ dS )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.
        c                       s   e Zd Z‡ fdd„ZdS )z5ResponseTests.test_dataReceived.<locals>.ListConsumerc                    s   ˆ   |¡ d S ra   rÂ   ©rM   r©   ©r�   r=   r>   rv   ö
  s    zBResponseTests.test_dataReceived.<locals>.ListConsumer.dataReceivedN©r9   r:   r;   rv   r=   rø  r=   r>   ÚListConsumerõ
  s   rú  r±   N)r   r^   r   rM  rÆ   rF   ©rM   rú  r‹  r·   r=   rø  r>   Útest_dataReceivedí
  s    


zResponseTests.test_dataReceivedc                    sj   g ‰ G ‡ fdd„dt ƒ}|ƒ }ttƒ ƒ}| |¡ | ¡  ˆ d  t¡ |  tˆ ƒd¡ |  	|j
d¡ dS )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.
        c                       s   e Zd Z‡ fdd„ZdS )z7ResponseTests.test_connectionLost.<locals>.ListConsumerc                    s   ˆ   |¡ d S ra   rÂ   rm  ©Zlostr=   r>   rø   
  s    zFResponseTests.test_connectionLost.<locals>.ListConsumer.connectionLostN)r9   r:   r;   rø   r=   rý  r=   r>   rú  	  s   rú  r   rY   N)r   r^   r   rM  rÇ   rE   r.   rF   rG   rØ   Z_bodyProtocolrû  r=   rý  r>   Útest_connectionLost   s    

z!ResponseTests.test_connectionLostc                    st   g ‰ G ‡ fdd„dt ƒ}|ƒ }ttƒ ƒ}| d¡ | d¡ | |¡ | d¡ |  ˆ dddg¡ |  |jd¡ dS )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.
        c                       s   e Zd Z‡ fdd„ZdS )z8ResponseTests.test_bufferEarlyData.<locals>.ListConsumerc                    s   ˆ   |¡ d S ra   rÂ   r÷  rø  r=   r>   rv   "  s    zEResponseTests.test_bufferEarlyData.<locals>.ListConsumer.dataReceivedNrù  r=   rø  r=   r>   rú  !  s   rú  r±   r€   r�   N)r   r^   r   rÆ   rM  rF   rØ   Z_bodyBuffer)rM   rú  rx   r·   r=   rø  r>   Útest_bufferEarlyData  s    




z"ResponseTests.test_bufferEarlyDatac                 C   s,   t tƒ ƒ}| tƒ ¡ |  t|jtƒ ¡ dS )zb
        L{Response.deliverBody} raises L{RuntimeError} if called more than
        once.
        N)r^   r   rM  r   rl   rm   rñ  r=   r=   r>   Ú test_multipleStartProducingFails0  s    
z.ResponseTests.test_multipleStartProducingFailsc                 C   s4   t tƒ ƒ}| tƒ ¡ | ¡  |  t|jtƒ ¡ dS )zw
        L{Response.deliverBody} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished}.
        N)r^   r   rM  r   rÇ   rl   rm   rñ  r=   r=   r>   Ú%test_startProducingAfterFinishedFails9  s    
z3ResponseTests.test_startProducingAfterFinishedFailsc                 C   s&   t tƒ ƒ}| ¡  |  t|jd¡ dS )z 
        L{Response._bodyDataReceived} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished} but before L{Response.deliverBody}.
        r±   N)r^   r   rÇ   rl   rm   rÆ   rñ  r=   r=   r>   Ú'test_bodyDataReceivedAfterFinishedFailsC  s    
z5ResponseTests.test_bodyDataReceivedAfterFinishedFailsc                 C   s2   t tƒ ƒ}| ¡  | tƒ ¡ |  t|jd¡ dS )zŸ
        L{Response._bodyDataReceived} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished} and after L{Response.deliverBody}.
        r±   N)r^   r   rÇ   rM  r   rl   rm   rÆ   rñ  r=   r=   r>   Ú'test_bodyDataReceivedAfterDeliveryFailsL  s    
z5ResponseTests.test_bodyDataReceivedAfterDeliveryFailsc                 C   s$   t tƒ ƒ}| ¡  |  t|j¡ dS )zh
        L{Response._bodyDataFinished} raises L{RuntimeError} if called more
        than once.
        N)r^   r   rÇ   rl   rm   rñ  r=   r=   r>   Ú'test_bodyDataFinishedAfterFinishedFailsV  s    
z5ResponseTests.test_bodyDataFinishedAfterFinishedFailsc                 C   s0   t tƒ ƒ}| ¡  | tƒ ¡ |  t|j¡ dS )z{
        L{Response._bodyDataFinished} raises L{RuntimeError} if called after
        the body has been delivered.
        N)r^   r   rÇ   rM  r   rl   rm   rñ  r=   r=   r>   Ú'test_bodyDataFinishedAfterDeliveryFails_  s    
z5ResponseTests.test_bodyDataFinishedAfterDeliveryFailsc                    sl   g ‰ G ‡ fdd„dt ƒ}tƒ }| ¡  |ƒ }t|ƒ}|  |jd¡ | |¡ |  ˆ dg¡ |  |jd¡ dS )z–
        L{Response.deliverBody} resumes the HTTP connection's transport
        after passing it to the consumer's C{makeConnection} method.
        c                       s   e Zd Z‡ fdd„ZdS )z9ResponseTests.test_transportResumed.<locals>.ListConsumerc                    s   ˆ   |j¡ d S ra   )rr   rÈ   r  ©ZtransportStater=   r>   rt   q  s    zHResponseTests.test_transportResumed.<locals>.ListConsumer.makeConnectionNrô  r=   r  r=   r>   rú  p  s   rú  rá   rÅ   N)r   r   r—  r^   rF   rÈ   rM  )rM   rú  r]   rx   r·   r=   r  r>   Útest_transportResumedi  s    
z#ResponseTests.test_transportResumedc                 C   sX   t ƒ }t|ƒ}| d¡ | d¡ | ¡  tƒ }| |¡ |  |jd¡ |j 	t
¡ dS )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€   rR  N)r   r^   rÆ   rÇ   r   rM  rF   r©   rZ  rE   r.   ©rM   r]   r·   rx   r=   r=   r>   Ú)test_bodyDataFinishedBeforeStartProducing}  s    


z7ResponseTests.test_bodyDataFinishedBeforeStartProducingc                 C   sL   t ƒ }t|ƒ}tƒ }| |¡ |  |jd¡ | ttƒ ƒ¡ |j	 
t¡ dS )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)r   r^   r   rM  rF   rh   rÇ   r   r7   rZ  rE   r  r=   r=   r>   Ú#test_finishedWithErrorWhenConnected�  s    
z1ResponseTests.test_finishedWithErrorWhenConnectedc                 C   sL   t ƒ }t|ƒ}|  |jd¡ | ttƒ ƒ¡ tƒ }| |¡ |j	 
t¡ dS )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.
        ZINITIALN)r   r^   rF   rh   rÇ   r   r7   r   rM  rZ  rE   r  r=   r=   r>   Ú!test_finishedWithErrorWhenInitial¢  s    
z/ResponseTests.test_finishedWithErrorWhenInitialN)r9   r:   r;   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<   Útypingr   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted.internet.deferr   r   r   r   Ztwisted.internet.errorr	   r
   Ztwisted.internet.interfacesr   r   Ztwisted.internet.protocolr   Ztwisted.internet.testingr   r   r   r   Ztwisted.loggerr   Ztwisted.protocols.basicr   Ztwisted.python.failurer   Ztwisted.trial.unittestr   Ztwisted.web._newclientr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   Ztwisted.web.clientr(   r)   r*   r+   r,   r-   r.   r/   r0   Ztwisted.web.httpr1   Ztwisted.web.http_headersr2   Ztwisted.web.iwebr3   r4   Ztwisted.web.test.requesthelperr5   r6   Ú	Exceptionr7   r?   r\   rS   rU   rV   rW   r^   r_   ro   rŸ   r¢   r¤   r  r"  r%  r�  r™  rÒ  rß  rã  rð  r=   r=   r=   r>   Ú<module>   sp   P,(


!      0      o#   ve,~