U
    sÍ@gv ã                   @   s   d Z ddlmZmZ ddlZddlZddlZddlZzddlmZm	Z	m
Z
 W n( ek
rt   ddlmZm	Z	m
Z
 Y nX 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 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% ddl&m'Z'm(Z( ddl&m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z: ddlm;Z;m<Z< ddl=m>Z> G dd„ dee#j?ƒƒZ@dd„ ZAG dd„ de#jBƒZCeAeCƒZDG dd „ d ej!ƒZEG d!d"„ d"e#jBƒZFeAeFƒZGee%jHƒG d#d$„ d$eFƒƒZIeAeIƒZJG d%d&„ d&eFƒZKeAeKƒZLG d'd(„ d(e#jMƒZNd)d*„ ZOG d+d,„ d,ePƒZQG d-d.„ d.ej!eQƒZRG d/d0„ d0eRƒZSG d1d2„ d2eRƒZTG d3d4„ d4eRƒZUG d5d6„ d6ej!eQƒZVG d7d8„ d8ej!ƒZWG d9d:„ d:ej!ƒZXG d;d<„ d<ej!ƒZYG d=d>„ d>ej!ƒZZd?d@„ Z[G dAdB„ dBej!ƒZ\G dCdD„ dDe!ƒZ]G dEdF„ dFej!ƒZ^G dGdH„ dHej!eQƒZ_G dIdJ„ dJej!ƒZ`G dKdL„ dLej!ƒZaG dMdN„ dNe#jMƒZbG dOdP„ dPej!ƒZcG dQdR„ dRej!eQƒZdG dSdT„ dTej!ƒZeG dUdV„ dVej!eQƒZfdWdX„ ZgG dYdZ„ dZej!ƒZhG d[d\„ d\ej!ƒZidS )]z
Test HTTP support.
é    )Úabsolute_importÚdivisionN)ÚurlparseÚ
urlunsplitÚclear_cache)ÚBytesIO)Úprovider)ÚverifyObject)Ú_PY3Ú	iterbytesÚlongÚnetworkStringÚunicodeÚ
intToBytes)ÚproxyForInterface)ÚFailure)Úunittest)ÚTestCase)ÚhttpÚhttp_headersÚiweb)ÚPotentialDataLossÚ	_DataLoss)Ú_IdentityTransferDecoder)Úaddress)ÚClock)ÚConnectionLostÚConnectionDone)Úloopback)ÚStringTransportÚNonStreamingProducerÚEventLoggingObserver)ÚDummyProducer)ÚDummyChannel©ÚdirectlyProvidesÚ
providedBy)ÚglobalLogPublisherc                   @   s   e Zd ZdZdS )Ú._IDeprecatedHTTPChannelToRequestInterfaceProxyz‰
    Proxy L{_IDeprecatedHTTPChannelToRequestInterface}.  Used to
    assert that the interface matches what L{HTTPChannel} expects.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r-   r-   ú</usr/lib/python3/dist-packages/twisted/web/test/test_http.pyr(   ,   s   r(   c                    s   ‡ fdd„}t |tˆ ƒƒ |S )a¾  
    Return a callable that proxies instances of C{clsToWrap} via
        L{_IDeprecatedHTTPChannelToRequestInterface}.

    @param clsToWrap: The class whose instances will be proxied.
    @type cls: L{_IDeprecatedHTTPChannelToRequestInterface}
        implementer.

    @return: A factory that returns
        L{_IDeprecatedHTTPChannelToRequestInterface} proxies.
    @rtype: L{callable} whose interface matches C{clsToWrap}'s constructor.
    c                     s   ˆ | |Ž}t |ƒS ©N)r(   )ÚargsÚkwargsÚinstance©Ú	clsToWrapr-   r.   Ú_makeRequestProxyC   s    
z3_makeRequestProxyFactory.<locals>._makeRequestProxyr$   )r4   r5   r-   r3   r.   Ú_makeRequestProxyFactory5   s    r6   c                   @   s   e Zd ZdZdd„ ZdS )ÚDummyPullProducerHandlerzš
    An HTTP request handler that registers a dummy pull producer to serve the
    body.

    The owner must call C{finish} to complete the response.
    c                 C   s&   t | ƒ| _|  d¡ |  | jd¡ d S )NéÈ   F)r    Ú_actualProducerÚsetResponseCodeÚregisterProducer©Úselfr-   r-   r.   ÚprocessT   s    

z DummyPullProducerHandler.processN©r)   r*   r+   r,   r>   r-   r-   r-   r.   r7   M   s   r7   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚDateTimeTestszTest date parsing functions.c                 C   s>   t dƒD ]0}t dd¡}t |¡}t |¡}|  ||¡ qd S )Ni'  r   i ”5w)ÚrangeÚrandomZrandintr   ZdatetimeToStringÚstringToDatetimeÚassertEqual)r=   ÚiÚtimeZtimestrZtime2r-   r-   r.   ÚtestRoundtripb   s
    

zDateTimeTests.testRoundtripc                 C   sV   ddddddddg}t  d	¡}|D ]}|  t |¡|¡ q"|  t d
¡t  d¡¡ d S )Ns   Sun, 06 Nov 1994 08:49:37 GMTs   06 Nov 1994 08:49:37 GMTs   Sunday, 06-Nov-94 08:49:37 GMTs   06-Nov-94 08:49:37 GMTs    Sunday, 06-Nov-1994 08:49:37 GMTs   06-Nov-1994 08:49:37 GMTs   Sun Nov  6 08:49:37 1994s   Nov  6 08:49:37 1994)	iÊ  é   é   é   é1   é%   rI   rI   r   s    Thursday, 29-Sep-16 17:15:29 GMT)	ià  é	   é   é   é   rN   é   i  r   )ÚcalendarZtimegmrD   r   rC   )r=   ZdateStringsZdateIntZ
dateStringr-   r-   r.   ÚtestStringToDatetimej   s     ø

þz"DateTimeTests.testStringToDatetimeN)r)   r*   r+   r,   rG   rS   r-   r-   r-   r.   r@   _   s   r@   c                   @   s   e Zd Zdd„ ZdS )ÚDummyHTTPHandlerc                 C   s¨   | j  dd¡ | j  ¡ }|  d¡}|d kr6t|ƒ ¡ }d| d | d }|  d¡ |  d| j¡ |  d| j	¡ |  d| j
¡ |  d	tt|ƒƒ¡ |  |¡ |  ¡  d S )
Nr   ó   content-lengths   '''
ó   
r8   s   Requests   Commands   Versionó   Content-Length)ÚcontentÚseekÚreadÚ	getHeaderÚstrÚencoder:   Ú	setHeaderÚuriÚmethodÚclientprotor   ÚlenÚwriteÚfinish)r=   ÚdataÚlengthÚrequestr-   r-   r.   r>   €   s    



zDummyHTTPHandler.processN©r)   r*   r+   r>   r-   r-   r-   r.   rT   ~   s   rT   c                   @   s   e Zd ZdZdd„ ZdS )ÚDummyNewHTTPHandlerzÉ
    This is exactly like the DummyHTTPHandler but it takes only one argument
    in its constructor, with no default arguments. This exists to test an
    alternative code path in L{HTTPChannel}.
    c                 C   s   t  | |¡ d S r/   )rT   Ú__init__)r=   Úchannelr-   r-   r.   rj   š   s    zDummyNewHTTPHandler.__init__N)r)   r*   r+   r,   rj   r-   r-   r-   r.   ri   “   s   ri   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚDelayedHTTPHandlerzD
    Like L{DummyHTTPHandler}, but doesn't respond immediately.
    c                 C   s   d S r/   r-   r<   r-   r-   r.   r>   ¥   s    zDelayedHTTPHandler.processc                 C   s   t  | ¡ d S r/   )rT   r>   r<   r-   r-   r.   ÚdelayedProcess©   s    z!DelayedHTTPHandler.delayedProcessN)r)   r*   r+   r,   r>   rm   r-   r-   r-   r.   rl   ¡   s   rl   c                   @   s   e Zd Zdd„ ZdS )ÚLoopbackHTTPClientc                 C   s0   |   dd¡ |  dd¡ |  ¡  | j d¡ d S )Nó   GETó   /foo/barrW   é
   ó
   0123456789)ZsendCommandZ
sendHeaderZ
endHeadersÚ	transportrc   r<   r-   r-   r.   ÚconnectionMade²   s    z!LoopbackHTTPClient.connectionMadeN)r)   r*   r+   rt   r-   r-   r-   r.   rn   °   s   rn   c                 C   s   |j | j_ | S )a   
    Parametrizes the L{TimeoutMixin} so that it works with whatever reactor is
    being used by the test.

    @param protocol: A L{_GenericHTTPChannel} or something implementing a
        similar interface.
    @type protocol: L{_GenericHTTPChannel}

    @param reactor: An L{IReactorTime} implementation.
    @type reactor: L{IReactorTime}

    @return: The C{channel}, with its C{callLater} method patched.
    )Ú	callLaterÚ_channel)ÚprotocolÚreactorr-   r-   r.   ÚparametrizeTimeoutMixinº   s    
ry   c                   @   s   e Zd ZdZdd„ ZdS )ÚResponseTestMixinz›
    A mixin that provides a simple means of comparing an actual response string
    to an expected response string by performing the minimal parsing.
    c                 C   s¦   |D ]œ}|dd… |d  }}|d }|dd… }|  dd¡\}}| ¡ }| d¡}	|  ||	¡ |  t|ƒt|ƒ¡ |dt|ƒ… }
|t|ƒd… }|  |
|¡ qdS )a(  
        Assert that the C{responses} matches the C{expected} responses.

        @type responses: C{bytes}
        @param responses: The bytes sent in response to one or more requests.

        @type expected: C{list} of C{tuple} of C{bytes}
        @param expected: The expected values for the responses.  Each tuple
            element of the list represents one response.  Each byte string
            element of the tuple is a full header line without delimiter, except
            for the last element which gives the full response body.
        Néÿÿÿÿr   é   ó   

)ÚsplitÚ
splitlinesÚpoprD   Úsetrb   )r=   Z	responsesÚexpectedÚresponseÚexpectedHeadersZexpectedContentZexpectedStatusÚheadersÚrestÚstatusrX   r-   r-   r.   ÚassertResponseEqualsÖ   s    
z&ResponseTestMixin.assertResponseEqualsN)r)   r*   r+   r,   rˆ   r-   r-   r-   r.   rz   Ð   s   rz   c                   @   s^   e Zd ZdZdg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 )ÚHTTP1_0Testss7   GET / HTTP/1.0

GET / HTTP/1.1
Accept: text/html

)ó   HTTP/1.0 200 OKó
   Request: /ó   Command: GETó   Version: HTTP/1.0ó   Content-Length: 13ó   '''
