%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/web/test/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/web/test/__pycache__/test_proxy.cpython-312.pyc

�

Ϫ�fLN���dZddlmZmZddlmZddlmZmZm	Z	m
Z
mZddlm
Z
ddlmZddlmZGd�d	e�ZGd
�d�ZGd�d
e�ZGd�de�ZGd�de�ZGd�d�ZGd�de�Zy)z 
Test for L{twisted.web.proxy}.
�)�
MemoryReactor� StringTransportWithDisconnection)�TestCase)�ProxyClient�ProxyClientFactory�ProxyRequest�ReverseProxyRequest�ReverseProxyResource)�Resource)�Site��DummyRequestc�4�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	y)	�ReverseProxyResourceTestsz,
    Tests for L{ReverseProxyResource}.
    c�R�t�}t�}tddd|�}|jd|�t	|�}t�}|j
d�}|j|�|j|jd�|jd|zdz�|j\\}	}
}}}
|j|	d�|j|
d�|j|t�|j|j|�|j|j dd	�y)
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

�hosts127.0.0.1:1234)rrr
�putChildrr�
buildProtocol�makeConnection�
addCleanup�connectionLost�dataReceived�
tcpClients�assertEqual�assertIsInstancer�rest�headers)�self�uri�expectedURI�root�reactor�resource�site�	transport�channel�host�port�factory�_timeout�
_bind_addrs              �=/usr/lib/python3/dist-packages/twisted/web/test/test_proxy.py�_testRenderz%ReverseProxyResourceTests._testRenders��
�z���/��'��T�8�W�M���
�
�h��)��D�z��4�6�	��$�$�T�*�����y�)�����.�.��5����W�s�]�-V�V�W�8?�8J�8J�5�	4�$��g�x������{�+�����t�$�	
���g�'9�:�������{�3��������1�3D�E�c�&�|jdd�S)z�
        Test that L{ReverseProxyResource.render} initiates a connection to the
        given server with a L{ProxyClientFactory} as parameter.
        s/indexr�r0�r!s r/�test_renderz%ReverseProxyResourceTests.test_render8s��
���	�8�4�4r1c�&�|jdd�S)z�
        Test that L{ReverseProxyResource.render} will instantiate a child
        resource that will initiate a connection to the given server
        requesting the apropiate url subpath.
        s/index/page1s/path/page1r3r4s r/�test_render_subpagez-ReverseProxyResourceTests.test_render_subpage?s�������@�@r1c�&�|jdd�S)zr
        Test that L{ReverseProxyResource.render} passes query parameters to the
        created factory.
        s/index?foo=bars
/path?foo=barr3r4s r/�test_renderWithQueryz.ReverseProxyResourceTests.test_renderWithQueryGs��
��� 1�3C�D�Dr1c�x�t�}tddd|�}|jdd�}|j|t�|j	|j
d�|j	|jd�|j	|jd�|j|j|j�y)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�pathr+r*�assertIdenticalr%)r!r%r&�childs    r/�
test_getChildz'ReverseProxyResourceTests.test_getChildNs��� �/��'��T�8�W�M���!�!�&�$�/�����e�%9�:�������\�2�������T�*�������[�1����U�]�]�H�,<�,<�=r1c�z�tddd�}|jdd�}|j|jd�y)zu
        The L{ReverseProxyResource} return by C{getChild} has a path which has
        already been quoted.
        rrrs /%Ns/path/%20%2F%25)r
r<rr=)r!r&r?s   r/�test_getChildWithSpecialz2ReverseProxyResourceTests.test_getChildWithSpecial_s9��
(��T�8�D���!�!�&�$�/��������%7�8r1N)
�__name__�
__module__�__qualname__�__doc__r0r5r7r9r@rB�r1r/rrs(���F�:5�A�E�>�"9r1rc�(�eZdZdZd�Zd�Zd�Zd�Zy)�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.
    c� �||_d|_y)z4
        Hold a reference to the transport.
        N)r(�
lostReason)r!r(s  r/�__init__zDummyChannel.__init__rs��#�����r1c��||_y)z;
        Keep track of the connection lost reason.
        N)rK)r!�reasons  r/rzDummyChannel.connectionLostys��!��r1c�6�|jj�S)z:
        Get peer information from the transport.
        )r(�getPeerr4s r/rPzDummyChannel.getPeer����~�~�%�%�'�'r1c�6�|jj�S)z:
        Get host information from the transport.
        )r(�getHostr4s r/rSzDummyChannel.getHost�rQr1N)rCrDrErFrLrrPrSrGr1r/rIrIis����!�(�(r1rIc�z�eZdZdZd�Zd�Zdd�Zd�Zd�Zd�Z	d	�Z
			dd
�Zd�Zd�Z
d
�Zd�Zd�Zd�Zd�Zd�Zy)�ProxyClientTestsz#
    Tests for L{ProxyClient}.
    c��|jd�\}}|jd�}|jd�}|td�|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

