o
    ¯b6« ã                   @   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 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  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)ÚglobalLogPublisher)ÚLineReceiver)ÚFailure)ÚAccumulatingProtocolÚEventLoggingObserverÚStringTransportÚ StringTransportWithDisconnection)ÚTestCase)ÚBODYÚDONEÚHEADERÚSTATUSÚUNKNOWN_LENGTHÚ
BadHeadersÚBadResponseVersionÚChunkedEncoderÚConnectionAbortedÚExcessWriteÚHTTPClientParserÚ
HTTPParserÚLengthEnforcingConsumerÚ
ParseErrorÚRequestNotSentÚTransportProxyProducerÚWrongBodyLengthÚmakeStatefulDispatcher)	ÚHTTP11ClientProtocolÚPotentialDataLossÚRequestÚRequestGenerationFailedÚRequestTransmissionFailedÚResponseÚResponseDoneÚResponseFailedÚResponseNeverReceived)Ú	_DataLoss)ÚHeaders)ÚIBodyProducerÚ	IResponse)ÚbytesLinearWhitespaceComponentsÚsanitizedBytesc                   @   ó   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>   úA/usr/lib/python3/dist-packages/twisted/web/test/test_newclient.pyr8   C   ó    r8   c                   @   r7   )ÚAnotherArbitraryExceptionzI
    Similar to L{ArbitraryException} but with a different identity.
    Nr9   r>   r>   r>   r?   rA   J   r@   rA   ó   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)rO   ÚdeferredÚmainTyperN   rP   Údr>   rM   r?   ÚassertWrapperExceptionTypesU   s   

rV   c                 C   ó   t | |t|ƒS )zo
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{ResponseFailed}.
    )rV   r/   ©rO   rS   rN   r>   r>   r?   ÚassertResponseFailed}   ó   rY   c                 C   rW   )zx
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestGenerationFailed}.
    )rV   r+   rX   r>   r>   r?   ÚassertRequestGenerationFailed…   ó   ÿr[   c                 C   rW   )zz
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestTransmissionFailed}.
    )rV   r,   rX   r>   r>   r?   ÚassertRequestTransmissionFailed�   r\   r]   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é   r_   éÈ   ó   OK)r-   Ú_boringHeaders)Ú	transportr>   r>   r?   ÚjustTransportResponse™   s   rd   c                   @   ó   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   ó   d S ©Nr>   ©rO   r>   r>   r?   Úbar±   ó   zGMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo.barÚquuxc                 S   ó   dS )NÚar>   rj   r>   r>   r?   Ú_quux_A¶   rl   zKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_Ac                 S   rn   )NÚbr>   rj   r>   r>   r?   Ú_quux_B¹   rl   zKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_BN)r:   r;   r<   Ú_staterk   r'   rp   rr   r>   r>   r>   r?   ÚFoo®   s    
rt   ro   ÚBrq   ÚCN)rH   rk   rs   ÚassertRaisesÚRuntimeError)rO   rt   Ústatefulr>   r>   r?   Útest_functionCalledByState¨   s   z6MakeStatefulDispatcherTests.test_functionCalledByStateN)r:   r;   r<   r=   rz   r>   r>   r>   r?   rf   £   s    rf   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   rH   Ústater   ÚdataReceivedr|   r   )rO   ÚstatusÚprotocolr>   r>   r?   Útest_statusCallbackÌ   s   z$_HTTPParserTests.test_statusCallbackc                 C   s6   i }t ƒ }|j|_| tƒ ¡ | d| j ¡ ||fS )Nr}   )r!   Ú__setitem__ÚheaderReceivedr€   r   r‚   r|   ©rO   Úheaderr„   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Š   r‚   r|   rH   r�   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   sZ   |   ¡ \}}| j g d¢¡}| 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Š   r|   Újoinr‚   rH   )rO   r‰   r„   Ú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 ri   )r~   r�   r   r>   ©Úcalledr„   r>   r?   ÚallHeadersReceived  s   
zD_HTTPParserTests.test_allHeadersCallback.<locals>.allHeadersReceivedN)rŠ   r—   r‚   r|   rH   r   r�   r   )rO   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}.
        r}   r�   ó
   X-Foo: bazr‹   rŒ   ó   bazN)	r!   r€   r   r‚   r|   rH   ÚlistÚheadersÚgetAllRawHeaders)rO   r„   Úexpectedr>   r>   r?   Útest_headersSavedOnResponse#  s   z,_HTTPParserTests.test_headersSavedOnResponsec                 C   sF   t ƒ }g d¢}|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!   Ú
assertTrueÚisConnectionControlHeaderÚassertFalse)rO   r„   Ú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!   r€   r   ÚswitchToBodyModeÚobjectrw   rx   ©rO   r„   r>   r>   r?   Útest_switchToBodyModeO  s   z&_HTTPParserTests.test_switchToBodyMode)r:   r;   r<   r=   r|   r   ÚbytesÚ__annotations__r…   rŠ   rŽ   r�   r”   r˜   r™   r    r§   r«   r>   r>   r>   r?   r{   Ä   s   
 
r{   c                   @   ó   e Zd ZdZdZdS )Ú$HTTPParserRFCComplaintDelimeterTestsz<
    L{_HTTPParserTests} using standard CR LF newlines.
    ó   
N©r:   r;   r<   r=   r|   r>   r>   r>   r?   r¯   Z  ó    r¯   c                   @   r®   )Ú'HTTPParserNonRFCComplaintDelimeterTestsz5
    L{_HTTPParserTests} using bare LF newlines.
    ó   