None
'''
c                 C   s`   t ƒ }t ¡ }t|_| |¡ t| jƒD ]}| |¡ q(| 	t
dƒ¡ | ¡ }|  || j¡ dS )zZ
        Send requests over a channel and check responses match what is expected.
        zall oneN©r   r   ÚHTTPChannelÚDummyHTTPHandlerProxyÚrequestFactoryÚmakeConnectionr   ÚrequestsÚdataReceivedÚconnectionLostÚIOErrorÚvaluerˆ   Úexpected_response©r=   ÚbÚaÚbyter™   r-   r-   r.   Útest_buffer  s    
zHTTP1_0Tests.test_bufferc                 C   sŒ   t ƒ }tƒ }t ¡ }d|_|j|_| |¡ | d¡ | d¡ |  	|j
¡ | d¡ | d¡ |  	|j
¡ | d¡ |  t|jƒd¡ dS )zq
        L{HTTPChannel} resets its timeout whenever data from a request body is
        delivered to it.
        éd   ó&   POST / HTTP/1.0
Content-Length: 2

éc   ó   xr|   N)r   r   r   r‘   ÚtimeOutru   r”   r–   ÚadvanceÚassertFalseÚdisconnectingrD   rb   r•   )r=   Úclockrs   rw   r-   r-   r.   Útest_requestBodyTimeout  s    





z$HTTP1_0Tests.test_requestBodyTimeoutc                 C   sl   t ƒ }tƒ }t ¡ }| d¡}t||ƒ}| |¡ | d¡ | d¡ |  	|j
¡ | d¡ |  |j
¡ dS )zA
        L{HTTPChannel}'s default timeout is 60 seconds.
        Nr¡   é;   r|   )r   r   r   ÚHTTPFactoryÚbuildProtocolry   r”   r–   r¥   r¦   r§   Ú
assertTrue©r=   r¨   rs   Úfactoryrw   r-   r-   r.   Útest_requestBodyDefaultTimeout*  s    





z+HTTP1_0Tests.test_requestBodyDefaultTimeoutc                 C   s  t  | t¡}tƒ }tƒ }t ¡ }| d¡}t||ƒ}| 	|¡ | 
d¡ |  |j¡ |  |j¡ | d¡ |  |j¡ |  |j¡ |  dt|ƒ¡ |d }|  d|d ¡ | d¡ |  |j¡ |  |j¡ | d¡ |  |j¡ |  |j¡ |  d	t|ƒ¡ |d }|  d
|d ¡ dS )ú}
        If a timed out transport doesn't close after 15 seconds, the
        L{HTTPChannel} will forcibly close it.
        Nr¡   é<   r|   r   zTiming out client: {peer}Ú
log_formaté   é   z"Forcibly timing out client: {peer})r!   ÚcreateWithCleanupr'   r   r   r   r«   r¬   ry   r”   r–   r¦   r§   Údisconnectedr¥   r­   ÚassertEqualsrb   ÚassertIn)r=   ÚlogObserverr¨   rs   r¯   rw   Úeventr-   r-   r.   Útest_transportForciblyClosed;  s>    þ






þz)HTTP1_0Tests.test_transportForciblyClosedc                 C   s°   t ƒ }tƒ }t ¡ }| d¡}t||ƒ}| |¡ | d¡ |  |j	¡ |  |j
¡ | d¡ |  |j	¡ |  |j
¡ | d¡ | d¡ | d¡ |  |j	¡ |  |j
¡ dS )z…
        If a timed out transport ends up calling C{connectionLost}, it prevents
        the force-closure of the transport.
        Nr¡   r²   r´   r|   )r   r   r   r«   r¬   ry   r”   r–   r¦   r§   r·   r¥   r­   r—   r®   r-   r-   r.   Ú+test_transportNotAbortedAfterConnectionLoste  s"    







z8HTTP1_0Tests.test_transportNotAbortedAfterConnectionLostc                 C   s¤   t ƒ }tƒ }t ¡ }| d¡}d|j_t||ƒ}| |¡ | 	d¡ |  
|j¡ |  
|j¡ | d¡ |  |j¡ |  
|j¡ | d¡ |  |j¡ |  
|j¡ dS )zh
        If the L{HTTPChannel} has its c{abortTimeout} set to L{None}, it never
        aborts.
        Nr¡   r²   l        )r   r   r   r«   r¬   rv   ZabortTimeoutry   r”   r–   r¦   r§   r·   r¥   r­   r®   r-   r-   r.   Ú,test_transportNotAbortedWithZeroAbortTimeoutƒ  s     





z9HTTP1_0Tests.test_transportNotAbortedWithZeroAbortTimeoutc                 C   sŽ   t ƒ }tƒ }t ¡ }| d¡}t||ƒ}| |¡ | d¡ |  |j	¡ |  |j
¡ | d¡ | d¡ |  |j	¡ |  |j
¡ | t¡ dS )r±   Nr¡   r²   rP   )r   r   r   r«   r¬   ry   r”   r–   r¦   r§   r·   r¥   r­   r—   r   r®   r-   r-   r.   Ú"test_connectionLostAfterForceClosež  s    





z/HTTP1_0Tests.test_connectionLostAfterForceClosec                 C   s`   t ƒ }t ¡ }t|_| |¡ t| jƒD ]}| |¡ q(| 	t
dƒ¡ | ¡ }|  || j¡ dS )zf
        Test that a L{http.Request} subclass with no queued kwarg works as
        expected.
        úall doneNr   r›   r-   r-   r.   Útest_noPipeliningApi¹  s    
z!HTTP1_0Tests.test_noPipeliningApic                 C   s¦   t ƒ }t ¡ }t|_| |¡ t| jƒD ]}| |¡ q(| 	¡ }|  
|d¡ |  
dt|jƒ¡ |jrŒ|  
dt|jƒ¡ |jd j}| ¡  q^| 	¡ }|  || j¡ dS )zW
        Test that pipelined requests get buffered, not processed in parallel.
        ó    r|   r   N)r   r   r‘   ÚDelayedHTTPHandlerProxyr“   r”   r   r•   r–   r™   rD   rb   Úoriginalrm   rˆ   rš   )r=   rœ   r   rž   r™   rg   r-   r-   r.   Útest_noPipeliningÊ  s    

zHTTP1_0Tests.test_noPipeliningN)r)   r*   r+   r•   rš   rŸ   r©   r°   r¼   r½   r¾   r¿   rÁ   rÅ   r-   r-   r-   r.   r‰   õ   s   ÿÿ*r‰   c                   @   s   e Zd ZdZddddgZdS )ÚHTTP1_1Testssš   GET / HTTP/1.1
Accept: text/html

POST / HTTP/1.1
Content-Length: 10

0123456789POST / HTTP/1.1
Content-Length: 10

0123456789HEAD / HTTP/1.1

)ó   HTTP/1.1 200 OKr‹   rŒ   ó   Version: HTTP/1.1rŽ   r   ©rÇ   r‹   ó   Command: POSTrÈ   s   Content-Length: 21ó   '''
10
0123456789'''
)rÇ   r‹   s   Command: HEADrÈ   rŽ   rÂ   N©r)   r*   r+   r•   rš   r-   r-   r-   r.   rÆ   æ  s   ÿírÆ   c                   @   s   e Zd ZdZdgZdS )ÚHTTP1_1_close_TestsóJ   GET / HTTP/1.1
Accept: text/html
Connection: close

GET / HTTP/1.0

)rÇ   s   Connection: closer‹   rŒ   rÈ   rŽ   r   NrÌ   r-   r-   r-   r.   rÍ     s   ÿ	ÿrÍ   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚHTTP0_9Testss   GET /
ó   HTTP/1.1 400 Bad Request

c                 C   s   |   ||¡ d S r/   )rD   )r=   rƒ   ZexpectedResponser-   r-   r.   rˆ   .  s    z!HTTP0_9Tests.assertResponseEqualsc                 C   s   t  d¡‚d S )NzHTTP/0.9 not supported)r   ZSkipTestr<   r-   r-   r.   rÅ   2  s    zHTTP0_9Tests.test_noPipeliningN)r)   r*   r+   r•   rš   rˆ   rÅ   r-   r-   r-   r.   rÏ   &  s
   ÿrÏ   c                   @   sL   e Zd ZdZdZddgZdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚPipeliningBodyTestszl
    Pipelined requests get buffered and executed in the order received,
    not processed in parallel.
    st   POST / HTTP/1.1
Content-Length: 10

0123456789POST / HTTP/1.1
Transfer-Encoding: chunked

a
0123456789
0

rÉ   )rÇ   r‹   rÊ   rÈ   s   Content-Length: 23s   '''
None
0123456789'''
c                 C   sž   t ƒ }t ¡ }t|_| |¡ t| jƒD ]}| |¡ q(|  	| 
¡ d¡ |  	dt|jƒ¡ |jrˆ|  	dt|jƒ¡ |jd j}| ¡  qZ|  | 
¡ | j¡ dS )zÃ
        Imitate a slow connection that delivers one byte at a time.
        The request handler (L{DelayedHTTPHandler}) is puppeted to
        step through the handling of each request.
        rÂ   r|   r   N)r   r   r‘   rÃ   r“   r”   r   r•   r–   rD   r™   rb   rÄ   rm   rˆ   ÚexpectedResponses)r=   rœ   r   rž   rg   r-   r-   r.   Útest_stepwiseTinyTubeX  s    

z)PipeliningBodyTests.test_stepwiseTinyTubec                 C   s   t ƒ }t ¡ }t|_| |¡ | | j¡ |  | 	¡ d¡ |  dt
|jƒ¡ |jrz|  dt
|jƒ¡ |jd j}| ¡  qL|  | 	¡ | j¡ dS )zã
        Imitate a fast connection where several pipelined
        requests arrive in a single read. The request handler
        (L{DelayedHTTPHandler}) is puppeted to step through the
        handling of each request.
        rÂ   r|   r   N)r   r   r‘   rÃ   r“   r”   r–   r•   rD   r™   rb   rÄ   rm   rˆ   rÒ   )r=   rœ   r   rg   r-   r-   r.   Útest_stepwiseDumpTruckr  s    

z*PipeliningBodyTests.test_stepwiseDumpTruckc                 C   s`   t ƒ }t ¡ }t|_| |¡ t| jƒD ] }| |¡ |  	t
|jƒd¡ q(|  | ¡ | j¡ dS )zŸ
        Imitate a slow connection that delivers one byte at a time.

        (L{DummyHTTPHandler}) immediately responds, but no more
        than one
        r|   N)r   r   r‘   r’   r“   r”   r   r•   r–   ZassertLessEqualrb   rˆ   r™   rÒ   )r=   rœ   r   rž   r-   r-   r.   Útest_immediateTinyTubeŒ  s    

z*PipeliningBodyTests.test_immediateTinyTubec                 C   s@   t ƒ }t ¡ }t|_| |¡ | | j¡ |  | 	¡ | j
¡ dS )aR  
        Imitate a fast connection where several pipelined
        requests arrive in a single read. The request handler
        (L{DummyHTTPHandler}) immediately responds.

        This doesn't check the at-most-one pending request
        invariant but exercises otherwise uncovered code paths.
        See GHSA-c8m8-j448-xjx7.
        N)r   r   r‘   r’   r“   r”   r–   r•   rˆ   r™   rÒ   )r=   rœ   r   r-   r-   r.   Útest_immediateDumpTruck   s    

z+PipeliningBodyTests.test_immediateDumpTruckc                 C   s\   t ƒ }t ¡ }t|_| |¡ |jt| jƒ d }| 	| j| ¡ |  
| ¡ | j| ¡ dS )a¯  
        Imitate a fast connection where a so many pipelined
        requests arrive in a single read that backpressure is indicated.
        The request handler (L{DummyHTTPHandler}) immediately responds.

        This doesn't check the at-most-one pending request
        invariant but exercises otherwise uncovered code paths.
        See GHSA-c8m8-j448-xjx7.

        @see: L{http.HTTPChannel._optimisticEagerReadSize}
        rq   N)r   r   r‘   r’   r“   r”   Ú_optimisticEagerReadSizerb   r•   r–   rˆ   r™   rÒ   )r=   rœ   r   ZoverLimitCountr-   r-   r.   Útest_immediateABiggerTruck´  s    
z.PipeliningBodyTests.test_immediateABiggerTruckc              	   C   sˆ   t ƒ }t ¡ }t|_| |¡ |jt| jƒ }t	d|d ƒD ],}| 
| j¡ |  |jddj|j|d¡ q<| 
| j¡ |  |jd¡ dS )zÝ
        When pipelined requests are received, we will optimistically continue
        receiving data up to a specified limit, then pause the transport.

        @see: L{http.HTTPChannel._optimisticEagerReadSize}
        r|   Ú	producingz(state was {state!r} after {x} iterations)ÚstateÚxÚpausedN)r   r   r‘   rÃ   r“   r”   r×   rb   r•   rA   r–   rD   ÚproducerStateÚformatr¸   )r=   rœ   r   Z
underLimitrÛ   r-   r-   r.   Útest_pipeliningReadLimitË  s    

 ÿÿz,PipeliningBodyTests.test_pipeliningReadLimitN)r)   r*   r+   r,   r•   rÒ   rÓ   rÔ   rÕ   rÖ   rØ   rß   r-   r-   r-   r.   rÑ   7  s   ÿùrÑ   c                   @   s.   e Zd ZdZG dd„ dejƒZdZdd„ ZdS )ÚShutdownTestszM
    Tests that connections can be shut down by L{http.Request} objects.
    c                   @   s   e Zd ZdZdd„ ZdS )z!ShutdownTests.ShutdownHTTPHandlerzL
        A HTTP handler that just immediately calls loseConnection.
        c                 C   s   |   ¡  d S r/   )ÚloseConnectionr<   r-   r-   r.   r>   ê  s    z)ShutdownTests.ShutdownHTTPHandler.processNr?   r-   r-   r-   r.   ÚShutdownHTTPHandleræ  s   râ   s1   POST / HTTP/1.1
Content-Length: 10

0123456789c                 C   sT   t ƒ }t ¡ }t| jƒ|_| |¡ | | j¡ |  	|j
¡ | ¡ }|  |d¡ dS )zi
        Calling L{http.Request.loseConnection} causes the transport to be
        disconnected.
        rÂ   N)r   r   r‘   r6   râ   r“   r”   r–   rg   r­   r§   r™   rD   )r=   rœ   r   r™   r-   r-   r.   Útest_losingConnectionö  s    
z#ShutdownTests.test_losingConnectionN)	r)   r*   r+   r,   r   ÚRequestrâ   rg   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 )	ÚSecurityTestsz]
    Tests that L{http.Request.isSecure} correctly takes the transport into
    account.
    c                 C   s6   t  ¡ }t ¡ }| |¡ t |¡}|  | ¡ ¡ dS )z‚
        Calling L{http.Request.isSecure} when the channel is backed with a
        secure transport will return L{True}.
        N)r#   ÚSSLr   r‘   r”   rä   r­   ÚisSecure©r=   rœ   r   Úreqr-   r-   r.   Útest_isSecure  s
    

zSecurityTests.test_isSecurec                 C   s6   t  ¡ }t ¡ }| |¡ t |¡}|  | ¡ ¡ dS )z‡
        Calling L{http.Request.isSecure} when the channel is not backed with a
        secure transport will return L{False}.
        N)r#   ÚTCPr   r‘   r”   rä   r¦   rç   rè   r-   r-   r.   Útest_notSecure  s
    

zSecurityTests.test_notSecurec                 C   sT   t  ¡ }t ¡ }| |¡ t |¡}|j |¡ | d¡ | 	¡  |  
| ¡ ¡ dS )zt
        After a request is finished, calling L{http.Request.isSecure} will
        always return L{False}.
        r8   N)r#   ræ   r   r‘   r”   rä   r•   Úappendr:   rd   r¦   rç   rè   r-   r-   r.   Útest_notSecureAfterFinish'  s    


z'SecurityTests.test_notSecureAfterFinishN)r)   r*   r+   r,   rê   rì   rî   r-   r-   r-   r.   rå   
  s   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	e
jsDde	_dd„ Ze
jrXde_dd„ Zdd„ Zdd„ Zdd„ Ze
js„de_dd„ Ze
js˜de_dS )ÚGenericHTTPChannelTestsz†
    Tests for L{http._genericHTTPChannelProtocol}, a L{HTTPChannel}-alike which
    can handle different HTTP protocol channels.
    rÎ   c                 C   sH   t  d¡}t|_| |¡ t| jƒD ]}| |¡ q$| t	dƒ¡ |j
S )z{
        Run a request using the specific instance of a transport. Returns the
        negotiated protocol string.
        rÂ   rÀ   )r   Ú"_genericHTTPChannelProtocolFactoryr’   r“   r”   r   r•   r–   r—   r˜   Z_negotiatedProtocol)r=   Útr   rž   r-   r-   r.   Ú'_negotiatedProtocolForTransportInstanceF  s    

z?GenericHTTPChannelTests._negotiatedProtocolForTransportInstancec                 C   s    t ƒ }|  |¡}|  |d¡ dS )zŠ
        If the transport has no support for protocol negotiation (no
        negotiatedProtocol attribute), HTTP/1.1 is assumed.
        ó   http/1.1N)r   rò   rD   ©r=   rœ   ÚnegotiatedProtocolr-   r-   r.   Útest_protocolUnspecifiedU  s    
z0GenericHTTPChannelTests.test_protocolUnspecifiedc                 C   s&   t ƒ }d|_|  |¡}|  |d¡ dS )zŽ
        If the transport has no support for protocol negotiation (returns None
        for negotiatedProtocol), HTTP/1.1 is assumed.
        Nró   ©r   rõ   rò   rD   rô   r-   r-   r.   Útest_protocolNone_  s    
z)GenericHTTPChannelTests.test_protocolNonec                 C   s&   t ƒ }d|_|  |¡}|  |d¡ dS )zi
        If the transport reports that HTTP/1.1 is negotiated, that's what's
        negotiated.
        ró   Nr÷   rô   r-   r-   r.   Útest_http11j  s    
z#GenericHTTPChannelTests.test_http11c                 C   s&   t ƒ }d|_|  |¡}|  |d¡ dS )z}
        If the transport reports that HTTP/2 is negotiated and HTTP/2 is
        present, that's what's negotiated.
        ó   h2Nr÷   rô   r-   r-   r.   Útest_http2_presentu  s    
z*GenericHTTPChannelTests.test_http2_presentzHTTP/2 support not presentc                 C   s    t ƒ }d|_|  t| j|¡ dS )z€
        If the transport reports that HTTP/2 is negotiated and HTTP/2 is not
        present, an error is encountered.
        rú   N)r   rõ   ÚassertRaisesÚ
ValueErrorrò   ©r=   rœ   r-   r-   r.   Útest_http2_absent‚  s    ýz)GenericHTTPChannelTests.test_http2_absentzHTTP/2 support presentc                 C   s    t ƒ }d|_|  t| j|¡ dS )z€
        If the transport reports that a protocol other than HTTP/1.1 or HTTP/2
        is negotiated, an error occurs.
        s   smtpN)r   rõ   rü   ÚAssertionErrorrò   rþ   r-   r-   r.   Útest_unknownProtocol’  s    ýz,GenericHTTPChannelTests.test_unknownProtocolc                 C   s$   t  d¡}d|j_|  |jd¡ dS )zK
        The C{factory} attribute is taken from the inner channel.
        rÂ   ó   FooN)r   rð   rv   r¯   rD   )r=   r   r-   r-   r.   Útest_factory   s    
z$GenericHTTPChannelTests.test_factoryc                 C   sB   t ƒ }tj|d}| d¡}|  |j|j¡ |  |jj|j¡ dS )zž
        If C{callLater} is patched onto the L{http._GenericHTTPChannelProtocol}
        then we need to propagate it through to the backing channel.
        ©rx   N)r   r   r«   r¬   rD   ru   rv   )r=   r¨   r¯   rw   r-   r-   r.   Ú*test_GenericHTTPChannelPropagatesCallLater©  s
    
zBGenericHTTPChannelTests.test_GenericHTTPChannelPropagatesCallLaterc                 C   sŠ   t ƒ }tj|d}| d¡}|  |j|j¡ |  |jj|j¡ tƒ }d|_t	|_
| |¡ | d¡ |  |j|j¡ |  |jj|j¡ dS )z·
        If C{callLater} is patched onto the L{http._GenericHTTPChannelProtocol}
        then we need to propagate it across onto a new backing channel after
        upgrade.
        r  Nrú   ó   P)r   r   r«   r¬   rD   ru   rv   r   rõ   rT   r“   r”   r–   ©r=   r¨   r¯   rw   rs   r-   r-   r.   Ú'test_genericHTTPChannelCallLaterUpgrade¶  s    


z?GenericHTTPChannelTests.test_genericHTTPChannelCallLaterUpgradec                 C   sf   t ƒ }d|_t d¡}t|_| |¡ |j}|  |j	|¡ | 
d¡ |  |j	|¡ |  |j	|j¡ dS )z‹
        The L{_GenericHTTPChannelProtocol} will unregister its proxy channel
        from the transport if upgrade is negotiated.
        rú   rÂ   r  N)r   rõ   r   rð   r’   r“   r”   rv   ÚassertIsÚproducerr–   ZassertIsNot)r=   rs   ZgenericProtocolZoriginalChannelr-   r-   r.   Útest_unregistersProducerÓ  s    


z0GenericHTTPChannelTests.test_unregistersProducerN)r)   r*   r+   r,   r•   rò   rö   rø   rù   rû   r   Z
H2_ENABLEDÚskiprÿ   r  r  r  r  r  r-   r-   r-   r.   rï   8  s.   ÿ	
		ÿrï   c                   @   sZ   e Zd ZdddddœZdZdZdZdZdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚHTTPLoopbackTestsrp   ro   ó   HTTP/1.0s   21)s   requests   commands   versionrU   r   c                 C   s"   d| _ |  |d¡ |  |d¡ d S )Nr|   r  s   200)Ú	gotStatusrD   ©r=   Úversionr‡   Úmessager-   r-   r.   Ú_handleStatusÿ  s    zHTTPLoopbackTests._handleStatusc                 C   s   d| _ |  |d¡ d S )Nr|   rË   )ÚgotResponserD   )r=   re   r-   r-   r.   Ú_handleResponse  s    z!HTTPLoopbackTests._handleResponsec                 C   s&   | j d | _ |  | j| ¡  |¡ d S )Nr|   )Ú
numHeadersrD   r„   Úlower)r=   Úkeyr™   r-   r-   r.   Ú_handleHeader  s    zHTTPLoopbackTests._handleHeaderc                 C   s   d| _ |  | jd¡ d S )Nr|   é   )ÚgotEndHeadersrD   r  r<   r-   r-   r.   Ú_handleEndHeaders  s    z#HTTPLoopbackTests._handleEndHeadersc                 C   sP   t  ¡ }t|_tƒ }| j|_| j|_| j	|_
| j|_t ||¡}| | j¡ |S r/   )r   r‘   r’   r“   rn   r  ZhandleResponser  ÚhandleHeaderr  ÚhandleEndHeadersr  ÚhandleStatusr   ZloopbackAsyncÚaddCallbackÚ_cbTestLoopback)r=   ZserverÚclientÚdr-   r-   r.   ÚtestLoopback  s    zHTTPLoopbackTests.testLoopbackc                 C   s>   | j r| jr| js*td| j | j| jg ƒ‚| `| `| ` | `d S )Nzdidn't got all callbacks %s)r  r  r  ÚRuntimeErrorr  )r=   Zignoredr-   r-   r.   r!    s    ÿÿz!HTTPLoopbackTests._cbTestLoopbackN)r)   r*   r+   r„   r  r  r  r  r  r  r  r  r$  r!  r-   r-   r-   r.   r  ô  s   ýr  c                  K   s6   t  tƒ d¡}|  ¡ D ]\}}|j t|ƒ|¡ q|S )zR
    Make a request with the given request headers for the persistence tests.
    F)r   rä   r#   ÚitemsÚrequestHeadersÚsetRawHeadersr   )r…   rg   Z
headerNameÚvr-   r-   r.   Ú	_prequest(  s    r*  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚPersistenceTestsz0
    Tests for persistent HTTP connections.
    c                 C   s   t  ¡ | _tƒ | _d S r/   )r   r‘   rk   r*  rg   r<   r-   r-   r.   ÚsetUp7  s    
zPersistenceTests.setUpc                 C   s6   | j  | jd¡}|  |¡ |  g t| jj ¡ ƒ¡ dS )zl
        After being used for an I{HTTP/0.9} request, the L{HTTPChannel} is not
        persistent.
        s   HTTP/0.9N©rk   ÚcheckPersistencerg   r¦   rD   ÚlistÚresponseHeadersÚgetAllRawHeaders©r=   Úpersistr-   r-   r.   Útest_http09<  s    
 ÿzPersistenceTests.test_http09c                 C   s6   | j  | jd¡}|  |¡ |  g t| jj ¡ ƒ¡ dS )zl
        After being used for an I{HTTP/1.0} request, the L{HTTPChannel} is not
        persistent.
        r  Nr-  r2  r-   r-   r.   Útest_http10G  s    
 ÿzPersistenceTests.test_http10c                 C   s6   | j  | jd¡}|  |¡ |  g t| jj ¡ ƒ¡ dS )zh
        After being used for an I{HTTP/1.1} request, the L{HTTPChannel} is
        persistent.
        ó   HTTP/1.1N)rk   r.  rg   r­   rD   r/  r0  r1  r2  r-   r-   r.   rù   R  s    
 ÿzPersistenceTests.test_http11c                 C   sF   t dgd}| j |d¡}|  |¡ |  ddgfgt|j ¡ ƒ¡ dS )z
        After being used for an I{HTTP/1.1} request with a I{Connection: Close}
        header, the L{HTTPChannel} is not persistent.
        s   close)Z
connectionr6  s
   ConnectionN)r*  rk   r.  r¦   rD   r/  r0  r1  )r=   rg   r3  r-   r-   r.   Útest_http11Close]  s    

þz!PersistenceTests.test_http11CloseN)	r)   r*   r+   r,   r,  r4  r5  rù   r7  r-   r-   r-   r.   r+  3  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S )ÚIdentityTransferEncodingTestsz0
    Tests for L{_IdentityTransferDecoder}.
    c                 C   s.   g | _ g | _d| _t| j| j j| jjƒ| _dS )z€
        Create an L{_IdentityTransferDecoder} with callbacks hooked up so that
        calls to them can be inspected.
        rq   N)re   rd   ÚcontentLengthr   rí   Údecoderr<   r-   r-   r.   r,  o  s      ÿz#IdentityTransferEncodingTests.setUpc                 C   s<   | j  d| j ¡ |  | jd| j g¡ |  | jdg¡ dS )aC  
        If L{_IdentityTransferDecoder.dataReceived} is called with a byte string
        with length equal to the content length passed to
        L{_IdentityTransferDecoder}'s initializer, the data callback is invoked
        with that string and the finish callback is invoked with a zero-length
        string.
        r£   rÂ   N©r:  r–   r9  rD   re   rd   r<   r-   r-   r.   Útest_exactAmountReceived{  s    z6IdentityTransferEncodingTests.test_exactAmountReceivedc                 C   sp   | j  d¡ |  | jdg¡ |  | jg ¡ | j  d| jd  ¡ |  | jdd| jd  g¡ |  | jdg¡ dS )a3  
        If L{_IdentityTransferDecoder.dataReceived} is called multiple times
        with byte strings which, when concatenated, are as long as the content
        length provided, the data callback is invoked with each string and the
        finish callback is invoked only after the second call.
        r£   ó   yr|   rÂ   N)r:  r–   rD   re   rd   r9  r<   r-   r-   r.   Útest_shortStringsˆ  s    z/IdentityTransferEncodingTests.test_shortStringsc                 C   s@   | j  d| j d ¡ |  | jd| j g¡ |  | jdg¡ dS )a3  
        If L{_IdentityTransferDecoder.dataReceived} is called with a byte string
        with length greater than the provided content length, only the prefix
        of that string up to the content length is passed to the data callback
        and the remainder is passed to the finish callback.
        r£   r=  Nr;  r<   r-   r-   r.   Útest_longString—  s    z-IdentityTransferEncodingTests.test_longStringc                    sj   g ‰‡ ‡fdd„}t d| jj|ƒ‰ ˆ  d¡ |  ˆg ¡ ˆ  d¡ ˆd  t¡ |  tˆd jƒd¡ dS )	zž
        If data is passed to L{_IdentityTransferDecoder.dataReceived} after the
        finish callback has been invoked, C{RuntimeError} is raised.
        c                    s,   zˆ   d¡ W n   ˆ tƒ ¡ Y nX d S )Nó   foo)r–   rí   r   ©Úbytes©r:  Zfailuresr-   r.   rd   ©  s    zJIdentityTransferEncodingTests.test_rejectDataAfterFinished.<locals>.finishé   s   xxxxr=  r   z;_IdentityTransferDecoder cannot decode data after finishingN)	r   re   rí   r–   rD   Ztrapr%  r\   r™   )r=   rd   r-   rC  r.   Útest_rejectDataAfterFinished£  s    

þz:IdentityTransferEncodingTests.test_rejectDataAfterFinishedc                 C   sZ   g }g }t d|j|jƒ}| d¡ |  |dg¡ | d¡ |  |ddg¡ |  |g ¡ dS )zµ
        If L{_IdentityTransferDecoder} is constructed with L{None} for the
        content length, it passes all data delivered to it through to the data
        callback.
        Nr£   r=  )r   rí   r–   rD   )r=   re   rd   r:  r-   r-   r.   Útest_unknownContentLength¸  s    

z7IdentityTransferEncodingTests.test_unknownContentLengthc                 C   s    |   |jd¡ |   |jd¡ dS )z
        Check the decoder's data and finish callbacks and make sure they are
        None in order to help avoid references cycles.
        N)ÚassertIdenticalZdataCallbackZfinishCallback)r=   r:  r-   r-   r.   Ú_verifyCallbacksUnreferencedÈ  s    z:IdentityTransferEncodingTests._verifyCallbacksUnreferencedc                 C   s6   | j  d| jd  ¡ |  t| j j¡ |  | j ¡ dS )z¶
        L{_IdentityTransferDecoder.noMoreData} raises L{_DataLoss} if it is
        called and the content length is known but not enough bytes have been
        delivered.
        r£   r|   N)r:  r–   r9  rü   r   Ú
noMoreDatarH  r<   r-   r-   r.   Útest_earlyConnectionLoseÑ  s    z6IdentityTransferEncodingTests.test_earlyConnectionLosec                 C   sN   g }g }t d|j|jƒ}|  t|j¡ |  |g ¡ |  |dg¡ |  |¡ dS )zµ
        L{_IdentityTransferDecoder.noMoreData} calls the finish callback and
        raises L{PotentialDataLoss} if it is called and the content length is
        unknown.
        NrÂ   )r   rí   rü   r   rI  rD   rH  )r=   ÚbodyÚfinishedr:  r-   r-   r.   Ú'test_unknownContentLengthConnectionLoseÜ  s    zEIdentityTransferEncodingTests.test_unknownContentLengthConnectionLosec                 C   s,   | j  d| j ¡ | j  ¡  |  | j ¡ dS )z¿
        L{_IdentityTransferDecoder.noMoreData} does not raise any exception if
        it is called when the content length is known and that many bytes have
        been delivered.
        r£   N)r:  r–   r9  rI  rH  r<   r-   r-   r.   Útest_finishedConnectionLoseë  s    
z9IdentityTransferEncodingTests.test_finishedConnectionLoseN)r)   r*   r+   r,   r,  r<  r>  r?  rE  rF  rH  rJ  rM  rN  r-   r-   r-   r.   r8  k  s   	r8  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S )ÚChunkedTransferEncodingTestsz¨
    Tests for L{_ChunkedTransferDecoder}, which turns a byte stream encoded
    using HTTP I{chunked} C{Transfer-Encoding} back into the original byte
    stream.
    c                 C   s<   g }t  |jd¡}| d¡ | d¡ |  |dddg¡ dS )z
        L{_ChunkedTransferDecoder.dataReceived} decodes chunked-encoded data
        and passes the result to the specified callback.
        Ns   3
abc
5
12345
s   a
0123456789
ó   abcs   12345rr   ©r   Ú_ChunkedTransferDecoderrí   r–   rD   ©r=   ÚLÚpr-   r-   r.   Útest_decodingý  s
    

z*ChunkedTransferEncodingTests.test_decodingc                 C   s^   g }g }t  |j|j¡}tdƒD ]}| |¡ q |  |dddddddd	g¡ |  |d
g¡ dS )z{
        L{_ChunkedTransferDecoder.dataReceived} decodes chunks broken up and
        delivered in multiple calls.
        s   3
abc
5
12345
0

ó   aó   bó   có   1ó   2ó   3ó   4ó   5rÂ   N)r   rR  rí   r   r–   rD   )r=   rT  rL  rU  Úsr-   r-   r.   Ú
test_short	  s    z'ChunkedTransferEncodingTests.test_shortc                 C   s.   g }t  |jd¡}| d¡ |  |dg¡ dS )z
        L{_ChunkedTransferDecoder.dataReceived} doesn't treat CR LF pairs
        embedded in chunk bodies specially.
        Ns   2


ó   
rQ  rS  r-   r-   r.   Útest_newlines  s    
z*ChunkedTransferEncodingTests.test_newlinesc                 C   s.   g }t  |jd¡}| d¡ |  |dg¡ dS )zd
        L{_ChunkedTransferDecoder.dataReceived} disregards chunk-extension
        fields.
        Ns   3; x-foo=bar
abc
rP  rQ  rS  r-   r-   r.   Útest_extensions"  s    
z,ChunkedTransferEncodingTests.test_extensionsc                 C   s.   g }t  d|j¡}| d¡ |  |dg¡ dS )z±
        L{_ChunkedTransferDecoder.dataReceived} interprets a zero-length
        chunk as the end of the chunked data stream and calls the completion
        callback.
        Nó   0

rÂ   rQ  ©r=   rL  rU  r-   r-   r.   Útest_finish-  s    
z(ChunkedTransferEncodingTests.test_finishc                 C   s.   g }t  d|j¡}| d¡ |  |dg¡ dS )z¡
        L{_ChunkedTransferDecoder.dataReceived} passes any bytes which come
        after the terminating zero-length chunk to the completion callback.
        Ns
   0

helloó   hellorQ  re  r-   r-   r.   Ú
test_extra9  s    
z'ChunkedTransferEncodingTests.test_extrac                 C   s.   t  ddd„ ¡}| d¡ |  t|jd¡ dS )zŠ
        L{_ChunkedTransferDecoder.dataReceived} raises C{RuntimeError} if it
        is called after it has seen the last chunk.
        Nc                 S   s   d S r/   r-   rA  r-   r-   r.   Ú<lambda>I  rÂ   zAChunkedTransferEncodingTests.test_afterFinished.<locals>.<lambda>rd  rg  )r   rR  r–   rü   r%  )r=   rU  r-   r-   r.   Útest_afterFinishedD  s    
z/ChunkedTransferEncodingTests.test_afterFinishedc                 C   s<   t  ddd„ ¡}| d¡ |  t|j¡}|  t|ƒd¡ dS )zž
        L{_ChunkedTransferDecoder.noMoreData} raises L{_DataLoss} if it is
        called and the end of the last trailer has not yet been received.
        Nc                 S   s   d S r/   r-   rA  r-   r-   r.   ri  S  rÂ   zGChunkedTransferEncodingTests.test_earlyConnectionLose.<locals>.<lambda>s   0
zYChunked decoder in 'TRAILER' state, still expecting more data to get to 'FINISHED' state.)r   rR  r–   rü   r   rI  rD   r\   )r=   ÚparserÚexcr-   r-   r.   rJ  N  s    
þz5ChunkedTransferEncodingTests.test_earlyConnectionLosec                 C   s&   t  ddd„ ¡}| d¡ | ¡  dS )zž
        L{_ChunkedTransferDecoder.noMoreData} does not raise any exception if
        it is called after the terminal zero length chunk is received.
        Nc                 S   s   d S r/   r-   rA  r-   r-   r.   ri  a  rÂ   zJChunkedTransferEncodingTests.test_finishedConnectionLose.<locals>.<lambda>rd  )r   rR  r–   rI  )r=   rk  r-   r-   r.   rN  \  s    
z8ChunkedTransferEncodingTests.test_finishedConnectionLosec                    sL   g ‰ g ‰‡ ‡‡fdd„}t  d|¡‰ˆ d¡ |  ˆ g ¡ |  ˆdg¡ dS )z†
        L{_ChunkedTransferDecoder.noMoreData} can be called from the finished
        callback without raising an exception.
        c                    s4   zˆ  ¡  W n   ˆ  tƒ ¡ Y nX ˆ d¡ d S ©NT)rI  rí   r   )Zextra©Úerrorsrk  Z	successesr-   r.   rL  m  s
    zOChunkedTransferEncodingTests.test_reentrantFinishedNoMoreData.<locals>.finishedNrd  T)r   rR  r–   rD   )r=   rL  r-   rn  r.   Ú test_reentrantFinishedNoMoreDataf  s    
z=ChunkedTransferEncodingTests.test_reentrantFinishedNoMoreDataN)r)   r*   r+   r,   rV  r`  rb  rc  rf  rh  rj  rJ  rN  rp  r-   r-   r-   r.   rO  ÷  s   

rO  c                   @   s4   e Zd ZddddddgZdd„ Zd	d
„ Zdd„ ZdS )ÚChunkingTestss   abcvrÂ   s   fdfsd423s	   Ffasfas
s   523523
fsdfs   4234c                 C   sF   | j D ]*}d t |¡¡}|  |dft |¡¡ q|  ttjd¡ d S )NrÂ   s   -5
malformed!
)ÚstringsÚjoinr   ÚtoChunkrD   Ú	fromChunkrü   rý   )r=   r_  Úchunkedr-   r-   r.   Ú
testChunks€  s    
zChunkingTests.testChunksc              	   C   sx   d  dd„ | jD ƒ¡}g }d}t|ƒD ]>}|| }zt |¡\}}| |¡ W q& tk
rb   Y q&X q&|  || j¡ d S )NrÂ   c                 S   s   g | ]}d   t |¡¡‘qS )rÂ   )rs  r   rt  )Ú.0rñ   r-   r-   r.   Ú
<listcomp>‡  s     z8ChunkingTests.testConcatenatedChunks.<locals>.<listcomp>)rs  rr  r   r   ru  rí   rý   rD   )r=   rv  ÚresultÚbufferÚcre   r-   r-   r.   ÚtestConcatenatedChunks†  s    z$ChunkingTests.testConcatenatedChunksc                 C   sn   t ƒ }t ¡ }| |¡ t |d¡}| d¡ d|_|j ddg¡ | 	d¡ | 	d¡ |  
| ¡ dg¡ d	S )
zV
        Test that the L{HTTPChannel} correctly chunks responses when needed.
        Fr8   r6  ó   testó   lemuró   Helloó   World!©rÇ   ó   Test: lemuró   Transfer-Encoding: chunkeds   5
Hello
6
World!
N)r   r   r‘   r”   rä   r:   ra   r0  r(  rc   rˆ   r™   )r=   Útransrk   ré   r-   r-   r.   Útest_chunkedResponses“  s    



þz#ChunkingTests.test_chunkedResponsesN)r)   r*   r+   rr  rw  r}  r†  r-   r-   r-   r.   rq  {  s    ÿrq  c                   @   s  e Zd ZdZdd„ ZdEd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er¨d(e_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%dS )FÚParsingTestsz7
    Tests for protocol parsing in L{HTTPChannel}.
    c                 C   s
   d| _ d S )NF©Ú
didRequestr<   r-   r-   r.   r,  °  s    zParsingTests.setUpNTc                 C   sŠ   |st  ¡ }|rt|ƒ|_| dd¡}tƒ }| |¡ t|ƒD ]}|jj	rN qZ| 
|¡ q>| tdƒ¡ |rz|  | j¡ n|  | j¡ |S )a›  
        Execute a web request based on plain text content.

        @param httpRequest: Content for the request which is processed. Each
            L{"
"} will be replaced with L{"
"}.
        @type httpRequest: C{bytes}

        @param requestFactory: 2-argument callable returning a Request.
        @type requestFactory: C{callable}

        @param success: Value to compare against I{self.didRequest}.
        @type success: C{bool}

        @param channel: Channel instance over which the request is processed.
        @type channel: L{HTTPChannel}

        @return: Returns the channel used for processing the request.
        @rtype: L{HTTPChannel}
        rV   ra  rÀ   )r   r‘   r6   r“   Úreplacer   r”   r   rs   r§   r–   r—   r˜   r­   r‰  r¦   )r=   ÚhttpRequestr“   Úsuccessrk   rs   rž   r-   r-   r.   Ú
runRequest´  s     

zParsingTests.runRequestc                    sd   d  |¡}g ‰ G ‡ fdd„dtjƒ}| j||dd}|  |j ¡ d¡ |  |jj¡ |  ˆ g ¡ dS )aJ  
        Execute a HTTP request and assert that it is rejected with a 400 Bad
        Response and disconnection.

        @param requestLines: Plain text lines of the request. These lines will
            be joined with newlines to form the HTTP request that is processed.
        @type requestLines: C{list} of C{bytes}
        rV   c                       s   e Zd Z‡ fdd„ZdS )z5ParsingTests.assertRequestRejected.<locals>.MyRequestc                    s   ˆ   | ¡ |  ¡  d S r/   ©rí   rd   r<   ©Ú	processedr-   r.   r>   î  s    
z=ParsingTests.assertRequestRejected.<locals>.MyRequest.processNrh   r-   r  r-   r.   Ú	MyRequestí  s   r‘  F©rŒ  rÐ   N)	rs  r   rä   r  rD   rs   r™   r­   r§   )r=   ÚrequestLinesr‹  r‘  rk   r-   r  r.   ÚassertRequestRejectedá  s    	
þz"ParsingTests.assertRequestRejectedc                    s\   g ‰ G ‡ fdd„dt jƒ}d}|  ||d¡}|  |j ¡ d¡ |  |jj¡ |  ˆ g ¡ dS )z
        When client sends invalid HTTP method containing
        non-ascii characters HTTP 400 'Bad Request' status will be returned.
        c                       s   e Zd Z‡ fdd„ZdS )z:ParsingTests.test_invalidNonAsciiMethod.<locals>.MyRequestc                    s   ˆ   | ¡ |  ¡  d S r/   rŽ  r<   r  r-   r.   r>     s    
zBParsingTests.test_invalidNonAsciiMethod.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘    s   r‘  s   GEÂ© / HTTP/1.1

r   rÐ   N)r   rä   r  rD   rs   r™   r­   r§   )r=   r‘  ZbadRequestLinerk   r-   r  r.   Útest_invalidNonAsciiMethodû  s    þz'ParsingTests.test_invalidNonAsciiMethodc                    sx   g ‰ G ‡ fdd„dt jƒ}dD ]T\}}t d ||f¡¡ ¡ }d| d }|  ||d¡ ˆ  ¡ }|  ||f|j	¡ qdS )	zÜ
        L{HTTPChannel} provides username and password information supplied in
        an I{Authorization} header to the L{Request} which makes it available
        via its C{getUser} and C{getPassword} methods.
        c                       s   e Zd Z‡ fdd„ZdS )z,ParsingTests.test_basicAuth.<locals>.Requestc                    s    |   ¡ |  ¡ f| _ˆ  | ¡ d S r/   ©ZgetUserZgetPasswordÚcredentialsrí   r<   ©r•   r-   r.   r>     s    z4ParsingTests.test_basicAuth.<locals>.Request.processNrh   r-   r˜  r-   r.   rä     s   rä   ))r@  ó   bar)rg  s   there:zó   :ó$   GET / HTTP/1.0
Authorization: Basic ó   

r   N)
r   rä   Úbase64Úencodestringrs  Ústripr  r€   rD   r—  )r=   rä   ÚurU  r_  Úfré   r-   r˜  r.   Útest_basicAuth  s    zParsingTests.test_basicAuthc                    sv   g ‰ G ‡ fdd„dt jƒ}ddddddg}|  d |¡|d	¡ ˆ \}|  |j d
¡dg¡ |  |j d¡ddg¡ dS )zo
        Headers received by L{HTTPChannel} in a request are made available to
        the L{Request}.
        c                       s   e Zd Z‡ fdd„ZdS )z,ParsingTests.test_headers.<locals>.MyRequestc                    s   ˆ   | ¡ |  ¡  d S r/   rŽ  r<   r  r-   r.   r>   *  s    
z4ParsingTests.test_headers.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘  )  s   r‘  ó   GET / HTTP/1.0ó   Foo: bars	   baz: Quuxs	   baz: quuxrÂ   rV   r   r@  r™  s   bAzs   Quuxó   quuxN)r   rä   r  rs  rD   r'  ÚgetRawHeaders)r=   r‘  r“  rg   r-   r  r.   Útest_headers#  s&    ú
 ÿ
 ÿzParsingTests.test_headersc                 C   sL   dg}t tjjd ƒD ]}| td|f ƒ¡ q| ddg¡ |  |¡ dS )z…
        C{HTTPChannel} enforces a limit of C{HTTPChannel.maxHeaders} on the
        number of headers received per request.
        r£  rµ   z%s: foorÂ   N)rA   r   r‘   Ú
maxHeadersrí   r   Úextendr”  )r=   r“  rE   r-   r-   r.   Útest_tooManyHeaders>  s
    z ParsingTests.test_tooManyHeadersc                 C   s   |   ddddg¡ dS )z¶
        If a I{Content-Length} header with a non-integer value is received,
        a 400 (Bad Request) response is sent to the client and the connection
        is closed.
        r£  s   Content-Length: xrÂ   N©r”  r<   r-   r-   r.   Útest_invalidContentLengthHeaderK  s    z,ParsingTests.test_invalidContentLengthHeaderc                 C   s   |   ddddg¡ dS )z
        If a header without colon is received a 400 (Bad Request) response
        is sent to the client and the connection is closed.
        r£  s   HeaderName rÂ   Nr«  r<   r-   r-   r.   Útest_invalidHeaderNoColonT  s    z&ParsingTests.test_invalidHeaderNoColonc                    s”   g ‰ G ‡ fdd„dt jƒ}|  t jdd¡ ddddddddg}|  d	 |¡|d
¡}ˆ \}}|  | d¡d¡ |  | d¡d¡ |  |j 	¡ d¡ dS )zÛ
        C{HTTPChannel} enforces the limit of C{HTTPChannel.maxHeaders} per
        request so that headers received in an earlier request do not count
        towards the limit when processing a later request.
        c                       s   e Zd Z‡ fdd„ZdS )z:ParsingTests.test_headerLimitPerRequest.<locals>.MyRequestc                    s   ˆ   | ¡ |  ¡  d S r/   rŽ  r<   r  r-   r.   r>   d  s    
zBParsingTests.test_headerLimitPerRequest.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘  c  s   r‘  r¨  r|   s   GET / HTTP/1.1r¤  rÂ   s   Bar: bazrV   r   r@  r™  ó   bazóh   HTTP/1.1 200 OK
Transfer-Encoding: chunked

0

HTTP/1.1 200 OK
Transfer-Encoding: chunked

0

N)
r   rä   Úpatchr‘   r  rs  rD   r[   rs   r™   )r=   r‘  r“  rk   ÚfirstÚsecondr-   r  r.   Útest_headerLimitPerRequest\  s(    ø
þz'ParsingTests.test_headerLimitPerRequestc                    s`   g ‰ G ‡ fdd„dt jƒ}t  ¡ }d|_d}| j|||dd}|  ˆ g ¡ |  |j ¡ d¡ dS )	z¥
        Enforces a limit of C{HTTPChannel.totalHeadersSize}
        on the size of headers received per request starting from initial
        command line.
        c                       s   e Zd Z‡ fdd„ZdS )z@ParsingTests.test_headersTooBigInitialCommand.<locals>.MyRequestc                    s   ˆ   | ¡ |  ¡  d S r/   rŽ  r<   r  r-   r.   r>     s    
zHParsingTests.test_headersTooBigInitialCommand.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘  Œ  s   r‘  rq   s"   GET /path/longer/than/10 HTTP/1.1
F©r‹  r“   rk   rŒ  rÐ   N©r   rä   r‘   ÚtotalHeadersSizer  rD   rs   r™   ©r=   r‘  rk   r‹  r-   r  r.   Ú test_headersTooBigInitialCommand…  s     üþz-ParsingTests.test_headersTooBigInitialCommandc                    s`   g ‰ G ‡ fdd„dt jƒ}t  ¡ }d|_d}| j|||dd}|  ˆ g ¡ |  |j ¡ d¡ dS )	z¨
        Enforces a limit of C{HTTPChannel.totalHeadersSize}
        on the size of headers received per request counting first line
        and total headers.
        c                       s   e Zd Z‡ fdd„ZdS )z>ParsingTests.test_headersTooBigOtherHeaders.<locals>.MyRequestc                    s   ˆ   | ¡ |  ¡  d S r/   rŽ  r<   r  r-   r.   r>   ª  s    
zFParsingTests.test_headersTooBigOtherHeaders.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘  ©  s   r‘  é(   s5   GET /less/than/40 HTTP/1.1
Some-Header: less-than-40
Fr´  rÐ   Nrµ  r·  r-   r  r.   Útest_headersTooBigOtherHeaders¢  s"    ÿ ýþz+ParsingTests.test_headersTooBigOtherHeadersc                 C   sP   G dd„ dt jƒ}t  ¡ }d|_||_d}| j||dd}|  |j ¡ d¡ dS )	z€
        Enforces total size of headers per individual request and counter
        is reset at the end of each request.
        c                   @   s   e Zd Zdd„ ZdS )z@ParsingTests.test_headersTooBigPerRequest.<locals>.SimpleRequestc                 S   s   |   ¡  d S r/   )rd   r<   r-   r-   r.   r>   Ç  s    zHParsingTests.test_headersTooBigPerRequest.<locals>.SimpleRequest.processNrh   r-   r-   r-   r.   ÚSimpleRequestÆ  s   r»  r²   sZ   GET / HTTP/1.1
Some-Header: total-less-than-60

GET / HTTP/1.1
Some-Header: less-than-60

F)r‹  rk   rŒ  r¯  N)	r   rä   r‘   r¶  r“   r  rD   rs   r™   )r=   r»  rk   r‹  r-   r-   r.   Útest_headersTooBigPerRequestÁ  s    ÿ	  ÿþz)ParsingTests.test_headersTooBigPerRequestc                    sH   d}i ‰ | ‰G ‡ ‡fdd„dt jƒ}|  ||¡ |  ˆ ddddœ¡ dS )	z3
        Test cookies parsing and reading.
        sK   GET / HTTP/1.0
Cookie: rabbit="eat carrot"; ninja=secret; spam="hey 1=1!"

c                       s   e Zd Z‡ ‡fdd„ZdS )z+ParsingTests.testCookies.<locals>.MyRequestc                    s*   dD ]}|   |¡ˆ |< qdˆ_|  ¡  d S )N©s   rabbits   ninjas   spamT)Z	getCookier‰  rd   )r=   Úname©ÚcookiesÚtestcaser-   r.   r>   ó  s    z3ParsingTests.testCookies.<locals>.MyRequest.processNrh   r-   r¿  r-   r.   r‘  ò  s   r‘  s   "eat carrot"s   secrets
   "hey 1=1!"r½  N©r   rä   r  rD   ©r=   r‹  r‘  r-   r¿  r.   ÚtestCookiesç  s    ýÿzParsingTests.testCookiesc                    sb   d}g ‰g ‰ | ‰G ‡ ‡‡fdd„dt jƒ}|  ||¡ |  ˆdg¡ |  ˆ dgdgddgg¡ d S )	NsJ   GET /?key=value&multiple=two+words&multiple=more%20words&empty= HTTP/1.0

c                       s   e Zd Z‡ ‡‡fdd„ZdS )z'ParsingTests.testGET.<locals>.MyRequestc                    s@   ˆ  | j¡ ˆ  | jd | jd | jd g¡ dˆ_|  ¡  d S ©Ns   keys   emptys   multipleT)rí   r`   r©  r0   r‰  rd   r<   ©r0   r`   rÁ  r-   r.   r>     s    ýz/ParsingTests.testGET.<locals>.MyRequest.processNrh   r-   rÆ  r-   r.   r‘  
  s   r‘  ro   ó   valuerÂ   ó	   two wordsó
   more wordsrÂ  rÃ  r-   rÆ  r.   ÚtestGET  s    
 ÿzParsingTests.testGETc                    sp   d}g ‰g ‰g ‰ | ‰G ‡ ‡‡‡fdd„dt jƒ}|  ||¡ |  ˆdg¡ |  ˆdg¡ |  ˆ dgdgg¡ dS )	zÝ
        While only a single '?' is allowed in an URL, several other servers
        allow several and pass all after the first through as part of the
        query arguments.  Test that we emulate this behavior.
        s"   GET /foo?bar=?&baz=quux HTTP/1.0

c                       s   e Zd Z‡ ‡‡‡fdd„ZdS )z6ParsingTests.test_extraQuestionMark.<locals>.MyRequestc                    sD   ˆ  | j¡ ˆ  | j¡ ˆ  | jd | jd g¡ dˆ_|  ¡  d S )Nr™  r®  T)rí   r`   Úpathr©  r0   r‰  rd   r<   ©r0   r`   rË  rÁ  r-   r.   r>   '  s
    z>ParsingTests.test_extraQuestionMark.<locals>.MyRequest.processNrh   r-   rÌ  r-   r.   r‘  &  s   r‘  ro   s   /fooó   ?r¥  NrÂ  rÃ  r-   rÌ  r.   Útest_extraQuestionMark  s    z#ParsingTests.test_extraQuestionMarkc                    sŽ   d}t dt|ƒ|f ƒ}g ‰g ‰ g ‰| ‰G ‡ ‡‡‡fdd„dtjƒ}|  ||¡ |  ˆdg¡ |  ˆ dgdgdd	gg¡ |  ˆt |ƒg¡ d
S )a]  
        The request body of a I{POST} request with a I{Content-Type} header
        of I{application/x-www-form-urlencoded} is parsed according to that
        content type and made available in the C{args} attribute of the
        request object.  The original bytes of the request may still be read
        from the C{content} attribute.
        z9key=value&multiple=two+words&multiple=more%20words&empty=zVPOST / HTTP/1.0
Content-Length: %d
Content-Type: application/x-www-form-urlencoded

%sc                       s   e Zd Z‡ ‡‡‡fdd„ZdS )z4ParsingTests.test_formPOSTRequest.<locals>.MyRequestc                    sP   ˆ  | j¡ ˆ  | jd | jd | jd g¡ ˆ  | j ¡ ¡ dˆ_|  ¡  d S rÅ  )rí   r`   r©  r0   rX   rZ   r‰  rd   r<   ©r0   rX   r`   rÁ  r-   r.   r>   I  s     þz<ParsingTests.test_formPOSTRequest.<locals>.MyRequest.processNrh   r-   rÏ  r-   r.   r‘  H  s   r‘  s   POSTrÇ  rÂ   rÈ  rÉ  N)r   rb   r   rä   r  rD   )r=   Úqueryr‹  r‘  r-   rÏ  r.   Útest_formPOSTRequest4  s     
û
 ÿz!ParsingTests.test_formPOSTRequestc                 C   s,   d}| j |tjdd}|  |j ¡ d¡ dS )zr
        If the C{Content-Disposition} header is missing, the request is denied
        as a bad request.
        sº   POST / HTTP/1.0
Content-Type: multipart/form-data; boundary=AaB03x
Content-Length: 103

--AaB03x
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

abasdfg
--AaB03x--
Fr’  rÐ   N©r  r   rä   rD   rs   r™   ©r=   ré   rk   r-   r-   r.   Útest_missingContentDisposition[  s    þz+ParsingTests.test_missingContentDispositionz<cgi.parse_multipart is much more error-tolerant on Python 3.c                 C   s,   d}| j |tjdd}|  |j ¡ d¡ dS )zX
        When the multipart processing fails the client gets a 400 Bad Request.
        s  POST / HTTP/1.0
Content-Type: multipart/form-data; boundary=â˜ƒ
Content-Length: 103

--â˜ƒ
Content-Type: text/plain
Content-Length: 999999999999999999999999999999999999999999999999999999999999999
Content-Transfer-Encoding: quoted-printable

abasdfg
--â˜ƒ--
Fr’  rÐ   NrÒ  rÓ  r-   r-   r.   Útest_multipartProcessingFailurev  s    þz,ParsingTests.test_multipartProcessingFailurec                    sV   g ‰ G ‡ fdd„dt jƒ}d}| j||dd}|  |j ¡ d¡ |  ˆ d ji ¡ dS )	zS
        When the multipart does not contain a header is should be skipped
        c                       s   e Zd Z‡ fdd„ZdS )zJParsingTests.test_multipartEmptyHeaderProcessingFailure.<locals>.MyRequestc                    s    ˆ   | ¡ |  d¡ |  ¡  d S ©Ns   done©rí   rc   rd   r<   r  r-   r.   r>   –  s    

zRParsingTests.test_multipartEmptyHeaderProcessingFailure.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘  •  s   r‘  sr   POST / HTTP/1.0
Content-Type: multipart/form-data; boundary=AaBb1313
Content-Length: 14

--AaBb1313

--AaBb1313--
Fr’  ó   HTTP/1.0 200 OK

doner   N)r   rä   r  rD   rs   r™   r0   ©r=   r‘  ré   rk   r-   r  r.   Ú*test_multipartEmptyHeaderProcessingFailure  s    	z7ParsingTests.test_multipartEmptyHeaderProcessingFailurec                    sl   g ‰ G ‡ fdd„dt jƒ}d}| j||dd}|  |j ¡ d¡ |  tˆ ƒd¡ |  ˆ d jd	d
gi¡ dS )z¶
        If the request has a Content-Type of C{multipart/form-data}, and the
        form data is parseable, the form arguments will be added to the
        request's args.
        c                       s   e Zd Z‡ fdd„ZdS )z6ParsingTests.test_multipartFormData.<locals>.MyRequestc                    s    ˆ   | ¡ |  d¡ |  ¡  d S rÖ  r×  r<   r  r-   r.   r>   ²  s    

z>ParsingTests.test_multipartFormData.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘  ±  s   r‘  sæ   POST / HTTP/1.0
Content-Type: multipart/form-data; boundary=AaB03x
Content-Length: 149

--AaB03x
Content-Type: text/plain
Content-Disposition: form-data; name="text"
Content-Transfer-Encoding: quoted-printable

abasdfg
--AaB03x--
Fr’  rØ  r|   r   s   textó   abasdfgN)r   rä   r  rD   rs   r™   rb   r0   rÙ  r-   r  r.   Útest_multipartFormDataª  s    ÿz#ParsingTests.test_multipartFormDatac                    s’   g ‰ G ‡ fdd„dt jƒ}d}dtt| dd¡ƒƒ d }| j| d¡| |d	d
}|  |j 	¡ d¡ |  tˆ ƒd¡ |  ˆ d j
ddgi¡ dS )zÈ
        If the request has a Content-Type of C{multipart/form-data},
        and the form data is parseable and contains files, the file
        portions will be added to the request's args.
        c                       s   e Zd Z‡ fdd„ZdS )z6ParsingTests.test_multipartFileData.<locals>.MyRequestc                    s    ˆ   | ¡ |  d¡ |  ¡  d S rÖ  r×  r<   r  r-   r.   r>   Ò  s    

z>ParsingTests.test_multipartFileData.<locals>.MyRequest.processNrh   r-   r  r-   r.   r‘  Ñ  s   r‘  sé   -----------------------------738837029596785559389649595
Content-Disposition: form-data; name="uploadedfile"; filename="test"
Content-Type: application/octet-stream

abasdfg
-----------------------------738837029596785559389649595--
zƒPOST / HTTP/1.0
Content-Type: multipart/form-data; boundary=---------------------------738837029596785559389649595
Content-Length: rV   ra  z


ÚasciiFr’  rØ  r|   r   s   uploadedfilerÛ  N)r   rä   r\   rb   rŠ  r  r]   rD   rs   r™   r0   )r=   r‘  rK  ré   rk   r-   r  r.   Útest_multipartFileDataÊ  s     ýýÿÿz#ParsingTests.test_multipartFileDatac                    s   d}g ‰g ‰g ‰ g ‰| ‰G ‡ ‡‡‡‡fdd„dt jƒ}|  ||¡ |  ˆ d t¡ |  ˆ d d¡ |  ˆdg¡ |  ˆdg¡ |  ˆd	g¡ d	S )
z¡
        If a request uses the I{chunked} transfer encoding, the request body is
        decoded accordingly before it is made available on the request.
        sh   GET / HTTP/1.0
Content-Type: text/plain
Transfer-Encoding: chunked

6
Hello,
14
 spam,eggs spam spam
0

c                       s    e Zd Z‡ ‡‡‡‡fdd„ZdS )z4ParsingTests.test_chunkedEncoding.<locals>.MyRequestc                    sX   ˆ   | j ¡ ¡ ˆ   | j ¡ ¡ ˆ  | j¡ ˆ  | j¡ ˆ  | jj¡ dˆ_|  	¡  d S rm  )
rí   rX   ÚfilenorZ   r`   rË  rk   Z_transferDecoderr‰  rd   r<   ©rX   r:  r`   rË  rÁ  r-   r.   r>     s    z<ParsingTests.test_chunkedEncoding.<locals>.MyRequest.processNrh   r-   rà  r-   r.   r‘    s   r‘  r   r|   s   Hello, spam,eggs spam spamro   ó   /N)r   rä   r  ÚassertIsInstanceÚintrD   rÃ  r-   rà  r.   Útest_chunkedEncodingî  s    
z!ParsingTests.test_chunkedEncodingc                    s^   d}g ‰ G ‡ fdd„dt jƒ}| j||dd}|  ˆ d¡ |  |j ¡ d¡ |  |jj¡ dS )	z
        If a request uses the I{chunked} transfer encoding, but provides an
        invalid chunk length value, the request fails with a 400 error.
        sˆ   GET / HTTP/1.1
Content-Type: text/plain
Transfer-Encoding: chunked

MALFORMED_LINE_THIS_SHOULD_BE_'6'
Hello,
14
 spam,eggs spam spam
0

c                       s   e Zd Z‡ fdd„ZdS )z=ParsingTests.test_malformedChunkedEncoding.<locals>.MyRequestc                    s   ˆ   d¡ d S rm  ©rí   r<   rˆ  r-   r.   r>   2  s    zEParsingTests.test_malformedChunkedEncoding.<locals>.MyRequest.processNrh   r-   rˆ  r-   r.   r‘  0  s   r‘  Fr’  úRequest.process calledrÐ   N)	r   rä   r  r¦   rD   rs   r™   r­   r§   )r=   r‹  r‘  rk   r-   rˆ  r.   Útest_malformedChunkedEncoding  s    þz*ParsingTests.test_malformedChunkedEncodingc           	         sÄ   t  | t¡}g ‰ G ‡ fdd„dtjƒ}d}d}t d ||f¡¡ ¡ }d| d }|  	tdd	d
„ ¡ |  
||d¡ ˆ  ¡ }|  d|j¡ |  dt|ƒ¡ |d }|d }|  |jt¡ |  t¡ dS )z•
        A L{Request} that throws an exception processing basic authorization
        logs an error and uses an empty username and password.
        c                       s   e Zd Z‡ fdd„ZdS )z5ParsingTests.test_basicAuthException.<locals>.Requestc                    s    |   ¡ |  ¡ f| _ˆ  | ¡ d S r/   r–  r<   r˜  r-   r.   r>   I  s    z=ParsingTests.test_basicAuthException.<locals>.Request.processNrh   r-   r˜  r-   r.   rä   H  s   rä   r@  r™  rš  r›  rœ  Zdecodestringc                 S   s   g S r/   r-   )rÛ   r-   r-   r.   ri  Q  rÂ   z6ParsingTests.test_basicAuthException.<locals>.<lambda>r   )Ú rè  r|   Úlog_failureN)r!   r¶   r'   r   rä   r  rž  rs  rŸ  r°  r  r€   rD   r—  r¸   rb   râ  r™   ÚAttributeErrorÚflushLoggedErrors)	r=   rº   rä   r   rU  r_  r¡  ré   r»   r-   r˜  r.   Útest_basicAuthException>  s&    þz$ParsingTests.test_basicAuthExceptionc                    sX   G ‡ fdd„dt jƒ‰ | j|ˆ dd}|  ˆ jd¡ |  |j ¡ d¡ |  |jj	¡ dS )zÉ
        Assert that the given request bytes fail with a 400 bad
        request without calling L{Request.process}.

        @param request: A raw HTTP request
        @type request: L{bytes}
        c                       s   e Zd ZdZ‡ fdd„ZdS )zAParsingTests.assertDisconnectingBadRequest.<locals>.FailedRequestFc                    s
   dˆ _ d S rm  r  r<   ©ÚFailedRequestr-   r.   r>   f  s    zIParsingTests.assertDisconnectingBadRequest.<locals>.FailedRequest.processN©r)   r*   r+   r  r>   r-   rí  r-   r.   rî  d  s   rî  Fr’  ræ  rÐ   N)
r   rä   r  r¦   r  rD   rs   r™   r­   r§   )r=   rg   rk   r-   rí  r.   ÚassertDisconnectingBadRequest\  s    þz*ParsingTests.assertDisconnectingBadRequestc                 C   s   |   ddddddg¡ dS )z“
        A request which includes multiple C{content-length} headers
        fails with a 400 response without calling L{Request.process}.
        ó   GET /a HTTP/1.1ó   Content-Length: 56ó   Content-Length: 0ó   Host: host.invalidrÂ   Nr«  r<   r-   r-   r.   Útest_duplicateContentLengthsq  s    úz)ParsingTests.test_duplicateContentLengthsc                 C   s"   |   ddddddddddg
¡ dS )z´
        Two pipelined requests, the first of which includes multiple
        C{content-length} headers, trigger a 400 response without
        calling L{Request.process}.
        rñ  rò  ró  rô  rÂ   Nr«  r<   r-   r-   r.   Ú1test_duplicateContentLengthsWithPipelinedRequests€  s    öz>ParsingTests.test_duplicateContentLengthsWithPipelinedRequestsc                 C   s   |   ddddddg¡ dS )z¯
        A request that includes both C{content-length} and
        C{transfer-encoding} headers fails with a 400 response without
        calling L{Request.process}.
        rñ  r„  ró  rô  rÂ   Nr«  r<   r-   r-   r.   Ú%test_contentLengthAndTransferEncoding”  s    úz2ParsingTests.test_contentLengthAndTransferEncodingc                 C   s"   |   ddddddddddg
¡ dS )zÊ
        Two pipelined requests, the first of which includes both
        C{content-length} and C{transfer-encoding} headers, triggers a
        400 response without calling L{Request.process}.
        rñ  r„  ró  rô  rÂ   Nr«  r<   r-   r-   r.   Ú:test_contentLengthAndTransferEncodingWithPipelinedRequests¤  s    özGParsingTests.test_contentLengthAndTransferEncodingWithPipelinedRequestsc                 C   s   |   dddddg¡ dS )zÁ
        A request whose C{transfer-encoding} header includes a value
        other than C{chunked} or C{identity} fails with a 400 response
        without calling L{Request.process}.
        rñ  s   Transfer-Encoding: unknownrô  rÂ   Nr«  r<   r-   r-   r.   Útest_unknownTransferEncoding¸  s    ûz)ParsingTests.test_unknownTransferEncodingc                    sP   g ‰ G ‡ fdd„dt jƒ}d}|  ||d¡}|  ˆ dg¡ |  |j ¡ d¡ dS )z‚
        A request with a valid C{content-length} and a
        C{transfer-encoding} whose value is C{identity} succeeds.
        c                       s   e Zd ZdZ‡ fdd„ZdS )zEParsingTests.test_transferEncodingIdentity.<locals>.SuccessfulRequestFc                    s(   ˆ   | j ¡ ¡ |  dd¡ |  ¡  d S )NrU   ó   0)rí   rX   rZ   r^   rd   r<   ©rK  r-   r.   r>   Ð  s    zMParsingTests.test_transferEncodingIdentity.<locals>.SuccessfulRequest.processNrï  r-   rû  r-   r.   ÚSuccessfulRequestÎ  s   rü  sT   GET / HTTP/1.1
Host: host.invalid
Content-Length: 2
Transfer-Encoding: identity

ok
Fs   oks&   HTTP/1.1 200 OK
Content-Length: 0

N)r   rä   r  rD   rs   r™   )r=   rü  rg   rk   r-   rû  r.   Útest_transferEncodingIdentityÇ  s    þz*ParsingTests.test_transferEncodingIdentity)NTN)&r)   r*   r+   r,   r,  r  r”  r•  r¢  r§  rª  r¬  r­  r³  r¸  rº  r¼  rÄ  rÊ  rÎ  rÑ  rÔ  r
   r  rÕ  rÚ  rÜ  rÞ  rä  rç  rì  rð  rõ  rö  r÷  rø  rù  rý  r-   r-   r-   r.   r‡  ¬  sL     ÿ
-	)&'ÿ $."r‡  c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚQueryArgumentsTestsc                 C   sh   |   t d¡t d¡¡ | jttjddd |   tjdddtjddd¡ |   t d¡t d¡¡ d S )Ns   a=b&d=c;+=fs   blahT)Zstrict_parsings   a=&b=cr|   )Zkeep_blank_values)rD   ÚcgiZparse_qsr   rü   rý   r<   r-   r-   r.   ÚtestParseqsç  s$    þ   ÿþþzQueryArgumentsTests.testParseqsc                    s   dd„ }‡ fdd„}t ƒ  |ƒ D ]}||dƒ ||dƒ q t ƒ  |ƒ D ]}||dƒ ||dƒ qF|ƒ D ]$}t ƒ  ||dƒ t ƒ  ||dƒ qfdS )z—
        For a given URL, L{http.urlparse} should behave the same as L{urlparse},
        except it should always return C{bytes}, never text.
        c               
   s   s\   dD ]R} dD ]H}dD ]>}dD ]4}|d k	r|d t t|ƒƒ }t| ||ddfƒV  qqqqd S )N)s   https   https)ó   example.com)Nr    )rÂ   s   pathrš  rÂ   )r   r\   r   )ÚschemeZhostZportrË  r-   r-   r.   Úurlsú  s    z/QueryArgumentsTests.test_urlparse.<locals>.urlsc                    sþ   | }|r|   d¡}t|ƒ}t|jtƒrh|j d¡|j d¡|j d¡|j d¡|j	 d¡|j
 d¡f}n|j|j|j|j|j	|j
f}t | ¡\}}}}}	}
ˆ  |||||	|
f|¡ ˆ  |t¡ ˆ  |t¡ ˆ  |t¡ ˆ  |t¡ ˆ  |	t¡ ˆ  |
t¡ dS )z‚
            Verify that C{url} is parsed into the same objects by both
            L{http.urlparse} and L{urlparse}.
            rÝ  zutf-8N)Údecoder   Ú
isinstancer  r   r]   ÚnetlocrË  ÚparamsrÐ  Úfragmentr   rD   râ  rB  )Úurlr  ZurlToStandardImplementationZstandardResultr‚   r  r  rË  r  rÐ  r  r<   r-   r.   ÚassertSameParsing	  s<    






ûû ÿz<QueryArgumentsTests.test_urlparse.<locals>.assertSameParsingTFN)r   )r=   r  r
  r	  r-   r<   r.   Útest_urlparseõ  s    
)





z!QueryArgumentsTests.test_urlparsec                 C   s   |   ttjd¡ dS )zE
        L{http.urlparse} should reject unicode input early.
        zhttp://example.org/pathN)rü   Ú	TypeErrorr   r   r<   r-   r-   r.   Útest_urlparseRejectsUnicode@	  s    z/QueryArgumentsTests.test_urlparseRejectsUnicodeN)r)   r*   r+   r   r  r  r-   r-   r-   r.   rþ  æ  s   Krþ  c                   @   s   e Zd Zdd„ ZdS )ÚClientDriverc                 C   s   || _ || _|| _d S r/   )r  r‡   r  r  r-   r-   r.   r  I	  s    zClientDriver.handleStatusN)r)   r*   r+   r  r-   r-   r-   r.   r  H	  s   r  c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚClientStatusParsingTestsc                 C   s>   t ƒ }| d¡ |  |jd¡ |  |jd¡ |  |jd¡ d S )Ns   HTTP/1.0 201 foor  ó   201r@  ©r  ÚlineReceivedrD   r  r‡   r  ©r=   r|  r-   r-   r.   ÚtestBaselineO	  s
    
z%ClientStatusParsingTests.testBaselinec                 C   s>   t ƒ }| d¡ |  |jd¡ |  |jd¡ |  |jd¡ d S )Nó   HTTP/1.0 201r  r  rÂ   r  r  r-   r-   r.   ÚtestNoMessageV	  s
    
z&ClientStatusParsingTests.testNoMessagec                 C   s>   t ƒ }| d¡ |  |jd¡ |  |jd¡ |  |jd¡ d S )Ns   HTTP/1.0 201 r  r  rÂ   r  r  r-   r-   r.   ÚtestNoMessage_trailingSpace]	  s
    
z4ClientStatusParsingTests.testNoMessage_trailingSpaceN)r)   r*   r+   r  r  r  r-   r-   r-   r.   r  N	  s   r  c                   @   sX  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%dFdG„ Z&dHdI„ Z'dJdK„ Z(dLdM„ Z)dNdO„ Z*dPdQ„ Z+dRdS„ Z,dTdU„ Z-dVdW„ Z.dXdY„ Z/dZd[„ Z0d\d]„ Z1d^d_„ Z2d`da„ Z3dbdc„ Z4ddde„ Z5dfdg„ Z6dhdi„ Z7djdk„ Z8dldm„ Z9dndo„ Z:dpdq„ Z;drds„ Z<dtdu„ Z=dvdw„ Z>dxdy„ Z?dzd{„ Z@d|d}„ ZAd~d„ ZBd€d„ ZCd‚dƒ„ ZDd„d…„ ZEd†d‡„ ZFdˆd‰„ ZGdŠd‹„ ZHdŒd„ ZIdŽd„ ZJdd‘„ ZKd’d“„ ZLd”S )•ÚRequestTestsz#
    Tests for L{http.Request}
    c                 C   s   t  tƒ d¡}t||ƒ ddg¡ |  t||ƒd d¡ t||ddiƒ |  tt||ƒ ¡ ƒddgfg¡ t||t	 
¡ ƒ |  t||ƒi ¡ dS )a0  
        Verify that each of two different attributes which are associated with
        the same state properly reflect changes made through the other.

        This is used to test that the C{headers}/C{responseHeaders} and
        C{received_headers}/C{requestHeaders} pairs interact properly.
        Fr~  r  r@  r™  r  N)r   rä   r#   Úgetattrr(  rD   Úsetattrr/  r1  r   ZHeaders)r=   ZoldNameZnewNameré   r-   r-   r.   Ú_compatHeadersTestj	  s    
þzRequestTests._compatHeadersTestc                 C   s4   t  tƒ d¡}|j ddg¡ |  | d¡d¡ dS )zb
        L{http.Request.getHeader} returns the value of the named request
        header.
        Fr~  r  N©r   rä   r#   r'  r(  rD   r[   ©r=   ré   r-   r-   r.   Útest_getHeader}	  s    zRequestTests.test_getHeaderc                 C   s6   t  tƒ d¡}|j dddg¡ |  | d¡d¡ dS )z‡
        When there are multiple values for a single request header,
        L{http.Request.getHeader} returns the last value.
        Fr~  r  ó   pandaNr  r  r-   r-   r.   Útest_getHeaderReceivedMultiples‡	  s    z,RequestTests.test_getHeaderReceivedMultiplesc                 C   s$   t  tƒ d¡}|  | d¡d¡ dS )z†
        L{http.Request.getHeader} returns L{None} when asked for the value of a
        request header which is not present.
        Fr~  N)r   rä   r#   rD   r[   r  r-   r-   r.   Útest_getHeaderNotFound‘	  s    z#RequestTests.test_getHeaderNotFoundc                 C   s6   t  tƒ d¡}|j ddg¡ |  | ¡ ddi¡ dS )z‰
        L{http.Request.getAllheaders} returns a C{dict} mapping all request
        header names to their corresponding values.
        Fr~  r  N©r   rä   r#   r'  r(  rD   ÚgetAllHeadersr  r-   r-   r.   Útest_getAllHeadersš	  s    zRequestTests.test_getAllHeadersc                 C   s"   t  tƒ d¡}|  | ¡ i ¡ dS )zq
        L{http.Request.getAllHeaders} returns an empty C{dict} if there are no
        request headers.
        FN)r   rä   r#   rD   r#  r  r-   r-   r.   Útest_getAllHeadersNoHeaders¤	  s    z(RequestTests.test_getAllHeadersNoHeadersc                 C   s8   t  tƒ d¡}|j dddg¡ |  | ¡ ddi¡ dS )z
        When there are multiple values for a single request header,
        L{http.Request.getAllHeaders} returns only the last value.
        Fr~  r  r  Nr"  r  r-   r-   r.   Ú!test_getAllHeadersMultipleHeaders­	  s    z.RequestTests.test_getAllHeadersMultipleHeadersc                 C   sF   t ƒ }t |d¡}| d¡ | d¡ |  |jj ¡  	¡ d d¡ dS )z~
        L{http.Request.setResponseCode} takes a status code and causes it to be
        used as the response status.
        FéÉ   rÂ   r   s    (no clientproto yet) 201 CreatedN©
r#   r   rä   r:   rc   rD   rs   ÚwrittenÚgetvaluer   ©r=   rk   ré   r-   r-   r.   Útest_setResponseCode·	  s    

þz!RequestTests.test_setResponseCodec                 C   sH   t ƒ }t |d¡}| dd¡ | d¡ |  |jj ¡  	¡ d d¡ dS )zŽ
        L{http.Request.setResponseCode} takes a status code and a message and
        causes them to be used as the response status.
        FéÊ   s   happily acceptedrÂ   r   s)   (no clientproto yet) 202 happily acceptedNr(  r+  r-   r-   r.   Útest_setResponseCodeAndMessageÅ	  s    
þz+RequestTests.test_setResponseCodeAndMessagec                 C   s(   t ƒ }t |d¡}|  t|jdd¡ dS )z•
        L{http.Request.setResponseCode} accepts C{bytes} for the message
        parameter and raises L{TypeError} if passed anything else.
        Fr-  znot happily acceptedN)r#   r   rä   rü   r  r:   r+  r-   r-   r.   Ú&test_setResponseCodeAndMessageNotBytesÓ	  s    
 ÿz3RequestTests.test_setResponseCodeAndMessageNotBytesc                 C   s,   t  tƒ d¡}| d¡ |  t|jd¡ dS )z
        L{http.Request.setResponseCode} accepts C{int} for the code parameter
        and raises L{TypeError} if passed anything else.
        Fr|   Ú1N)r   rä   r#   r:   rü   r  r  r-   r-   r.   Ú#test_setResponseCodeAcceptsIntegersÞ	  s    
z0RequestTests.test_setResponseCodeAcceptsIntegersc                 C   s    t  tƒ d¡}| tdƒ¡ dS )za
        L{http.Request.setResponseCode} accepts C{long} for the code
        parameter.
        Fr|   N)r   rä   r#   r:   r   r  r-   r-   r.   Ú'test_setResponseCodeAcceptsLongIntegersè	  s    z4RequestTests.test_setResponseCodeAcceptsLongIntegersc                 C   s*   t  tƒ d¡}| d¡ |  |jd¡ dS )zì
        When no previous value was set and no 'if-modified-since' value was
        requested, L{http.Request.setLastModified} takes a timestamp in seconds
        since the epoch and sets the request's lastModified attribute.
        Fé*   N©r   rä   r#   ÚsetLastModifiedrD   ÚlastModifiedr  r-   r-   r.   Útest_setLastModifiedNeverSetñ	  s    
z)RequestTests.test_setLastModifiedNeverSetc                 C   s4   t  tƒ d¡}| d¡ | d¡ |  |jd¡ dS )z°
        If the supplied timestamp is later than the lastModified attribute's
        value, L{http.Request.setLastModified} updates the lastModifed
        attribute.
        Fr   r|   Nr4  r  r-   r-   r.   Útest_setLastModifiedUpdateþ	  s    

z'RequestTests.test_setLastModifiedUpdatec                 C   s4   t  tƒ d¡}| d¡ | d¡ |  |jd¡ dS )z—
        If the supplied timestamp occurs earlier than the current lastModified
        attribute, L{http.Request.setLastModified} ignores it.
        Fr|   r   Nr4  r  r-   r-   r.   Útest_setLastModifiedIgnore
  s    

z'RequestTests.test_setLastModifiedIgnorec                 C   s>   t  tƒ d¡}|j tdƒdg¡ | d¡}|  |t j¡ dS )z¡
        If the resource is older than the if-modified-since date in the request
        header, L{http.Request.setLastModified} returns L{http.CACHED}.
        Fúif-modified-sinces   02 Jan 1970 00:00:00 GMTr3  N©	r   rä   r#   r'  r(  r   r5  rD   ZCACHED©r=   ré   rz  r-   r-   r.   Útest_setLastModifiedCached
  s    þ
z'RequestTests.test_setLastModifiedCachedc                 C   s<   t  tƒ d¡}|j tdƒdg¡ | d¡}|  |d¡ dS )z–
        If the resource is newer than the if-modified-since date in the request
        header, L{http.Request.setLastModified} returns None
        Fr:  s   01 Jan 1970 00:00:00 GMTé@B N©r   rä   r#   r'  r(  r   r5  rD   r<  r-   r-   r.   Útest_setLastModifiedNotCached)
  s    þ
z*RequestTests.test_setLastModifiedNotCachedc                 C   sF   t  tƒ d¡}|j tdƒdg¡ | d¡ | d¡}|  |d¡ dS )zï
        When L{http.Request.setLastModified} is called multiple times, the
        highest supplied value is honored. If that value is higher than the
        if-modified-since date in the request header, the method returns None.
        Fr:  s   01 Jan 1970 00:00:01 GMTr>  r   Nr?  r<  r-   r-   r.   Ú"test_setLastModifiedTwiceNotCached9
  s    þ

z/RequestTests.test_setLastModifiedTwiceNotCachedc                 C   sH   t  tƒ d¡}|j tdƒdg¡ | d¡ | d¡}|  |t j¡ dS )a   
        When L{http.Request.setLastModified} is called multiple times, the
        highest supplied value is honored. If that value is lower than the
        if-modified-since date in the request header, the method returns
        L{http.CACHED}.
        Fr:  s   01 Jan 1999 00:00:01 GMTr|   r   Nr;  r<  r-   r-   r.   Útest_setLastModifiedTwiceCachedK
  s    þ

z,RequestTests.test_setLastModifiedTwiceCachedc                 C   s4   t  tƒ d¡}| dd¡ |  |j d¡dg¡ dS )ú”
        L{http.Request.setHost} sets the value of the host request header.
        The port should not be added because it is the default.
        Fr  éP   ó   hostN©r   rä   r#   ÚsetHostrD   r'  r¦  r  r-   r-   r.   Útest_setHost^
  s    
 ÿzRequestTests.test_setHostc                 C   sB   t ƒ }t  ¡ |_t |d¡}| dd¡ |  |j d¡dg¡ dS )rC  Fr  i»  rE  N©	r#   ræ   rs   r   rä   rG  rD   r'  r¦  ©r=   r#  ré   r-   r-   r.   Útest_setHostSSLi
  s    

 ÿzRequestTests.test_setHostSSLc                 C   s4   t  tƒ d¡}| dd¡ |  |j d¡dg¡ dS ©z”
        L{http.Request.setHost} sets the value of the host request header.
        The port should be added because it is not the default.
        Fr  éQ   rE  s   example.com:81NrF  r  r-   r-   r.   Útest_setHostNonDefaultPortv
  s    
 ÿz'RequestTests.test_setHostNonDefaultPortc                 C   sB   t ƒ }t  ¡ |_t |d¡}| dd¡ |  |j d¡dg¡ dS rL  rI  rJ  r-   r-   r.   Útest_setHostSSLNonDefaultPort
  s    

 ÿz*RequestTests.test_setHostSSLNonDefaultPortc                 C   s4   t  tƒ d¡}| dd¡ |  |j d¡dg¡ dS )zX
        L{http.Request.setHeader} sets the value of the given response header.
        Fr~  r  N)r   rä   r#   r^   rD   r0  r¦  r  r-   r-   r.   Útest_setHeaderŽ
  s    zRequestTests.test_setHeaderc                 O   s‚   t ƒ }t |d¡}|j||Ž |  |jd |¡ | d¡ |jj 	¡  
d¡}dd„ |D ƒ}|  t|ƒd¡ |  |d d| ¡ d	S )
z›
        Call L{http.Request.setCookie} with C{*args} and C{**kwargs}, and check
        that the cookie value is equal to C{expectedCookieValue}.
        Fr   rÂ   ra  c                 S   s   g | ]}|  d ¡r|‘qS )s
   Set-Cookie)Ú
startswith)rx  rÛ   r-   r-   r.   ry  ¦
  s    
ÿz-RequestTests._checkCookie.<locals>.<listcomp>r|   s   Set-Cookie: N)r#   r   rä   Z	addCookierD   rÀ  rc   rs   r)  r*  r~   rb   )r=   ÚexpectedCookieValuer0   r1   rk   ré   ZwrittenLinesZsetCookieLinesr-   r-   r.   Ú_checkCookie—
  s    

ÿzRequestTests._checkCookiec                 C   s   d}|   |dd¡ dS )zÈ
        L{http.Request.setCookie} adds a new cookie to be sent with the
        response, and can be called with just a key and a value. L{unicode}
        arguments are encoded using UTF-8.
        ó   foo=barÚfooÚbarN©rS  ©r=   rR  r-   r-   r.   Ú)test_addCookieWithMinimumArgumentsUnicode­
  s    z6RequestTests.test_addCookieWithMinimumArgumentsUnicodec                 C   s&   d}| j |dddddddd	d	d

 dS )z‘
        L{http.Request.setCookie} adds a new cookie to be sent with the
        response. L{unicode} arguments are encoded using UTF-8.
        ó}   foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT; Domain=.example.com; Path=/; Max-Age=31536000; Comment=test; Secure; HttpOnlyrU  rV  zFri, 31 Dec 9999 23:59:59 GMTz.example.comú/Z31536000ZtestT©ZexpiresZdomainrË  Zmax_ageZcommentZsecureZhttpOnlyNrW  rX  r-   r-   r.   Ú%test_addCookieWithAllArgumentsUnicode¸
  s    ÿ      ýz2RequestTests.test_addCookieWithAllArgumentsUnicodec                 C   s   d}|   |dd¡ dS )z¾
        L{http.Request.setCookie} adds a new cookie to be sent with the
        response, and can be called with just a key and a value. L{bytes}
        arguments are not decoded.
        rT  r@  r™  NrW  rX  r-   r-   r.   Ú'test_addCookieWithMinimumArgumentsBytesÈ
  s    z4RequestTests.test_addCookieWithMinimumArgumentsBytesc                 C   s&   d}| j |dddddddd	d	d

 dS )z‡
        L{http.Request.setCookie} adds a new cookie to be sent with the
        response. L{bytes} arguments are not decoded.
        rZ  r@  r™  s   Fri, 31 Dec 9999 23:59:59 GMTs   .example.comrá  s   31536000r~  Tr\  NrW  rX  r-   r-   r.   Ú#test_addCookieWithAllArgumentsBytesÓ
  s    ÿ      ýz0RequestTests.test_addCookieWithAllArgumentsBytesc                 C   s\   d}|   |dd¡ |  | j g¡}|  dt|ƒ¡ |  |d d t¡ |  |d d d¡ d	S )
z×
        L{http.Request.setCookie} will raise a L{DeprecationWarning} if
        non-string (not L{bytes} or L{unicode}) arguments are given, and will
        call C{str()} on it to preserve past behaviour.
        s   foo=10r@  rq   r|   r   Úcategoryr  zSPassing non-bytes or non-unicode cookie arguments is deprecated since Twisted 16.1.N)rS  ÚflushWarningsrD   rb   ÚDeprecationWarning)r=   rR  Úwarningsr-   r-   r.   Útest_addCookieNonStringArgumentã
  s    
þz,RequestTests.test_addCookieNonStringArgumentc                 C   sR   | j ddddd | j ddddd | j ddddd | jt| j d	ddd
d dS )zL
        L{http.Request.setCookie} supports a C{sameSite} argument.
        s   foo=bar; SameSite=laxr@  r™  Zlax)ZsameSiteZLaxs   foo=bar; SameSite=strictÚstrictrÂ   zanything-elseN)rS  rü   rý   r<   r-   r-   r.   Útest_addCookieSameSiteö
  s4       ÿ   ÿ   ÿ   ýz#RequestTests.test_addCookieSameSitec                 C   s^   t ƒ }t |d¡}tƒ }||_| d¡ d|_|j ddg¡ | 	d¡ |  
| ¡ dg¡ dS )	z
        For an HTTP 1.0 request, L{http.Request.write} sends an HTTP 1.0
        Response-Line and whatever response headers are set.
        Fr8   r  r~  r  r€  )rŠ   rƒ  r€  N©r#   r   rä   r   rs   r:   ra   r0  r(  rc   rˆ   r™   ©r=   rk   ré   r…  r-   r-   r.   Útest_firstWrite  s    

þzRequestTests.test_firstWritec                 C   s¦   t ƒ }t |d¡}tƒ }||_| d¡ d|_|j ddg¡ | 	d¡ |  
| ¡ dg¡ | j| jgd}|  d	t|ƒ¡ |  |d
 d t¡ |  |d
 d d¡ dS )zd
        L{http.Request.write} casts non-bytes header value to bytes
        transparently.
        Fr8   r  r~  rq   r€  )rŠ   s   Test: 10r€  ©ZoffendingFunctionsr|   r   r`  r  zZPassing non-bytes header values is deprecated since Twisted 12.3. Pass only bytes instead.N)r#   r   rä   r   rs   r:   ra   r0  r(  rc   rˆ   r™   ra  Útest_nonByteHeaderValuerD   rb   rb  )r=   rk   ré   r…  rc  r-   r-   r.   rk    s*    

þÿ
þz$RequestTests.test_nonByteHeaderValuec                 C   sh   t ƒ }t |d¡}tƒ }||_| d¡ d|_|j ddg¡ | 	d¡ | 	d¡ |  
| ¡ dg¡ d	S )
zÅ
        For an HTTP 1.1 request, L{http.Request.write} sends an HTTP 1.1
        Response-Line, whatever response headers are set, and uses chunked
        encoding for the response body.
        Fr8   r6  r~  r  r€  r  r‚  Nrg  rh  r-   r-   r.   Útest_firstWriteHTTP11Chunked>  s    


þz)RequestTests.test_firstWriteHTTP11Chunkedc                 C   sd   t ƒ }t |d¡}tƒ }||_| d¡ d|_d|_|j 	ddg¡ | 
d¡ |  | ¡ dg¡ d	S )
zê
        For an HTTP 1.0 request for a resource with a known last modified time,
        L{http.Request.write} sends an HTTP Response-Line, whatever response
        headers are set, and a last-modified header with that time.
        Fr8   r  r   r~  r  r€  )rŠ   rƒ  ó,   Last-Modified: Thu, 01 Jan 1970 00:00:00 GMTr€  N)r#   r   rä   r   rs   r:   ra   r6  r0  r(  rc   rˆ   r™   rh  r-   r-   r.   Útest_firstWriteLastModifiedX  s    

þz(RequestTests.test_firstWriteLastModifiedc                 C   s˜   t  | t¡}tƒ }t |d¡}tƒ }||_| d¡ d|_	d|_
|j ddg¡ | d¡ |  | ¡ dg¡ |  d	t|ƒ¡ |d
 }|  d|d ¡ dS )z´
        If the last-modified header already exists in the L{http.Request}
        response headers, the lastModified attribute is ignored and a message
        is logged.
        Fr8   r  i Êš;s   last-modifieds   Thu, 01 Jan 1970 00:00:00 GMTr€  )rŠ   rm  r€  r|   r   zPWarning: last-modified specified both in header list and lastModified attribute.r³   N)r!   r¶   r'   r#   r   rä   r   rs   r:   ra   r6  r0  r(  rc   rˆ   r™   r¸   rb   )r=   rº   rk   ré   r…  r»   r-   r-   r.   Útest_lastModifiedAlreadyWrittens  s4    þ
þ
þýz,RequestTests.test_lastModifiedAlreadyWrittenc                 C   s    t  tƒ d¡}|  |ji ¡ dS )zP
        L{http.Request.received_cookies} defaults to an empty L{dict}.
        FN)r   rä   r#   rD   Úreceived_cookiesr  r-   r-   r.   Útest_receivedCookiesDefaultš  s    z(RequestTests.test_receivedCookiesDefaultc                 C   s>   t  tƒ d¡}|j ddg¡ | ¡  |  |jdddœ¡ dS )z„
        L{http.Request.parseCookies} extracts cookies from C{requestHeaders}
        and adds them to C{received_cookies}.
        Fó   cookies   test="lemur"; test2="panda"ó   "lemur"ó   "panda"©r~  s   test2N©r   rä   r#   r'  r(  ÚparseCookiesrD   rp  r  r-   r-   r.   Útest_parseCookies¢  s     ÿ ÿzRequestTests.test_parseCookiesc                 C   s@   t  tƒ d¡}|j dddg¡ | ¡  |  |jdddœ¡ dS )	zh
        L{http.Request.parseCookies} can extract cookies from multiple Cookie
        headers.
        Frr  s   test="lemur"s   test2="panda"rs  rt  ru  Nrv  r  r-   r-   r.   Ú test_parseCookiesMultipleHeaders¯  s     ÿ ÿz-RequestTests.test_parseCookiesMultipleHeadersc                 C   s(   t  tƒ d¡}| ¡  |  |ji ¡ dS )zj
        L{http.Request.parseCookies} can be called on a request without a
        cookie header.
        FN)r   rä   r#   rw  rD   rp  r  r-   r-   r.   Útest_parseCookiesNoCookie¼  s    z&RequestTests.test_parseCookiesNoCookiec                 C   s6   t  tƒ d¡}|j dg ¡ | ¡  |  |ji ¡ dS )zn
        L{http.Request.parseCookies} can be called on a request with an
        empty cookie header.
        Frr  Nrv  r  r-   r-   r.   Útest_parseCookiesEmptyCookieÆ  s     ÿz)RequestTests.test_parseCookiesEmptyCookiec                 C   s8   t  tƒ d¡}|j ddg¡ | ¡  |  |ji ¡ dS )z`
        L{http.Request.parseCookies} ignores cookies which don't have a
        value.
        Frr  s   foo; bar; baz;Nrv  r  r-   r-   r.   Ú test_parseCookiesIgnoreValuelessÒ  s     ÿ ÿz-RequestTests.test_parseCookiesIgnoreValuelessc                 C   s<   t  tƒ d¡}|j ddg¡ | ¡  |  |jddi¡ dS )zR
        L{http.Request.parseCookies} parses cookies with an empty value.
        Frr  s   foo=r@  rÂ   Nrv  r  r-   r-   r.   Útest_parseCookiesEmptyValueß  s     ÿ ÿz(RequestTests.test_parseCookiesEmptyValuec                 C   s<   t  tƒ d¡}|j ddg¡ | ¡  |  |jddi¡ dS )zf
        L{http.Request.parseCookies} leaves trailing whitespace in the
        cookie value.
        Frr  s   foo=bar r@  s   bar Nrv  r  r-   r-   r.   Ú!test_parseCookiesRetainRightSpaceë  s     ÿ ÿz.RequestTests.test_parseCookiesRetainRightSpacec                 C   s<   t  tƒ d¡}|j ddg¡ | ¡  |  |jddi¡ dS )zc
        L{http.Request.parseCookies} strips leading whitespace in the
        cookie key.
        Frr  s    foo=barr@  r™  Nrv  r  r-   r-   r.   Útest_parseCookiesStripLeftSpaceø  s     ÿ ÿz,RequestTests.test_parseCookiesStripLeftSpacec                 C   s>   t  tƒ d¡}|j ddg¡ | ¡  |  |jdddœ¡ dS )zr
        L{http.Request.parseCookies} parses valid cookies set before or
        after malformed cookies.
        Frr  s0   12345; test="lemur"; 12345; test2="panda"; 12345rs  rt  ru  Nrv  r  r-   r-   r.   Ú-test_parseCookiesContinueAfterMalformedCookie  s     ÿ ÿz:RequestTests.test_parseCookiesContinueAfterMalformedCookiec                 C   sX   t  tƒ d¡}| d¡ |j}| d¡ | ttdƒƒ¡ |  	|j
¡ |  |jd¡ dS )z£
        L{http.Request.connectionLost} closes L{Request.content} and drops the
        reference to the L{HTTPChannel} to assist with garbage collection.
        Frq   rg  ZFinishedN)r   rä   r#   Ú	gotLengthrX   ZhandleContentChunkr—   r   r   r­   ÚclosedrG  rk   )r=   ré   rX   r-   r-   r.   Útest_connectionLost  s    

z RequestTests.test_connectionLostc                 C   s4   t  tƒ d¡}| tƒ d¡ |  t|jtƒ d¡ dS )zv
        Calling L{Request.registerProducer} when a producer is already
        registered raises ValueError.
        FTN)r   rä   r#   r;   r"   rü   rý   r  r-   r-   r.   Útest_registerProducerTwiceFails(  s       ÿz,RequestTests.test_registerProducerTwiceFailsc                 C   s:   t  tƒ d¡}tƒ }| |d¡ |  |dfg|jj¡ dS )zÀ
        Calling L{Request.registerProducer} with an IPushProducer when the
        request is not queued registers the producer as a push producer on the
        request's transport.
        FTN©r   rä   r#   r"   r;   rD   rs   Z	producers©r=   ré   r
  r-   r-   r.   Ú7test_registerProducerWhenNotQueuedRegistersPushProducer3  s    zDRequestTests.test_registerProducerWhenNotQueuedRegistersPushProducerc                 C   s:   t  tƒ d¡}tƒ }| |d¡ |  |dfg|jj¡ dS )zÀ
        Calling L{Request.registerProducer} with an IPullProducer when the
        request is not queued registers the producer as a pull producer on the
        request's transport.
        FNr…  r†  r-   r-   r.   Ú7test_registerProducerWhenNotQueuedRegistersPullProducer?  s    zDRequestTests.test_registerProducerWhenNotQueuedRegistersPullProducerc                 C   sF   t ƒ }t |d¡}| ¡ }| ttdƒƒ¡ |  |jd¡ |  	|t¡S )z
        L{Request.connectionLost} triggers all finish notification Deferreds
        and cleans up per-request state.
        TzConnection doneN)
r#   r   rä   ÚnotifyFinishr—   r   r   rG  rk   ZassertFailure)r=   r#  rg   rL  r-   r-   r.   Útest_connectionLostNotificationK  s    z,RequestTests.test_connectionLostNotificationc                 C   s,   t  tƒ d¡}| ¡ }| d¡ | ¡  |S )zO
        L{Request.finish} triggers all finish notification Deferreds.
        Fr|   )r   rä   r#   r‰  r  rd   ©r=   rg   rL  r-   r-   r.   Útest_finishNotificationX  s
    
z$RequestTests.test_finishNotificationc                 C   sF   t  tƒ d¡}| ¡ }| d¡ | d¡ | ¡  |  t|jd¡ |S )z…
        Calling L{Request.write} after L{Request.finish} has been called results
        in a L{RuntimeError} being raised.
        Fr|   s   foobar)	r   rä   r#   r‰  r  rc   rd   rü   r%  r‹  r-   r-   r.   Útest_writeAfterFinishe  s    

z"RequestTests.test_writeAfterFinishc                 C   s6   t ƒ }t |d¡}| ttdƒƒ¡ |  t|j¡ dS )zŽ
        Calling L{Request.finish} after L{Request.connectionLost} has been
        called results in a L{RuntimeError} being raised.
        FzThe end.N)	r#   r   rä   r—   r   r   rü   r%  rd   r+  r-   r-   r.   Útest_finishAfterConnectionLostu  s    z+RequestTests.test_finishAfterConnectionLostc                 C   s,   t  tƒ d¡}|  t|ƒdt|ƒf ¡ dS )z¹
        L{Request.__repr__} returns the class name, object address, and
        dummy-place holder values when used on a L{Request} which has not yet
        been initialized.
        FzZ<Request at 0x%x method=(no method yet) uri=(no uri yet) clientproto=(no clientproto yet)>N©r   rä   r#   rD   ÚreprÚid©r=   rg   r-   r-   r.   Útest_reprUninitialized€  s    ÿþz#RequestTests.test_reprUninitializedc                 C   s>   t  tƒ d¡}d|_d|_d|_|  t|ƒdt|ƒf ¡ dS )a  
        L{Request.__repr__} returns, as a L{str}, the class name, object
        address, and the method, uri, and client protocol of the HTTP request
        it represents.  The string is in the form::

          <Request at ADDRESS method=METHOD uri=URI clientproto=PROTOCOL>
       Fr  ro   rp   z><Request at 0x%x method=GET uri=/foo/bar clientproto=HTTP/1.0>N)	r   rä   r#   ra   r`   r_   rD   r  r‘  r’  r-   r-   r.   Útest_reprInitialized  s    ÿþz!RequestTests.test_reprInitializedc                 C   s<   G dd„ dt jƒ}|tƒ dƒ}|  t|ƒdt|ƒf ¡ dS )zŸ
        Subclasses of L{Request} inherit a C{__repr__} implementation which
        includes the subclass's name in place of the string C{"Request"}.
        c                   @   s   e Zd ZdS )z1RequestTests.test_reprSubclass.<locals>.OtherwiseN)r)   r*   r+   r-   r-   r-   r.   Ú	Otherwise¤  s   r•  Fz\<Otherwise at 0x%x method=(no method yet) uri=(no uri yet) clientproto=(no clientproto yet)>Nr  )r=   r•  rg   r-   r-   r.   Útest_reprSubclassŸ  s    ÿþzRequestTests.test_reprSubclassc                 C   sF   t  tƒ d¡}tƒ |_| tƒ d¡ | ¡  |  d|j	|jj	f¡ dS )z•
        L{Request.unregisterProducer} unregisters a non-queued non-streaming
        producer from the request and the request's transport.
        F©NNN©
r   rä   r#   r   rs   r;   r"   ÚunregisterProducerrD   r
  r  r-   r-   r.   Ú,test_unregisterNonQueuedNonStreamingProducer®  s
    z9RequestTests.test_unregisterNonQueuedNonStreamingProducerc                 C   sF   t  tƒ d¡}tƒ |_| tƒ d¡ | ¡  |  d|j	|jj	f¡ dS )z‘
        L{Request.unregisterProducer} unregisters a non-queued streaming
        producer from the request and the request's transport.
        FTr—  Nr˜  r  r-   r-   r.   Ú)test_unregisterNonQueuedStreamingProducerº  s
    z6RequestTests.test_unregisterNonQueuedStreamingProducerc                 C   s†   t  ¡ }d|_d|_d|_| ¡  tƒ |_| d¡}ddg}t	ƒ }| 
|¡ |D ]}| |¡ qP|jjd  ¡  |  d|j ¡ ¡ dS )zj
        L{http.Request.finish} will call the channel's factory to produce a log
        message.
        NÚsometimeTó   GET /path HTTP/1.1
r}   r   s   sometime "GET /path HTTP/1.1")r   r«   r¤   Ú_logDateTimeÚ_logDateTimeCallÚstartFactoryr   ÚlogFiler¬   r   r”   r–   rv   r•   rd   r¹   r*  ©r=   r¯   ÚprotoÚvalr…  rÛ   r-   r-   r.   Útest_finishProducesLogÆ  s$    
þ
ÿz#RequestTests.test_finishProducesLogc                 C   sˆ   t ƒ }tjd|d}| ¡  | d¡}tƒ }t||ƒ}|  |jd¡ | 	|¡ | 
d¡ | d¡ |  |j¡ | d¡ |  |j¡ dS )z¢
        L{HTTPChannel} timeouts whenever data from a request body is not
        delivered to it in time, even when it gets built from a L{HTTPFactory}.
        r    )Ztimeoutrx   Nr¡   r¢   rµ   )r   r   r«   r   r¬   r   ry   rD   r¤   r”   r–   r¥   r¦   r§   r­   r  r-   r-   r.   Ú"test_requestBodyTimeoutFromFactoryå  s    





z/RequestTests.test_requestBodyTimeoutFromFactoryc                 C   s°   t  ¡ }d|_d|_d|_| ¡  tƒ |_| d¡}d|j	_
ddg}tƒ }| |¡ |  |jd¡ |D ]}| |¡ qf| d¡ |  |jd	¡ |j	jd  ¡  |  |jd¡ dS )
zÊ
        L{http.Request.finish} will notify the channel that it is finished, and
        will put the transport back in the producing state so that the reactor
        can close the connection.
        Nrœ  Tr   r  r}   rÙ   s   GET rÜ   )r   r«   r¤   rž  rŸ  r   r   r¡  r¬   rv   r×   r   r”   rD   rÝ   r–   r•   rd   r¢  r-   r-   r.   Útest_finishCleansConnectionü  s(    
þ

z(RequestTests.test_finishCleansConnectionc                 C   s   t  tƒ d¡}tt j|ƒ dS )z¦
        L{http.Request} provides
        L{http._IDeprecatedHTTPChannelToRequestInterface}, which
        defines the interface used by L{http.HTTPChannel}.
        FN)r   rä   r#   r	   Ú)_IDeprecatedHTTPChannelToRequestInterfacer  r-   r-   r.   Ú6test_provides_IDeprecatedHTTPChannelToRequestInterface  s    zCRequestTests.test_provides_IDeprecatedHTTPChannelToRequestInterfacec                 C   s   t  tƒ d¡}|  ||¡ dS )z7
        A L{http.Request} is equal to itself.
        FN)r   rä   r#   rD   r  r-   r-   r.   Útest_eq)  s    zRequestTests.test_eqc                 C   s(   t  tƒ d¡}|  |t  tƒ d¡¡ dS )zC
        A L{http.Request} is not equal to another object.
        FN)r   rä   r#   ÚassertNotEqualr  r-   r-   r.   Útest_ne1  s    zRequestTests.test_nec                 C   s   t  tƒ d¡}t|ƒ dS )z0
        A L{http.Request} is hashable.
        FN)r   rä   r#   Úhashr  r-   r-   r.   Útest_hashable9  s    zRequestTests.test_hashablec                    sF   g ‰ G ‡ fdd„dt ƒ}t tƒ d¡}|  ||ƒ ¡ |  ˆ |g¡ dS )zÔ
        A L{http.Request} on the left hand side of an equality
        comparison to an instance that is not a L{http.Request} hands
        the comparison off to that object's C{__eq__} implementation.
        c                       s   e Zd Z‡ fdd„ZdS )z8RequestTests.test_eqWithNonRequest.<locals>._NotARequestc                    s   ˆ   |¡ dS rm  rå  ©r=   Úother©ZeqCallsr-   r.   Ú__eq__K  s    
z?RequestTests.test_eqWithNonRequest.<locals>._NotARequest.__eq__N)r)   r*   r+   r²  r-   r±  r-   r.   Ú_NotARequestI  s   r³  FN)Úobjectr   rä   r#   rD   ©r=   r³  ré   r-   r±  r.   Útest_eqWithNonRequestA  s
    z"RequestTests.test_eqWithNonRequestc                    sF   g ‰ G ‡ fdd„dt ƒ}t tƒ d¡}|  ||ƒ ¡ |  ˆ |g¡ dS )zÖ
        A L{http.Request} on the left hand side of an inequality
        comparison to an instance that is not a L{http.Request} hands
        the comparison off to that object's C{__ne__} implementation.
        c                       s   e Zd Z‡ fdd„ZdS )z8RequestTests.test_neWithNonRequest.<locals>._NotARequestc                    s   ˆ   |¡ dS rm  rå  r¯  r±  r-   r.   Ú__ne___  s    
z?RequestTests.test_neWithNonRequest.<locals>._NotARequest.__ne__N)r)   r*   r+   r·  r-   r±  r-   r.   r³  ]  s   r³  FN)r´  r   rä   r#   r«  rD   rµ  r-   r±  r.   Útest_neWithNonRequestU  s
    z"RequestTests.test_neWithNonRequestc                 C   sl   t  | t¡}t tƒ d¡}| tƒ d¡ | ¡  |  	dt
|ƒ¡ |d }|d }|  |jt¡ |  t¡ dS )zy
        A RuntimeError is logged if a producer is still registered
        when an L{http.Request} is finished.
        FTr|   r   ré  N)r!   r¶   r'   r   rä   r#   r;   r"   rd   r¸   rb   râ  r™   r%  rë  )r=   rº   rg   r»   r¡  r-   r-   r.   Ú"test_finishProducerStillRegisteredi  s    þz/RequestTests.test_finishProducerStillRegisteredc                 C   s.   t  tt ddd¡d¡}|  | ¡ d¡ dS )z}
        L{http.Request.getClientIP} returns the host part of the
        client's address when connected over IPv4.
        rë   ú	127.0.0.1é80  ©ZpeerN©r   rä   r#   r   ÚIPv6AddressrD   ÚgetClientIPr’  r-   r-   r.   Útest_getClientIPWithIPv4|  s    ÿz%RequestTests.test_getClientIPWithIPv4c                 C   s.   t  tt ddd¡d¡}|  | ¡ d¡ dS )z}
        L{http.Request.getClientIP} returns the host part of the
        client's address when connected over IPv6.
        rë   z::1r»  r¼  Nr½  r’  r-   r-   r.   Útest_getClientIPWithIPv6†  s    ÿz%RequestTests.test_getClientIPWithIPv6c                 C   s*   t  tt d¡d¡}|  | ¡ d¡ dS )zŠ
        L{http.Request.getClientIP} returns L{None} for the client's
        IP address when connected over a non-TCP transport.
        ú/path/to/socketr¼  N)r   rä   r#   r   ÚUNIXAddressrD   r¿  r’  r-   r-   r.   Útest_getClientIPWithNonTCPPeer  s    ÿz+RequestTests.test_getClientIPWithNonTCPPeerc                 C   s.   t  d¡}t t|d¡}|  | ¡ |¡ dS )zs
        L{http.Request.getClientAddress} returns the client's address
        as an L{IAddress} provider.
        rÂ  r¼  N)r   rÃ  r   rä   r#   r	  ZgetClientAddress)r=   r"  rg   r-   r-   r.   Útest_getClientAddressš  s    
z"RequestTests.test_getClientAddressN)Mr)   r*   r+   r,   r  r  r   r!  r$  r%  r&  r,  r.  r/  r1  r2  r7  r8  r9  r=  r@  rA  rB  rH  rK  rN  rO  rP  rS  rY  r]  r^  r_  rd  rf  ri  rk  rl  rn  ro  rq  rx  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®  r¶  r¸  r¹  rÀ  rÁ  rÄ  rÅ  r-   r-   r-   r.   r  f	  s”   

	
	

		 '
#



r  c                   @   sL   e Zd ZdZdd„ ZddddœZdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚMultilineHeadersTestsa  
    Tests to exercise handling of multiline headers by L{HTTPClient}.  RFCs 1945
    (HTTP 1.0) and 2616 (HTTP 1.1) state that HTTP message header fields can
    span multiple lines if each extra line is preceded by at least one space or
    horizontal tab.
    c                 C   s   d| _ d| _dS )zv
        Initialize variables used to verify that the header-processing functions
        are getting called.
        FN)ÚhandleHeaderCalledÚhandleEndHeadersCalledr<   r-   r-   r.   r,  ¬  s    zMultilineHeadersTests.setUps   10s   line-0	line-1s   line-2 line-3)rW   s   X-Multilines   X-Multiline2c                 C   s   d| _ |  || j| ¡ dS )zE
        Dummy implementation of L{HTTPClient.handleHeader}.
        TN)rÇ  rD   r„   )r=   r  r¤  r-   r-   r.   ÚourHandleHeaderº  s    z%MultilineHeadersTests.ourHandleHeaderc                 C   s
   d| _ dS )zI
        Dummy implementation of L{HTTPClient.handleEndHeaders}.
        TN)rÈ  r<   r-   r-   r.   ÚourHandleEndHeadersÂ  s    z)MultilineHeadersTests.ourHandleEndHeadersc                 C   s„   t ƒ }| j|_| j|_| d¡ | d¡ |  |jd¡ |  | j	¡ |  | j
¡ | d¡ |  | j	¡ |  | j
¡ |  |jd¡ dS )z»
        A header isn't processed by L{HTTPClient.extractHeader} until it is
        confirmed in L{HTTPClient.lineReceived} that the header has been
        received completely.
        r  ó   Content-Length: 10NrÂ   rq   )r  rÉ  r  rÊ  r  r  rG  rf   r¦   rÇ  rÈ  r­   rD   r  r-   r-   r.   Útest_extractHeaderÉ  s    


z(MultilineHeadersTests.test_extractHeaderc                 C   sb   t ƒ }| j|_| j|_| d¡ | d¡ |  | j¡ |  | j	¡ |  
|jd¡ |  
|jd¡ dS )zº
        An HTTP request with no headers will not cause any calls to
        L{handleHeader} but will cause L{handleEndHeaders} to be called on
        L{HTTPClient} subclasses.
        r  rÂ   r  r  N)r  rÉ  r  rÊ  r  r  r¦   rÇ  r­   rÈ  rD   r  r‡   r  r-   r-   r.   Útest_noHeadersá  s    

z$MultilineHeadersTests.test_noHeadersc                 C   s®   t ƒ }| j|_| j|_| d¡ | d¡ |  | j¡ | d¡ | d¡ |  | j¡ | d¡ | d¡ | d¡ |  | j	¡ |  
|jd¡ |  
|jd	¡ |  
|jd
¡ dS )zð
        L{HTTPClient} parses multiline headers by buffering header lines until
        an empty line or a line that does not start with whitespace hits
        lineReceived, confirming that the header has been received completely.
        r  s   X-Multiline: line-0s   	line-1s   X-Multiline2: line-2s    line-3rË  rÂ   r  r  rq   N)r  rÉ  r  rÊ  r  r  r¦   rÇ  r­   rÈ  rD   r  r‡   rf   r  r-   r-   r.   Útest_multilineHeadersõ  s     






z+MultilineHeadersTests.test_multilineHeadersN)r)   r*   r+   r,   r,  r„   rÉ  rÊ  rÌ  rÍ  rÎ  r-   r-   r-   r.   rÆ  ¥  s   
þrÆ  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚExpect100ContinueServerTestsai  
    Test that the HTTP server handles 'Expect: 100-continue' header correctly.

    The tests in this class all assume a simplistic behavior where user code
    cannot choose to deny a request. Once ticket #288 is implemented and user
    code can run before the body of a POST is processed this should be
    extended to support overriding this behavior.
    c                 C   s€   t ƒ }t ¡ }t|_| |¡ | d¡ | d¡ | d¡ | d¡ | d¡ |  | ¡ d¡ | d¡ |  	| ¡ dg¡ d	S )
z‹
        HTTP/1.0 requests do not get 100-continue returned, even if 'Expect:
        100-continue' is included (RFC 2616 10.1.1).
        s   GET / HTTP/1.0
ó   Host: www.example.com
ó   Content-Length: 3
ó   Expect: 100-continue
ra  rÂ   rP  )rŠ   rŒ   rŽ   r   r‹   ó   '''
3
abc'''
N)
r   r   r‘   r’   r“   r”   r–   rD   r™   rˆ   )r=   rs   rk   r-   r-   r.   Útest_HTTP10  s    






þz(Expect100ContinueServerTests.test_HTTP10c                 C   s´   t ƒ }t ¡ }t|_| |¡ | d¡ | d¡ | d¡ | d¡ |  | ¡ d¡ | d¡ |  | ¡ d¡ | d¡ | ¡ }|  	| 
d¡¡ |tdƒd	… }|  |d
g¡ d	S )a!  
        If a HTTP/1.1 client sends a 'Expect: 100-continue' header, the server
        responds with a 100 response code before handling the request body, if
        any. The normal resource rendering code will then be called, which
        will send an additional response code.
        s   GET / HTTP/1.1
rÐ  rÒ  rÑ  rÂ   ra  s   HTTP/1.1 100 Continue

rP  N)rÇ   rŒ   rŽ   rÈ   r‹   rÓ  )r   r   r‘   r’   r“   r”   r–   rD   r™   r­   rQ  rb   rˆ   )r=   rs   rk   rƒ   r-   r-   r.   Útest_expect100ContinueHeader9  s*    






ÿþz9Expect100ContinueServerTests.test_expect100ContinueHeaderN)r)   r*   r+   r,   rÔ  rÕ  r-   r-   r-   r.   rÏ    s   	rÏ  c                    s   t ‡ fdd„| D ƒƒS )a  
    Create a new dict containing only a subset of the items of an existing
    dict.

    @param keys: An iterable of the keys which will be added (with values from
        C{d}) to the result.

    @param d: The existing L{dict} from which to copy items.

    @return: The new L{dict} with keys given by C{keys} and values given by the
        corresponding values in C{d}.
    @rtype: L{dict}
    c                    s   g | ]}|ˆ | f‘qS r-   r-   )rx  Úk©r#  r-   r.   ry  m  s     zsub.<locals>.<listcomp>)Údict)Úkeysr#  r-   r×  r.   Úsub_  s    rÚ  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú DeprecatedRequestAttributesTestszI
    Tests for deprecated attributes of L{twisted.web.http.Request}.
    c                 C   s~   t  tt ddd¡d¡}| d¡ | ddd¡ | ¡  | j| j	gd	}|  
d
t|ƒ¡ |  
tddœtddg|d ƒ¡ dS )zg
        L{Request.getClientIP} is deprecated in favor of
        L{Request.getClientAddress}.
        rë   rº  i90  r¼  r   ro   rá  r6  rj  r|   zjtwisted.web.http.Request.getClientIP was deprecated in Twisted 18.4.0; please use getClientAddress instead©r`  r  r`  r  N)r   rä   r#   r   r¾  r  ZrequestReceivedr¿  ra  Útest_getClientIPrD   rb   rb  rÚ  )r=   rg   rc  r-   r-   r.   rÝ  v  s     ÿ
ÿýûz1DeprecatedRequestAttributesTests.test_getClientIPc                 C   s\   t ƒ }t |¡}| ¡  | j| jgd}|  dt|ƒ¡ |  tddœt	ddg|d ƒ¡ dS )	zs
        L{Request.noLongerQueued} is deprecated, as we no longer process
        requests simultaneously.
        rj  r|   zHtwisted.web.http.Request.noLongerQueued was deprecated in Twisted 16.3.0rÜ  r`  r  r   N)
r#   r   rä   ZnoLongerQueuedra  Útest_noLongerQueuedrD   rb   rb  rÚ  )r=   rk   rg   rc  r-   r-   r.   rÞ    s    
ÿýûz4DeprecatedRequestAttributesTests.test_noLongerQueuedN)r)   r*   r+   r,   rÝ  rÞ  r-   r-   r-   r.   rÛ  q  s   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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 )ÚChannelProductionTestsz=
    Tests for the way HTTPChannel manages backpressure.
    s#   GET / HTTP/1.1
Host: localhost

c                 C   s(   |}t  ¡ }t|ƒ|_| |¡ ||fS )a  
        Setup a L{HTTPChannel} and a transport and associate them.

        @param transport: A transport to back the L{HTTPChannel}
        @param requestFactory: An object that can construct L{Request} objects.
        @return: A tuple of the channel and the transport.
        )r   r‘   r6   r“   r”   )r=   rs   r“   rk   r-   r-   r.   ÚbuildChannelAndTransport®  s
    

z/ChannelProductionTests.buildChannelAndTransportc                 C   s0   |   tƒ t¡\}}|  |j|¡ |  |j¡ dS )zu
        L{HTTPChannel} registers itself as a producer with its transport when a
        connection is made.
        N)rà  r   rT   rD   r
  r­   Ú	streaming©r=   rk   rs   r-   r-   r.   Útest_HTTPChannelIsAProducer¾  s     ÿz2ChannelProductionTests.test_HTTPChannelIsAProducerc                 C   s:   |   tƒ t¡\}}| ¡  |  |jd¡ |  |jd¡ dS )zV
        L{HTTPChannel} unregisters itself when it has loseConnection called.
        N)rà  r   rT   rá   r	  r
  rá  râ  r-   r-   r.   Ú8test_HTTPChannelUnregistersSelfWhenCallingLoseConnectionË  s     ÿzOChannelProductionTests.test_HTTPChannelUnregistersSelfWhenCallingLoseConnectionc                 C   s8   |   tƒ t¡\}}| tƒ d¡ |  t|jtƒ d¡ dS )z
        If two producers are registered on a L{HTTPChannel} without the first
        being unregistered, a L{RuntimeError} is thrown.
        TN)rà  r   rT   r;   r"   rü   r%  râ  r-   r-   r.   Ú(test_HTTPChannelRejectsMultipleProducersØ  s     ÿ   ÿz?ChannelProductionTests.test_HTTPChannelRejectsMultipleProducersc                 C   s,   |   tƒ t¡\}}| ¡  |  |jd¡ dS )zr
        If there is no producer, the L{HTTPChannel} can still have
        C{unregisterProducer} called.
        N©rà  r   rT   r™  r	  Z_requestProducerrâ  r-   r-   r.   Ú+test_HTTPChannelCanUnregisterWithNoProducerç  s     ÿzBChannelProductionTests.test_HTTPChannelCanUnregisterWithNoProducerc                 C   s,   |   tƒ t¡\}}| ¡  |  |jd¡ dS )zn
        If there is no request producer currently registered, C{stopProducing}
        does nothing.
        Nræ  râ  r-   r-   r.   Ú,test_HTTPChannelStopWithNoRequestOutstandingô  s     ÿzCChannelProductionTests.test_HTTPChannelStopWithNoRequestOutstandingc                 C   sf   |   tƒ t¡\}}| | j¡ |jd j}tƒ }| |d¡ |  	|j
g ¡ | ¡  |  	|j
dg¡ dS )z
        If there is a request producer registered with L{HTTPChannel}, calling
        C{stopProducing} causes that producer to be stopped as well.
        r   TÚstopN)rà  r   rl   r–   rg   r•   rÄ   r"   r;   rD   ÚeventsZstopProducing©r=   rk   rs   rg   r
  r-   r-   r.   Ú#test_HTTPChannelStopRequestProducer  s     ÿz:ChannelProductionTests.test_HTTPChannelStopRequestProducerc                 C   sP   |   tƒ t¡\}}|  |jd¡ | ¡  |  |jd¡ | ¡  |  |jd¡ dS )zë
        When L{HTTPChannel} has C{pauseProducing} called on it by the transport
        it will call C{pauseProducing} on the transport. When unpaused, the
        L{HTTPChannel} will call C{resumeProducing} on its transport.
        rÙ   rÜ   N)rà  r   rT   rD   rÝ   ÚpauseProducingÚresumeProducingrâ  r-   r-   r.   Ú;test_HTTPChannelPropagatesProducingFromTransportToTransport  s     ÿzRChannelProductionTests.test_HTTPChannelPropagatesProducingFromTransportToTransportc                 C   sØ   |   tƒ t¡\}}d|_| | j¡ | d¡ |jd j}tƒ }| 	|d¡ |  
|jd¡ |  
|jg ¡ | ¡  |  
|jd¡ |  
|jdg¡ | ¡  |  
|jd¡ |  
|jddg¡ | ¡  | ¡  |  
|jd¡ dS )	zÙ
        If a L{Request} object has registered itself as a producer with a
        L{HTTPChannel} object, and the L{HTTPChannel} object is paused, both
        the transport and L{Request} objects get paused.
        r   s   123TrÜ   ÚpauseZresumerÙ   N)rà  r   rl   r×   r–   rg   r•   rÄ   r"   r;   rD   rÝ   rê  rí  rî  r™  rm   rë  r-   r-   r.   Ú3test_HTTPChannelPropagatesPausedProductionToRequest0  s*     ÿ
zJChannelProductionTests.test_HTTPChannelPropagatesPausedProductionToRequestc                 C   sÆ   |   tƒ t¡\}}d|_| | j¡ | d¡ |jd j}tƒ }| 	|d¡ |  
|jd¡ |  
|jg ¡ | ¡  |  
|jd¡ |  
|jdg¡ | ¡  | ¡  |  
|jd¡ | ¡  |  
|jd¡ dS )z˜
        If a L{Request} object completes its response while the transport is
        paused, the L{HTTPChannel} does not resume the transport.
        r   s   extraTrÜ   rð  rÙ   N)rà  r   rl   r×   r–   rg   r•   rÄ   r"   r;   rD   rÝ   rê  rí  r™  rm   rî  rë  r-   r-   r.   Ú/test_HTTPChannelStaysPausedWhenRequestCompletes]  s(     ÿ
zFChannelProductionTests.test_HTTPChannelStaysPausedWhenRequestCompletesc                 C   sª   G dd„ dt ƒ}|ƒ }| ¡  |  t|j¡ |  |ƒ t¡\}}|  |jd¡ | ¡  |  |jd¡ | | j	¡ |  |jd¡ |  
| ¡  d¡¡ | ¡  |  |jd¡ dS )zŸ
        If the L{HTTPChannel} has paused the transport, it still tolerates
        receiving data, and does not attempt to pause the transport again.
        c                   @   s   e Zd ZdZdd„ ZdS )zgChannelProductionTests.test_HTTPChannelToleratesDataWhenTransportPaused.<locals>.NoDoublePauseTransportz|
            A version of L{StringTransport} that fails tests if it is paused
            while already paused.
            c                 S   s    | j dkrtdƒ‚t | ¡ d S )NrÜ   zTransport was paused twice!)rÝ   r%  r   rí  r<   r-   r-   r.   rí  ’  s    
zvChannelProductionTests.test_HTTPChannelToleratesDataWhenTransportPaused.<locals>.NoDoublePauseTransport.pauseProducingN)r)   r*   r+   r,   rí  r-   r-   r-   r.   ÚNoDoublePauseTransport  s   ró  rÙ   rÜ   s   HTTP/1.1 200 OK
N)r   rí  rü   r%  rà  rT   rD   rÝ   r–   rg   r­   r™   rQ  rî  )r=   ró  rs   rk   r-   r-   r.   Ú0test_HTTPChannelToleratesDataWhenTransportPausedˆ  s      ÿzGChannelProductionTests.test_HTTPChannelToleratesDataWhenTransportPausedc                    sh   ˆ   tƒ t¡\}‰tƒ ‰t ¡ }t|_| ˆ¡ | ˆ j	¡ |j
d j}|jj}‡ ‡fdd„}| |¡S )zŽ
        If the L{HTTPChannel} has a L{IPullProducer} registered with it it can
        adapt that producer into an L{IPushProducer}.
        r   c                    s(   ˆ  ¡  dd¡d }d}ˆ  ||¡ d S )Nr}   r|   s<   1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
)r™   r~   rD   )ZignZresponseBodyZexpectedResponseBody©r=   rs   r-   r.   ÚvalidateÅ  s    ÿzOChannelProductionTests.test_HTTPChannelToleratesPullProducers.<locals>.validate)rà  r   r7   r   r‘   ÚDummyPullProducerHandlerProxyr“   r”   r–   rg   r•   rÄ   r9   rz  r   )r=   rk   rg   ZresponseCompleterö  r-   rõ  r.   Ú&test_HTTPChannelToleratesPullProducers´  s     ÿ
z=ChannelProductionTests.test_HTTPChannelToleratesPullProducersc                 C   s|   t ƒ }tƒ }t ¡ }d|_|j|_| |¡ | d¡ |  |j	|¡ |  |j
d¡ | d¡ |  |j	d¡ |  |j
d¡ dS )zS
        L{HTTPChannel} unregisters itself when it times out a connection.
        r    r¢   Tr|   N)r   r   r   r‘   r¤   ru   r”   r¥   r	  r
  rá  )r=   r¨   rs   rk   r-   r-   r.   Ú,test_HTTPChannelUnregistersSelfWhenTimingOutØ  s    


zCChannelProductionTests.test_HTTPChannelUnregistersSelfWhenTimingOutN)r)   r*   r+   r,   rg   rà  rã  rä  rå  rç  rè  rì  rï  rñ  rò  rô  rø  rù  r-   r-   r-   r.   rß  £  s    ÿ-+,$rß  )jr,   Z
__future__r   r   rB   rÿ  r  rR   r   r   r   ÚImportErrorZurllib.parseÚior   Zzope.interfacer   Zzope.interface.verifyr	   Ztwisted.python.compatr
   r   r   r   r   r   Ztwisted.python.componentsr   Ztwisted.python.failurer   Ztwisted.trialr   Ztwisted.trial.unittestr   Ztwisted.webr   r   r   Ztwisted.web.httpr   r   r   Ztwisted.internetr   Ztwisted.internet.taskr   Ztwisted.internet.errorr   r   Ztwisted.protocolsr   Ztwisted.test.proto_helpersr   r    r!   Ztwisted.test.test_internetr"   Ztwisted.web.test.requesthelperr#   r%   r&   Ztwisted.loggerr'   r¨  r(   r6   rä   r7   r÷  r@   rT   r’   ZINonQueuedRequestFactoryri   ZDummyNewHTTPHandlerProxyrl   rÃ   Z
HTTPClientrn   ry   r´  rz   r‰   rÆ   rÍ   rÏ   rÑ   rà   rå   rï   r  r*  r+  r8  rO  rq  r‡  rþ  r  r  r  rÆ  rÏ  rÚ  rÛ  rß  r-   r-   r-   r.   Ú<module>   s°     ÿ	ÿ

% r+ +). =48  1      @b        GpJ2