o
    Óó·iˆ· ã                   @   s¦  d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZD ddlEmFZFmGZG G dd„ deHƒZIG dd„ deHƒZJeAddgiƒZKdd„ ZLdd„ ZMdd„ ZNd d!„ ZOd"d#„ ZPG d$d%„ d%e ƒZQG d&d'„ d'ƒZRG d(d)„ d)eRe ƒZSG d*d+„ d+eRe ƒZTG d,d-„ d-e ƒZUG d.d/„ d/ƒZVG d0d1„ d1ƒZWG d2d3„ d3e ƒZXeeCƒG d4d5„ d5ƒƒZYG d6d7„ d7e ƒZZG d8d9„ d9e ƒZ[G d:d;„ d;e ƒZ\G d<d=„ d=e ƒZ]G d>d?„ d?e ƒZ^d@S )Az&
Tests for L{twisted.web._newclient}.
é    )ÚOptional)Úimplementer)ÚverifyObject)ÚCancelledErrorÚDeferredÚfailÚsucceed)ÚConnectionDoneÚConnectionLost)Ú	IConsumerÚIPushProducer)ÚProtocol)ÚAccumulatingProtocolÚEventLoggingObserverÚStringTransportÚ StringTransportWithDisconnection)ÚglobalLogPublisher)ÚLineReceiver)ÚFailure)ÚTestCase)ÚBODYÚDONEÚHEADERÚSTATUSÚUNKNOWN_LENGTHÚ
BadHeadersÚBadResponseVersionÚChunkedEncoderÚConnectionAbortedÚExcessWriteÚHTTPClientParserÚ
HTTPParserÚLengthEnforcingConsumerÚ
ParseErrorÚRequestNotSentÚTransportProxyProducerÚWrongBodyLengthÚmakeStatefulDispatcher)	ÚHTTP11ClientProtocolÚPotentialDataLossÚRequestÚRequestGenerationFailedÚRequestTransmissionFailedÚResponseÚResponseDoneÚResponseFailedÚResponseNeverReceived)Ú	_DataLoss)ÚHeaders)ÚIBodyProducerÚ	IResponse)ÚbytesLinearWhitespaceComponentsÚsanitizedBytesc                   @   ó   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>   úeC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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>.cbFailedN)ÚassertFailureÚaddCallback)rO   ÚdeferredZmainTyperN   rP   Údr>   rM   r?   ÚassertWrapperExceptionTypesU   s   

rU   c                 C   ó   t | |t|ƒS )zo
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{ResponseFailed}.
    N)rU   r/   ©rO   rS   rN   r>   r>   r?   ÚassertResponseFailed}   ó   rX   c                 C   rV   )zx
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestGenerationFailed}.
    N)rU   r+   rW   r>   r>   r?   ÚassertRequestGenerationFailed…   ó   ÿrZ   c                 C   rV   )zz
    A simple helper to invoke L{assertWrapperExceptionTypes} with a C{mainType}
    of L{RequestTransmissionFailed}.
    N)rU   r,   rW   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^   éÈ   ó   OKN)r-   Ú_boringHeaders)Ú	transportr>   r>   r?   ÚjustTransportResponse™   s   rc   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.barZquuxc                 S   ó   dS )NÚar>   ri   r>   r>   r?   Ú_quux_A¶   rk   zKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_Ac                 S   rl   )NÚbr>   ri   r>   r>   r?   Ú_quux_B¹   rk   zKMakeStatefulDispatcherTests.test_functionCalledByState.<locals>.Foo._quux_BN)r:   r;   r<   Ú_staterj   r'   rn   rp   r>   r>   r>   r?   ÚFoo®   s    
rr   rm   ÚBro   ÚCN)rH   rj   rq   ÚassertRaisesÚRuntimeError)rO   rr   Zstatefulr>   r>   r?   Útest_functionCalledByState¨   s   z6MakeStatefulDispatcherTests.test_functionCalledByStateN)r:   r;   r<   r=   rw   r>   r>   r>   r?   re   £   s    re   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   ÚdataReceivedry   r   )rO   ÚstatusÚprotocolr>   r>   r?   Útest_statusCallbackÌ   s   z$_HTTPParserTests.test_statusCallbackc                 C   s6   i }t ƒ }|j|_| tƒ ¡ | d| j ¡ ||fS )Nrz   )r!   Ú__setitem__ÚheaderReceivedr}   r   r   ry   ©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   ry   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‡   ry   Ú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 rh   )r{   r~   r   r>   ©Úcalledr�   r>   r?   ÚallHeadersReceived  s   
zD_HTTPParserTests.test_allHeadersCallback.<locals>.allHeadersReceivedN)r‡   r”   r   ry   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}.
        rz   rŒ   ó
   X-Foo: bazrˆ   r‰   ó   bazN)	r!   r}   r   r   ry   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!   Ú
assertTrueZisConnectionControlHeaderÚassertFalse)rO   r�   ZconnHeaderNamesr†   r>   r>   r?   Útest_connectionControlHeaders1  s   ÿþþz._HTTPParserTests.test_connectionControlHeadersc                 C   s4   t ƒ }| tƒ ¡ | tƒ ¡ |  t|jtƒ ¡ dS )zi
        L{HTTPParser.switchToBodyMode} raises L{RuntimeError} if called more
        than once.
        N)r!   r}   r   ZswitchToBodyModeÚobjectru   rv   ©rO   r�   r>   r>   r?   Útest_switchToBodyModeO  s   z&_HTTPParserTests.test_switchToBodyMode)r:   r;   r<   r=   ry   r   ÚbytesÚ__annotations__r‚   r‡   r‹   r�   r‘   r•   r–   r�   r¢   r¥   r>   r>   r>   r?   rx   Ä   s   
 
rx   c                   @   ó   e Zd ZdZdZdS )Ú$HTTPParserRFCComplaintDelimeterTestsz<
    L{_HTTPParserTests} using standard CR LF newlines.
    ó   