Nr±   r>   r>   r>   r?   r³   b  r²   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<S )=Ú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    rH   Ú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 ri   )rw   rH   Údata©ÚsÚexc©ÚeÚfrO   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¸   )rO   r„   rÁ   r>   r¾   r?   Útest_parseBadVersionx  s   
z*HTTPClientParserTests.test_parseBadVersionc                 C   ób   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
r^   r`   ra   ©r*   rb   r    r€   r   r‚   rH   ÚresponseÚversionÚcodeÚphrase©rO   Úrequestr„   r>   r>   r?   Útest_responseStatusParsingŽ  s   

z0HTTPClientParserTests.test_responseStatusParsingc                 C   rÄ   )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
r^   r`   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 ri   )rw   r#   r   rH   rº   r»   ©r„   rO   r>   r?   rÁ   °  ó   zBHTTPClientParserTests.test_badResponseStatus.<locals>.checkParsingrÂ   s   HTTP/1.1 bar OK)r    )rO   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                      ó
   ˆ   d¡S ©NT©r~   r>   ©ÚbodyDataFinishedr>   r?   Ú<lambda>Ö  ó   
 z3HTTPClientParserTests._noBodyTest.<locals>.<lambda>Ú	producingr‘   Tr   )r    r~   r†   r‡   r   r€   r‚   rÉ   Ú_bodyDataReceivedÚ_bodyDataFinishedrH   ÚproducerStater�   r   Úlength)	rO   rÎ   rƒ   rÉ   r‰   ÚfinishedÚbodyr„   rc   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*   rb   râ   rH   )rO   rÎ   rƒ   rÉ   r‰   r>   r>   r?   Útest_headResponseà  s
   z'HTTPClientParserTests.test_headResponsec                 C   ó(   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*   rb   râ   ©rO   rÎ   rƒ   rÉ   r>   r>   r?   Útest_noContentResponseì  ó   z,HTTPClientParserTests.test_noContentResponsec                 C   rå   )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ö  ré   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   rh   ri   r>   ©Úrestr>   r>   r?   rÙ     ó    z<HTTPClientParserTests.test_responseHeaders.<locals>.<lambda>rÇ   s   X-Foo: bar
r°   ó   x-foorŒ   ©r    r*   rb   r€   r   r‚   rH   Ú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   rh   ri   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   rh   ri   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*   rb   r€   r   r‚   rH   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   rh   ri   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*   rb   r~   r   r€   r‚   rÉ   rÜ   rH   rÞ   r�   r   r   ©rO   rà   r„   rc   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*   rb   r~   r€   r   r‚   rÉ   rÜ   rH   r�   r   rß   ©rO   rà   r„   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 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*   rb   r€   r   rw   Ú
ValueErrorr‚   rª   r>   r>   r?   Ú!test_multipleContentLengthHeaders�  s   ýz7HTTPClientParserTests.test_multipleContentLengthHeadersc                 C   ód   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*   rb   r~   r€   r   r‚   rH   r�   r   ©rO   rà   r„   r>   r>   r?   Útest_extraBytesPassedBack“  ó   ÿ


z/HTTPClientParserTests.test_extraBytesPassedBackc                 C   r  )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¤  r  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*   rb   r~   r€   r   r‚   rÉ   rÜ   rH   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*   rb   r~   r   r€   r‚   rÉ   rÜ   rH   Ú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*   rb   r~   r   r€   r‚   rÉ   rÜ   rH   rû   r>   r>   r?   Ú%test_contentLengthAndTransferEncodingò  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*   rb   r€   Ú_responseDeferredr  r   r8   rY   )rO   rc   r„   Ú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   ó   t ƒ ‚ri   ©r8   )rJ   r>   r>   r?   ÚfakeBodyDataFinished0  ó   zPHTTPClientParserTests.test_connectionLostWithError.<locals>.fakeBodyDataFinishedr_   Úlog_failureri   )r   ÚcreateWithCleanupr   r   r    r*   rb   r€   r  rR   r~   r‚   rÝ   r  ÚassertEqualsrI   ÚassertIsInstancer“   r8   ÚflushLoggedErrors)rO   ÚlogObserverrc   r„   rÉ   r  ÚeventrÀ   r>   r>   r?   Útest_connectionLostWithError  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   rh   ri   r>   ©Úignr>   r>   r?   rÙ   B  rí   z<HTTPClientParserTests.test_noResponseAtAll.<locals>.<lambda>)
r    r*   rb   r  r€   r   r  r
   rQ   r0   ©rO   r„   rU   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   rh   ri   r>   r   r>   r>   r?   rÙ   Q  rí   zBHTTPClientParserTests.test_someResponseButNotAll.<locals>.<lambda>ó   2)r    r*   rb   r  r€   r   r‚   r  r
   rQ   r/   rR   r  r"  r>   r>   r?   Útest_someResponseButNotAllJ  s   ÿ
