HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/lib/python3/dist-packages/twisted/web/test/__pycache__/test_proxy.cpython-38.pyc
U


W[|N�@s�dZddlmZddlmZddlmZddlmZddlm	Z	ddl
mZmZddl
m
Z
mZmZdd	lmZGd
d�de�ZGdd
�d
e�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZdS)z 
Test for L{twisted.web.proxy}.
�)�TestCase)� StringTransportWithDisconnection)�
MemoryReactor)�Resource)�Site)�ReverseProxyResource�ProxyClientFactory)�ProxyClient�ProxyRequest�ReverseProxyRequest��DummyRequestc@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�ReverseProxyResourceTestsz,
    Tests for L{ReverseProxyResource}.
    c
Cs�t�}t�}tddd|�}|�d|�t|�}t�}|�d�}|�|�|�|j	d�|�
d|d�|�t|j
�d�|�|j
d	d	d�|�|j
d	dd�|j
d	d
}	|�|	t�|�|	j|�|�|	jdd�dS)
z�
        Check that a request pointing at C{uri} produce a new proxy connection,
        with the path of this request pointing at C{expectedURI}.
        �	127.0.0.1���/pathsindexNsGET s  HTTP/1.1
Accept: text/html

�r��hosts127.0.0.1:1234)rrrZputChildrr�
buildProtocol�makeConnectionZ
addCleanup�connectionLost�dataReceived�assertEqual�len�
tcpClients�assertIsInstancer�rest�headers)
�self�uri�expectedURI�root�reactor�resourceZsite�	transport�channel�factory�r(�=/usr/lib/python3/dist-packages/twisted/web/test/test_proxy.py�_testRenders*

��z%ReverseProxyResourceTests._testRendercCs|�dd�S)z�
        Test that L{ReverseProxyResource.render} initiates a connection to the
        given server with a L{ProxyClientFactory} as parameter.
        s/indexr�r*�rr(r(r)�test_render9sz%ReverseProxyResourceTests.test_rendercCs|�dd�S)zr
        Test that L{ReverseProxyResource.render} passes query parameters to the
        created factory.
        s/index?foo=bars
/path?foo=barr+r,r(r(r)�test_renderWithQueryAsz.ReverseProxyResourceTests.test_renderWithQuerycCsjt�}tddd|�}|�dd�}|�|t�|�|jd�|�|jd�|�|jd�|�|j	|j	�dS)a
        The L{ReverseProxyResource.getChild} method should return a resource
        instance with the same class as the originating resource, forward
        port, host, and reactor values, and update the path value with the
        value passed.
        rrr�fooNs	/path/foo)
rr�getChildrr�path�port�hostZassertIdenticalr#)rr#r$�childr(r(r)�
test_getChildIsz'ReverseProxyResourceTests.test_getChildcCs*tddd�}|�dd�}|�|jd�dS)zu
        The L{ReverseProxyResource} return by C{getChild} has a path which has
        already been quoted.
        rrrs /%Ns/path/%20%2F%25)rr0rr1)rr$r4r(r(r)�test_getChildWithSpecial[sz2ReverseProxyResourceTests.test_getChildWithSpecialN)	�__name__�
__module__�__qualname__�__doc__r*r-r.r5r6r(r(r(r)rs!rc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�DummyChannelz�
    A dummy HTTP channel, that does nothing but holds a transport and saves
    connection lost.

    @ivar transport: the transport used by the client.
    @ivar lostReason: the reason saved at connection lost.
    cCs||_d|_dS)z4
        Hold a reference to the transport.
        N)r%�
lostReason)rr%r(r(r)�__init__oszDummyChannel.__init__cCs
||_dS)z;
        Keep track of the connection lost reason.
        N)r<)r�reasonr(r(r)rwszDummyChannel.connectionLostcCs
|j��S)z:
        Get peer information from the transport.
        )r%�getPeerr,r(r(r)r?~szDummyChannel.getPeercCs
|j��S)z:
        Get host information from the transport.
        )r%�getHostr,r(r(r)r@�szDummyChannel.getHostN)r7r8r9r:r=rr?r@r(r(r(r)r;fs
r;c@s�eZdZdZdd�Zdd�Zd&d	d
�Zdd�Zd
d�Zdd�Z	dd�Z
d'dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�ZdS)(�ProxyClientTestsz#
    Tests for L{ProxyClient}.
    cCs:|�d�\}}|�d�}|�d�}|tdd�|D��|fS)aC
        Parse the headers out of some web content.

        @param content: Bytes received from a web server.
        @return: A tuple of (requestLine, headers, body). C{headers} is a dict
            of headers, C{requestLine} is the first line (e.g. "POST /foo ...")
            and C{body} is whatever is left.
        s