N©r:   r;   r<   r=   ry   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<d=„ Z!d>d?„ Z"d@dA„ Z#dBdC„ Z$dDdE„ Z%dFS )GÚHTTPClientParserTestszd
    Tests for L{HTTPClientParser} which is responsible for parsing HTTP
    response messages.
    c                 C   s    t ddƒ}|  | d¡d¡ dS )zj
        L{HTTPClientParser.parseVersion} parses a status line into its three
        components.
        Ns	   CANDY/7.2)s   CANDYé   é   )r    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 rh   )ru   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_   r`   ©r*   ra   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 rh   )ru   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>   ©ZbodyDataFinishedr>   r?   Ú<lambda>Ö  ó   
 z3HTTPClientParserTests._noBodyTest.<locals>.<lambda>Ú	producingrŽ   Tr   N)r    r{   rƒ   r„   r   r}   r   rÃ   Ú_bodyDataReceivedÚ_bodyDataFinishedrH   ÚproducerStater~   r   Úlength)	rO   rÈ   r€   rÃ   r†   ÚfinishedÚbodyr�   rb   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*   ra   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*   ra   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   rg   rh   r>   ©Úrestr>   r>   r?   rÒ     ó    z<HTTPClientParserTests.test_responseHeaders.<locals>.<lambda>rÁ   s   X-Foo: bar
rª   ó   x-foor‰   ©r    r*   ra   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   rg   rh   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   rg   rh   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*   ra   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   rg   rh   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*   ra   r{   r   r}   r   rÃ   rÕ   rH   r×   r~   r   r   ©rO   rÙ   r�   rb   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*   ra   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   ó4   t tddtdƒdƒ}| tƒ ¡ |  t|jd¡ dS )zè
        If a response includes multiple, distinct I{Content-Length} headers,
        L{HTTPClientParser.dataReceived} raises L{ValueError} to indicate that
        the response is invalid and the transport is now unusable.
        r¿   rÀ   Ns9   HTTP/1.1 200 OK
Content-Length: 1
Content-Length: 2

©r    r*   ra   r}   r   ru   Ú
ValueErrorr   r¤   r>   r>   r?   Ú)test_multipleDistinctContentLengthHeaders�  ó   ýz?HTTPClientParserTests.test_multipleDistinctContentLengthHeadersc                 C   óL   t tddtdƒdƒ}| tƒ ¡ | d¡ |  |jjd¡ |  |j	t
¡ dS )zË
        If a response includes multiple, yet equal, I{Content-Length} headers,
        L{HTTPClientParser.dataReceived} successfully handles and passes single
        length for body processing
        r¿   rÀ   Ns9   HTTP/1.1 200 OK
Content-Length: 1
Content-Length: 1

r^   ©r    r*   ra   r}   r   r   rH   rÃ   rØ   r~   r   r¤   r>   r>   r?   Ú&test_multipleEqualContentLengthHeaders“  s   ÿz<HTTPClientParserTests.test_multipleEqualContentLengthHeadersc                 C   rù   )zô
        If a response includes multiple, distinct I{Content-Length} header
        field-values, L{HTTPClientParser.dataReceived} raises L{ValueError} to
        indicate that the response is invalid and the transport is now unusable.
        r¿   rÀ   Ns)   HTTP/1.1 200 OK
Content-Length: 1, 2

rú   r¤   r>   r>   r?   Ú3test_multipleDistinctContentLengthHeaderFieldValues¥  rý   zIHTTPClientParserTests.test_multipleDistinctContentLengthHeaderFieldValuesc                 C   rþ   )a%  
        If a response includes multiple, equal I{Content-Length} header
        field-values, L{HTTPClientParser.dataReceived} successfully handles and
        passes single content-length header for body processing. Field-values
        are considered equal by parsed decimal value.
        r¿   rÀ   Ns+   HTTP/1.1 200 OK
Content-Length: 1, 001

r^   rÿ   r¤   r>   r>   r?   Ú0test_multipleEqualContentLengthHeaderFieldValues´  s   ÿzFHTTPClientParserTests.test_multipleEqualContentLengthHeaderFieldValuesc                 C   rù   )zç
        If the I{Content-Length} header contains anything other than digits
        L{HTTPClientParser.dataReceived} raises L{ValueError} to
        indicate that the response is invalid and the transport is now unusable.
        r¿   rÀ   Ns'   HTTP/1.1 200 OK
Content-Length: +1

rú   r¤   r>   r>   r?   Útest_contentLengthTooPositiveÄ  rý   z3HTTPClientParserTests.test_contentLengthTooPositivec                 C   rù   )zØ
        If the I{Content-Length} header has a negative value
        L{HTTPClientParser.dataReceived} raises L{ValueError} to
        indicate that the response is invalid and the transport is now unusable.
        r¿   rÀ   Ns'   HTTP/1.1 200 OK
Content-Length: -1

rú   r¤   r>   r>   r?   Útest_contentLengthNegativeÓ  rý   z0HTTPClientParserTests.test_contentLengthNegativec                 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*   ra   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*   ra   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*   ra   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*   ra   r{   r   r}   r   rÃ   rÕ   rH   rô   r>   r>   r?   Ú%test_contentLengthAndTransferEncodingA  s   ÿ


ÿ
z;HTTPClientParserTests.test_contentLengthAndTransferEncodingc                 C   sH   t ƒ }ttddtdƒdƒ}| |¡ |j}| ttƒ ƒ¡ t	| |tgƒS )zÅ
        If L{HTTPClientParser.connectionLost} is called before the headers are
        finished, the C{_responseDeferred} is fired with the L{Failure} passed
        to C{connectionLost}.
        r¿   rÀ   N)
r   r    r*   ra   r}   Ú_responseDeferredr  r   r8   rX   )rO   rb   r�   ZresponseDeferredr>   r>   r?   Útest_connectionLostBeforeBody_  s   
z3HTTPClientParserTests.test_connectionLostBeforeBodyc                 C   sª   t  | t¡}tƒ }ttddtdƒdƒ}| |¡ g }|j 	|j
¡ | d¡ |d }d
dd„}||_| d¡ |  dt|ƒ¡ |d }|d	 }|  |jt¡ |  t¡ dS )z°
        If one of the L{Response} methods called by
        L{HTTPClientParser.connectionLost} raises an exception, the exception
        is logged and not re-raised.
        r¿   rÀ   Nó&   HTTP/1.1 200 OK
Content-Length: 1

r   c                 S   ó   t ƒ ‚rh   ©r8   )rJ   r>   r>   r?   ÚfakeBodyDataFinished  ó   zPHTTPClientParserTests.test_connectionLostWithError.<locals>.fakeBodyDataFinishedr^   Úlog_failurerh   )r   ÚcreateWithCleanupr   r   r    r*   ra   r}   r  rR   r{   r   rÖ   r  ÚassertEqualsrI   ÚassertIsInstancer�   r8   ÚflushLoggedErrors)rO   ÚlogObserverrb   r�   rÃ   r  Úeventrº   r>   r>   r?   Útest_connectionLostWithErrorn  s    



z2HTTPClientParserTests.test_connectionLostWithErrorc                 C   sB   t tddtdƒdd„ ƒ}|j}| tƒ ¡ | tƒ ¡ |  |t	¡S )z‰
        If no response at all was received and the connection is lost, the
        resulting error is L{ResponseNeverReceived}.
        rÜ   rÀ   Nc                 S   rg   rh   r>   ©Zignr>   r>   r?   rÒ   ‘  ræ   z<HTTPClientParserTests.test_noResponseAtAll.<locals>.<lambda>)
r    r*   ra   r  r}   r   r  r
   rQ   r0   ©rO   r�   rT   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   rg   rh   r>   r"  r>   r>   r?   rÒ      ræ   zBHTTPClientParserTests.test_someResponseButNotAll.<locals>.<lambda>ó   2)r    r*   ra   r  r}   r   r   r  r
   rQ   r/   rR   r  r#  r>   r>   r?   Útest_someResponseButNotAll™  s   ÿ
ÿz0HTTPClientParserTests.test_someResponseButNotAllc                 C   sš   d}t tddtdƒdd„ ƒ}| tƒ ¡ | |¡ |  t|ddƒd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   rg   rh   r>   r"  r>   r>   r?   rÒ   ¹  ræ   zCHTTPClientParserTests.test_1XXResponseIsSwallowed.<locals>.<lambda>rÃ   r   )r    r*   ra   r}   r   r   r    ÚgetattrrH   r~   r   rI   r™   rš   r›   ré   Z_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   rg   rh   r>   r"  r>   r>   r?   rÒ   Ô  ræ   zUHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinal.<locals>.<lambda>r_   rž   rî   rï   ©r    r*   ra   r}   r   r   rH   rÃ   rÅ   rš   r2   ré   rØ   ©rO   r)  Zfollowing200Responser�   r>   r>   r?   Ú-test_1XXFollowedByFinalResponseOnlyEmitsFinalÅ  s   ÿÿzCHTTPClientParserTests.test_1XXFollowedByFinalResponseOnlyEmitsFinalc                 C   s’   d}d}t tddtdƒdd„ ƒ}| tƒ ¡ | || | | ¡ |  |jjd¡ |  |jj	t
i ƒ¡ |  |jt
d	d
giƒ¡ |  |jjd¡ dS )zp
        It is acceptable for multiple 1XX responses to come through, all of
        which get ignored.
        r'  r+  r¿   rÀ   Nc                 S   rg   rh   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   rg   rh   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*   ra   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  r!  r$  r&  r*  r.  r/  r2  r>   r>   r>   r?   r¯   j  sH    $

%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 rh   )r   rÙ   ©rO   rb   r>   r>   r?   ÚwriteTo%  s   zSlowRequest.writeToc                 C   ó
   d| _ d S rÏ   ©Ústoppedri   r>   r>   r?   ÚstopWriting)  ó   
zSlowRequest.stopWritingN)	r:   r;   r<   r=   Úmethodr8  Ú
persistentr5  r9  r>   r>   r>   r?   r3    s    r3  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   r4  r>   r>   r?   r5  7  s   
zSimpleRequest.writeToN)r:   r;   r<   r=   r<  r5  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   rb   r(   r�   r}   ri   r>   r>   r?   ÚsetUpB  s   zHTTP11ClientProtocolTests.setUpc                 C   s$   | j  tƒ ¡ |  | j ¡ d¡ dS )z†
        L{HTTP11ClientProtocol.request} accepts a L{Request} and calls its
        C{writeTo} method with its own transport.
        r>  N)r�   rÈ   r=  rH   rb   r�   ri   r>   r>   r?   Útest_requestJ  s   z&HTTP11ClientProtocolTests.test_requestc                    s>   ˆ j  tƒ ¡ ‡ fdd„}ˆ  ˆ j  tƒ ¡t¡}| |¡ |S )zÇ
        The second time L{HTTP11ClientProtocol.request} is called, it returns a
        L{Deferred} which immediately fires with a L{Failure} wrapping a
        L{RequestNotSent} exception.
        c                    ó   ˆ   ˆ j ¡ d¡ d S ©NrŽ   ©rH   rb   r�   ©Zignoredri   r>   r?   Ú	cbNotSentZ  ó   z?HTTP11ClientProtocolTests.test_secondRequest.<locals>.cbNotSentN)r�   rÈ   r3  rQ   r=  r$   rR   ©rO   rG  rT   r>   ri   r?   Útest_secondRequestR  s
   
z,HTTP11ClientProtocolTests.test_secondRequestc                    sD   ˆ j  ttdƒƒ¡ ‡ fdd„}ˆ  ˆ j  tƒ ¡t¡}| |¡ |S )zÒ
        L{HTTP11ClientProtocol.request} returns a L{Deferred} which immediately
        fires with a L{Failure} wrapping a L{RequestNotSent} if called after
        the protocol has been disconnected.
        zsad transportc                    rC  rD  rE  rF  ri   r>   r?   rG  i  rH  zLHTTP11ClientProtocolTests.test_requestAfterConnectionLost.<locals>.cbNotSentN)	r�   r  r   r	   rQ   rÈ   r=  r$   rR   rI  r>   ri   r?   Útest_requestAfterConnectionLosta  s
   
z9HTTP11ClientProtocolTests.test_requestAfterConnectionLostc                    sD   G dd„ dƒ}ˆ j  |ƒ ¡}‡ fdd„}tˆ |tgƒ}| |¡ |S )a%  
        If the L{Deferred} returned by L{Request.writeTo} fires with a
        L{Failure}, L{HTTP11ClientProtocol.request} disconnects its transport
        and returns a L{Deferred} which fires with a L{Failure} of
        L{RequestGenerationFailed} wrapping the underlying failure.
        c                   @   rd   )zCHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequestFc                 S   s
   t tƒ ƒS rh   )r   r8   r4  r>   r>   r?   r5  {  r:  zKHTTP11ClientProtocolTests.test_failedWriteTo.<locals>.BrokenRequest.writeToN©r:   r;   r<   r<  r5  r>   r>   r>   r?   ÚBrokenRequestx  ó    rM  c                    s&   ˆ   ˆ jj¡ ˆ j ttdƒƒ¡ d S )Nzyou asked for it)r    rb   Údisconnectingr�   r  r   r	   rF  ri   r>   r?   rP   €  s   z>HTTP11ClientProtocolTests.test_failedWriteTo.<locals>.cbFailedN)r�   rÈ   rZ   r8   rR   )rO   rM  rT   rP   r>   ri   r?   Útest_failedWriteTop  s   
z,HTTP11ClientProtocolTests.test_failedWriteToc                 C   s*   G dd„ dƒ}| j  |ƒ ¡}t| |tgƒS )z×
        If L{Request.writeTo} raises an exception,
        L{HTTP11ClientProtocol.request} returns a L{Deferred} which fires with
        a L{Failure} of L{RequestGenerationFailed} wrapping that exception.
        c                   @   rd   )zMHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequestFc                 S   r  rh   r  r4  r>   r>   r?   r5  ”  r  zUHTTP11ClientProtocolTests.test_synchronousWriteToError.<locals>.BrokenRequest.writeToNrL  r>   r>   r>   r?   rM  ‘  rN  rM  N)r�   rÈ   rZ   r8   )rO   rM  rT   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^   )r3  r�   rÈ   r\   r8   r¡   r8  r  r   r    rÙ   rR  rS  rA   r  rH   rI   )rO   ÚmoderÈ   rT   Úerrorsr>   r>   r?   Ú*test_connectionLostDuringRequestGenerationš  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.
        rR  N©rV  ri   r>   r>   r?   Ú+test_connectionLostBeforeGenerationFinishedº  ó   
zEHTTP11ClientProtocolTests.test_connectionLostBeforeGenerationFinishedc                 C   rW  )zÃ
        If the request passed to L{HTTP11ClientProtocol} finished generation
        with an error after the L{HTTP11ClientProtocol}'s connection has been
        lost, nothing happens.
        rS  NrX  ri   r>   r>   r?   Ú)test_connectionLostBeforeGenerationFailedÂ  rZ  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  r0  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>.checkrS  N)r   r  r   rV  rR   )rO   r`  r>   r_  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    rb   rO  r�   r~   ©rÃ   ri   r>   r?   Ú	cbRequestê  s   zIHTTP11ClientProtocolTests.test_receiveSimplestResponse.<locals>.cbRequests9   HTTP/1.1 200 OK
