AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/python3/dist-packages/twisted/web/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/lib/python3/dist-packages/twisted/web/test/__pycache__/test_wsgi.cpython-310.pyc
o

�b�*�@s�dZddlZddlZddlZddlmZddlmZddl	m
Z
ddlmZddl
mZmZddlmZmZdd	lmZdd
lmZmZddlmZddlmZdd
lmZddlmZddl m!Z!m"Z"ddl#m$Z$ddl%m&Z&m'Z'ddl(m)Z)m*Z*m+Z+ddl,m-Z-ddl.m/Z/Gdd�d�Z0Gdd�d�Z1Gdd�de"�Z2Gdd�d�Z3Gdd�de3e"�Z4Gdd �d e3�Z5Gd!d"�d"e5e"�Z6Gd#d$�d$e5e"�Z7Gd%d&�d&e5e"�Z8Gd'd(�d(e5e"�Z9Gd)d*�d*e3e"�Z:Gd+d,�d,e3e"�Z;dS)-z 
Tests for L{twisted.web.wsgi}.
�N)�exc_info)�quote)�verifyObject)�reactor)�IPv4Address�IPv6Address)�Deferred�
gatherResults)�ConnectionLost)�Logger�globalLogPublisher)�Failure)�getThreadID)�
ThreadPool)�EventLoggingObserver)�SkipTest�TestCase)�http)�	IResource�Resource)�Request�Site�version)�DummyChannel)�WSGIResourcec@seZdZdZe�Zdd�ZdS)�SynchronousThreadPoola
    A single-threaded implementation of part of the L{ThreadPool} interface.
    This implementation calls functions synchronously rather than running
    them in a thread pool.  It is used to make the tests which are not
    directly for thread-related behavior deterministic.
    cOs6z
||i|��WdSty|j�d�YdSw)zp
        Call C{f(*a, **kw)} in this thread rather than scheduling it to be
        called in a thread.
        z<Callable passed to SynchronousThreadPool.callInThread failedN)�
BaseException�_log�failure��self�f�a�kw�r$�</usr/lib/python3/dist-packages/twisted/web/test/test_wsgi.py�callInThread+s
��z"SynchronousThreadPool.callInThreadN)�__name__�
__module__�__qualname__�__doc__rrr&r$r$r$r%r!src@�eZdZdZdd�ZdS)�SynchronousReactorThreadsa�
    A single-threaded implementation of part of the L{IReactorThreads}
    interface.  This implementation assumes that it will only be invoked
    from the reactor thread, so it calls functions synchronously rather than
    trying to schedule them to run in the reactor thread.  It is used in
    conjunction with L{SynchronousThreadPool} to make the tests which are
    not directly for thread-related behavior deterministic.
    cOs||i|��dS)ze
        Call C{f(*a, **kw)} in this thread which should also be the reactor
        thread.
        Nr$rr$r$r%�callFromThreadEsz(SynchronousReactorThreads.callFromThreadN�r'r(r)r*r-r$r$r$r%r,;s	r,c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�WSGIResourceTestscCstt�t�dd��|_dS)z�
        Create a L{WSGIResource} with synchronous threading objects and a no-op
        application object.  This is useful for testing certain things about
        the resource implementation which are unrelated to WSGI.
        cS�dS�Nr$��environ�
startResponser$r$r%�<lambda>W�z)WSGIResourceTests.setUp.<locals>.<lambda>N)rr,r�resource�r r$r$r%�setUpNs

�zWSGIResourceTests.setUpcCstt|j�|�|jj�dS)zW
        L{WSGIResource} implements L{IResource} and stops resource traversal.
        N)rrr7�
assertTrue�isLeafr8r$r$r%�test_interfacesZsz!WSGIResourceTests.test_interfacescCs6|�t|jjdtt�d��|�t|jjdt��dS)z�
        A L{WSGIResource} cannot have L{IResource} children.  Its
        C{getChildWithDefault} and C{putChild} methods raise L{RuntimeError}.
        �fooFN)�assertRaises�RuntimeErrorr7�getChildWithDefaultrr�putChildrr8r$r$r%�test_unsupportedas
�z"WSGIResourceTests.test_unsupportedcs�t�|t�}Gdd�dt��Gdd�dt�}�fdd�}G�fdd�d�}t|�t�|�|_|j�|t	�d	��|�
d
t|��|dd}|�|j
��|���d
S)z�
        If an exception is thrown by the application, and then in the
        exception handling code, verify it should be propagated to the
        provided L{ThreadPool}.
        c@seZdZdZdS)zIWSGIResourceTests.test_applicationAndRequestThrow.<locals>.ArbitraryErrorz;
            An arbitrary error for this class
            N)r'r(r)r*r$r$r$r%�ArbitraryErrorvsrCc@s(eZdZdZdd�Zdd�Zdd�ZdS)	zPWSGIResourceTests.test_applicationAndRequestThrow.<locals>.FinishThrowingRequestzL
            An L{IRequest} request whose finish method throws.
            c_s.tj|g|�Ri|��d|_d|_d|_dS)N�swww.example.com/stuff)r�__init__�prepath�postpath�uri)r �args�kwargsr$r$r%rE�s
zYWSGIResourceTests.test_applicationAndRequestThrow.<locals>.FinishThrowingRequest.__init__cS�dS)zi
                Return loopback address.

                @return: loopback ip address.
                �	127.0.0.1r$r8r$r$r%�getClientIP�sz\WSGIResourceTests.test_applicationAndRequestThrow.<locals>.FinishThrowingRequest.getClientIPcSstddd�S)z`
                Return a fake Address

                @return: A fake address
                �TCPrLi0u)rr8r$r$r%�getHost�szXWSGIResourceTests.test_applicationAndRequestThrow.<locals>.FinishThrowingRequest.getHostN)r'r(r)r*rErMrOr$r$r$r%�FinishThrowingRequest{s
rPc����)z�
            An application object that throws an exception.

            @param environ: unused

            @param startResponse: unused
            r$r2�rCr$r%�application�szFWSGIResourceTests.test_applicationAndRequestThrow.<locals>.applicationcseZdZdZ�fdd�ZdS)zQWSGIResourceTests.test_applicationAndRequestThrow.<locals>.ThrowingReactorThreadszt
            An L{IReactorThreads} implementation whose callFromThread raises
            an exception.
            crQ)z�
                Raise an exception to the caller.

                @param f: unused

                @param a: unused

                @param kw: unused
                r$rrRr$r%r-�s
z`WSGIResourceTests.test_applicationAndRequestThrow.<locals>.ThrowingReactorThreads.callFromThreadNr.r$rRr$r%�ThrowingReactorThreads�srTF�r�log_failureN)r�createWithCleanupr�	Exceptionrrrr7�renderr�assertEquals�len�assertIsInstance�value�flushLoggedErrors)r �logObserverrPrSrTr!r$rRr%�test_applicationAndRequestThrowns

�z1WSGIResourceTests.test_applicationAndRequestThrowN)r'r(r)r9r<rBr`r$r$r$r%r/Ms

r/c@sTeZdZdZeZdd�Zdgddfdd�Zdd	�Ze	fd
d�Z
dd
�Zddd�ZdS)�WSGITestsMixinz�
    @ivar channelFactory: A no-argument callable which will be invoked to
        create a new HTTP channel to associate with request objects.
    cCst�|_t�|_dSr1)r�
threadpoolr,rr8r$r$r%r9�szWSGITestsMixin.setUpNrDcsdd�}t|j|j|��}
|��|D]}t�}|�||�|
�|}
q|�}t|
�|_||d�}|	D]\}}|j�	||�||��q2|�
d�|
rU|j�|
�|j�
d�dd��fdd�|D��}|durw|d	d
��fdd�|D��7}|�||�||�d||��|S)
a�
        @param method: A C{str} giving the request method to use.

        @param version: A C{str} like C{'1.1'} giving the request version.

        @param resourceSegments: A C{list} of unencoded path segments which
            specifies the location in the resource hierarchy at which the
            L{WSGIResource} will be placed, eg C{['']} for I{/}, C{['foo',
            'bar', '']} for I{/foo/bar/}, etc.

        @param requestSegments: A C{list} of unencoded path segments giving the
            request URI.

        @param query: A C{list} of two-tuples of C{str} giving unencoded query
            argument keys and values.

        @param headers: A C{list} of two-tuples of C{str} giving request header
            names and corresponding values.

        @param safe: A C{str} giving the bytes which are to be considered
            I{safe} for inclusion in the request URI and not quoted.

        @return: A L{Deferred} which will be called back with a two-tuple of
            the arguments passed which would be passed to the WSGI application
            object for this configuration and request (ie, the environment and
            start_response callable).
        cSst|t�r|S|�d�S)Nz
iso-8859-1)�
isinstance�bytes�encode)�stringr$r$r%�
_toByteString�s