ÿz0HTTPClientParserTests.test_someResponseButNotAllc                 C   sš   d}t tddtdƒdd„ ƒ}| tƒ ¡ | |¡ |  t|ddƒdu ¡ |  |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   rh   ri   r>   r   r>   r>   r?   rÙ   j  rí   zCHTTPClientParserTests.test_1XXResponseIsSwallowed.<locals>.<lambda>rÉ   r   )r    r*   rb   r€   r   r‚   r£   ÚgetattrrH   r�   r   rI   rœ   r�   rž   rð   Ú_everReceivedData)rO   Úsample103Responser„   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   rh   ri   r>   r   r>   r>   r?   rÙ   …  rí   zUHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinal.<locals>.<lambda>r`   r¡   rõ   rö   ©r    r*   rb   r€   r   r‚   rH   rÉ   rË   r�   r2   rð   rß   ©rO   r)  Úfollowing200Responser„   r>   r>   r?   Ú-test_1XXFollowedByFinalResponseOnlyEmitsFinalv  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   rh   ri   r>   r   r>   r>   r?   rÙ   ž  rí   zKHTTPClientParserTests.test_multiple1XXResponsesAreIgnored.<locals>.<lambda>r`   r¡   rõ   rö   r,  r-  r>   r>   r?   Ú#test_multiple1XXResponsesAreIgnored�  s(   ÿÿÿþýÿz9HTTPClientParserTests.test_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   rh   ri   r>   r   r>   r>   r?   rÙ   »  rí   zHHTTPClientParserTests.test_ignored1XXResponseCausesLog.<locals>.<lambda>r_   r   Ú
log_formatz#Ignoring unexpected {code} responserË   ég   )r   r  r   r    r*   rb   r€   r   r‚   r  rI   )rO   r  r)  r„   r  r>   r>   r?   Ú test_ignored1XXResponseCausesLog­  s   ÿÿ
z6HTTPClientParserTests.test_ignored1XXResponseCausesLogN)!r:   r;   r<   r=   r¹   rÃ   rÏ   rÐ   rÓ   râ   rä   rè   rê   rò   ró   rø   rù   rü   rÿ   r  r  r	  r  r  r  r  r  r#  r%  r*  r/  r0  r3  r>   r>   r>   r?   rµ   j  s>    $

%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 ri   )r   rà   ©rO   rc   r>   r>   r?   ÚwriteToÖ  s   zSlowRequest.writeToc                 C   ó
   d| _ d S rÕ   ©Ústoppedrj   r>   r>   r?   ÚstopWritingÚ  ó   
zSlowRequest.stopWritingN)	r:   r;   r<   r=   Úmethodr9  Ú
persistentr6  r:  r>   r>   r>   r?   r4  Æ  s    r4  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   r5  r>   r>   r?   r6  è  s   
zSimpleRequest.writeToN)r:   r;   r<   r=   r=  r6  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   rc   r(   r„   r€   rj   r>   r>   r?   ÚsetUpó  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)r„   rÎ   r>  rH   rc   r“   rj   r>   r>   r?   Útest_requestû  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                    ó   ˆ   ˆ j ¡ d¡ d S ©Nr‘   ©rH   rc   r“   ©Úignoredrj   r>   r?   Ú	cbNotSent  ó   z?HTTP11ClientProtocolTests.test_secondRequest.<locals>.cbNotSent)r„   rÎ   r4  rQ   r>  r$   rR   ©rO   rI  rU   r>   rj   r?   Útest_secondRequest  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                    rD  rE  rF  rG  rj   r>   r?   rI    rJ  zLHTTP11ClientProtocolTests.test_requestAfterConnectionLost.<locals>.cbNotSent)	r„   r  r   r	   rQ   rÎ   r>  r$   rR   rK  r>   rj   r?   Útest_requestAfterConnectionLost  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                   @   re   )zCHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequestFc                 S   s
   t tƒ ƒS ri   )r   r8   r5  r>   r>   r?   r6  ,  r;  zKHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequest.writeToN©r:   r;   r<   r=  r6  r>   r>   r>   r?   ÚBrokenRequest)  ó    rO  c                    s&   ˆ   ˆ jj¡ ˆ j ttdƒƒ¡ d S )Nzyou asked for it)r£   rc   Údisconnectingr„   r  r   r	   rG  rj   r>   r?   rP   1  s   z>HTTP11ClientProtocolTests.test_failedWriteTo.<locals>.cbFailed)r„   rÎ   r[   r8   rR   )rO   rO  rU   rP   r>   rj   r?   Útest_failedWriteTo!  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                   @   re   )zMHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequestFc                 S   r  ri   r  r5  r>   r>   r?   r6  E  r  zUHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequest.writeToNrN  r>   r>   r>   r?   rO  B  rP  rO  )r„   rÎ   r[   r8   )rO   rO  rU   r>   r>   r?   Útest_synchronousWriteToError;  s   z6HTTP11ClientProtocolTests.test_synchronousWriteToErrorNc                 C   s    t ƒ }| j |¡}t| |tgƒ}|  |j¡ | j ttƒ ƒ¡ |  	|j¡ |dkr1|j
 d¡ |S |dkrM|j
 ttƒ ƒ¡ |  t¡}|  t|ƒd¡ |S 	 |S )a2  
        If L{HTTP11ClientProtocol}'s transport is disconnected before the
        L{Deferred} returned by L{Request.writeTo} fires, the L{Deferred}
        returned by L{HTTP11ClientProtocol.request} fires with a L{Failure} of
        L{RequestTransmissionFailed} wrapping the underlying failure.
        ÚcallbackNÚerrbackr_   )r4  r„   rÎ   r]   r8   r¥   r9  r  r   r£   rà   rT  rU  rA   r  rH   rI   )rO   ÚmoderÎ   rU   Úerrorsr>   r>   r?   Ú*test_connectionLostDuringRequestGenerationK  s    ù
ÿzDHTTP11ClientProtocolTests.test_connectionLostDuringRequestGenerationc                 C   ó
   |   d¡S )zÂ
        If the request passed to L{HTTP11ClientProtocol} finishes generation
        successfully after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        rT  ©rX  rj   r>   r>   r?   Ú+test_connectionLostBeforeGenerationFinishedk  ó   