Content-Length: 0
Connection: close

©r�   rÈ   r*   ra   rR   r   ©rO   rT   re  r>   ri   r?   Útest_receiveSimplestResponseâ  s   
ÿz6HTTP11ClientProtocolTests.test_receiveSimplestResponsec                    rb  )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œ   ri   r>   r?   re    rÌ   zHHTTP11ClientProtocolTests.test_receiveResponseHeaders.<locals>.cbRequests+   HTTP/1.1 200 OK
X-Foo: bar
X-Foo: baz

rf  rg  r>   ri   r?   Útest_receiveResponseHeadersù  s   
ÿz5HTTP11ClientProtocolTests.test_receiveResponseHeadersc                    sp   g ‰t ƒ ‰tˆjƒ‰ ˆ  ˆ¡ tƒ ‰ˆ  ˆ¡}ˆ  d¡ ‡ ‡‡‡fdd„}| |¡ ‡‡fdd„}| |¡ |S )a[  
        If response bytes are delivered to L{HTTP11ClientProtocol} before the
        L{Deferred} returned by L{Request.writeTo} fires, those response bytes
        are parsed as part of the response.

        The connection is also closed, because we're in a confusing state, and
        therefore the C{quiescentCallback} isn't called.
        s8   HTTP/1.1 200 OK
X-Foo: bar
Content-Length: 6

foobarc                    sV   t ƒ ‰ tƒ  }ˆ _ˆ ˆ ¡ ˆ ˆjd¡ ˆ ˆj¡ ˆ ˆg ¡ | ‡ ‡fdd„¡S )NZ%TRANSMITTING_AFTER_RECEIVING_RESPONSEc                    ó
   ˆˆ j fS rh   ©r´   r"  ©ÚprÃ   r>   r?   rÒ   *  rÓ   zoHTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)	r   r   ÚclosedDeferredÚdeliverBodyrH   r~   r    rO  rR   ©rÃ   ÚwhenFinished)r�   ÚquiescentResultrO   rb   rl  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_   r`   rç   r‰   ó   foobar)rH   rÄ   rÅ   rÆ   rš   r2   rÙ   rR  )ÚresultrÃ   rÚ   ©rÈ   rO   r>   r?   ÚcbAllResponse.  s   z`HTTP11ClientProtocolTests.test_receiveResponseBeforeRequestGenerationDone.<locals>.cbAllResponseN)r   r(   r{   r}   r3  rÈ   r   rR   ©rO   rT   rs  rw  r>   )r�   rr  rÈ   rO   rb   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   Z
MAX_LENGTHrÈ   r*   ra   r   rX   r	   )rO   rb   r�   ZlongLinerT   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                    rj  rh   rk  r"  rl  r>   r?   rÒ   j  rÓ   z„HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponse.<locals>.<lambda>)r   r   rn  ro  rR   rp  r>   rl  r?   rs  f  s   
zrHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbResponsec                    s2   ˆ j  d ¡ ˆj ttƒ ƒ¡ ˆ ˆjjd¡ d S )Nr\  )rÙ   rR  r�   r  r   r8   rH   rq   )r^  rv  r>   r?   rw  n  s   zuHTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone.<locals>.cbAllResponseN)r3  r�   rÈ   r   rR   rx  r>   rv  r?   ÚDtest_connectionLostAfterReceivingResponseBeforeRequestGenerationDoneW  s   ÿ