z4WSGITestsMixin.lowLevelRender.<locals>._toByteStringFr�/csg|]}t|���qSr$)�urlquote)�.0�seg��safer$r%�
<listcomp>sz1WSGITestsMixin.lowLevelRender.<locals>.<listcomp>N�?�&cs*g|]\}}d�t|��t|��g��qS)�=)�joinri)rj�k�vrlr$r%rns*sHTTP/)rrrb�reverserrAr�site�requestHeaders�addRawHeader�	gotLength�content�write�seekrr�requestReceived)r �requestFactory�applicationFactory�channelFactory�methodr�resourceSegments�requestSegments�query�headers�bodyrmrg�rootrk�tmp�channel�requestrsrtrHr$rlr%�lowLevelRender�s2*	


��zWSGITestsMixin.lowLevelRendercs@t���fdd�}|�d|j�}|jt||g|�Ri|���S)Nc��fdd�}|S)Ncs$|\}}��|�|dg�td�S�N�200 OKr$��callback�iter�rIr3r4��resultr$r%rSs

zFWSGITestsMixin.render.<locals>.applicationFactory.<locals>.applicationr$�rSr�r$r%rsz1WSGITestsMixin.render.<locals>.applicationFactoryr�)r�popr�r�r)r r"r#rr�r$r�r%rYs
	zWSGITestsMixin.rendercst����fdd�}�|fS)Ncs �|i|��}|�����|Sr1)�notifyFinish�
chainDeferred)r"r#r���d�requestClassr$r%r~)sz<WSGITestsMixin.requestFactoryFactory.<locals>.requestFactory�r)r r�r~r$r�r%�requestFactoryFactory&�	z$WSGITestsMixin.requestFactoryFactorycCs|�dd�dS)N�

rU)�split)r �responser$r$r%�getContentFromResponse4sz%WSGITestsMixin.getContentFromResponsecsvt����fdd�}t|j|j|�}t�}|�d|�|��}t|�|_Gdd�dt	�}||dd�}|�
d�|�fS)	aj
        Prepare a L{Request} which, when a request is received, captures the
        C{environ} and C{start_response} callable passed to a WSGI app.

        @param application: An optional WSGI application callable that accepts
            the familiar C{environ} and C{start_response} args and returns an
            iterable of body content. If not supplied, C{start_response} will
            be called with a "200 OK" status and no headers, and no content
            will be yielded.

        @return: A two-tuple of (C{request}, C{deferred}). The former is a
            Twisted L{Request}. The latter is a L{Deferred} which will be
            called back with a two-tuple of the arguments passed to a WSGI
            application (i.e. the C{environ} and C{start_response} callable),
            or will errback with any error arising within the WSGI app.
        csjz�dur|dg�td�}n�||�}Wnty+���|dg�td�YSw��||f�|S)Nr�r$z	500 Error)r�r�errbackr�)r3r4rz�rSr�r$r%�outerApplicationJs


�
�z7WSGITestsMixin.prepareRequest.<locals>.outerApplicationsresc@seZdZdZddd�ZdS)	z4WSGITestsMixin.prepareRequest.<locals>.CannedRequestz�
            Convenient L{Request} derivative which has canned values for all
            of C{requestReceived}'s arguments.
            �GET�/res�1.1cSstj||||d�S)N)�command�pathr)rr})r r�r�rr$r$r%r}gs�zDWSGITestsMixin.prepareRequest.<locals>.CannedRequest.requestReceivedN)r�r�r�)r'r(r)r*r}r$r$r$r%�
CannedRequestasr�F)�queuedr)rrrrbrrAr�rrvrry)r rSr�r7r�r�r�r�r$r�r%�prepareRequest7s

zWSGITestsMixin.prepareRequestr1)
r'r(r)r*rr�r9r�rYrr�r�r�r$r$r$r%ra�s

�Mrac@seZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z d<d=�Z!d>S)?�EnvironTestsz�
    Tests for the values in the C{environ} C{dict} passed to the application
    object by L{twisted.web.wsgi.WSGIResource}.
    cs���fdd�}|S)Ncs|\}}��|����Sr1��assertEqual�r�r3r4��keyr r]r$r%�assertEnvironKeyEqualysz;EnvironTests.environKeyEqual.<locals>.assertEnvironKeyEqualr$)r r�r]r�r$r�r%�environKeyEqualxszEnvironTests.environKeyEqualcs,��ddgdg�}�fdd�}|�|�|S)z�
        L{WSGIResource} calls the application object with an C{environ}
        parameter which is exactly of type C{dict}.
        �GET�1.1rDcs2|\}}��t|�t�|D]}��|t�qdSr1)�assertIdentical�type�dictr\�str)r�r3r4�namer8r$r%�
cbRendered�s
�z3EnvironTests.test_environIsDict.<locals>.cbRendered)rY�addCallback)r r�r�r$r8r%�test_environIsDict�s
zEnvironTests.test_environIsDictcCsT|�ddgdg�}|�|�dd��|�ddgdg�}|�|�dd��t||g�S)z�
        The C{'REQUEST_METHOD'} key of the C{environ} C{dict} passed to the
        application contains the HTTP method in the request (RFC 3875, section
        4.1.12).
        r�r�rD�REQUEST_METHOD�POST�rYr�r�r	)r �get�postr$r$r%�test_requestMethod�s
zEnvironTests.test_requestMethodcCsHdD]}|��\}}|�|�|�|�dd��|�|�|�t�qdS)z�
        The C{'REQUEST_METHOD'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        )r�r�r�r�N�r�r}r�r�r\�successResultOfr�)r r�r�r�r$r$r%� test_requestMethodIsNativeString�s
�z-EnvironTests.test_requestMethodIsNativeStringcCs|�ddgdg�}|�|�dd��|�dddgdg�}|�|�dd��|�dddgdg�}|�|�dd��|�ddddgddg�}|�|�dd��|�dddgdd	g�}|�|�dd��|�ddgd
�gd
��}|�|�dd��t||||||g�S)z�
        The C{'SCRIPT_NAME'} key of the C{environ} C{dict} passed to the
        application contains the I{abs_path} (RFC 2396, section 3) to this
        resource (RFC 3875, section 4.1.13).
        r�r�rD�SCRIPT_NAMErh�fooz/fooz/foo/�bar�r�rhsbar��/foo///barÿr�)r r��
emptyChild�leaf�	container�internal�	unencodedr$r$r%�test_scriptName�s�zEnvironTests.test_scriptNamecCs^|��\}}|jdd�|�|�dd��|�|�|�t�|��\}}|jt|jdd�dS)z�
        The C{'SCRIPT_NAME'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        r��r�r�z/resN�	r�r}r�r�r\r�r�r>�	TypeError�r r�r�r$r$r%�test_scriptNameIsNativeString��z*EnvironTests.test_scriptNameIsNativeStringc	Cs|�dd�}|�ddgdg�}|�|�dd��|�dddgdg�}|�|�|�dddgdg�}|�|�|�ddddgddg�}|�|�|�dddgddg�}|�|�dd��|�dddgddg�}|�|�dd��|�ddggd	��}|�|�dd
��t||||||g�S)z�
        The C{'PATH_INFO'} key of the C{environ} C{dict} passed to the
        application contains the suffix of the request URI path which is not
        included in the value for the C{'SCRIPT_NAME'} key (RFC 3875, section
        4.1.5).
        �	PATH_INFOrDr�r�rhr�r�z/barr�r�)r�rYr�r	)	r �assertKeyEmptyr�r�r�r��internalLeaf�internalContainerr�r$r$r%�
test_pathInfo�s$


�zEnvironTests.test_pathInfocC�^|��\}}|jdd�|�|�dd��|�|�|�t�|��\}}|jt|jdd�dS)z
        The C{'PATH_INFO'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        s/res/foo/barr�r�z/foo/barz/res/foo/barNr�r�r$r$r%�test_pathInfoIsNativeString�r�z(EnvironTests.test_pathInfoIsNativeStringcCs�|�ddgdgd�}|�|�dd��|�ddgdgg�}|�|�dd��|�ddgdgdg�}|�|�dd��|�ddgdgdg�}|�|�dd	��|jddgdgd
gdd�}|�|�dd
��t|||||g�S)z�
        The C{'QUERY_STRING'} key of the C{environ} C{dict} passed to the
        application contains the portion of the request URI after the first
        I{?} (RFC 3875, section 4.1.7).
        r�r�rDN�QUERY_STRING�r�r��foo=bar)rhrhz%2F=%2F)r�z?barrorlzfoo=?barr�)r �missing�empty�presentr��doubleQuestionr$r$r%�test_queryString