�
rc3�>K�|]}|jd����y�w)�: N)�split)�.0�headers  r/�	<genexpr>z4ProxyClientTests._parseOutHeaders.<locals>.<genexpr>�s����!L�&�&�,�,�u�"5�!L�s�)rZ�pop�dict)r!�contentr �body�requestLines     r/�_parseOutHeadersz!ProxyClientTests._parseOutHeaders�sI�� �
�
�k�2�
����-�-��(���k�k�!�n���T�!L�G�!L�L�d�S�Sr1c��t|�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
)r!r=s  r/�makeRequestzProxyClientTests.makeRequest�s���D�!�!r1Nc�L�|�ddi}d|jz}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}
        �accept�	text/html�/�HTTP/1.0)�postpathr)r!�request�methodr �requestBodyr=s      r/�makeProxyClientz ProxyClientTests.makeProxyClient�s8���?� �,�/�G��g�&�&�&���6�4��g�{�G�T�Tr1c�J�t�}||_|j|�|S)z�
        Connect a proxy client to a L{StringTransportWithDisconnection}.

        @param proxyClient: A L{ProxyClient}.
        @return: The L{StringTransportWithDisconnection}.
        )r�protocolr)r!�proxyClient�clientTransports   r/�connectProxyzProxyClientTests.connectProxy�s(��;�<��#.�� ��"�"�?�3��r1c���|j|�|jj�}|j|�\}}}|j	||�|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.
        )rtr(�valuercr)r!rrrbr �requestContent�receivedLine�receivedHeadersras        r/�assertForwardsHeadersz&ProxyClientTests.assertForwardsHeaders�sb��	
���+�&�$�.�.�4�4�6��.2�.C�.C�N�.S�+��o�t�����{�3�����'�2��r1c���dt|�jd�zdz|zg}|D]#\}}|D]}|j|dz|z���%|jd|g�dj	|�S)Ns	HTTP/1.0 �ascii� rYr1rW)�str�encode�append�extend�join)	r!�code�messager ra�linesr\�valuesrvs	         r/�makeResponseBytesz"ProxyClientTests.makeResponseBytes�s�����D�	� 0� 0�� 9�9�D�@�7�J�K��%�	5�N�F�F��
5�����V�e�^�e�3�4�
5�	5�	���c�4�[�!��|�|�E�"�"r1c�~�|j|j|�|j|j|�t|jj��}|j
�|dd}|j
�|j||�|jdj|j�|�y)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.
        Nr1)	r�responseCode�responseMessage�list�responseHeaders�getAllRawHeaders�sortr��written)r!rlr�r�r rary�expectedHeaderss        r/�assertForwardsResponsez'ProxyClientTests.assertForwardsResponse�s���	
����-�-�t�4�����0�0�'�:��w�6�6�G�G�I�J������!�!�*����������/�:�������'�/�/�2�D�9r1c���|jd�}|j||ddi|�}	|j|	|dzddd��}
|j|
|�|	j	|j||||��|j
|||||�|r|	jj�|j|	jj�|j|jd�y)	z�
        Build a fake proxy connection, and send C{data} over it, checking that
        it's forwarded to the originating request.
        r;rgrhs /foo HTTP/1.0�close)�
connectionrg�N)rerorzrrr�r�r(�loseConnection�assertFalse�	connected�finished)r!r�r�r rarmrnr�rl�client�receivedBodys           r/�_testDataForwardz!ProxyClientTests._testDataForward�s����"�"�6�*���%�%��V�i��6��
���1�1���&�&�$��>�
��	
����{�3�	���D�2�2�4��'�4�P�Q�	
�#�#�G�T�7�G�T�J�����+�+�-�
	
����)�)�3�3�4�����)�)�1�-r1c�4�|jdddddgfgd�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
�r�r4s r/�test_forwardzProxyClientTests.test_forwards.���$�$���&�6�6�"2�3�4�6F�
�	
r1c�6�|jddddgfgddd�S)z~
        Try to post content in the request, and check that the proxy client
        forward the body of the request.
        r�r�r�r�r��POST�Some contentr�r4s r/�
test_postDatazProxyClientTests.test_postData's/��
�$�$���&�6�(�+�,�.>���
�	
r1c�*�|jddgd�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 Foundr1r�r4s r/�test_statusWithMessagez'ProxyClientTests.test_statusWithMessage0s��
�$�$�S�,��C�@�@r1c
�x�d}|jdddtt|��jd�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 bazr�r��Content-Lengthr|�r�r~�lenr�r!�datas  r/�test_contentLengthz#ProxyClientTests.test_contentLength7sE��
���$�$���+�c�#�d�)�n�.C�.C�G�.L�-M�N�O�QU�
�	
r1c
�|�d}|jdddtt|��jd�gfg|d��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�r�r�r�r|F)r�r�r�s  r/�test_losesConnectionz%ProxyClientTests.test_losesConnectionAsN��
���$�$����#�c�$�i�.�"7�"7��"@�!A�
B�C�� �%�
�	
r1c�h�tdddddd�dd�}|j|jdd	d
��y)z�
        The headers given at initialization should be modified:
        B{proxy-connection} should be removed if present, and B{connection}
        should be added.
        �GET�/foorjrhr;)rgsproxy-connectionr1Nr��rgr�)rrr )r!r�s  r/�test_headersCleanupsz%ProxyClientTests.test_headersCleanupsOsC������$�6�B���

��	
����N�N��X�N�	
r1c��dddd�}|j�}d|d<|d=tddd	|d
d�}|j|d|�y)
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.
        rhs300�
keep-alive)rgr�r�r�r�r�r�rjr1NsGET /foo HTTP/1.0)�copyrrz)r!r r�r�s    r/�test_keepaliveNotForwardedz+ProxyClientTests.test_keepaliveNotForwardedasZ��$�!�(�
��
"�,�,�.��)1��
�&��M�*��V�W�k�7�C��N���"�"�6�+?��Qr1c	���|jd�}|jjddg�|jjddg�|jjddg�|j|dd	i�
�}|j	|�dgdgd
gd�}|j|j
dd|j�d��|j|ddt|j��d�y)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/quxrgrh)r r�s
2010-01-01sapplication/x-baz)sServersDate�Content-Typer�r�r1N)
rer��
setRawHeadersrortrr��itemsr�r�)r!rlr�r s    r/�test_defaultHeadersOverriddenz.ProxyClientTests.test_defaultHeadersOverriddenrs����"�"�6�*�����-�-�i�*��F����-�-�g�
�|�D����-�-�o�
�|�L��%�%�g�	�<�7P�%�Q�����&�!��x�#�_�2�3�
��
	���D�2�2�3��w�}�}��PS�T�U��#�#�G�S�%��g�m�m�o�9N�PS�Tr1)r�Nr1)r�r1T)rCrDrErFrcrerortrzr�r�r�r�r�r�r�r�r�r�r�rGr1r/rUrU�sh���T�"�U�"
��"#�:�2���*.�X
�
�A�
�
�
�$R�"Ur1rUc��eZdZdZd�Zd�Zy)�ProxyClientFactoryTestsz*
    Tests for L{ProxyClientFactory}.
    c���tdg�}tdddddid|�}|jdd�|j|jd	�|j|j
d
�|jt
|jj��ddgfg�|jdj|j�d
�|j|jd�y)z�
        Check that L{ProxyClientFactory.clientConnectionFailed} produces
        a B{501} response to the parent request.
        r;r�r�rjrgrh�Ni�s
Gateway errorr�r1s<H1>Could not connect</H1>r�)rr�clientConnectionFailedrr�r�r�r�r�r�r�r�)r!rlr,s   r/�test_connectionFailedz-ProxyClientFactoryTests.test_connectionFailed�s���
��x�(��$��G�[�9�l�*C�R��
��	�&�&�t�T�2�����-�-�s�3�����0�0�2B�C������(�(�9�9�;�<���~�
.�/�	
�	
������'�/�/�2�4Q�R�����)�)�1�-r1c�\�tdddddidd�}|jd�}|j|t�|j	|j
d�|j	|jd�|j	|jd�|j	|jddd	��y)
z�
        L{ProxyClientFactory.buildProtocol} should produce a L{ProxyClient}
        with the same values of attributes (with updates on the headers).
        r�r�rjrgrhs	Some dataNr�r�)	rrrrr�commandrr�r )r!r,�protos   r/�test_buildProtocolz*ProxyClientFactoryTests.test_buildProtocol�s���
%��G�[�9�l�*C�\�SW�
���%�%�d�+�����e�[�1��������/�������W�-�������\�2�����M�M�|�H�M�	
r1N)rCrDrErFr�r�rGr1r/r�r��s���.�(
r1r�c�0�eZdZdZdd�Zd�Zd�Zd�Zd�Zy)	�ProxyRequestTestsz$
    Tests for L{ProxyRequest}.
    c�T�t�}t|�}t�}t|d|�}|j	t|��|j