z^HTTP11ClientProtocolTests.test_connectionLostAfterReceivingResponseBeforeRequestGenerationDonec                    s¢   t ƒ ‰ tƒ  }ˆ _ˆj tddtdƒ¡}ˆj d¡ g }| |j	¡ ˆ 
|g ¡ ˆj d¡ |d }| ˆ ¡ ˆj d¡ ˆj d¡ ‡ ‡fd	d
„}| |¡ |S )zÌ
        The C{deliverBody} method of the response object with which the
        L{Deferred} returned by L{HTTP11ClientProtocol.request} fires can be
        used to get the body of the response.
        r¿   rÀ   Ns%   HTTP/1.1 200 OK
Content-Length: 6
r®   r   r¼   r‰   c                    s   ˆ  ˆ jd¡ ˆ j t¡ d S )Nrt  )rH   r´   ÚclosedReasonrG   r.   rF  rË   r>   r?   rw  ™  s   zIHTTP11ClientProtocolTests.test_receiveResponseBody.<locals>.cbAllResponse)r   r   rn  r�   rÈ   r*   ra   r   rR   r{   rH   ro  )rO   rq  ÚrequestDeferredru  rÃ   rw  r>   rË   r?   Útest_receiveResponseBodyw  s&   ÿÿ

z2HTTP11ClientProtocolTests.test_receiveResponseBodyc                 C   s”   | j  tddtdƒ¡}| j  d¡ g }| |j¡ |d }tƒ }| |¡ | j  d¡ | j  d¡ |  	|j
d¡ | j  ttd	ƒƒ¡ |j t¡ dS )
zù
        If the length of the response body is unknown, the protocol passed to
        the response's C{deliverBody} method has its C{connectionLost}
        method called with a L{Failure} wrapping a L{PotentialDataLoss}
        exception.
        r¿   rÀ   Ns   HTTP/1.1 200 OK

r   r¼   r‰   rt  z low-level transport disconnected)r�   rÈ   r*   ra   r   rR   r{   r   ro  rH   r´   r  r   r	   r|  rG   r)   ©rO   r}  ru  rÃ   r�   r>   r>   r?   ÚEtest_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknown   s    ÿ

ÿz_HTTP11ClientProtocolTests.test_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknownc                 C   s˜   | j  tddtdƒ¡}| j  d¡ g }| |j¡ |d }tƒ }| |¡ | j  d¡ | j  d¡ |  	|j
d¡ | j  ttƒ ƒ¡ t| t|jƒttgƒS )	a  
        If the final chunk has not been received when the connection is lost
        (for any reason), the protocol passed to C{deliverBody} has its
        C{connectionLost} method called with a L{Failure} wrapping the
        exception for that reason.
        r¿   rÀ   Ns/   HTTP/1.1 200 OK
Transfer-Encoding: chunked

r   ó   3
foo
s   3
bar
rt  )r�   rÈ   r*   ra   r   rR   r{   r   ro  rH   r´   r  r   r8   rX   r   r|  r1   r  r>   r>   r?   Ú4test_chunkedResponseBodyUnfinishedWhenConnectionLost½  s$   ÿÿ
ÿzNHTTP11ClientProtocolTests.test_chunkedResponseBodyUnfinishedWhenConnectionLostc                    sJ   ˆ j  tddtdƒ¡}ˆ j  d¡ tˆ |tgƒ}‡ fdd„}| |¡ |S )a!  
        If the parser L{HTTP11ClientProtocol} delivers bytes to in
        C{dataReceived} raises an exception, the exception is wrapped in a
        L{Failure} and passed to the parser's C{connectionLost} and then the
        L{HTTP11ClientProtocol}'s transport is disconnected.
        r¿   rÀ   Ns   unparseable garbage goes here
c                    s<   ˆ   ˆ jj¡ ˆ  | jd jjd¡ ˆ j t	t
dƒƒ¡ d S )Nr   s   unparseable garbage goes herez
it is done)r    rb   rO  rH   rF   r�   r´   r�   r  r   r	   ©r·   ri   r>   r?   rP   ê  s
   ÿzLHTTP11ClientProtocolTests.test_parserDataReceivedException.<locals>.cbFailed)r�   rÈ   r*   ra   r   rX   r#   rR   )rO   r}  rT   rP   r>   ri   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*   ra   Ú_parserrb   rê   Ú	_producerZ_disconnectParserr   r	   rX   )rO   r}  rb   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    rO  r  r   r	   )rO   rb   r�   Zr1Zr2r>   r>   r?   Útest_abortClosesConnection  s   
z4HTTP11ClientProtocolTests.test_abortClosesConnectionc                 C   sZ   t ƒ }tƒ }| |¡ | ttƒ ƒ¡ g }| ¡  |j¡ |  	|dg¡ |  	|j
d¡ dS )z‰
        L{HTTP11ClientProtocol.abort} called after the connection is lost
        returns a C{Deferred} that fires immediately.
        Nr\  )r   r(   r}   r  r   r	   rˆ  rR   r{   rH   rq   ©rO   rb   r�   ru  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*   ra   rˆ  r    rO  r  r   r	   rX   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ˆ  ri   )r�   r>   r?   ÚconnectionMadeV  s   z`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionMadec                    s   ˆ   |¡ dS )z�
                Make the reason for the losing of the connection available to
                the unit test via C{testResult}.
                N)rS  ©rO   rK   )Ú
testResultr>   r?   r  \  rY   z`HTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.BodyDestination.connectionLostN)r:   r;   r<   r=   rŽ  r  r>   )r�   r�  r>   r?   ÚBodyDestinationP  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)ro  r    rO  r  r   r	   rd  )r‘  r�   rO   rb   r>   r?   ro  c  s   zMHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.deliverBodyc                    s   ˆ   | jt¡ d S rh   )r  rÃ   r-   )Úerrorri   r>   r?   Ú
checkErrorm  ó   zLHTTP11ClientProtocolTests.test_abortAfterResponseHeaders.<locals>.checkError)r   r(   r}   rÈ   r*   ra   r   r   r   rR   rX   r   r1   )rO   ru  ro  r“  rS   r>   )r‘  r�   rO   r�  rb   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 ©Nrc  ©rH   r~   r{   ©rm  ©r�   rr  rO   r>   r?   rR  „  ó   zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.callbackr¿   rÀ   NT©r<  s&   HTTP/1.1 200 OK
Content-length: 3

r   c                    rÎ   )Núresponse donerÐ   r"  )rr  r>   r?   rÒ   ž  rÓ   zHHTTP11ClientProtocolTests.test_quiescentCallbackCalled.<locals>.<lambda>r  r�  )r   r(   r}   rÈ   r*   ra   r   rH   rR   r{   r   r   rn  ro  r|  rG   r.   r…  Z_finishedRequestZ_currentRequestZ_transportProxyr  )rO   rR  rb   r}  ru  rÃ   ÚbodyProtocolr>   rš  r?   Útest_quiescentCallbackCalledv  s6   
ÿ

ÿ

z6HTTP11ClientProtocolTests.test_quiescentCallbackCalledc              	      sˆ   g ‰‡ ‡‡fdd„}t ƒ }t|ƒ‰ ˆ  |¡ ˆ  tddtddd�¡}ˆ  d¡ ˆ |¡}ˆ |j	d	¡ ˆ t
ˆƒd
¡ ˆ |jd¡ dS )aÜ  
        The C{quiescentCallback} passed to L{HTTP11ClientProtocol} will only be
        invoked once that protocol is in a state similar to its initial state.
        One of the aspects of this initial state is the producer-state of its
        transport; an L{HTTP11ClientProtocol} begins with a transport that is
        producing, i.e. not C{pauseProducing}'d.

        Therefore, when C{quiescentCallback} is invoked the protocol will still
        be producing.
        c                    r–  r—  r˜  r™  rš  r>   r?   rR  ¼  r›  z]HTTP11ClientProtocolTests.test_transportProducingWhenQuiescentAfterFullBody.<locals>.callbackr¿   rÀ   NTrœ  s)   HTTP/1.1 200 OK