zEHTTP11ClientProtocolTests.test_connectionLostBeforeGenerationFinishedc                 C   rY  )zÃ
        If the request passed to L{HTTP11ClientProtocol} finished generation
        with an error after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        rU  rZ  rj   r>   r>   r?   Ú)test_connectionLostBeforeGenerationFaileds  r\  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 )Nr_   r   r  r1  zJError writing request, but not in valid state to finalize request: {state}r�   ÚCONNECTION_LOST)r  rI   ÚassertInrH   )Úignorer  ©r  rO   r>   r?   Úcheck„  s   þznHTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse.<locals>.checkrU  )r   r  r   rX  rR   )rO   rb  r>   ra  r?   ÚEtest_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse{  s
   
ÿz_HTTP11ClientProtocolTests.test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfusec                    ó<   ˆ 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 )Nr`   Ú	QUIESCENT)	rH   rË   r�   r2   r£   rc   rQ  r„   r�   ©rÉ   rj   r>   r?   Ú	cbRequest›  s   zIHTTP11ClientProtocolTests.test_receiveSimplestResponse.<locals>.cbRequests9   HTTP/1.1 200 OK
Content-Length: 0
Connection: close

©r„   rÎ   r*   rb   rR   r‚   ©rO   rU   rg  r>   rj   r?   Útest_receiveSimplestResponse“  s   
ÿz6HTTP11ClientProtocolTests.test_receiveSimplestResponsec                    rd  )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   rH   r�   )rÉ   rŸ   rj   r>   r?   rg  ²  rÒ   zHHTTP11ClientProtocolTests.test_receiveResponseHeaders.<locals>.cbRequests+   HTTP/1.1 200 OK
X-Foo: bar
X-Foo: baz

rh  ri  r>   rj   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 )NÚ%TRANSMITTING_AFTER_RECEIVING_RESPONSEc                    ó
   ˆˆ j fS ri   ©rº   r   ©ÚprÉ   r>   r?   rÙ   Û  rÚ   zoHTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)	r   r   ÚclosedDeferredÚdeliverBodyrH   r�   r£   rQ  rR   ©rÉ   ÚwhenFinished)r„   ÚquiescentResultrO   rc   ro  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 )Nr^   r`   ra   rî   rŒ   ó   foobar)rH   rÊ   rË   rÌ   r�   r2   rà   rT  )ÚresultrÉ   rá   ©rÎ   rO   r>   r?   ÚcbAllResponseß  s   z`HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)r   r(   r~   r€   r4  rÎ   r‚   rR   ©rO   rU   rv  rz  r>   )r„   ru  rÎ   rO   rc   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(   r„   r€   r   Ú
MAX_LENGTHrÎ   r*   rb   r‚   rY   r	   )rO   rc   r„   ÚlongLinerU   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                    rm  ri   rn  r   ro  r>   r?   rÙ     rÚ   z„HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)r   r   rq  rr  rR   rs  r>   ro  r?   rv    s   
zrHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponsec                    s2   ˆ j  d ¡ ˆj ttƒ ƒ¡ ˆ ˆjjd¡ d S )Nr^  )rà   rT  r„   r  r   r8   rH   rs   )r`  ry  r>   r?   rz    s   zuHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbAllResponse)r4  r„   rÎ   r‚   rR   r{  r>   ry  r?   ÚDtest_connectionLostAfterReceivingResponseBeforeRequestGenerationDone  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 )Nrw  )rH   rº   ÚclosedReasonrG   r.   rG  rÑ   r>   r?   rz  J  s   zIHTTP11ClientProtocolTests.test_receiveResponseBody.<locals>.cbAllResponse)r   r   rq  r„   rÎ   r*   rb   r‚   rR   r~   rH   rr  )rO   rt  ÚrequestDeferredrx  rÉ   rz  r>   rÑ   r?   Útest_receiveResponseBody(  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Œ   rw  z low-level transport disconnected)r„   rÎ   r*   rb   r‚   rR   r~   r   rr  rH   rº   r  r   r	   r�  rG   r)   ©rO   r‚  rx  rÉ   r„   r>   r>   r?   ÚEtest_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknownQ  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
rw  )r„   rÎ   r*   rb   r‚   rR   r~   r   rr  rH   rº   r  r   r8   rY   r   r�  r1   r„  r>   r>   r?   Ú4test_chunkedResponseBodyUnfinishedWhenConnectionLostn  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£   rc   rQ  rH   rF   r“   rº   r„   r  r   r	   ©r½   rj   r>   r?   rP   ›  s
   ÿzLHTTP11ClientProtocolTests.test_parserDataReceivedException.<locals>.cbFailed)r„   rÎ   r*   rb   r‚   rY   r#   rR   )rO   r‚  rU   rP   r>   rj   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)r„   rÎ   r*   rb   Ú_parserrc   rñ   Ú	_producerÚ_disconnectParserr   r	   rY   )rO   r‚  rc   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(   r€   ÚabortrR   r~   rH   r£   rQ  r  r   r	   )rO   rc   r„   Úr1Ú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(   r€   r  r   r	   rŽ  rR   r~   rH   rs   ©rO   rc   r„   rx  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(   r€   rÎ   r*   rb   rŽ  r£   rQ  r  r   r	   rY   r   r’  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)rŽ  rj   )r„   r>   r?   ÚconnectionMade  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)rU  ©rO   rK   )Ú