s�zEnvironTests.test_queryStringcCr�)z�
        The C{'QUERY_STRING'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        s/res?foo=barr�r�r�z/res?foo=barNr�r�r$r$r%�test_queryStringIsNativeString&r�z+EnvironTests.test_queryStringIsNativeStringcC�Z|�ddgdg�}|�|�dd��|�ddgdgddg�}|�|�dd��t||g�S)z�
        The C{'CONTENT_TYPE'} key of the C{environ} C{dict} passed to the
        application contains the value of the I{Content-Type} request header
        (RFC 3875, section 4.1.3).
        r�r�rD�CONTENT_TYPEN)�content-type�	x-foo/barr�r��r r�r�r$r$r%�test_contentType5��zEnvironTests.test_contentTypecC�TdD]%}|��\}}|j�d|�|��|�|�dd��|�|�|�t�qdS)z�
        The C{'CONTENT_TYPE'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        )s	x-foo/barr�sContent-Typer�r�N�	r�rwrxr}r�r�r\r�r�)r �contentTyper�r�r$r$r%�test_contentTypeIsNativeStringE��z+EnvironTests.test_contentTypeIsNativeStringcCr�)z�
        The C{'CONTENT_LENGTH'} key of the C{environ} C{dict} passed to the
        application contains the value of the I{Content-Length} request header
        (RFC 3875, section 4.1.2).
        r�r�rD�CONTENT_LENGTHN)�content-length�1234r�r�r�r$r$r%�test_contentLengthQr�zEnvironTests.test_contentLengthcCr�)z�
        The C{'CONTENT_LENGTH'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        )s1234r�sContent-Lengthr�r�Nr�)r �
contentLengthr�r�r$r$r%� test_contentLengthIsNativeStringar�z-EnvironTests.test_contentLengthIsNativeStringcCsZ|�ddgdg�}|�|�dd��|�ddgdgddg�}|�|�dd��t||g�S)	a_
        The C{'SERVER_NAME'} key of the C{environ} C{dict} passed to the
        application contains the best determination of the server hostname
        possible, using either the value of the I{Host} header in the request
        or the address the server is listening on if that header is not
        present (RFC 3875, section 4.1.14).
        r�r�rD�SERVER_NAMEz10.0.0.1N)�host�example.orgr�r�r�r$r$r%�test_serverNamems
zEnvironTests.test_serverNamecsTdD]%�|��\}}�fdd�|_|��|�|�dd��|�|�|�t�qdS)z�
        The C{'SERVER_NAME'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        )shost.example.com�host.example.comc��Sr1r$r$��
serverNamer$r%r5�r6z<EnvironTests.test_serverNameIsNativeString.<locals>.<lambda>r�rN)r��getRequestHostnamer}r�r�r\r�r�r�r$rr%�test_serverNameIsNativeStrings�z*EnvironTests.test_serverNameIsNativeStringcsBd��fdd�}||_|�ddgdg�}|�|�dt����|S)z�
        The C{'SERVER_PORT'} key of the C{environ} C{dict} passed to the
        application contains the port number of the server which received the
        request (RFC 3875, section 4.1.15).
        iB0cst�}t��|_�|j_|Sr1)rrN�	transport�port�r���
portNumberr$r%�makeChannel�s
z1EnvironTests.test_serverPort.<locals>.makeChannelr�r�rD�SERVER_PORT)r�rYr�r�r�)r rr�r$r	r%�test_serverPort�szEnvironTests.test_serverPortcCs<|��\}}|��|�|�dd��|�|�|�t�dS)z�
        The C{'SERVER_PORT'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        r�80Nr�r�r$r$r%�test_serverPortIsNativeString�sz*EnvironTests.test_serverPortIsNativeStringcCsT|�ddgdg�}|�|�dd��|�ddgdg�}|�|�dd��t||g�S)z�
        The C{'SERVER_PROTOCOL'} key of the C{environ} C{dict} passed to the
        application contains the HTTP version number received in the request
        (RFC 3875, section 4.1.16).
        r�z1.0rD�SERVER_PROTOCOLzHTTP/1.0r�zHTTP/1.1r�)r �old�newr$r$r%�test_serverProtocol�s
z EnvironTests.test_serverProtocolcCsTdD]%}|��\}}dd�|_|jdd�|�|�dd��|�|�|�t�qdS)	z�
        The C{'SERVER_PROTOCOL'} key of the C{environ} C{dict} passed to the
        application is always a native string.
        )r�r�cSr0r1r$)�datar$r$r%r5�r6z@EnvironTests.test_serverProtocolIsNativeString.<locals>.<lambda>r�)rrr�N)r�r{r}r�r�r\r�r�)r �serverProtocolr�r�r$r$r%�!test_serverProtocolIsNativeString�s
�z.EnvironTests.test_serverProtocolIsNativeStringcC�(|�ddgdg�}|�|�dd��|S)z�
        The C{'REMOTE_ADDR'} key of the C{environ} C{dict} passed to the
        application contains the address of the client making the request.
        r�r�rD�REMOTE_ADDRz192.168.1.1�rYr�r��r r�r$r$r%�test_remoteAddr�szEnvironTests.test_remoteAddrcCs4dd�}|jddgdg|d�}|�|�dd��|S)	z�
        The C{'REMOTE_ADDR'} key of the C{environ} C{dict} passed to
        the application contains the address of the client making the
        request when connecting over IPv6.
        cSsttddd�d�S)NrN�::1i�)�peer)rrr$r$r$r%r���z8EnvironTests.test_remoteAddrIPv6.<locals>.channelFactoryr�r�rD)r�rrr)r r�r�r$r$r%�test_remoteAddrIPv6�sz EnvironTests.test_remoteAddrIPv6c	s���ddgdgdddg�}�fdd�}|�|���ddgdgddd	g�}|���d
d����ddgdgddg�}|���d
d����ddgdgddg�}|���d
d��t||||g�S)z�
        HTTP request headers are copied into the C{environ} C{dict} passed to
        the application with a C{HTTP_} prefix added to their names.
        r�r�rDNr���baz�quuxcs,|\}}��|dd���|dd�dS)N�HTTP_FOOr��HTTP_BAZr"r�r�r8r$r%r��sz-EnvironTests.test_headers.<locals>.cbRendered)r�r!r#zbar,baz)zfoo-barr!�HTTP_FOO_BARr!)r�zbar
	bazzbar 	bazr�)r �singleValuer��
multiValue�
withHyphen�	multiLiner$r8r%�test_headers�s�
�zEnvironTests.test_headerscCr)z�
        The C{'wsgi.version'} key of the C{environ} C{dict} passed to the
        application has the value C{(1, 0)} indicating that this is a WSGI 1.0
        container.
        r�r�rDzwsgi.version)rUrr)r �versionDeferredr$r$r%�test_wsgiVersion�szEnvironTests.test_wsgiVersioncCr)z|
        The C{'wsgi.run_once'} key of the C{environ} C{dict} passed to the
        application is set to C{False}.
        r�r�rDz
wsgi.run_onceFr)r �oncer$r$r%�test_wsgiRunOnce	�zEnvironTests.test_wsgiRunOncecCr)z~
        The C{'wsgi.multithread'} key of the C{environ} C{dict} passed to the
        application is set to C{True}.
        r�r�rDzwsgi.multithreadTr)r �threadr$r$r%�test_wsgiMultithreadr/z!EnvironTests.test_wsgiMultithreadcCr)z�
        The C{'wsgi.multiprocess'} key of the C{environ} C{dict} passed to the
        application is set to C{False}.
        r�r�rDzwsgi.multiprocessFr)r �processr$r$r%�test_wsgiMultiprocessr/z"EnvironTests.test_wsgiMultiprocesscCshdd�}t|_|�ddgdg�}|�|�dd��||_|�ddgdg�}|�|�dd��t||g�S)	z�
        The C{'wsgi.url_scheme'} key of the C{environ} C{dict} passed to the
        application has the request URL scheme.
        cSst�}t��|_|Sr1)r�SSLrrr$r$r%r�+s