�
rcss|]}|�d�VqdS)�: N)�split)�.0�headerr(r(r)�	<genexpr>�sz4ProxyClientTests._parseOutHeaders.<locals>.<genexpr>)rD�pop�dict)rZcontentr�body�requestLiner(r(r)�_parseOutHeaders�s	

�z!ProxyClientTests._parseOutHeaderscCst|�S)z�
        Make a dummy request object for the URL path.

        @param path: A URL path, beginning with a slash.
        @return: A L{DummyRequest}.
        r)rr1r(r(r)�makeRequest�szProxyClientTests.makeRequest�GETN�cCs,|dkrddi}d|j}t||d|||�S)a�
        Make a L{ProxyClient} object used for testing.

        @param request: The request to use.
        @param method: The HTTP method to use, GET by default.
        @param headers: The HTTP headers to use expressed as a dict. If not
            provided, defaults to {'accept': 'text/html'}.
        @param requestBody: The body of the request. Defaults to the empty
            string.
        @return: A L{ProxyClient}
        N�accept�	text/html�/�HTTP/1.0)Zpostpathr	)r�request�methodr�requestBodyr1r(r(r)�makeProxyClient�s

�z ProxyClientTests.makeProxyClientcCst�}||_|�|�|S)z�
        Connect a proxy client to a L{StringTransportWithDisconnection}.

        @param proxyClient: A L{ProxyClient}.
        @return: The L{StringTransportWithDisconnection}.
        )rZprotocolr)r�proxyClientZclientTransportr(r(r)�connectProxy�s
zProxyClientTests.connectProxycCs@|�|�|j��}|�|�\}}}|�||�|�||�|S)a`
        Assert that C{proxyClient} sends C{headers} when it connects.

        @param proxyClient: A L{ProxyClient}.
        @param requestLine: The request line we expect to be sent.
        @param headers: A dict of headers we expect to be sent.
        @return: If the assertion is successful, return the request body as
            bytes.
        )rYr%�valuerLr)rrXrKrZrequestContentZreceivedLine�receivedHeadersrJr(r(r)�assertForwardsHeaders�s


�
z&ProxyClientTests.assertForwardsHeadersc	Cs^dt|��d�d|g}|D]$\}}|D]}|�|d|�q,q |�d|g�d�|�S)Ns	HTTP/1.0 �ascii� rCrOrB)�str�encode�append�extend�join)	r�code�messagerrJ�linesrF�valuesrZr(r(r)�makeResponseBytes�sz"ProxyClientTests.makeResponseBytescCsj|�|j|�|�|j|�t|j���}|��|dd�}|��|�||�|�d�|j�|�dS)aK
        Assert that C{request} has forwarded a response from the server.

        @param request: A L{DummyRequest}.
        @param code: The expected HTTP response code.
        @param message: The expected HTTP message.
        @param headers: The expected HTTP headers.
        @param body: The expected response body.
        NrO)	r�responseCode�responseMessage�list�responseHeaders�getAllRawHeaders�sortrc�written)rrTrdrerrJr[�expectedHeadersr(r(r)�assertForwardsResponse�s
z'ProxyClientTests.assertForwardsResponseTcCs�|�d�}|�||ddi|�}	|�|	|dddd��}
|�|
|�|	�|�||||��|�|||||�|rx|	j��|�	|	jj
�|�|jd�dS)	z�
        Build a fake proxy connection, and send C{data} over it, checking that
        it's forwarded to the originating request.
        r/rPrQs /foo HTTP/1.0�close)�
connectionrPrN)rMrWr\rrrhrqr%�loseConnectionZassertFalseZ	connected�finished)rrdrerrJrUrVrtrT�clientZreceivedBodyr(r(r)�_testDataForward�s*
���
z!ProxyClientTests._testDataForwardcCs|�dddddgfgd�S)z�
        When connected to the server, L{ProxyClient} should send the saved
        request, with modifications of the headers, and then forward the result
        to the parent request.
        ���OK�Foo�barsbaz�Some data
�rwr,r(r(r)�test_forward s�zProxyClientTests.test_forwardcCs|�ddddgfgddd�S)z~
        Try to post content in the request, and check that the proxy client
        forward the body of the request.
        rxryrzr{r|�POST�Some contentr}r,r(r(r)�
test_postData*s
�zProxyClientTests.test_postDatacCs|�ddgd�S)z�
        If the response contains a status with a message, it should be
        forwarded to the parent request with all the information.
        i�s	Not FoundrOr}r,r(r(r)�test_statusWithMessage3s�z'ProxyClientTests.test_statusWithMessagecCs*d}|�dddtt|���d�gfg|�S)z�
        If the response contains a I{Content-Length} header, the inbound
        request object should still only have C{finish} called on it once.
        �foo bar bazrxry�Content-Lengthr]�rwr_rr`�r�datar(r(r)�test_contentLength<s�z#ProxyClientTests.test_contentLengthcCs.d}|jdddtt|���d�gfg|dd�S)z�
        If the response contains a I{Content-Length} header, the outgoing
        connection is closed when all response body data has been received.
        r�rxryr�r]F)rtr�r�r(r(r)�test_losesConnectionIs�z%ProxyClientTests.test_losesConnectioncCs0tdddddd�dd�}|�|jdd	d