Content-length: 3

BBBZDEFERRED_CLOSEr^   rÔ   )r   r(   r}   rÈ   r*   ra   r   ZsuccessResultOfrH   rq   rI   r×   )rO   rR  rb   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?   rR  ä  r›  zUHTTP11ClientProtocolTests.test_quiescentCallbackCalledEmptyResponse.<locals>.callbackr¿   rÀ   NTrœ  ó&   HTTP/1.1 200 OK
Content-length: 0

r±   r   r^   )r   r(   r}   rÈ   r*   ra   rR   r{   r   rH   rI   rê   r  r-   )rO   rR  rb   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*   ra   r   rR   r   ro  r|  rG   r.   rH   r    rO  ©rO   rr  rb   r�   r}  ru  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  rh   )ÚZeroDivisionErrorr™  r>   r>   r?   rR  5  r  zHHTTP11ClientProtocolTests.test_quiescentCallbackThrows.<locals>.callbackr¿   rÀ   NTrœ  r¡  r   r^   r  )r   r  r   r   r(   r}   rÈ   r*   ra   r   rR   r{   r   ro  r|  rG   r.   r  rI   r  r�   r¨  r  r    rO  )rO   rR  r  rb   r�   r}  ru  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*   ra   Úcancelr    ÚdisconnectedrU   r0   r   rŠ  r>   r>   r?   Útest_cancelBeforeResponseR  s   

ÿz3HTTP11ClientProtocolTests.test_cancelBeforeResponsec                 C   sV   t ƒ }tƒ }| |¡ | tddtdƒ¡}| d¡ | ¡  |  |j	¡ t
| |tgƒS )a  
        The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
        with a L{ResponseFailed} failure containing a L{CancelledError}
        exception if the request was cancelled before all response headers were
        received.
        r¿   rÀ   NrÁ   )r   r(   r}   rÈ   r*   ra   r   rª  r    r«  rX   r   rŠ  r>   r>   r?   Útest_cancelDuringResponsec  s   

z3HTTP11ClientProtocolTests.test_cancelDuringResponsec                    s–   t ƒ }tƒ }| |¡ t|ƒ‰ddi‰‡fdd„‰ ‡ ‡fdd„}|ˆ_| tddtˆƒ¡}ˆj 	d	¡ | 
¡  |  |j¡ |  ˆd ¡ t| |tgƒS )a  
        The L{Deferred} returned by L{HTTP11ClientProtocol.request} will fire
        with a L{RequestGenerationFailed} failure containing a
        L{CancelledError} exception if the request was cancelled before a
        C{bodyProducer} has finished producing.
        Ú	cancelledFc                    s   dˆ d< d S )NTr®  r>   r"  )ÚnonLocalr>   r?   rª  �  ó   zJHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.cancelc                    s   | ˆ_ tˆ ƒˆ_ˆjS rh   ©Úconsumerr   rÙ   )r²  )rª  Úproducerr>   r?   ÚstartProducing„  s   
zRHTTP11ClientProtocolTests.assertCancelDuringBodyProduction.<locals>.startProducingó   POSTó   /bars   xxxxxN)r   r(   r}   ÚStringProducerr´  rÈ   r*   ra   r²  r?  rª  r    r«  rZ   r   )rO   ZproducerLengthrb   r�   r´  ru  r>   )rª  r¯  r³  r?   Ú assertCancelDuringBodyProductions  s   
z:HTTP11ClientProtocolTests.assertCancelDuringBodyProductionc                 C   rW  )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.
        é
   N)r¸  ri   r>   r>   r?   Útest_cancelDuringBodyProduction’  ó   
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.
        N)r¸  r   ri   r>   r>   r?   Ú&test_cancelDuringChunkedBodyProduction›  r»  z@HTTP11ClientProtocolTests.test_cancelDuringChunkedBodyProductionrh   )'r:   r;   r<   r=   rA  rB  rJ  rK  rP  rQ  rV  rY  r[  ra  rh  ri  ry  rz  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@  <  sJ    
 2 ) :9.#	r@  c                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )r·  a»  
    L{StringProducer} is a dummy body producer.

    @ivar stopped: A flag which indicates whether or not C{stopProducing} has
        been called.
    @ivar consumer: After C{startProducing} is called, the value of the
        C{consumer} argument to that method.
    @ivar finished: After C{startProducing} is called, a L{Deferred} which was
        returned by that method.  L{StringProducer} will never fire this
        L{Deferred}.
    Fc                 C   s
   || _ d S rh   )rØ   )rO   rØ   r>   r>   r?   Ú__init__µ  r:  zStringProducer.__init__c                 C   s   || _ tƒ | _| jS rh   r±  )rO   r²  r>   r>   r?   r´  ¸  s   zStringProducer.startProducingc                 C   r6  rÏ   r7  ri   r>   r>   r?   ÚstopProducing½  r:  zStringProducer.stopProducingc                 C   rg   rh   r>   ri   r>   r>   r?   ÚpauseProducingÀ  ó   zStringProducer.pauseProducingc                 C   rg   rh   r>   ri   r>   r>   r?   ÚresumeProducingÄ  rÀ  zStringProducer.resumeProducingN)
r:   r;   r<   r=   r8  r½  r´  r¾  r¿  rÁ  r>   r>   r>   r?   r·  ¥  s    r·  c                   @   sÚ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd5d)d*„Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd(S )6ÚRequestTestsz
    Tests for L{Request}.
    c                 C   s   t ƒ | _d S rh   )r   rb   ri   r>   r>   r?   rA  Î  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*   ra   r5  rb   rH   r�   ri   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*   r5  rb   r�   ÚsplitrH   Úsort)rO   rš   Úlinesr>   r>   r?   Útest_sendRequestHeadersê  s   þz$RequestTests.test_sendRequestHeadersc                 C   s¦   t D ]N}t|gdgdœƒ}tƒ }tdd|dƒ |¡ | ¡  d¡}|  |d d¡ |  |d	d… d
d
g¡ |d= |d	d…= | d¡ | d¡ |  |dt	 g¡ qdS )ze
        Linear whitespace in request header values is replaced with a
        single space.
        s   example.invalidrÇ  r¿   rÈ  Nrª   r   rÉ  rÊ  rŽ   rË  s   Host: example.invalids   X-Foo: )
r5   r2   r   r*   r5  r�   rÌ  rH   Úremover6   )rO   Ú	componentrš   rb   rÎ  r>   r>   r?   Ú-test_sanitizeLinearWhitespaceInRequestHeadersû  s   

öz:RequestTests.test_sanitizeLinearWhitespaceInRequestHeadersc                 C   s´   t tƒ}tddt|ƒ}| | j¡ |  |jd¡ |  | jj	|¡ |  
| jj¡ |  | j ¡ d¡ | j ¡  |j d¡ |j d¡ |j d¡ |  | jj	d¡ |  | j ¡ d¡ dS )zË
        L{Request.writeTo} uses chunked encoding to write data from the request
        body producer to the given transport.  It registers the request body
        producer with the transport.
        rµ  r¶  NsX   POST /bar HTTP/1.1
Connection: close
Transfer-Encoding: chunked
Host: example.com

s   xxxs   yyyyyyyyyyyyyyys!   3
xxx
f
yyyyyyyyyyyyyyy
0

)r·  r   r*   ra   r5  rb   ÚassertNotIdenticalr²  rê   r³  r    Ú	streamingrH   r�   Úclearr?  rÙ   rR  ©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 rD  )rH   rb   r�   rê   r³  rF  ri   r>   r?   rP   <  s   zCRequestTests.test_sendChunkedRequestBodyWithError.<locals>.cbFailedN)r·  r   r*   ra   r5  rb   rÕ  rÙ   rS  r8   rQ   rR   )rO   r³  rÈ   ÚwriteDeferredrP   rT   r>   ri   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*   ra   r5  rb   rÓ  r²  rê   r³  r    rÔ  rH   r�   rÕ  r?  rÙ   rR  rÖ  r>   r>   r?   Útest_sendRequestBodyWithLengthD  s   þ
z+RequestTests.test_sendRequestBodyWithLengthc                 C   s4   t |dtdƒ}| | j¡ |  | j ¡ |d ¡ dS )a"  
        Verify that the message generated by a L{Request} initialized with
        the given method and C{None} as the C{bodyProducer} includes
        I{Content-Length: 0} in the header.

        @param method: The HTTP method issue in the request.
        @type method: L{bytes}
        rÈ  NsK    /foo HTTP/1.1