z7EnvironTests.test_wsgiURLScheme.<locals>.channelFactoryr�r�rDzwsgi.url_schemer�https)rr�rYr�r�r	)r r��httpDeferred�
httpsDeferredr$r$r%�test_wsgiURLScheme$szEnvironTests.test_wsgiURLSchemecs:t��t����ddgdg�}��fdd�}|�|�|S)a`
        The C{'wsgi.errors'} key of the C{environ} C{dict} passed to the
        application is a file-like object (as defined in the U{Input and Errors
        Streams<http://www.python.org/dev/peps/pep-0333/#input-and-error-streams>}
        section of PEP 333) which converts bytes written to it into events for
        the logging system.
        r�r�rDcs�|\}}|d}|�d�|�dg�|�����ddd����ddd����dd	����d
dd����d
dd����d
d	���t��d�dS)
N�wsgi.errors�
some message
�another
message
r�message)r:�system�wsgi�isErrorrU)r;�)r{�
writelines�flushr�r:r[)r�r3�startApplication�errors��eventsr r$r%�cbErrorsFs
z.EnvironTests.test_wsgiErrors.<locals>.cbErrors)rrWrrYr�)r rDrGr$rEr%�test_wsgiErrors:s

zEnvironTests.test_wsgiErrorscCsN|��\}}|��|�|�\}}|d}|�t|jd�}|�dt|��dS)z�
        The C{'wsgi.errors'} file-like object from the C{environ} C{dict}
        permits writes of only native strings in Python 3, and raises
        C{TypeError} for writes of non-native strings.
        r9sfredz1write() argument must be str, not b'fred' (bytes)N)r�r}r�r>r�r{r�r�)r r�r�r3�_rD�errorr$r$r%�0test_wsgiErrorsAcceptsOnlyNativeStringsInPython3Ws�z=EnvironTests.test_wsgiErrorsAcceptsOnlyNativeStringsInPython3N)"r'r(r)r*r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr
rrrrrr*r,r.r1r3r8rHrKr$r$r$r%r�rs@"


			r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.S)/�InputStreamTestMixinz�
    A mixin for L{TestCase} subclasses which defines a number of tests against
    L{_InputStream}.  The subclass is expected to create a file-like object to
    be wrapped by an L{_InputStream} under test.
    cCst|jj�d���)Nz.getFile must be implemented)�NotImplementedError�	__class__r'r8r$r$r%�getFileTypeos�z InputStreamTestMixin.getFileTypecsR|���G�fdd�dt�}dd�}||�\}}|�||tddgdgdg|�
|S)Nc�eZdZ�fdd�ZdS)zLInputStreamTestMixin._renderAndReturnReaderResult.<locals>.CustomizedRequestcs��|_dSr1)rz)r �length�r�r$r%ryxszVInputStreamTestMixin._renderAndReturnReaderResult.<locals>.CustomizedRequest.gotLengthN)r'r(r)ryr$rRr$r%�CustomizedRequestw�rScst����fdd�}�|fS)Nc���fdd�}|S)Ncs,|\}}���|d��|dg�td�S)Nz
wsgi.inputr�r$r�r���readerr�r$r%rS�s
z}InputStreamTestMixin._renderAndReturnReaderResult.<locals>.appFactoryFactory.<locals>.applicationFactory.<locals>.applicationr$r�rVr$r%r�szhInputStreamTestMixin._renderAndReturnReaderResult.<locals>.appFactoryFactory.<locals>.applicationFactoryr�)rWrr$rVr%�appFactoryFactory~r�zLInputStreamTestMixin._renderAndReturnReaderResult.<locals>.appFactoryFactory�PUTr�rD)rOrr�r)r rWrzrSrXr��
appFactoryr$rRr%�_renderAndReturnReaderResultts"�z1InputStreamTestMixin._renderAndReturnReaderResultcC�&d}|�dd�|�}|�|j|�|S)zi
        Calling L{_InputStream.read} with no arguments returns the entire input
        stream.
        �some bytes are herecS�|��Sr1��read��inputr$r$r%r5��z3InputStreamTestMixin.test_readAll.<locals>.<lambda>�r[r�r��r rdr�r$r$r%�test_readAll��z!InputStreamTestMixin.test_readAllcC�&d}|�dd�|�}|�|jd�|S)z�
        Calling L{_InputStream.read} with an integer returns that many bytes
        from the input stream, as long as it is less than or equal to the total
        number of bytes available.
        s
hello, world.cS�
|�d�S�N�r_rar$r$r%r5��
z4InputStreamTestMixin.test_readSome.<locals>.<lambda>shelrdrer$r$r%�
test_readSome�sz"InputStreamTestMixin.test_readSomecs*d�|��fdd���}|�|j��|S)z�
        Calling L{_InputStream.read} with an integer that is greater than the
        total number of bytes in the input stream returns all bytes in the
        input stream.
        r]cs|�t��d�Srj)r`r[ra�rdr$r%r5�sz8InputStreamTestMixin.test_readMoreThan.<locals>.<lambda>rdrr$rnr%�test_readMoreThan�s�z&InputStreamTestMixin.test_readMoreThancCs2d}dd�}|�||�}|�|j|dd��|S)z�
        Calling L{_InputStream.read} a second time returns bytes starting from
        the position after the last byte returned by the previous read.
        ssome bytes, hellocSs|�d�|��Srjr_rar$r$r%r`��
z1InputStreamTestMixin.test_readTwice.<locals>.readrkNrd)r rdr`r�r$r$r%�test_readTwice�s
z#InputStreamTestMixin.test_readTwicecCr\)zy
        Calling L{_InputStream.read} with L{None} as an argument returns all
        bytes in the input stream.
        sthe entire streamcS�
|�d�Sr1r_rar$r$r%r5�rlz4InputStreamTestMixin.test_readNone.<locals>.<lambda>rdrer$r$r%�
test_readNone�rgz"InputStreamTestMixin.test_readNonecCr\)z�
        Calling L{_InputStream.read} with a negative integer as an argument
        returns all bytes in the input stream.
        sall of the inputcSri�N���r_rar$r$r%r5�rlz8InputStreamTestMixin.test_readNegative.<locals>.<lambda>rdrer$r$r%�test_readNegative�rgz&InputStreamTestMixin.test_readNegativecCrh)zs
        Calling L{_InputStream.readline} with no argument returns one line from
        the input stream.
        shello
worldcSr^r1��readlinerar$r$r%r5�rcz4InputStreamTestMixin.test_readline.<locals>.<lambda>shello
rdrer$r$r%�
test_readline�rgz"InputStreamTestMixin.test_readlinecCrh)a
        Calling L{_InputStream.readline} with an integer returns at most that
        many bytes, even if it is not enough to make up a complete line.

        COMPATIBILITY NOTE: the size argument is excluded from the WSGI
        specification, but is provided here anyhow, because useful libraries
        such as python stdlib's cgi.py assume their input file-like-object
        supports readline with a size argument. If you use it, be aware your
        application may not be portable to other conformant WSGI servers.
        s
goodbye
worldcSrirjrwrar$r$r%r5�rlz8InputStreamTestMixin.test_readlineSome.<locals>.<lambda>sgoordrer$r$r%�test_readlineSome�sz&InputStreamTestMixin.test_readlineSomecCrh)z�
        Calling L{_InputStream.readline} with an integer which is greater than
        the number of bytes in the next line returns only the next line.
        ssome lines
of textcSri)N�rwrar$r$r%r5rlz<InputStreamTestMixin.test_readlineMoreThan.<locals>.<lambda>ssome lines
rdrer$r$r%�test_readlineMoreThan�rgz*InputStreamTestMixin.test_readlineMoreThancCs*d}dd�}|�||�}|�|jd�|S)z�
        Calling L{_InputStream.readline} a second time returns the line
        following the line returned by the first call.
        s first line
second line
last linecSs|��|��Sr1rwrar$r$r%rxsz9InputStreamTestMixin.test_readlineTwice.<locals>.readlinessecond line
rd)r rdrxr�r$r$r%�test_readlineTwices
z'InputStreamTestMixin.test_readlineTwicecCrh)z~
        Calling L{_InputStream.readline} with L{None} as an argument returns
        one line from the input stream.
        s%this is one line
this is another linecSrrr1rwrar$r$r%r5rlz8InputStreamTestMixin.test_readlineNone.<locals>.<lambda>sthis is one line
rdrer$r$r%�test_readlineNonergz&InputStreamTestMixin.test_readlineNonecCrh)z�
        Calling L{_InputStream.readline} with a negative integer as an argument
        returns one line from the input stream.
        sinput stream line one
line twocSrirtrwrar$r$r%r5#rlz<InputStreamTestMixin.test_readlineNegative.<locals>.<lambda>sinput stream line one
rdrer$r$r%�test_readlineNegativergz*InputStreamTestMixin.test_readlineNegativecC�*d}|�dd�|�}|�|jgd��|S)z�
        Calling L{_InputStream.readlines} with no arguments returns a list of
        all lines from the input stream.
        salice
bob
carolcSr^r1��	readlinesrar$r$r%r5-rcz5InputStreamTestMixin.test_readlines.<locals>.<lambda>)salice
sbob
scarolrdrer$r$r%�test_readlines'�z#InputStreamTestMixin.test_readlinescs.d}��dd�|�}�fdd�}|�|�|S)z�
        Calling L{_InputStream.readlines} with an integer as an argument
        returns a list of lines from the input stream with the argument serving
        as an approximate bound on the total number of bytes to read.
        s