��dS)z�
        The headers given at initialization should be modified:
        B{proxy-connection} should be removed if present, and B{connection}
        should be added.
        rN�/foorSrQr/)rPsproxy-connectionrONrr�rPrs)r	rr)rrvr(r(r)�test_headersCleanupsWs��z%ProxyClientTests.test_headersCleanupscCsFdddd�}|��}d|d<|d=tddd	|d
d�}|�|d|�dS)
z�
        The proxy doesn't really know what to do with keepalive things from
        the remote server, so we stomp over any keepalive header we get from
        the client.
        rQs300�
keep-alive)rPr�rsrrrsrNr�rSrONsGET /foo HTTP/1.0)�copyr	r\)rrrprvr(r(r)�test_keepaliveNotForwardedcs��z+ProxyClientTests.test_keepaliveNotForwardedcCs�|�d�}|j�ddg�|j�ddg�|j�ddg�|j|dd	id
�}|�|�dgdgd
gd�}|�|�dd|��d��|�|ddt	|���d�dS)z�
        L{server.Request} within the proxy sets certain response headers by
        default. When we get these headers back from the remote server, the
        defaults are overridden rather than simply appended.
        r/sserversold-barsdatesold-bazscontent-typesold/quxrPrQ)rr{s
2010-01-01sapplication/x-baz)sServersDate�Content-TyperxryrON)
rMrlZ
setRawHeadersrWrYrrh�itemsrqrk)rrTrvrr(r(r)�test_defaultHeadersOverriddenvs(

��
�z.ProxyClientTests.test_defaultHeadersOverridden)rNNrO)rNrOT)r7r8r9r:rLrMrWrYr\rhrqrwr~r�r�r�r�r�r�r�r(r(r(r)rA�s,
�

	�
#
		
rAc@s eZdZdZdd�Zdd�ZdS)�ProxyClientFactoryTestsz*
    Tests for L{ProxyClientFactory}.
    cCs�tdg�}tdddddid|�}|�dd�|�|jd	�|�|jd
�|�t|j���ddgfg�|�d�	|j
�d
�|�|jd�dS)z�
        Check that L{ProxyClientFactory.clientConnectionFailed} produces
        a B{501} response to the parent request.
        r/rNr�rSrPrQ�Ni�s
Gateway errorr�rOs<H1>Could not connect</H1>r)r
rZclientConnectionFailedrrirjrkrlrmrcroru)rrTr'r(r(r)�test_connectionFailed�s$
�
�
�z-ProxyClientFactoryTests.test_connectionFailedcCsntdddddidd�}|�d�}|�|t�|�|jd�|�|jd�|�|jd�|�|jddd	��dS)
z�
        L{ProxyClientFactory.buildProtocol} should produce a L{ProxyClient}
        with the same values of attributes (with updates on the headers).
        rNr�rSrPrQs	Some dataNrrr�)	rrrr	r�commandrr�r)rr'�protor(r(r)�test_buildProtocol�s�
�z*ProxyClientFactoryTests.test_buildProtocolN)r7r8r9r:r�r�r(r(r(r)r��sr�c@s:eZdZdZddd�Zdd�Zdd	�Zd
d�Zdd
�ZdS)�ProxyRequestTestsz$
    Tests for L{ProxyRequest}.
    rNrOc
Cs�t�}t|�}t�}t|d|�}|�t|��|�|�|�|d|d�|�t|j	�d�|�|j	ddd�|�|j	ddd�|j	dd}	|�
|	t�|�|	j|�|�|	j
d�|�|	jd	d
i�|�|	j|�|�|	j|�|�|	j|�dS)z�
        Build a request pointing at C{uri}, and check that a proxied request
        is created, pointing a C{expectedURI}.
        Fshttp://example.comrSrr�example.com�Prr�example.comN)rr;rr
�	gotLengthrZhandleContentChunk�requestReceivedrrrrr��versionrr�rZfather)
rr r!rUr�r%r&r#rTr'r(r(r)�_testProcess�s(
�zProxyRequestTests._testProcesscCs|�dd�S)a�
        L{ProxyRequest.process} should create a connection to the given server,
        with a L{ProxyClientFactory} as connection factory, with the correct
        parameters:
            - forward comment, version and data values
            - update headers with the B{host} value
            - remove the host from the URL
            - pass the request as parent request
        �/foo/bar�r�r,r(r(r)�test_process�s
zProxyRequestTests.test_processcCs|�dd�S)z�
        If the incoming request doesn't contain a slash,
        L{ProxyRequest.process} should add one when instantiating
        L{ProxyClientFactory}.
        rOrRr�r,r(r(r)� test_processWithoutTrailingSlash�sz2ProxyRequestTests.test_processWithoutTrailingSlashcCs|�dddd�S)zl
        L{ProxyRequest.process} should be able to retrieve request body and
        to forward it.
        r�rr�r�r,r(r(r)�test_processWithData�s�z&ProxyRequestTests.test_processWithDatacCszt�}t|�}t�}t|d|�}|�d�|�ddd�|�t|j�d�|�|jddd�|�|jddd�d	S)
z�
        Check that L{ProxyRequest.process} correctly parse port in the incoming
        URL, and create an outgoing connection with this port.
        FrrNshttp://example.com:1234/foo/barrSrr�rN)	rr;rr
r�r�rrr)rr%r&r#rTr(r(r)�test_processWithPort�s
�z&ProxyRequestTests.test_processWithPortN)rNrO)	r7r8r9r:r�r�r�r�r�r(r(r(r)r��s

		r�c@seZdZdZdd�ZdS)�DummyFactoryz>
    A simple holder for C{host} and C{port} information.
    cCs||_||_dS)N)r3r2)rr3r2r(r(r)r=szDummyFactory.__init__N)r7r8r9r:r=r(r(r(r)r�sr�c@seZdZdZdd�ZdS)�ReverseProxyRequestTestsz+
    Tests for L{ReverseProxyRequest}.
    cCs�t�}t|�}t�}t|d|�}tdd�|_|�d�|�ddd�|�t	|j
�d�|�|j
ddd�|�|j
ddd�|j
dd	}|�|t�|�|j
d
di�dS)
a
        L{ReverseProxyRequest.process} should create a connection to its
        factory host/port, using a L{ProxyClientFactory} instantiated with the
        correct parameters, and particularly set the B{host} header to the
        factory host.
        Fr�rrrNr�rSrrrr�N)rr;rrr�r'r�r�rrrrrr)rr%r&r#rTr'r(r(r)r�s
z%ReverseProxyRequestTests.test_processN)r7r8r9r:r�r(r(r(r)r�sr�N)r:Ztwisted.trial.unittestrZtwisted.test.proto_helpersrrZtwisted.web.resourcerZtwisted.web.serverrZtwisted.web.proxyrrr	r
rZtwisted.web.test.test_webr
r�objectr;rAr�r�r�r�r(r(r(r)�<module>s"S',T