Connection: close
Content-Length: 0
Host: example.com

rÃ  )rO   r;  rÈ   r>   r>   r?   Ú_sendRequestEmptyBodyWithLengtha  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Ü  ri   r>   r>   r?   Útest_sendPUTRequestEmptyBodyv  ó   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Þ  ri   r>   r>   r?   Útest_sendPOSTRequestEmptyBody~  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*   ra   r5  rb   r²  r?  rÙ   rR  rê   r³  rQ   r&   ©rO   r³  rÈ   rØ  r>   r>   r?   Ú#test_sendRequestBodyWithTooFewBytes†  s   z0RequestTests.test_sendRequestBodyWithTooFewBytesc                    sŒ   t dƒ‰tddtˆƒ}| ˆj¡}ˆj d¡ ˆ ˆj¡ ˆj d¡ ˆ 	ˆj¡ ˆ 
ˆjjd¡ ‡ ‡‡fdd„}ˆ |t¡}| |¡ |S )	a7  
        Verify that when too many bytes have been written by a body producer
        and then the body producer's C{startProducing} L{Deferred} fires that
        the producer is unregistered from the transport and that the
        L{Deferred} returned from L{Request.writeTo} is fired with a L{Failure}
        wrapping a L{WrongBodyLength}.

        @param finisher: A callable which will be invoked with the body
            producer after too many bytes have been written to the transport.
            It should fire the startProducing Deferred somehow.
        rÚ  rµ  r¶  râ  ó   cdNc                    sL   ˆ  ˆj ¡ d¡ ˆj ¡  ˆ tˆjjd¡ ˆ ˆƒ ˆ  ˆj ¡ d¡ d S )NsQ   POST /bar HTTP/1.1
Connection: close
Content-Length: 3
Host: example.com

abs   efrŽ   )rH   rb   r�   rÕ  ru   r   r²  r?  rƒ  ©Úfinisherr³  rO   r>   r?   rP   µ  s   þ
	zCRequestTests._sendRequestBodyWithTooManyBytesTest.<locals>.cbFailed)r·  r*   ra   r5  rb   r²  r?  r¡   r8  r    rê   r³  rQ   r&   rR   )rO   rç  rÈ   rØ  rP   rT   r>   ræ  r?   Ú$_sendRequestBodyWithTooManyBytesTest•  s   
z1RequestTests._sendRequestBodyWithTooManyBytesTestc                 C   s   dd„ }|   |¡S )á  
        If L{Request} is created with a C{bodyProducer} with a known length and
        the producer tries to produce more than than many bytes, the
        L{Deferred} returned by L{Request.writeTo} fires with a L{Failure}
        wrapping a L{WrongBodyLength} exception.
        c                 S   ó   | j  d ¡ d S rh   ©rÙ   rR  ©r³  r>   r>   r?   rç  Þ  ó   zCRequestTests.test_sendRequestBodyWithTooManyBytes.<locals>.finisherN)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Ù   rS  r8   r]  r  r�   r  rH   rI   )r³  r   rº   rU  r_  r>   r?   rç  ì  s   
zHRequestTests.test_sendRequestBodyErrorWithTooManyBytes.<locals>.finisherN)r   r  r   rè  rî  r>   r_  r?   Ú)test_sendRequestBodyErrorWithTooManyBytesã  s   
	z6RequestTests.test_sendRequestBodyErrorWithTooManyBytesc                 C   sž   t  | t¡}tdƒ}tddt|ƒ}| | j¡ |jj	}|j 
d¡ |j d¡ | tƒ ¡ |d }|  d|¡ |d }|  |jt¡ |  t|  t¡ƒd¡ dS )	a’  
        Though there should be no way for the internal C{finishedConsuming}
        L{Deferred} in L{Request._writeToBodyProducerContentLength} to fire a
        L{Failure} after the C{finishedProducing} L{Deferred} has fired, in
        case this does happen, the error should be logged with a message about
        how there's probably a bug in L{Request}.

        This is a whitebox test.
        rÚ  rµ  r¶  r  Nr   r  r^   )r   r  r   r·  r*   ra   r5  rb   r²  Ú	_finishedr?  rÙ   rR  rS  r8   r]  r  r�   rH   rI   r  )rO   r  r³  rÈ   ZfinishedConsumingr   rº   r>   r>   r?   Ú*test_sendRequestBodyErrorWithConsumerError÷  s   
z7RequestTests.test_sendRequestBodyErrorWithConsumerErrorc                 C   sx   t dƒ}tddt|ƒ}| | j¡}|j d¡ ||ƒ |  | jjd¡ | j 	¡  |  
t|jjd¡ |  | j ¡ d¡ |S )a  
        Verify that if the body producer fires its Deferred and then keeps
        writing to the consumer that the extra writes are ignored and the
        L{Deferred} returned by L{Request.writeTo} fires with a L{Failure}
        wrapping the most appropriate exception type.
        rÚ  rµ  r¶  râ  Nrå  rŽ   )r·  r*   ra   r5  rb   r²  r?  rê   r³  rÕ  ru   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ê  rh   rë  rì  r>   r>   r?   rç  -	  rí  zPRequestTests.test_sendRequestBodyFinishedEarlyThenTooManyBytes.<locals>.finisherN)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 rh   )rÙ   rS  r8   rì  r>   r>   r?   rç  =	  r”  zORequestTests.test_sendRequestBodyErroredEarlyThenTooManyBytes.<locals>.finisherN)rQ   ró  r8   rî  r>   r>   r?   Ú0test_sendRequestBodyErroredEarlyThenTooManyBytes5	  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Ž   N)r·  r   r*   ra   r5  rb   rÙ   rR  rÕ  ru   r   r²  r?  rH   r�   )rO   Z_withr³  rÈ   rØ  r>   r>   r?   Ú0test_sendChunkedRequestBodyFinishedThenWriteMoreE	  s   
z=RequestTests.test_sendChunkedRequestBodyFinishedThenWriteMorec                 C   s   |   ttƒ ƒ¡}|  |t¡S )a$  
        If the request body producer with an unknown length tries to write
        after firing the L{Deferred} returned by its C{startProducing} method
        with a L{Failure}, the C{write} call raises an exception and does not
        write anything to the underlying transport.
        N)rø  r   r8   rQ   )rO   rT   r>   r>   r?   Ú9test_sendChunkedRequestBodyFinishedWithErrorThenWriteMoreV	  s   ÿzFRequestTests.test_sendChunkedRequestBodyFinishedWithErrorThenWriteMorec                 C   s¨   t dƒ}tddt|ƒ}| | j¡}|  | jj|¡ |  | jj¡ |j	 
d¡ |  | j ¡ d¡ |  | jj¡ |j ttƒ ƒ¡ |  | jj¡ |  | jjd¡ |  |t¡S )zú
        If the L{Deferred} returned from the C{startProducing} method of the
        L{IBodyProducer} passed to L{Request} fires with a L{Failure}, the
        L{Deferred} returned from L{Request.writeTo} fails with that
        L{Failure}.
        é   rµ  r¶  râ  sQ   POST /bar HTTP/1.1
Connection: close
Content-Length: 5
Host: example.com

abN)r·  r*   ra   r5  rb   rê   r³  r    rÔ  r²  r?  rH   r�   r¡   rO  rÙ   rS  r   r8   rQ   rã  r>   r>   r?   Útest_sendRequestBodyWithErrorb	  s   þ
z*RequestTests.test_sendRequestBodyWithErrorc                 C   sx   t ddti ƒdƒ}|  t|j| j¡ |  | j ¡ d¡ t ddtdddgiƒdƒ}|  t|j| j¡ |  | j ¡ d¡ dS )z—
        L{Request.writeTo} raises L{BadHeaders} if there is not exactly one
        I{Host} header and writes nothing to the given transport.
        r¿   rÀ   NrŽ   s   HostrC   s   example.org)r*   r2   ru   r   r5  rb   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*   ra   r5  rb   r¡   r8  r9  r    rÖ  r>   r>   r?   Útest_stopWriting˜	  s   zRequestTests.test_stopWritingc                 C   s˜   t  | t¡}tdƒ}dd„ }||_tddt|ƒ}| | j¡ | 	¡  |  