123
456
789
0cSri)N�r�rar$r$r%r58rlz9InputStreamTestMixin.test_readlinesSome.<locals>.<lambda>cs��|dd�ddg�dS)Nr@s123
s456
r�)�linesr8r$r%�cbLines:sz8InputStreamTestMixin.test_readlinesSome.<locals>.cbLines)r[r�)r rdr�r�r$r8r%�test_readlinesSome1s

z'InputStreamTestMixin.test_readlinesSomecCr�)z�
        Calling L{_InputStream.readlines} with an integer which is greater than
        the total number of bytes in the input stream returns a list of all
        lines from the input.
        s"one potato
two potato
three potatocSri)N�dr�rar$r$r%r5Irlz=InputStreamTestMixin.test_readlinesMoreThan.<locals>.<lambda>)sone potato
stwo potato
sthree potatordrer$r$r%�test_readlinesMoreThanBs
�z+InputStreamTestMixin.test_readlinesMoreThancC�.d}dd�}|�||�}|�|jddg�|S)z�
        Calling L{_InputStream.readlines} after a call to L{_InputStream.read}
        returns lines starting at the byte after the last byte returned by the
        C{read} call.
        shello
world
foocSs|�d�|��S)N�)r`r�rar$r$r%r�Wrpz?InputStreamTestMixin.test_readlinesAfterRead.<locals>.readlinessorld
r=rd)r rdr�r�r$r$r%�test_readlinesAfterReadO�
z,InputStreamTestMixin.test_readlinesAfterReadcC�*d}|�dd�|�}|�|jddg�|S)zy
        Calling L{_InputStream.readlines} with L{None} as an argument returns
        all lines from the input.
        sone fish
two fish
cSrrr1r�rar$r$r%r5frlz9InputStreamTestMixin.test_readlinesNone.<locals>.<lambda>s	one fish
s	two fish
rdrer$r$r%�test_readlinesNone_s�z'InputStreamTestMixin.test_readlinesNonecCr�)z�
        Calling L{_InputStream.readlines} with a negative integer as an
        argument returns a list of all lines from the input.
        sred fish
blue fish
cSrirtr�rar$r$r%r5qrlz=InputStreamTestMixin.test_readlinesNegative.<locals>.<lambda>s	red fish
s
blue fish
rdrer$r$r%�test_readlinesNegativekr�z+InputStreamTestMixin.test_readlinesNegativecCr�)zV
        Iterating over L{_InputStream} produces lines from the input stream.
        �green eggs
and ham
cSst|�Sr1)�listrar$r$r%r5zrcz4InputStreamTestMixin.test_iterable.<locals>.<lambda>sgreen eggs
�and ham
rdrer$r$r%�
test_iterableusz"InputStreamTestMixin.test_iterablecCr�)z�
        Iterating over L{_InputStream} after calling L{_InputStream.read}
        produces lines from the input stream starting from the first byte after
        the last byte returned by the C{read} call.
        r�cSs|�d�t|�Srj)r`r�rar$r$r%�iterate�rpz<InputStreamTestMixin.test_iterableAfterRead.<locals>.iteratesen eggs
r�rd)r rdr�r�r$r$r%�test_iterableAfterRead~r�z+InputStreamTestMixin.test_iterableAfterReadN)r'r(r)r*rOr[rfrmrorqrsrvryrzr|r}r~rr�r�r�r�r�r�r�r�r$r$r$r%rLhs0'










	rLc@r+)�InputStreamStringIOTestsz�
    Tests for L{_InputStream} when it is wrapped around a
    L{StringIO.StringIO}.

    This is only available in Python 2.
    cCs*z	ddlm}W|Stytd��w)Nr��StringIOz#StringIO.StringIO is not available.)r��ImportErrorr�r r�r$r$r%rO����z$InputStreamStringIOTests.getFileTypeN�r'r(r)r*rOr$r$r$r%r���r�c@r+)�InputStreamCStringIOTestsz�
    Tests for L{_InputStream} when it is wrapped around a
    L{cStringIO.StringIO}.

    This is only available in Python 2.
    cCs*z	ddlm}W|Stytd��w)Nrr�z$cStringIO.StringIO is not available.)�	cStringIOr�r�rr�r$r$r%rO�r�z%InputStreamCStringIOTests.getFileTypeNr�r$r$r$r%r��r�r�c@r+)�InputStreamBytesIOTestszO
    Tests for L{_InputStream} when it is wrapped around an L{io.BytesIO}.
    cCsddlm}|S)Nr)�BytesIO)�ior�)r r�r$r$r%rO�sz#InputStreamBytesIOTests.getFileTypeNr�r$r$r$r%r���r�c@r+)�InputStreamTemporaryFileTestszZ
    Tests for L{_InputStream} when it is wrapped around a L{tempfile.TemporaryFile}.
    cCstjSr1)�tempfile�
TemporaryFiler8r$r$r%rO�sz)InputStreamTemporaryFileTests.getFileTypeNr�r$r$r$r%r��r�r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,S)-�StartResponseTestszl
    Tests for the I{start_response} parameter passed to the application object
    by L{WSGIResource}.
    c�Zt��dd�}���\}}��fdd�}|�|���||�fdd�ddgd	gd
g�	|S)z�
        The response status passed to the I{start_response} callable is written
        as the status of the response to the request.
        cS�dd�}|S)NcS�|dg�td�S)Nz107 Strange messager$�r�r2r$r$r%rS�rpzOStartResponseTests.test_status.<locals>.applicationFactory.<locals>.applicationr$r�r$r$r%r��z:StartResponseTests.test_status.<locals>.applicationFactoryc����jj���d��dS)NsHTTP/1.1 107 Strange message�r:r�written�getvalue�
startswith��ignored�r�r r$r%r��s
��z2StartResponseTests.test_status.<locals>.cbRenderedcrr1r$r$rr$r%r5�r6z0StartResponseTests.test_status.<locals>.<lambda>r�r�rDN�rr�r�r��r rr�r~r�r$r�r%�test_status�s"

�zStartResponseTests.test_statuscsDd��fdd�}��|�\}}|���fdd�}��|t��|�S)z�
        The response status passed to the I{start_response} callable MUST be a
        native string in Python 2 and Python 3.
        s200 OKcs|�g�td�S)Nr$r�r2)�statusr$r%rS�rpzEStartResponseTests.test_statusMustBeNativeString.<locals>.applicationc���dt|��dS)Nz)status must be str, not b'200 OK' (bytes)�r�r��rJr8r$r%�checkMessageszFStartResponseTests.test_statusMustBeNativeString.<locals>.checkMessage�r�r}�
assertFailurer�r��r rSr�r�r�r$)r r�r%�test_statusMustBeNativeString�sz0StartResponseTests.test_statusMustBeNativeStringcsr��tddd��t���fdd�}���\}}���fdd�}|�|���||�fdd�d	d
gdgdg�	|S)
a
        Verify that if the response headers given by C{appHeaders} are passed
        to the I{start_response} callable, then the response header lines given
        by C{expectedHeaders} plus I{Server} and I{Date} header lines are
        included in the response.
        �datetimeToStringcSrK)N�Tuesdayr$r$r$r$r%r5