testResultr>   r?   r    rZ   z`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionLostN)r:   r;   r<   r=   r–  r  r>   )r„   r˜  r>   r?   ÚBodyDestination  s    r™  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)rr  r£   rQ  r  r   r	   rf  )r™  r„   rO   rc   r>   r?   rr    s   zMHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.deliverBodyc                    s   ˆ   | jt¡ d S ri   )r  rÉ   r-   )Úerrorrj   r>   r?   Ú
checkError  ó   zLHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.checkError)r   r(   r€   rÎ   r*   rb   r‚   r   r   rR   rY   r   r1   )rO   rx  rr  r›  rS   r>   )r™  r„   rO   r˜  rc   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                    ó(   ˆ  | ˆ ¡ ˆ  | jd¡ ˆ | ¡ d S ©Nre  ©rH   r�   r~   ©rp  ©r„   ru  rO   r>   r?   rT  5  ó   zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.callbackrÅ   rÆ   NT©r=  s&   HTTP/1.1 200 OK
Content-length: 3

r   c                    rÔ   )Núresponse donerÖ   r   )ru  r>   r?   rÙ   O  rÚ   zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.<lambda>r  r¥  )r   r(   r€   rÎ   r*   rb   r‚   rH   rR   r~   r   r   rq  rr  r�  rG   r.   rŠ  Ú_finishedRequestÚ_currentRequestÚ_transportProxyr  )rO   rT  rc   r‚  rx  rÉ   ÚbodyProtocolr>   r¢  r?   Útest_quiescentCallbackCalled'  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                    rž  rŸ  r   r¡  r¢  r>   r?   rT  m  r£  z]HTTP11ClientProtocolTests.test_transportProducingWhenQuiescentAfterFullBody.<locals>.callbackrÅ   rÆ   NTr¤  s)   HTTP/1.1 200 OK
Content-length: 3

BBBÚDEFERRED_CLOSEr_   rÛ   )r   r(   r€   rÎ   r*   rb   r‚   ÚsuccessResultOfrH   rs   rI   rÞ   )rO   rT  rc   r‚  rÉ   r>   r¢  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                    rž  rŸ  r   r¡  r¢  r>   r?   rT  •  r£  zUHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponse.<locals>.callbackrÅ   rÆ   NTr¤  ó&   HTTP/1.1 200 OK
Content-length: 0

r·   r   r_   )r   r(   r€   rÎ   r*   rb   rR   r~   r‚   rH   rI   rñ   r  r-   )rO   rT  rc   r‚  r>   r¢  r?   Ú)test_quiescentCallbackCalledEmptyResponseŽ  s   
ÿ
zCHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponsec              	   C   ó�   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Æ   NTr¤  s9   HTTP/1.1 200 OK
Content-length: 0
Connection: close

r   ©r   r(   r~   r€   rÎ   r*   rb   r‚   rR   r   rr  r�  rG   r.   rH   r£   rQ  ©rO   ru  rc   r„   r‚  rx  rÉ   r©  r>   r>   r?   Útest_quiescentCallbackNotCalled¨  s$   

ÿÿ
z9HTTP11ClientProtocolTests.test_quiescentCallbackNotCalledc              	   C   r°  )z 
        If the request was non-persistent (i.e. sent C{Connection: close}),
        the C{quiescentCallback} is not called and the connection is lost.
        rÅ   rÆ   NFr¤  r®  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   r  ri   )ÚZeroDivisionErrorr¡  r>   r>   r?   rT  æ  r  zHHTTP11ClientProtocolTests.test_quiescentCallbackThrows.<locals>.callbackrÅ   rÆ   NTr¤  r®  r   r_   r  )r   r  r   r   r(   r€   rÎ   r*   rb   r‚   rR   r~   r   rr  r�  rG   r.   r  rI   r  r“   rµ  r  r£   rQ  )rO   rT  r  rc   r„   r‚  rx  rÉ   r©  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(   r€   rÎ   r*   rb   Úcancelr£   ÚdisconnectedrV   r0   r   r’  r>   r>   r?   Útest_cancelBeforeResponse  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(   r€   rÎ   r*   rb   r‚   r·  r£   r¸  rY   r   r’  r>   r>   r?   Útest_cancelDuringResponse  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·  2  ó   zJHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.cancelc                    s   | ˆ_ tˆ ƒˆ_ˆjS ri   ©Úconsumerr   rà   )r¿  )r·  Úproducerr>   r?   ÚstartProducing5  s   
zRHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.startProducingó   POSTó   /bars   xxxxx)r   r(   r€   ÚStringProducerrÁ  rÎ   r*   rb   r¿  r@  r·  r£   r¸  r[   r   )rO   ÚproducerLengthrc   r„   rÁ  rx  r>   )r·  r¼  rÀ  r?   Ú assertCancelDuringBodyProduction$  s   
z:HTTP11ClientProtocolTests.assertCancelDuringBodyProductionc                 C   rY  )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Æ  rj   r>   r>   r?   Útest_cancelDuringBodyProductionC  ó   
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   rj   r>   r>   r?   Ú&test_cancelDuringChunkedBodyProductionL  rÉ  z@HTTP11ClientProtocolTests.test_cancelDuringChunkedBodyProductionri   )'r:   r;   r<   r=   rB  rC  rL  rM  rR  rS  rX  r[  r]  rc  rj  rk  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?   rA  í  sJ    
 2 ) :9.#	rA  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 ri   )rß   )rO   rß   r>   r>   r?   Ú__init__f  r;  zStringProducer.__init__c                 C   s   || _ tƒ | _| jS ri   r¾  )rO   r¿  r>   r>   r?   rÁ  i  s   zStringProducer.startProducingc                 C   r7  rÕ   r8  rj   r>   r>   r?   ÚstopProducingn  r;  zStringProducer.stopProducingc                 C   rh   ri   r>   rj   r>   r>   r?   ÚpauseProducingq  ó   zStringProducer.pauseProducingc                 C   rh   ri   r>   rj   r>   r>   r?   ÚresumeProducingu  rÎ  zStringProducer.resumeProducingN)
r:   r;   r<   r=   r9  rË  rÁ  rÌ  rÍ  rÏ  r>   r>   r>   r?   rÄ  V  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 ri   )r   rc   rj   r>   r>   r?   rB    r½  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*   rb   r6  rc   rH   r“   rj   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Æ   NTr¤  s%   GET / HTTP/1.1
Host: example.com

rÑ  )rO   Ú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…= | ¡  |  |g d¢¡ dS )zf
        L{Request.writeTo} formats header data and writes it to the given
        transport.
        rŒ   r›   rC   )rî   rB   rÅ   ó   /fooNr°   r   ó   GET /foo HTTP/1.1éþÿÿÿr‘   )ó   Connection: closes   Host: example.comr�   rš   )r2   r*   r6  rc   r“   ÚsplitrH   Úsort)rO   r�   Úlinesr>   r>   r?   Útest_sendRequestHeaders›  s   þz$RequestTests.test_sendRequestHeadersc                 C   s²   t D ]T}t||gddgiƒ}tƒ }tdd|dƒ |¡ | ¡  d¡}|  |d d¡ |  |d	d… d
d
g¡ |d= |d	d…= | d¡ | d¡ d 	t
t
g¡}|  ||g¡ qdS )z_
        Linear whitespace in request headers is replaced with a single
        space.
        rB   s   example.invalidrÅ   rÕ  Nr°   r   rÖ  r×  r‘   rØ  s   Host: example.invalids   : )r5   r2   r   r*   r6  r“   rÙ  rH   Úremover’   r6   )rO   Ú	componentr�   rc   rÛ  ÚsanitizedHeaderLiner>   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*   rb   r6  rc   ÚassertNotIdenticalr¿  rñ   rÀ  r£   Ú	streamingrH   r“   Úclearr@  rà   rT  ©rO   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 rE  )rH   rc   r“   rñ   rÀ  rG  rj   r>   r?   rP   î  s   zCRequestTests.test_sendChunkedRequestBodyWithError.<locals>.cbFailed)rÄ  r   r*   rb   r6  rc   rã  rà   rU  r8   rQ   rR   )rO   rÀ  rÎ   ÚwriteDeferredrP   rU   r>   rj   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*   rb   r6  rc   rá  r¿  rñ   rÀ  r£   râ  rH   r“   rã  r@  rà   rT  rä  r>   r>   r?   Útest_sendRequestBodyWithLengthö  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Ñ  )rO   r<  rÎ   r>   r>   r?   Ú_sendRequestEmptyBodyWithLength  s   	þz,RequestTests._sendRequestEmptyBodyWithLengthc                 C   ó   |   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ê  rj   r>   r>   r?   Útest_sendPUTRequestEmptyBody(  ó   z)RequestTests.test_sendPUTRequestEmptyBodyc                 C   rë  )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ì  rj   r>   r>   r?   Útest_sendPOSTRequestEmptyBody0  rî  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*   rb   r6  rc   r¿  r@  rà   rT  rñ   rÀ  rQ   r&   ©rO   rÀ  rÎ   ræ  r>   r>   r?   Ú#test_sendRequestBodyWithTooFewBytes8  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‘   )rH   rc   r“   rã  rw   r   r¿  r@  rˆ  ©ÚfinisherrÀ  rO   r>   r?   rP   g  s   þ
	zCRequestTests._sendRequestBodyWithTooManyBytesTest.<locals>.cbFailed)rÄ  r*   rb   r6  rc   r¿  r@  r¥   r9  r£   rñ   rÀ  rQ   r&   rR   )rO   rõ  rÎ   ræ  rP   rU   r>   rô  r?   Ú$_sendRequestBodyWithTooManyBytesTestG  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   ó   | j  d ¡ d S ri   ©rà   rT  ©rÀ  r>   r>   r?   rõ  �  ó   zCRequestTests.test_sendRequestBodyWithTooManyBytes.<locals>.finisher)rö  ©rO   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  r_   )	rà   rU  r8   r_  r  r“   r  rH   rI   )rÀ  r  rÀ   rW  ra  r>   r?   rõ  ž  s   
zHRequestTests.test_sendRequestBodyErrorWithTooManyBytes.<locals>.finisher)r   r  r   rö  rü  r>   ra  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  r_   )r   r  r   rÄ  r*   rb   r6  rc   r¿  Ú	_finishedr@  rà   rT  rU  r8   r_  r  r“   rH   rI   r  )rO   r  rÀ  rÎ   Ú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*   rb   r6  rc   r¿  r@  rñ   rÀ  rã  rw   r   rH   r“   )rO   rõ  rÀ  rÎ   ræ  r>   r>   r?   Ú-_sendRequestBodyFinishedEarlyThenTooManyBytesÄ  s   
z:RequestTests._sendRequestBodyFinishedEarlyThenTooManyBytesc                 C   ó   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   rø  ri   rù  rú  r>   r>   r?   rõ  ß  rû  zPRequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytes.<locals>.finisher)rQ   r  r&   rü  r>   r>   r?   Ú1test_sendRequestBodyFinishedEarlyThenTooManyBytes×  ó
   þz>RequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytesc                 C   r  )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 ri   )rà   rU  r8   rú  r>   r>   r?   rõ  ï  rœ  zORequestTests.test_sendRequestBodyErroredEarlyThenTooManyBytes.<locals>.finisher)rQ   r  r8   rü  r>   r>   r?   Ú0test_sendRequestBodyErroredEarlyThenTooManyBytesç  r  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*   rb   r6  rc   rà   rT  rã  rw   r   r¿  r@  rH   r“   )rO   Ú_withrÀ  rÎ   ræ  r>   r>   r?   Ú0test_sendChunkedRequestBodyFinishedThenWriteMore÷  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   r8   rQ   )rO   rU   r>   r>   r?   Ú9test_sendChunkedRequestBodyFinishedWithErrorThenWriteMore	  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*   rb   r6  rc   rñ   rÀ  r£   râ  r¿  r@  rH   r“   r¥   rQ  rà   rU  r   r8   rQ   rñ  r>   r>   r?   Útest_sendRequestBodyWithError	  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   HostrC   s   example.org)r*   r2   rw   r   r6  rc   rH   r“   )rO   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*   rb   r6  rc   r¥   r9  r:  r£   rä  r>   r>   r?   Útest_stopWritingJ	  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ƒ‚©NzstopProducing is bustedr  r>   r>   r>   r?   ÚbrokenStopProducing^	  s   zBRequestTests.test_brokenStopProducing.<locals>.brokenStopProducingrÅ   rÆ   r_   r   r  N)r   r  r   rÄ  rÌ  r*   rb   r6  rc   r:  rH   rI   r  r8   r  r_  r  r“   )rO   r  rÀ  r  rÎ   r  rÀ   r>   r>   r?   Útest_brokenStopProducingV	  s   z%RequestTests.test_brokenStopProducingri   )r:   r;   r<   r=   rB  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Ð  z  s6    !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   rx  rÄ  rÀ  r   rc   r"   Úenforcerrj   r>   r>   r?   rB  s	  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@  rH   rc   r“   rã  rj   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@  rw   r&   Ú_noMoreWritesExpectedrj   r>   r>   r?   Útest_finishedEarly‡	  s   z/LengthEnforcingConsumerTests.test_finishedEarlyFc                 C   sP   | j  d¡ |  | jj¡ | j  d¡ |  | jj¡ |r!| 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À  r9  r£   r  rQ   rx  r&   )rO   Ú_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À  r9  rw   r   r£   rj   r>   r>   r?   Útest_writeAfterNoMoreExpected 	  s
   
z:LengthEnforcingConsumerTests.test_writeAfterNoMoreExpectedc                 C   rY  )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  rj   r>   r>   r?   Útest_finishedLate¬	  r\  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  rj   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ƒ‚r  )rÄ  rÌ  rÀ  r8   r>   rj   r>   r?   r  Å	  s   zRLengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.brokenStopProducingc                    s   ˆ   tˆ  t¡ƒd¡ d S )Nr_   )rH   rI   r  r8   rG  rj   r>   r?   Ú
cbFinishedË	  s   zILengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.cbFinished)rÀ  rÌ  r  rR   )rO   r  r  rU   r>   rj   r?   Útest_stopProducingRaises¼	  s   	
z5LengthEnforcingConsumerTests.test_stopProducingRaisesN)F)r:   r;   r<   r=   rB  r  r  r  r  r  r  r  r>   r>   r>   r?   r  n	  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   rj   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@  rH   r“   rã  )rO   rc   Ú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   ÚregisterProducerrñ   rÀ  r£   râ  ÚunregisterProducerrH   r“   )rO   rc   rÀ  r!  r>   r>   r?   Útest_producerRegistrationí	  s   z2RequestBodyConsumerTests.test_producerRegistrationN)r:   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%   rj   r>   r>   r?   r   
  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ñ   r‹  ÚstopProxying©rO   rc   Úproxyr>   r>   r?   Ú%test_stopProxyingUnreferencesProducer
  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%   rH   rÞ   rÏ  r&  r'  r>   r>   r?   Útest_resumeProducing
  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%   rH   rÞ   rÍ  rÏ  r&  r'  r>   r>   r?   Útest_pauseProducing,
  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Û   r9  N)r   r%   rH   rÞ   rÌ  r&  r'  r>   r>   r?   Útest_stopProducingA
  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   r€   r„   r%   r£   Ú	connectedrH   rÞ   ÚloseConnectionr¥   ©rO   rc   r„   r(  r>   r>   r?   Ú test_loseConnectionWhileProxyingU
  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   r€   r„   r%   r&  r£   r-  r.  r/  r>   r>   r?   Útest_loseConnectionNotProxyingj
  s   
z:TransportProxyProducerTests.test_loseConnectionNotProxyingN)r:   r;   r<   r=   r   r)  r*  r+  r,  r0  r1  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 S )!ÚResponseTestsz 
    Tests for L{Response}.
    c                 C   s   t tƒ ƒ}|  tt|ƒ¡ dS )z=
        L{Response} instances provide L{IResponse}.
        N)rd   r   r£   r   r4   ©rO   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                       ó   e Zd Z‡ fdd„ZdS )z7ResponseTests.test_makeConnection.<locals>.SomeProtocolc                    ó   ˆ   |¡ d S ri   rÖ   )rO   rÀ  ©Ú	producersr>   r?   r€   “
  ó   zFResponseTests.test_makeConnection.<locals>.SomeProtocol.makeConnectionN©r:   r;   r<   r€   r>   r7  r>   r?   ÚSomeProtocol’
  ó    r;  rú   rÛ   N)r   r   rd   rr  rÍ  rH   rÞ   rÏ  )rO   rc   r;  r¿  rÉ   ÚtheProducerr>   r7  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                       r5  )z5ResponseTests.test_dataReceived.<locals>.ListConsumerc                    r6  ri   rÖ   ©rO   rº   ©r¬   r>   r?   r‚   ¨
  r9  zBResponseTests.test_dataReceived.<locals>.ListConsumer.dataReceivedN©r:   r;   r<   r‚   r>   r@  r>   r?   ÚListConsumer§
  r<  rB  rÂ   N)r   rd   r   rr  rÜ   rH   ©rO   rB  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                       r5  )z7ResponseTests.test_connectionLost.<locals>.ListConsumerc                    r6  ri   rÖ   r—  ©Úlostr>   r?   r  ¼
  r9  zFResponseTests.test_connectionLost.<locals>.ListConsumer.connectionLostN)r:   r;   r<   r  r>   rE  r>   r?   rB  »
  r<  rB  r   r_   N)r   rd   r   rr  rÝ   rG   r.   rH   rI   rñ   Ú_bodyProtocolrC  r>   rE  r?   Útest_connectionLost²
  s   

z!ResponseTests.test_connectionLostc                    sr   g ‰ G ‡ fdd„dt ƒ}|ƒ }ttƒ ƒ}| d¡ | d¡ | |¡ | d¡ |  ˆ g d¢¡ |  |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                       r5  )z8ResponseTests.test_bufferEarlyData.<locals>.ListConsumerc                    r6  ri   rÖ   r?  r@  r>   r?   r‚   Ô
  r9  zEResponseTests.test_bufferEarlyData.<locals>.ListConsumer.dataReceivedNrA  r>   r@  r>   r?   rB  Ó
  r<  rB  rÂ   rŒ   r›   )rÂ   rŒ   r›   N)r   rd   r   rÜ   rr  rH   rñ   Ú_bodyBuffer)rO   rB  r„   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)rd   r   rr  r   rw   rx   r3  r>   r>   r?   Ú test_multipleStartProducingFailsâ
  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)rd   r   rr  r   rÝ   rw   rx   r3  r>   r>   r?   Ú%test_startProducingAfterFinishedFailsë
  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)rd   r   rÝ   rw   rx   rÜ   r3  r>   r>   r?   Ú'test_bodyDataReceivedAfterFinishedFailsõ
  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)rd   r   rÝ   rr  r   rw   rx   rÜ   r3  r>   r>   r?   Ú'test_bodyDataReceivedAfterDeliveryFailsþ
  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)rd   r   rÝ   rw   rx   r3  r>   r>   r?   Ú'test_bodyDataFinishedAfterFinishedFails  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)rd   r   rÝ   rr  r   rw   rx   r3  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                       r5  )z9ResponseTests.test_transportResumed.<locals>.ListConsumerc                    s   ˆ   |j¡ d S ri   )r~   rÞ   r5  ©ÚtransportStater>   r?   r€   #  rû  zHResponseTests.test_transportResumed.<locals>.ListConsumer.makeConnectionNr:  r>   rQ  r>   r?   rB  "  r<  rB  rú   rÛ   N)r   r   rÍ  rd   rH   rÞ   rr  )rO   rB  rc   r„   rÉ   r>   rQ  r?   Útest_transportResumed  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Œ   rw  N)r   rd   rÜ   rÝ   r   rr  rH   rº   r�  rG   r.   ©rO   rc   rÉ   r„   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.
        Ú	CONNECTEDN)r   rd   r   rr  rH   rs   rÝ   r   r8   r�  rG   rT  r>   r>   r?   Ú#test_finishedWithErrorWhenConnectedA  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.
        ÚINITIALN)r   rd   rH   rs   rÝ   r   r8   r   rr  r�  rG   rT  r>   r>   r?   Ú!test_finishedWithErrorWhenInitialT  s   
z/ResponseTests.test_finishedWithErrorWhenInitialN)r:   r;   r<   r=   r4  r>  rD  rH  rJ  rK  rL  rM  rN  rO  rP  rS  rU  rW  rY  r>   r>   r>   r?   r2  }
  s"    	
	
	
r2  N)_r=   Útypingr   Úzope.interfacer   Úzope.interface.verifyr   Útwisted.internet.deferr   r   r   r   Útwisted.internet.errorr	   r
   Útwisted.internet.interfacesr   r   Útwisted.internet.protocolr   Útwisted.loggerr   Útwisted.protocols.basicr   Útwisted.python.failurer   Útwisted.test.proto_helpersr   r   r   r   Útwisted.trial.unittestr   Útwisted.web._newclientr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   Útwisted.web.clientr(   r)   r*   r+   r,   r-   r.   r/   r0   Útwisted.web.httpr1   Útwisted.web.http_headersr2   Útwisted.web.iwebr3   r4   Útwisted.web.test.requesthelperr5   r6   Ú	Exceptionr8   rA   rb   rV   rY   r[   r]   rd   rf   r{   r¯   r³   rµ   r4  r>  rA  rÄ  rÐ  r  r  r%  r2  r>   r>   r>   r?   Ú<module>   sp   P,(


!     `      o#   we,~