t|  t¡ƒd¡ |  dt|ƒ¡ |d }|  d|¡ |d }|  |jt¡ d	S )
z–
        If the body producer's C{stopProducing} method raises an exception,
        L{Request.stopWriting} logs it and does not re-raise it.
        rÚ  c                   S   s   t dƒ‚©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*   ra   r5  rb   r9  rH   rI   r  r8   r  r]  r  r�   )rO   r  r³  rÿ  rÈ   r   rº   r>   r>   r?   Útest_brokenStopProducing¤	  s   z%RequestTests.test_brokenStopProducingrh   )r:   r;   r<   r=   rA  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Â  É  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   ru  r·  r³  r   rb   r"   Úenforcerri   r>   r>   r?   rA  Á	  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   rb   r�   rÕ  ri   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?  ru   r&   Ú_noMoreWritesExpectedri   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ó   xN)
r  r?  r¡   r³  r8  r    r  rQ   ru  r&   )rO   Z_unregisterAfterr>   r>   r?   Útest_writeTooManyÞ	  s   
z.LengthEnforcingConsumerTests.test_writeTooManyc                 C   sH   | j  d¡ | j  ¡  |  | jj¡ |  t| j jd¡ |  | jj¡ dS )zÒ
        If L{LengthEnforcingConsumer.write} is called after
        L{LengthEnforcingConsumer._noMoreWritesExpected}, it calls the
        producer's C{stopProducing} method and raises L{ExcessWrite}.
        r  r  N)	r  r?  r  r¡   r³  r8  ru   r   r    ri   r>   r>   r?   Útest_writeAfterNoMoreExpectedî	  s
   
z:LengthEnforcingConsumerTests.test_writeAfterNoMoreExpectedc                 C   rW  )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}.
        TN)r  ri   r>   r>   r?   Útest_finishedLateú	  rZ  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  ri   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>   ri   r>   r?   rÿ  
  s   zRLengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.brokenStopProducingc                    s   ˆ   tˆ  t¡ƒd¡ d S )Nr^   )rH   rI   r  r8   rF  ri   r>   r?   Ú
cbFinished
  s   zILengthEnforcingConsumerTests.test_stopProducingRaises.<locals>.cbFinishedN)r³  r¾  r  rR   )rO   rÿ  r  rT   r>   ri   r?   Útest_stopProducingRaises

  s   	
z5LengthEnforcingConsumerTests.test_stopProducingRaisesN)F)r:   r;   r<   r=   rA  r  r  r  r	  r
  r  r  r>   r>   r>   r?   r  ¼	  s    
	r  c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚRequestBodyConsumerTestsz¤
    Tests for L{ChunkedEncoder} which sits between an L{ITransport} and a
    request/response body producer and chunked encodes everything written to
    it.
    c                 C   s   |   ttttƒ ƒƒ¡ dS )zC
        L{ChunkedEncoder} instances provide L{IConsumer}.
        N)r    r   r   r   r   ri   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   rb   Úencoderr>   r>   r?   r  .
  s   

z#RequestBodyConsumerTests.test_writec                 C   sd   t ƒ }tƒ }t|ƒ}| |d¡ |  |j|¡ |  |j¡ | ¡  |  |jd¡ |  	| 
¡ d¡ dS )a  
        L{ChunkedEncoder.registerProducer} registers the given streaming
        producer with its transport and L{ChunkedEncoder.unregisterProducer}
        writes a zero-length chunk to its transport and unregisters the
        transport's producer.
        TNs   0

)r   r£   r   ZregisterProducerrê   r³  r    rÔ  ZunregisterProducerrH   r�   )rO   rb   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%   ri   r>   r>   r?   r  S
  s   z*TransportProxyProducerTests.test_interfacec                 C   s6   t ƒ }t|ƒ}|  |j|¡ | ¡  |  |jd¡ dS )z~
        L{TransportProxyProducer.stopProxying} drops the reference to the
        wrapped L{IPushProducer} provider.
        N)r   r%   rê   r†  ÚstopProxying©rO   rb   Úproxyr>   r>   r?   Ú%test_stopProxyingUnreferencesProducerY
  s
   zATransportProxyProducerTests.test_stopProxyingUnreferencesProducerc                 C   sd   t ƒ }| ¡  t|ƒ}|  |jd¡ | ¡  |  |jd¡ | ¡  | ¡  | ¡  |  |jd¡ dS )z™
        L{TransportProxyProducer.resumeProducing} calls the wrapped
        transport's C{resumeProducing} method unless told to stop proxying.
        ró   rÔ   N)r   r¿  r%   rH   r×   rÁ  r  r  r>   r>   r?   Útest_resumeProducingd
  s   z0TransportProxyProducerTests.test_resumeProducingc                 C   s\   t ƒ }t|ƒ}|  |jd¡ | ¡  |  |jd¡ | ¡  | ¡  | ¡  |  |jd¡ dS )z—
        L{TransportProxyProducer.pauseProducing} calls the wrapped transport's
        C{pauseProducing} method unless told to stop proxying.
        rÔ   ró   N)r   r%   rH   r×   r¿  rÁ  r  r  r>   r>   r?   Útest_pauseProducingz
  s   z/TransportProxyProducerTests.test_pauseProducingc                 C   sb   t ƒ }t|ƒ}|  |jd¡ | ¡  |  |jd¡ t ƒ }t|ƒ}| ¡  | ¡  |  |jd¡ dS )z•
        L{TransportProxyProducer.stopProducing} calls the wrapped transport's
        C{stopProducing} method unless told to stop proxying.
        rÔ   r8  N)r   r%   rH   r×   r¾  r  r  r>   r>   r?   Útest_stopProducing�
  s   z.TransportProxyProducerTests.test_stopProducingc                 C   sd   t ƒ }tƒ }| |¡ ||_t|ƒ}|  |j¡ |  |jd¡ | 	¡  |  |jd¡ |  
|j¡ dS )zs
        L{TransportProxyProducer.loseConnection} calls the wrapped transport's
        C{loseConnection}.
        rÔ   N)r   r   r}   r�   r%   r    Ú	connectedrH   r×   ÚloseConnectionr¡   ©rO   rb   r�   r  r>   r>   r?   Ú test_loseConnectionWhileProxying£
  s   
z<TransportProxyProducerTests.test_loseConnectionWhileProxyingc                 C   sP   t ƒ }tƒ }| |¡ ||_t|ƒ}| ¡  |  |j¡ | ¡  |  |j¡ dS )zm
        L{TransportProxyProducer.loseConnection} does nothing when the
        proxy is not active.
        N)	r   r   r}   r�   r%   r  r    r  r  r  r>   r>   r?   Útest_loseConnectionNotProxying¸
  s   
z:TransportProxyProducerTests.test_loseConnectionNotProxyingN)r:   r;   r<   r=   r  r  r  r  r  r  r  r>   r>   r>   r?   r  M
  s    r  c                   @   sˆ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd S )!ÚResponseTestsz 
    Tests for L{Response}.
    c                 C   s   t tƒ ƒ}|  tt|ƒ¡ dS )z=
        L{Response} instances provide L{IResponse}.
        N)rc   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 rh   rÐ   )rO   r³  ©Z	producersr>   r?   r}   á
  ó   zFResponseTests.test_makeConnection.<locals>.SomeProtocol.makeConnectionN©r:   r;   r<   r}   r>   r$  r>   r?   ÚSomeProtocolà
  ó    r'  ró   rÔ   N)r   r   rc   ro  r¿  rH   r×   rÁ  )rO   rb   r'  r²  rÃ   ZtheProducerr>   r$  r?   Útest_makeConnection×
  s   