r6z1StartResponseTests._headersTest.<locals>.<lambda>cr�)Ncs|d��td�Sr�r�r2��
appHeadersr$r%rSrpzPStartResponseTests._headersTest.<locals>.applicationFactory.<locals>.applicationr$r�r�r$r%r�z;StartResponseTests._headersTest.<locals>.applicationFactorycs`�jj��}|�dd�\}}|�d�dd�}|���ddtdg}|����||�dS)Nr�rUs
s
Date: TuesdaysServer: sTransfer-Encoding: chunked)rr�r�r��sortrr�)r�r�r��rest�headerLines�allExpectedHeaders)r��expectedHeadersr r$r%r�s�z3StartResponseTests._headersTest.<locals>.cbRenderedcrr1r$r$rr$r%r5,r6r�r�rDN)�patchrrr�r�r�)r r�r�rr�r~r�r$)r�r�r�r r%�_headersTests$


�zStartResponseTests._headersTestcCs|�ddgddg�S)z�
        The headers passed to the I{start_response} callable are included in
        the response as are the required I{Date} and I{Server} headers and the
        necessary connection (hop to hop) header I{Transfer-Encoding}.
        r�r s	Baz: quuxsFoo: bar�r�r8r$r$r%r*6s�zStartResponseTests.test_headerscsFdg��fdd�}��|�\}}|���fdd�}��|t��|�S)zb
        The headers passed to the I{start_response} callable MUST be a
        sequence.
        �r�r]cs|dt���td�Sr�r�r2)r�r$r%rSGszBStartResponseTests.test_headersMustBeSequence.<locals>.applicationc���t|�d�dS)NzKheaders must be a list, not <(list_?|sequence)iterator .+> [(]\1iterator[)]��assertRegexr�r�r8r$r%r�N��zCStartResponseTests.test_headersMustBeSequence.<locals>.checkMessager�r�r$)r�r r%�test_headersMustBeSequence@sz-StartResponseTests.test_headersMustBeSequencecC�dd�}|�|�\}}tjdd��}|��|�|�}Wd�n1s%wY|�dt|��|�t|dj�|�dt	|dj
��dS)	a�
        According to PEP-3333, the headers passed to the I{start_response}
        callable MUST be a plain list:

          The response_headers argument ... must be a Python list; i.e.
          type(response_headers) is ListType

        However, for bug-compatibility, any sequence is accepted. In both
        Python 2 and Python 3, only a warning is issued when a sequence other
        than a list is encountered.
        cSs|dd�td�S)Nr�))�notr�r$r�r2r$r$r%rSdrpzEStartResponseTests.test_headersShouldBePlainList.<locals>.applicationT��recordNrUrz8headers should be a list, not (('not', 'list'),) (tuple)�r��warnings�catch_warningsr}r�r�r[�RuntimeWarning�categoryr�r<�r rSr�r��caughtr$r$r%�test_headersShouldBePlainListW�
��z0StartResponseTests.test_headersShouldBePlainListcsDd��fdd�}��|�\}}|���fdd�}��|t��|�S)zb
        Each header passed to the I{start_response} callable MUST be a
        sequence.
        r�cs|dt��g�td�Sr�r�r2)�headerr$r%rS{�zFStartResponseTests.test_headersMustEachBeSequence.<locals>.applicationcr�)Nz[header must be a [(]str, str[)] tuple, not <(tuple_?|sequence)iterator .+> [(]\1iterator[)]r�r�r8r$r%r��r�zGStartResponseTests.test_headersMustEachBeSequence.<locals>.checkMessager�r�r$)r�r r%�test_headersMustEachBeSequencetsz1StartResponseTests.test_headersMustEachBeSequencecCr�)	a�
        According to PEP-3333, each header passed to the I{start_response}
        callable should be a tuple:

          The response_headers argument is a list of (header_name,
          header_value) tuples

        However, for bug-compatibility, any 2 element sequence is also
        accepted. In both Python 2 and Python 3, only a warning is issued when
        a sequence other than a tuple is encountered.
        cSs|dddgg�td�S)Nr�r��tupler$r�r2r$r$r%rS�r�zEStartResponseTests.test_headersShouldEachBeTuple.<locals>.applicationTr�NrUrz@header should be a (str, str) tuple, not ['not', 'tuple'] (list)r�r�r$r$r%�test_headersShouldEachBeTuple�r�z0StartResponseTests.test_headersShouldEachBeTuplec�<dd�}��|�\}}|���fdd�}��|t��|�S)z�
        Each header passed to the I{start_response} callable MUST hold a key
        and a value, and ONLY a key and a value.
        cSs|ddg�td�S)Nr�)�too�many�cooksr$r�r2r$r$r%rS�szMStartResponseTests.test_headersShouldEachHaveKeyAndValue.<locals>.applicationcr�)Nz?header must be a (str, str) tuple, not ('too', 'many', 'cooks')r�r�r8r$r%r��s�zNStartResponseTests.test_headersShouldEachHaveKeyAndValue.<locals>.checkMessager�r�r$r8r%�%test_headersShouldEachHaveKeyAndValue�s
z8StartResponseTests.test_headersShouldEachHaveKeyAndValuecsFd��fdd�}��|�\}}|����fdd�}��|t��|�S)z�
        Each header key passed to the I{start_response} callable MUST be at
        native string in Python 2 and Python 3.
        skeycs|d�dfg�td�S)Nr�r]r$r�r2)r�r$r%rS�r�zHStartResponseTests.test_headerKeyMustBeNativeString.<locals>.applicationcs��d��d�t|��dS)Nz&header must be (str, str) tuple, not (z
, 'value')r�r��r�r r$r%r���
�zIStartResponseTests.test_headerKeyMustBeNativeString.<locals>.checkMessager�r�r$r�r%� test_headerKeyMustBeNativeString��z3StartResponseTests.test_headerKeyMustBeNativeStringcsFd��fdd�}��|�\}}|����fdd�}��|t��|�S)z�
        Each header value passed to the I{start_response} callable MUST be at
        native string in Python 2 and Python 3.
        svaluecs|dd�fg�td�S)Nr�r�r$r�r2)r]r$r%rS�r�zJStartResponseTests.test_headerValueMustBeNativeString.<locals>.applicationcs��d��d�t|��dS)Nz-header must be (str, str) tuple, not ('key', �)r�r��r r]r$r%r��r�zKStartResponseTests.test_headerValueMustBeNativeString.<locals>.checkMessager�r�r$r�r%�"test_headerValueMustBeNativeString�r�z5StartResponseTests.test_headerValueMustBeNativeStringcCs|�dgdg�S)z�
        If I{Content-Type} is included in the headers passed to the
        I{start_response} callable, one I{Content-Type} header is included in
        the response.
        )r�zmonkeys are greatsContent-Type: monkeys are greatr�r8r$r$r%�#test_applicationProvidedContentType�s�z6StartResponseTests.test_applicationProvidedContentTypecCs|�gd�g�S)z�
        If either I{Server} or I{Date} is included in the headers passed to the
        I{start_response} callable, they are disregarded.
        ))�serverr�)�Serverr�)�dater�)�dATEr�r�r8r$r$r%�%test_applicationProvidedServerAndDate�s�z8StartResponseTests.test_applicationProvidedServerAndDatec�pt��g���fdd���fdd�}���\}}��fdd�}|�|���||�fdd�d	d
gdgdg�	|S)
a
        Nothing is written in response to a request when the I{start_response}
        callable is invoked.  If the iterator returned by the application
        object produces only empty strings, the response is written after the
        last element is produced.
        c����jj���dSr1��appendrr�r�r$�r��intermediateValuesr$r%r��z:StartResponseTests.test_delayedUntilReturn.<locals>.recordcr�)Nc3s �|dddg�dV��dS)Nr�r�r �r$r2r�r$r%rSs�
z[StartResponseTests.test_delayedUntilReturn.<locals>.applicationFactory.<locals>.applicationr$r�r�r$r%rszFStartResponseTests.test_delayedUntilReturn.<locals>.applicationFactorycs���dg�dS)Nrr�r��r�r r$r%r�rz>StartResponseTests.test_delayedUntilReturn.<locals>.cbRenderedcrr1r$r$rr$r%r5r6z<StartResponseTests.test_delayedUntilReturn.<locals>.<lambda>r�r�rDNr�r�r$�r�r�r�r r%�test_delayedUntilReturn�s&

�z*StartResponseTests.test_delayedUntilReturncr�)
a
        Nothing is written in response to a request when the I{start_response}
        callable is invoked.  Once a non-empty string has been produced by the
        iterator returned by the application object, the response status and
        headers are written.
        cr�r1r�r$r�r$r%r�3rz;StartResponseTests.test_delayedUntilContent.<locals>.recordcr�)Nc3�*�|ddg�dV��dV��dS)Nr�r�rr=r$r2r�r$r%rS7��
z\StartResponseTests.test_delayedUntilContent.<locals>.applicationFactory.<locals>.applicationr$r�r�r$r%r6�zGStartResponseTests.test_delayedUntilContent.<locals>.applicationFactorycs ���d����d�dS)NrrU)�assertFalser:r�rr$r%r�Bsz?StartResponseTests.test_delayedUntilContent.<locals>.cbRenderedcrr1r$r$rr$r%r5Kr6z=StartResponseTests.test_delayedUntilContent.<locals>.<lambda>r�r�rDNr�r�r$rr%�test_delayedUntilContent(s&


�z+StartResponseTests.test_delayedUntilContentcr�)
z�
        Content produced by the iterator returned by the application object is
        written to the request as it is produced.
        cr�r1r�r$r�r$r%r�_rz/StartResponseTests.test_content.<locals>.recordcr�)Nc3r)Nr��r��6r=�barr$r2r�r$r%rScrzPStartResponseTests.test_content.<locals>.applicationFactory.<locals>.applicationr$r�r�r$r%rbrz;StartResponseTests.test_content.<locals>.applicationFactoryc�0�����d�d������d�d�dS�Nrr=rUsfoobar�r�r�r�rr$r%r�n��z3StartResponseTests.test_content.<locals>.cbRenderedcrr1r$r$rr$r%r5yr6z1StartResponseTests.test_content.<locals>.<lambda>r�r�rDNr�r�r$rr%�test_contentVs&


�zStartResponseTests.test_contentcr�)z�
        If the I{start_response} callable is invoked multiple times before a
        data for the response body is produced, the values from the last call
        are used.
        cSr�)NcSs|dg�|dg�td�S)N�100 Fooz200 Barr$r�r2r$r$r%rS�s

z^StartResponseTests.test_multipleStartResponse.<locals>.applicationFactory.<locals>.applicationr$r�r$r$r%r��zIStartResponseTests.test_multipleStartResponse.<locals>.applicationFactorycr�)NsHTTP/1.1 200 Bar
r�r�r�r$r%r����zAStartResponseTests.test_multipleStartResponse.<locals>.cbRenderedcrr1r$r$rr$r%r5�r6z?StartResponseTests.test_multipleStartResponse.<locals>.<lambda>r�r�rDNr�r�r$r�r%�test_multipleStartResponse�s"

�z-StartResponseTests.test_multipleStartResponsecr�)z�
        If the I{start_response} callable is invoked with a third positional
        argument before the status and headers have been written to the
        response, the status and headers become the newly supplied values.
        cSr�)NcSs|dgttd�df�td�S)Nrr�r$)rXr�r2r$r$r%rS�szcStartResponseTests.test_startResponseWithException.<locals>.applicationFactory.<locals>.applicationr$r�r$r$r%r�r�zNStartResponseTests.test_startResponseWithException.<locals>.applicationFactorycr�)NsHTTP/1.1 100 Foo
r�r�r�r$r%r��rzFStartResponseTests.test_startResponseWithException.<locals>.cbRenderedcrr1r$r$rr$r%r5�r6zDStartResponseTests.test_startResponseWithException.<locals>.<lambda>r�r�rDNr�r�r$r�r%�test_startResponseWithException�s"

�z2StartResponseTests.test_startResponseWithExceptioncs�t��Gdd�dt�}z|��tyt��Ynwg���fdd�}���\}}����fdd�}|�|���||�fdd�d	d
gdgdg�	|S)
z�
        If the I{start_response} callable is invoked with a third positional
        argument after the status and headers have been written to the
        response, the supplied I{exc_info} values are re-raised to the
        application.
        c@seZdZdS)zPStartResponseTests.test_startResponseWithExceptionTooLate.<locals>.SomeExceptionN)r'r(r)r$r$r$r%�
SomeException�srcrU)Nc3sF�|dg�dVz	|dg��WdSty"��t��YdSw)Nr�r=z500 ERR)rr�rr2��excInfo�reraisedr$r%rS�s�
�zjStartResponseTests.test_startResponseWithExceptionTooLate.<locals>.applicationFactory.<locals>.applicationr$r�rr$r%r�szUStartResponseTests.test_startResponseWithExceptionTooLate.<locals>.applicationFactorycs����jj���d�����dd�d����dd�d��ddj}�d}��t�|�dt�|�d�dS)NsHTTP/1.1 200 OK
rrUr@)	r:rr�r�r�r��tb_next�	traceback�
extract_tb)r��tb1�tb2�r�rrr r$r%r��s�$zMStartResponseTests.test_startResponseWithExceptionTooLate.<locals>.cbRenderedcrr1r$r$rr$r%r5r6zKStartResponseTests.test_startResponseWithExceptionTooLate.<locals>.<lambda>r�r�rDN)rrXrrr�r�r�)r rrr�r~r�r$r r%�&test_startResponseWithExceptionTooLate�s0
�

�z9StartResponseTests.test_startResponseWithExceptionTooLatecr�)
z�
        I{start_response} returns the I{write} callable which can be used to
        write bytes to the response body without buffering.
        cr�r1r�r$r�r$r%r�rz-StartResponseTests.test_write.<locals>.recordcr�)Ncs0|ddg�}|d���|d���td�S)Nrr
r=rr$r��r3r4r{r�r$r%rSszNStartResponseTests.test_write.<locals>.applicationFactory.<locals>.applicationr$r�r�r$r%rsz9StartResponseTests.test_write.<locals>.applicationFactorycr
rrr�rr$r%r�&rz1StartResponseTests.test_write.<locals>.cbRenderedcrr1r$r$rr$r%r51r6z/StartResponseTests.test_write.<locals>.<lambda>r�r�rDNr�r�r$rr%�
test_write
s&

�zStartResponseTests.test_writecr�)zj
        The C{write} callable returned from C{start_response} only accepts
        byte strings.
        cS�|dg�}|d�td�S)Nr��bogusr$r�r"r$r$r%rSB�
zHStartResponseTests.test_writeAcceptsOnlyByteStrings.<locals>.applicationcr�)Nz0Can only write bytes to a transport, not 'bogus'r�r�r8r$r%r�Js�zIStartResponseTests.test_writeAcceptsOnlyByteStrings.<locals>.checkMessager�r�r$r8r%� test_writeAcceptsOnlyByteStrings<s
z3StartResponseTests.test_writeAcceptsOnlyByteStringsN)r'r(r)r*r�r�r�r*r�r�r�r�r�r�r�r�r�rr	rrrr!r#r'r$r$r$r%r��s.'1
	+..'&</r�c@sxeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�ApplicationTestszd
    Tests for things which are done to the application object and the iterator
    it returns.
    cCs*t|_t�|_|j��|�|jj�dSr1)rrrb�start�
addCleanup�stopr8r$r$r%�
enableThreadsXs
zApplicationTests.enableThreadsc	spt��Gdd�d�}|���fdd�}���\}}���fdd�}|�|���||�fdd�d	d
gdg�|S)z�
        If the application object returns an iterator which also has a I{close}
        method, that method is called after iteration is complete.
        c@s$eZdZdd�Zdd�Zdd�ZdS)z+ApplicationTests.test_close.<locals>.ResultcS�
d|_dS)NT��openr8r$r$r%rEf�
z4ApplicationTests.test_close.<locals>.Result.__init__css&�td�D]}|jrd|fVqdS�Nrks%d)�ranger/)r �ir$r$r%�__iter__is���z4ApplicationTests.test_close.<locals>.Result.__iter__cSr-)NFr.r8r$r$r%�closenr0z1ApplicationTests.test_close.<locals>.Result.closeN�r'r(r)rEr4r5r$r$r$r%�Resultesr7cr�)Ncs|ddg��S)Nr��r��3r$r2r�r$r%rStszLApplicationTests.test_close.<locals>.applicationFactory.<locals>.applicationr$r�r�r$r%rsr�z7ApplicationTests.test_close.<locals>.applicationFactorycs*�����jj���d����j�dS)Ns012)r�r�rr�r�rr/r��r�r�r r$r%r�|s
�z/ApplicationTests.test_close.<locals>.cbRenderedcrr1r$r$rr$r%r5�r6z-ApplicationTests.test_close.<locals>.<lambda>r�r�rDr�)r r7rr�r~r�r$r:r%�
test_close^s
�zApplicationTests.test_closec	sX���g��fdd�}���\}}��fdd�}|�|���||tddgdg�|S)zu
        The application object is invoked and iterated in a thread which is not
        the reactor thread.
        cr�)Ncs*�fdd�}��t��|ddg�|�S)Nc3s,�td�D]}��t��d|fVqdSr1)r2r�r)r3��invokedr$r%r��s
��zpApplicationTests.test_applicationCalledInThread.<locals>.applicationFactory.<locals>.application.<locals>.resultr�r8)r�r)r3r4r�r<r$r%rS�sz`ApplicationTests.test_applicationCalledInThread.<locals>.applicationFactory.<locals>.applicationr$r�r<r$r%r�s
zKApplicationTests.test_applicationCalledInThread.<locals>.applicationFactorycs&��t�����tt���d�dS)NrU)�assertNotInrr�r[�setr��r=r r$r%r��szCApplicationTests.test_applicationCalledInThread.<locals>.cbRenderedr�r�rD)r,r�r�r�rr�r$r@r%�test_applicationCalledInThread�s