|�|j|d|zd�|jt|j�d�|j|jddd�|j|jddd�|jdd}	|j|	t�|j|	j|�|j|	jd�|j|	jd	d
i�|j|	j|�|j|	j |�|j|	j"|�y)z�
        Build a request pointing at C{uri}, and check that a proxied request
        is created, pointing a C{expectedURI}.
        Fshttp://example.comrjr�r�example.com�P�r�example.comN)rrIrr�	gotLengthr��handleContentChunk�requestReceivedrrrrr��versionr r�r�father)
r!r"r#rmr�r(r)r%rlr,s
          r/�_testProcesszProxyRequestTests._testProcess�sa��
5�6�	��y�)���/���w��w�7�����#�d�)�$��"�"�4�(�����(=��(C�[�Q�����W�/�/�0�!�4�����+�+�A�.�q�1�=�A�����+�+�A�.�q�1�2�6��$�$�Q�'��*�����g�'9�:�������&�1�������+�6�������7�N�*C�D�������t�,�������{�3��������1r1c�&�|jdd�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�r4s r/�test_processzProxyRequestTests.test_process�s��� � ��k�:�:r1c�&�|jdd�S)z�
        If the incoming request doesn't contain a slash,
        L{ProxyRequest.process} should add one when instantiating
        L{ProxyClientFactory}.
        r1rir�r4s r/� test_processWithoutTrailingSlashz2ProxyRequestTests.test_processWithoutTrailingSlash�s��� � ��d�+�+r1c�*�|jdddd�S)zl
        L{ProxyRequest.process} should be able to retrieve request body and
        to forward it.
        r�r�r�r�r4s r/�test_processWithDataz&ProxyRequestTests.test_processWithData�s��
� � ��k�7�O�T�Tr1c�v�t�}t|�}t�}t|d|�}|j	d�|jddd�|j
t|j�d�|j
|jddd�|j
|jddd�y	)
z�
        Check that L{ProxyRequest.process} correctly parse port in the incoming
        URL, and create an outgoing connection with this port.
        Frr�shttp://example.com:1234/foo/barrjr�r�rN)	rrIrrr�r�rr�r)r!r(r)r%rls     r/�test_processWithPortz&ProxyRequestTests.test_processWithPort�s���
5�6�	��y�)���/���w��w�7�����!������(J�K�X�	
����W�/�/�0�!�4�����+�+�A�.�q�1�=�A�����+�+�A�.�q�1�4�8r1N)r�r1)	rCrDrErFr�r�r�r�r�rGr1r/r�r��s!���2�4
;�,�U�9r1r�c��eZdZdZd�Zy)�DummyFactoryz>
    A simple holder for C{host} and C{port} information.
    c� �||_||_y)N)r*r+)r!r*r+s   r/rLzDummyFactory.__init__s����	���	r1N)rCrDrErFrLrGr1r/r�r��s���r1r�c��eZdZdZd�Zy)�ReverseProxyRequestTestsz+
    Tests for L{ReverseProxyRequest}.
    c�$�t�}t|�}t�}t|d|�}t	dd�|_|j
d�|jddd�|jt|j�d�|j|jddd�|j|jddd�|jdd	}|j|t�|j|jd
di�y)
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�rrr�r�rjr�r�rr�N)rrIrr	r�r,r�r�rr�rrrr )r!r(r)r%rlr,s      r/r�z%ReverseProxyRequestTests.test_process
s���5�6�	��y�)���/��%�g�u�g�>��&�}�d�;������!�������[�A�	
����W�/�/�0�!�4�����+�+�A�.�q�1�=�A�����+�+�A�.�q�1�4�8��$�$�Q�'��*�����g�'9�:�������7�N�*C�Dr1N)rCrDrErFr�rGr1r/r�r�s
���Er1r�N)rF�twisted.internet.testingrr�twisted.trial.unittestr�twisted.web.proxyrrrr	r
�twisted.web.resourcer�twisted.web.serverr�twisted.web.test.test_webrrrIrUr�r�r�r�rGr1r/�<module>r�s����U�+���*�#�2�P9��P9�f (� (�FxU�x�xU�v(
�h�(
�VI9��I9�X��E�x�Er1

Zerion Mini Shell 1.0