z!ResponseTests.test_makeConnectionc                    sN   g ‰ G ‡ fdd„dt ƒ}|ƒ }ttƒ ƒ}| |¡ | d¡ |  ˆ dg¡ dS )zµ
        The L{IProtocol} provider passed to L{Response.deliverBody} has its
        C{dataReceived} method called with bytes received as part of the
        response body.
        c                       r"  )z5ResponseTests.test_dataReceived.<locals>.ListConsumerc                    r#  rh   rÐ   ©rO   r´   ©r¦   r>   r?   r   ö
  r%  zBResponseTests.test_dataReceived.<locals>.ListConsumer.dataReceivedN©r:   r;   r<   r   r>   r+  r>   r?   ÚListConsumerõ
  r(  r-  r¼   N)r   rc   r   ro  rÕ   rH   ©rO   r-  r²  rÃ   r>   r+  r?   Útest_dataReceivedí
  s   


zResponseTests.test_dataReceivedc                    sj   g ‰ G ‡ fdd„dt ƒ}|ƒ }ttƒ ƒ}| |¡ | ¡  ˆ d  t¡ |  tˆ ƒd¡ |  	|j
d¡ dS )zó
        The L{IProtocol} provider passed to L{Response.deliverBody} has its
        C{connectionLost} method called with a L{Failure} wrapping
        L{ResponseDone} when the response's C{_bodyDataFinished} method is
        called.
        c                       r"  )z7ResponseTests.test_connectionLost.<locals>.ListConsumerc                    r#  rh   rÐ   r�  ©Zlostr>   r?   r  
  r%  zFResponseTests.test_connectionLost.<locals>.ListConsumer.connectionLostN)r:   r;   r<   r  r>   r0  r>   r?   r-  	  r(  r-  r   r^   N)r   rc   r   ro  rÖ   rG   r.   rH   rI   rê   Z_bodyProtocolr.  r>   r0  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                       r"  )z8ResponseTests.test_bufferEarlyData.<locals>.ListConsumerc                    r#  rh   rÐ   r*  r+  r>   r?   r   "  r%  zEResponseTests.test_bufferEarlyData.<locals>.ListConsumer.dataReceivedNr,  r>   r+  r>   r?   r-  !  r(  r-  r¼   r‰   r˜   )r¼   r‰   r˜   N)r   rc   r   rÕ   ro  rH   rê   Z_bodyBuffer)rO   r-  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)rc   r   ro  r   ru   rv   r   r>   r>   r?   Ú test_multipleStartProducingFails0  s   
z.ResponseTests.test_multipleStartProducingFailsc                 C   s4   t tƒ ƒ}| tƒ ¡ | ¡  |  t|jtƒ ¡ dS )zw
        L{Response.deliverBody} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished}.
        N)rc   r   ro  r   rÖ   ru   rv   r   r>   r>   r?   Ú%test_startProducingAfterFinishedFails9  s   
z3ResponseTests.test_startProducingAfterFinishedFailsc                 C   s&   t tƒ ƒ}| ¡  |  t|jd¡ dS )z 
        L{Response._bodyDataReceived} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished} but before L{Response.deliverBody}.
        r¼   N)rc   r   rÖ   ru   rv   rÕ   r   r>   r>   r?   Ú'test_bodyDataReceivedAfterFinishedFailsC  s   
z5ResponseTests.test_bodyDataReceivedAfterFinishedFailsc                 C   s2   t tƒ ƒ}| ¡  | tƒ ¡ |  t|jd¡ dS )zŸ
        L{Response._bodyDataReceived} raises L{RuntimeError} if called after
        L{Response._bodyDataFinished} and after L{Response.deliverBody}.
        r¼   N)rc   r   rÖ   ro  r   ru   rv   rÕ   r   r>   r>   r?   Ú'test_bodyDataReceivedAfterDeliveryFailsL  s   
z5ResponseTests.test_bodyDataReceivedAfterDeliveryFailsc                 C   s$   t tƒ ƒ}| ¡  |  t|j¡ dS )zh
        L{Response._bodyDataFinished} raises L{RuntimeError} if called more
        than once.
        N)rc   r   rÖ   ru   rv   r   r>   r>   r?   Ú'test_bodyDataFinishedAfterFinishedFailsV  s   
z5ResponseTests.test_bodyDataFinishedAfterFinishedFailsc                 C   s0   t tƒ ƒ}| ¡  | tƒ ¡ |  t|j¡ dS )z{
        L{Response._bodyDataFinished} raises L{RuntimeError} if called after
        the body has been delivered.
        N)rc   r   rÖ   ro  r   ru   rv   r   r>   r>   r?   Ú'test_bodyDataFinishedAfterDeliveryFails_  s   
z5ResponseTests.test_bodyDataFinishedAfterDeliveryFailsc                    sl   g ‰ G ‡ fdd„dt ƒ}tƒ }| ¡  |ƒ }t|ƒ}|  |jd¡ | |¡ |  ˆ dg¡ |  |jd¡ dS )z–
        L{Response.deliverBody} resumes the HTTP connection's transport
        after passing it to the consumer's C{makeConnection} method.
        c                       r"  )z9ResponseTests.test_transportResumed.<locals>.ListConsumerc                    s   ˆ   |j¡ d S rh   )r{   r×   r4  ©ZtransportStater>   r?   r}   q  rí  zHResponseTests.test_transportResumed.<locals>.ListConsumer.makeConnectionNr&  r>   r9  r>   r?   r-  p  r(  r-  ró   rÔ   N)r   r   r¿  rc   rH   r×   ro  )rO   r-  rb   r�   rÃ   r>   r9  r?   Útest_transportResumedi  s   
z#ResponseTests.test_transportResumedc                 C   sX   t ƒ }t|ƒ}| d¡ | d¡ | ¡  tƒ }| |¡ |  |jd¡ |j 	t
¡ dS )zó
        If the entire body is delivered to the L{Response} before the
        response's C{deliverBody} method is called, the protocol passed to
        C{deliverBody} is immediately given the body data and then
        disconnected.
        r¼   r‰   rt  N)r   rc   rÕ   rÖ   r   ro  rH   r´   r|  rG   r.   ©rO   rb   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.
        Z	CONNECTEDN)r   rc   r   ro  rH   rq   rÖ   r   r8   r|  rG   r;  r>   r>   r?   Ú#test_finishedWithErrorWhenConnected�  s   
z1ResponseTests.test_finishedWithErrorWhenConnectedc                 C   sL   t ƒ }t|ƒ}|  |jd¡ | ttƒ ƒ¡ tƒ }| |¡ |j	 
t¡ dS )a  
        The L{Failure} passed to L{Response._bodyDataFinished} when the response
        is in the I{initial} state is passed to the C{connectionLost} method of
        the L{IProtocol} provider passed to the L{Response}'s C{deliverBody}
        method.
        ZINITIALN)r   rc   rH   rq   rÖ   r   r8   r   ro  r|  rG   r;  r>   r>   r?   Ú!test_finishedWithErrorWhenInitial¢  s   
z/ResponseTests.test_finishedWithErrorWhenInitialN)r:   r;   r<   r=   r!  r)  r/  r1  r2  r3  r4  r5  r6  r7  r8  r:  r<  r=  r>  r>   r>   r>   r?   r  Ë
  s"    	
	
	
r  N)_r=   Útypingr   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted.internet.deferr   r   r   r   Ztwisted.internet.errorr	   r
   Ztwisted.internet.interfacesr   r   Ztwisted.internet.protocolr   Ztwisted.internet.testingr   r   r   r   Ztwisted.loggerr   Ztwisted.protocols.basicr   Ztwisted.python.failurer   Ztwisted.trial.unittestr   Ztwisted.web._newclientr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   Ztwisted.web.clientr(   r)   r*   r+   r,   r-   r.   r/   r0   Ztwisted.web.httpr1   Ztwisted.web.http_headersr2   Ztwisted.web.iwebr3   r4   Ztwisted.web.test.requesthelperr5   r6   Ú	Exceptionr8   rA   ra   rU   rX   rZ   r\   rc   re   rx   r©   r­   r¯   r3  r=  r@  r·  rÂ  r  r  r  r  r>   r>   r>   r?   Ú<module>   sr   P,(


!      0      o#   ve,~