�z/ApplicationTests.test_applicationCalledInThreadc	�j���g�G�fdd�dt�}dd�}��|�\}}��fdd�}|�|���||tddgd	g�|S)
z�
        The I{write} callable returned by I{start_response} calls the request's
        C{write} method in the reactor thread.
        crP)zCApplicationTests.test_writeCalledFromThread.<locals>.ThreadVerifierc���t��t�||�Sr1�r�rrr{�r rdr<r$r%r{��zIApplicationTests.test_writeCalledFromThread.<locals>.ThreadVerifier.writeN�r'r(r)r{r$r<r$r%�ThreadVerifier�rTrHcSr�)NcSr$)Nr�r=r$r�r"r$r$r%rS�r&z\ApplicationTests.test_writeCalledFromThread.<locals>.applicationFactory.<locals>.applicationr$r�r$r$r%r�rzGApplicationTests.test_writeCalledFromThread.<locals>.applicationFactoryc���t��t�h�dSr1�r�r?rr�r@r$r%r���z?ApplicationTests.test_writeCalledFromThread.<locals>.cbRenderedr�r�rD�r,rr�r�r�r�r rHrr�r~r�r$r@r%�test_writeCalledFromThread�s
�z+ApplicationTests.test_writeCalledFromThreadc	rB)
z�
        Strings produced by the iterator returned by the application object are
        written to the request in the reactor thread.
        crP)zMApplicationTests.test_iteratedValuesWrittenFromThread.<locals>.ThreadVerifiercrCr1rDrEr<r$r%r{�rFzSApplicationTests.test_iteratedValuesWrittenFromThread.<locals>.ThreadVerifier.writeNrGr$r<r$r%rH�rTrHcSr�)Ncss�|dg�dVdS)Nr�r=r$r2r$r$r%rS�s�

zfApplicationTests.test_iteratedValuesWrittenFromThread.<locals>.applicationFactory.<locals>.applicationr$r�r$r$r%r�r�zQApplicationTests.test_iteratedValuesWrittenFromThread.<locals>.applicationFactorycrIr1rJr�r@r$r%r��rKzIApplicationTests.test_iteratedValuesWrittenFromThread.<locals>.cbRenderedr�r�rDrLrMr$r@r%�$test_iteratedValuesWrittenFromThread�s
�z5ApplicationTests.test_iteratedValuesWrittenFromThreadc	rB)
zY
        The response status is set on the request object in the reactor thread.
        crP)zEApplicationTests.test_statusWrittenFromThread.<locals>.ThreadVerifiercs��t��t�|||�Sr1)r�rr�setResponseCode)r �coder<r<r$r%rP�szUApplicationTests.test_statusWrittenFromThread.<locals>.ThreadVerifier.setResponseCodeN)r'r(r)rPr$r<r$r%rH�rTrHcSr�)NcSr�r�r�r2r$r$r%rS�rpz^ApplicationTests.test_statusWrittenFromThread.<locals>.applicationFactory.<locals>.applicationr$r�r$r$r%r�r�zIApplicationTests.test_statusWrittenFromThread.<locals>.applicationFactorycrIr1rJr�r@r$r%r�rKzAApplicationTests.test_statusWrittenFromThread.<locals>.cbRenderedr�r�rDrLrMr$r@r%�test_statusWrittenFromThread�s
�z-ApplicationTests.test_statusWrittenFromThreadc	svGdd�dt�}d�_�fdd���fdd�}��|�\}}�fdd	�}|�|���||td
dgdg���|t�S)
z�
        If the request connection is lost while the application object is being
        iterated, iteration is stopped.
        c@r+)zSApplicationTests.test_connectionClosedDuringIteration.<locals>.UnreliableConnectionz�
            This is a request which pretends its connection is lost immediately
            after the first write is done to it.
            cSs|�ttd���dS)NzNo more connection)�connectionLostr
r
rEr$r$r%r{rzYApplicationTests.test_connectionClosedDuringIteration.<locals>.UnreliableConnection.writeN)r'r(r)r*r{r$r$r$r%�UnreliableConnectionsrTFc3s�dVd�_td��)Nr=TzShould not have gotten here)�badIterrXr$r8r$r%�appIter"s�zFApplicationTests.test_connectionClosedDuringIteration.<locals>.appItercr�)Ncs|dg���S�Nr�r$r2�rVr$r%rS(s
zfApplicationTests.test_connectionClosedDuringIteration.<locals>.applicationFactory.<locals>.applicationr$r�rXr$r%r'r�zQApplicationTests.test_connectionClosedDuringIteration.<locals>.applicationFactorycs���jd�dS)Nz!Should not have resumed iteration)rrUr�r8r$r%r�0rzIApplicationTests.test_connectionClosedDuringIteration.<locals>.cbRenderedr�r�rD)rrUr�r�r�rr�r
)r rTrr�r~r�r$)rVr r%�$test_connectionClosedDuringIterations	
�z5ApplicationTests.test_connectionClosedDuringIterationcs^t���fdd�}���\}}��fdd�}|�|���||�fdd�ddgd	gdg�	|S)
Ncrr1r$r$r�r$r%r>�zEApplicationTests._internalServerErrorTest.<locals>.applicationFactorycs6��t�}��t|�d����jj���d��dS)NrUs"HTTP/1.1 500 Internal Server Error)	r^r?r�r[r:rr�r�r�)r�rDr�r$r%r�Cs
��z=ApplicationTests._internalServerErrorTest.<locals>.cbRenderedcrr1r$r$rr$r%r5Rr6z;ApplicationTests._internalServerErrorTest.<locals>.<lambda>r�r�rDr�)r rSrr�r~r�r$)rSr�r r%�_internalServerErrorTest;s"


�z)ApplicationTests._internalServerErrorTestcC�dd�}|�|�S)z�
        If the application raises an exception before calling I{start_response}
        then the response status is I{500} and the exception is logged.
        cS�td���N� This application had some error.�r?r2r$r$r%rSc�zRApplicationTests.test_applicationExceptionBeforeStartResponse.<locals>.application�r[�r rSr$r$r%�,test_applicationExceptionBeforeStartResponse]s
z=ApplicationTests.test_applicationExceptionBeforeStartResponsecCr\)z�
        If the application calls I{start_response} but then raises an exception
        before any data is written to the response then the response status is
        I{500} and the exception is logged.
        cSs|dg�td���Nr�r_r`r2r$r$r%rSorpzQApplicationTests.test_applicationExceptionAfterStartResponse.<locals>.applicationrbrcr$r$r%�+test_applicationExceptionAfterStartResponsehs
z<ApplicationTests.test_applicationExceptionAfterStartResponsecs�t��t��t���fdd�}���\}�g���fdd�}����fdd�}|�|���||�fdd�d	d
gdgdg�	���jj	��d�
ttd
���|S)Ncrr1r$r$r�r$r%rzrZzBApplicationTests._connectionClosedTest.<locals>.applicationFactorycs���|i|����dSrt)r�)r"r#)r~�requestsr$r%�requestFactoryWrapper�szEApplicationTests._connectionClosedTest.<locals>.requestFactoryWrappercsd��dt����d}|d}��|jt���t��jj��}��	|�
d�����|�dS)NrUrrVsHTTP/1.1 200 OK)rZr[r\r]r?r^rr�r�r:r��assertIn)r��eventr!r�)r�r_�responseContentr r$r%�
ebRendered�s
z:ApplicationTests._connectionClosedTest.<locals>.ebRenderedcrr1r$r$rr$r%r5�r6z8ApplicationTests._connectionClosedTest.<locals>.<lambda>r�r�rDrzAll gone)
rrrWrr��
addErrbackr�r:r�disconnectedrSr
r
)r rSrkrr�rhrlr$)rSr�r_r~rgrkr r%�_connectionClosedTestus,

�
z&ApplicationTests._connectionClosedTestcsd��fdd�}|�|��S)z�
        If the application raises an exception after the response status has
        already been sent then the connection is closed and the exception is
        logged.
        s?Some bytes, triggering the server to start sending the responsec3s�|dg��Vtd��rer`r2�rkr$r%rS�s�
zIApplicationTests.test_applicationExceptionAfterWrite.<locals>.application�rorcr$rpr%�#test_applicationExceptionAfterWrite�s�z4ApplicationTests.test_applicationExceptionAfterWritecs"d�G�fdd�d�}|�|��S)z�
        If the application returns a closeable iterator and the C{close} method
        raises an exception when called then the connection is still closed and
        the exception is logged.
        r=cs(eZdZdd�Z�fdd�Zdd�ZdS)zDApplicationTests.test_applicationCloseException.<locals>.ApplicationcSs|dg�dSrWr$)r r3r4r$r$r%rE�szMApplicationTests.test_applicationCloseException.<locals>.Application.__init__c3s��VdSr1r$r8rpr$r%r4�s�
zMApplicationTests.test_applicationCloseException.<locals>.Application.__iter__cSr]r^r`r8r$r$r%r5�razJApplicationTests.test_applicationCloseException.<locals>.Application.closeNr6r$rpr$r%�Application�srsrq)r rsr$rpr%�test_applicationCloseException�s
z/ApplicationTests.test_applicationCloseExceptionN)r'r(r)r*r,r;rArNrOrRrYr[rdrfrorrrtr$r$r$r%r(Rs-#"! *"
2r()<r*r�rr��sysr�urllib.parserri�zope.interface.verifyr�twisted.internetr�twisted.internet.addressrr�twisted.internet.deferrr	�twisted.internet.errorr
�twisted.loggerrr�twisted.python.failurer
�twisted.python.threadabler�twisted.python.threadpoolr�twisted.test.proto_helpersr�twisted.trial.unittestrr�twisted.webr�twisted.web.resourcerr�twisted.web.serverrrr�twisted.web.test.test_webr�twisted.web.wsgirrr,r/rar�rLr�r�r�r�r�r(r$r$r$r%�<module>sZp6y)	

Anon7 - 2022
AnonSec Team