%PDF- %PDF-
Mini Shell

Mini Shell

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

�

Ϫ�fRF���dZddlmZddlmZ	ddlZeZddlm	Z	ddl
mZddlm
Z
mZddlmZdd	lmZmZdd
lmZddlmZddlmZdd
lmZddlmZmZmZm Z m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'm(Z(Gd�de jR�Z*Gd�dejV�Z+Gd�de,�Z-Gd�de�Z.Gd�d�Z/Gd�de�Z0y#e$rdZY��wxYw)z#
Tests for L{twisted.web.distrib}.
�)�abspath)�parseStringN)�skipIf)�verifyObject)�defer�reactor)�globalLogPublisher)�failure�filepath)�pb��
SIZE_LIMIT)�
proto_helpers)�TestCase)�client�distrib�resource�server�static)�Headers)�_render)�DummyChannel�DummyRequestc��eZdZy)�MySiteN)�__name__�
__module__�__qualname__���?/usr/lib/python3/dist-packages/twisted/web/test/test_distrib.pyrr#s��r rc��eZdZdZdZd�Zy)�PBServerFactoryz�
    A PB server factory which keeps track of the most recent protocol it
    created.

    @ivar proto: L{None} or the L{Broker} instance most recently returned
        from C{buildProtocol}.
    Nc�d�tjj||�|_|jS�N)rr#�
buildProtocol�proto)�self�addrs  r!r&zPBServerFactory.buildProtocol2s&���'�'�5�5�d�D�A��
��z�z�r )rrr�__doc__r'r&rr r!r#r#'s���
�E�r r#c��eZdZdZy)�ArbitraryErrorz%
    An exception for this test.
    N)rrrr*rr r!r,r,7s��r r,c�j�eZdZdZdZdZdZd�Zd�Zd�Z	d�Z
d�Zd�Zd�Z
d	�Zd
�Zd�Zd�Zd
�Zd�Zy)�DistribTestsNc�R��tj�tj�g�|j�?|jj�)|jjj	�fd��n�djd�|j��|jj�k|jjjj	�fd��|jjjjj�n�djd�|j�)�j|jj��|j�)�j|jj��tj��S)z�
        Clean up all the event sources left behind by either directly by
        test methods or indirectly via some distrib API.
        Nc�,���djd�S�Nr��callback��dls�r!�<lambda>z'DistribTests.tearDown.<locals>.<lambda>Js���R��U�^�^�D�5I�r rc�,���djd�S)N�r2r4s�r!r6z'DistribTests.tearDown.<locals>.<lambda>Ns����A����PT�AU�r r8)r�Deferred�f1r'�notifyOnDisconnectr3�sub�	publisher�broker�	transport�loseConnection�port1�append�
stopListening�port2�
gatherResults)r(r5s @r!�tearDownzDistribTests.tearDownCs���
�n�n����� 0�
1���7�7��4�7�7�=�=�#<��G�G�M�M�,�,�-I�J��q�E�N�N�4� ��8�8��D�H�H�$6�$6�$B��H�H���%�%�8�8�9U�V��H�H���%�%�/�/�>�>�@��q�E�N�N�4� ��:�:�!��I�I�d�j�j�.�.�0�1��:�:�!��I�I�d�j�j�.�.�0�1��"�"�2�&�&r c��tj�}|jdtjdd��tj|�}ttj|��|_
tjd|j�|_
tjd|jj�j �|_tj�}|jd|j"�t%|�}tjd|�|_t)j*t�}d|j&j�j �d�}|j-d	�}|j/d
|�}|j1t(j2�|j1|j4d�|S)Nstheresrootz
text/plainr�	127.0.0.1sherezhttp://127.0.0.1:z/here/there�ascii�GET)r�Resource�putChildr�Datar�Siter#r�ResourcePublisherr:r�	listenTCPrA�ResourceSubscription�getHost�portr<rrDr�Agent�encode�request�addCallback�readBody�assertEqual)r(�r1�site1�r2�f2�agent�url�ds        r!�testDistribzDistribTests.testDistribXs@��
�
�
�
 ��
���H�f�k�k�'�<�@�A����B���!�'�";�";�E�"B�C����&�&�q�$�'�'�2��
��/�/��T�Z�Z�=O�=O�=Q�=V�=V�W���
�
�
�
 ��
���G�T�X�X�&�
�B�Z���&�&�q�"�-��
����W�%��!�$�*�*�"4�"4�"6�";�";�!<�K�H���j�j��!���M�M�&�#�&��	�
�
�f�o�o�&�	�
�
�d�&�&��0��r c�b�tj�}|jd|�tj|�}tt
j|��x|_}tjd|d��}|j|j�|j�}t
j|j|j �x|_}tj|�}tjd|d��}	|j|	j�|	j�}
|	|
fS)z�
        Set up a resource on a distrib site using L{ResourcePublisher}.

        @param child: The resource to publish using distrib.

        @return: A tuple consisting of the host and port on which to contact
            the created site.
        schildrrH)�	interface)rrKrLrrNr#rrOr:rrP�
addCleanuprCrRrQ�hostrSr<)r(�child�distribRoot�distribSite�distribFactory�distribPortr)�mainRoot�mainSite�mainPort�mainAddrs           r!�_setupDistribServerz DistribTests._setupDistribServerls����'�'�)�����X�u�-��k�k�+�.��#2��%�%�k�2�$
�	
���.��'�'��>�[�Q������1�1�2��"�"�$��%�:�:�4�9�9�d�i�i�P�P���8��;�;�x�(���$�$�Q��K�H������.�.�/��#�#�%����!�!r c�"�|j|�\}}tjt�}d|j�d|j
�d�}|j
d�}|jd|fi|��}|jtj�|S)a�
        Set up a resource on a distrib site using L{ResourcePublisher} and
        then retrieve it from a L{ResourceSubscription} via an HTTP client.

        @param child: The resource to publish using distrib.
        @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when
            requesting the resource.

        @return: A L{Deferred} which fires with the result of the request.
        �http://�:�/childrIrJ)
rorrTrrerSrUrVrWrX)r(rf�kwargsrmrnr^r_r`s        r!�_requestTestzDistribTests._requestTest�s}��"�5�5�e�<���(����W�%����
�
��a��
�
��f�=���j�j��!���E�M�M�&�#�0��0��	�
�
�f�o�o�&��r c��|j|�\}}d|j�d|j�d�}|jd�}t	j
t�jd|fi|��}d�}|j|�|S)a;
        Set up a resource on a distrib site using L{ResourcePublisher} and
        then retrieve it from a L{ResourceSubscription} via an HTTP client.

        @param child: The resource to publish using distrib.
        @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when
            requesting the resource.

        @return: A L{Deferred} which fires with a tuple consisting of a
            L{twisted.test.proto_helpers.AccumulatingProtocol} containing the
            body of the response and an L{IResponse} with the response itself.
        rqrrrsrIrJc����tj���j��tj�x}�_|j
��fd��|S)Nc�����fSr%r)�_�protocol�responses ��r!r6zGDistribTests._requestAgentTest.<locals>.cbCollectBody.<locals>.<lambda>�s���X�x�$8�r )r�AccumulatingProtocol�deliverBodyrr9�closedDeferredrW)r{r`rzs` @r!�
cbCollectBodyz5DistribTests._requestAgentTest.<locals>.cbCollectBody�sE���$�9�9�;�H�� � ��*�*/�.�.�*:�:�A��'�
�M�M�8�9��Hr )	rorerSrUrrTrrVrW)r(rfrtrmrnr_r`rs        r!�_requestAgentTestzDistribTests._requestAgentTest�s}��"�5�5�e�<���(���
�
��a��
�
��f�=���j�j��!��)�F�L�L��!�)�)�&�#�@��@��	�	
�
�
�m�$��r c�*������i�tj��tj��dg�G��fd�dtj
�}���fd���j
|�tddgi���}���fd�}|j|�|S)	z�
        The request headers are available on the request object passed to a
        distributed resource's C{render} method.
        Nc���eZdZ��fd�Zy)�>DistribTests.test_requestHeaders.<locals>.ReportRequestHeadersc�t��|�d<�jt|jj���y)Nrr )�update�dict�requestHeaders�getAllRawHeaders)r(rV�reqr�s  ��r!�renderzEDistribTests.test_requestHeaders.<locals>.ReportRequestHeaders.render�s1��� ��A���%�%�d�7�+A�+A�+R�+R�+T�&U�V�r N�rrrr�)r�r�s��r!�ReportRequestHeadersr��s���
r r�c�����D�cgc]}|d��	}}�jd|��jd|��j�d|�tj��ycc}w)N�
log_formatzconnected to publisherz3could not connect to distributed web service: {msg}r)�assertInr	�removeObserver)�e�msgs�logObserverr�r(s  ���r!�
check_logsz4DistribTests.test_requestHeaders.<locals>.check_logs�s^���-8�9��A�l�O�9�D�9��M�M�2�D�9��M�M�O�QU�V��M�M�#�a�&�$�'��-�-�k�:��	:s�A#�foo�bar)�headersc�|���jjj���j�ddg�y)NsFoosbar)r:r'r;rY)�resultr�r�r(s ���r!�cbRequestedz5DistribTests.test_requestHeaders.<locals>.cbRequested�s0����G�G�M�M�,�,�Z�8����^�F�3�f�X�>r )	r�EventLoggingObserverr	�addObserverrrKrurrW)r(r�rVr�r�r�r�r�s`   @@@@r!�test_requestHeadersz DistribTests.test_requestHeaders�s����
��#�8�8�:���&�&�{�3��f��	�8�#4�#4�	�	;��#�#� �"�G�U�U�G�4D�,E�$�
��	?�	���K�(��r c���Gd�dtj�}�j|��}�fd�}|j|�|S)z�
        The response code can be set by the request object passed to a
        distributed resource's C{render} method.
        c��eZdZd�Zy)�>DistribTests.test_requestResponseCode.<locals>.SetResponseCodec�&�|jd�y)N�����setResponseCode�r(rVs  r!r�zEDistribTests.test_requestResponseCode.<locals>.SetResponseCode.render�s���'�'��,�r Nr�rr r!�SetResponseCoder�����
r r�c����j|djd��j|djd��j|djd�y)Nrr r8r�sOK�rY�data�code�phrase�r�r(s �r!r�z:DistribTests.test_requestResponseCode.<locals>.cbRequested�sM������V�A�Y�^�^�S�1����V�A�Y�^�^�S�1����V�A�Y�-�-�u�5r �rrKr�rW�r(r�rVr�s`   r!�test_requestResponseCodez%DistribTests.test_requestResponseCode�sD���	�h�/�/�	�
�(�(��):�;��	6�
	���K�(��r c���Gd�dtj�}�j|��}�fd�}|j|�|S)z�
        The response code and message can be set by the request object passed to
        a distributed resource's C{render} method.
        c��eZdZd�Zy)�EDistribTests.test_requestResponseCodeMessage.<locals>.SetResponseCodec�(�|jdd�y)Nr��some-messager�r�r�s  r!r�zLDistribTests.test_requestResponseCodeMessage.<locals>.SetResponseCode.render�s���'�'��_�=�r Nr�rr r!r�r��r�r r�c����j|djd��j|djd��j|djd�y)Nrr r8r�r�r�r�s �r!r�zADistribTests.test_requestResponseCodeMessage.<locals>.cbRequested�sM������V�A�Y�^�^�S�1����V�A�Y�^�^�S�1����V�A�Y�-�-��?r r�r�s`   r!�test_requestResponseCodeMessagez,DistribTests.test_requestResponseCodeMessage�sE���	�h�/�/�	�
�(�(��):�;��	@�
	���K�(��r c��Gd�dtj�}|j|��}|j|jdt
zdz�|S)z�
        If a string longer than the Banana size limit is passed to the
        L{distrib.Request} passed to the remote resource, it is broken into
        smaller strings to be transported over the PB connection.
        c��eZdZd�Zy)�0DistribTests.test_largeWrite.<locals>.LargeWritec�x�|jdtzdz�|j�tjS�N�x�y)�writer�finishr�NOT_DONE_YETr�s  r!r�z7DistribTests.test_largeWrite.<locals>.LargeWrite.renders.���
�
�d�Z�/�$�6�7���� ��*�*�*r Nr�rr r!�
LargeWriter�
s��
+r r�r�r��rrKrurWrYr)r(r�rVs   r!�test_largeWritezDistribTests.test_largeWritesM��	+��*�*�	+��#�#�J�L�1�����D�,�,�d�Z�.?�$�.F�G��r c��Gd�dtj�}|j|��}|j|jdt
zdz�|S)z�
        Like L{test_largeWrite}, but for the case where C{render} returns a
        long string rather than explicitly passing it to L{Request.write}.
        c��eZdZd�Zy)�2DistribTests.test_largeReturn.<locals>.LargeReturnc��dtzdzSr�r
r�s  r!r�z9DistribTests.test_largeReturn.<locals>.LargeReturn.renders���j�(�4�/�/r Nr�rr r!�LargeReturnr�s��
0r r�r�r�r�)r(r�rVs   r!�test_largeReturnzDistribTests.test_largeReturnsM��	0�(�+�+�	0��#�#�K�M�2�����D�,�,�d�Z�.?�$�.F�G��r c�T���ttj��x�_}t	j
d|�x�_}tjd|j�j�x�_}tdg��t|��}��fd�}|j|�|S)zz
        If there is an error issuing the request to the remote publisher, an
        error response is returned.
        rrHr c����j�jd��jtj�}�jt|�d�gd�}�jdj
|�g�j�y)Ni�r8)
r s<html>s:  <head><title>500 - Server Connection Lost</title></head>s  <body>s#    <h1>Server Connection Lost</h1>s�    <p>Connection to distributed server lost:<pre>[Failure instance: Traceback from remote host -- twisted.spread.flavors.NoSuchMethod: No such method: remote_requests]</pre></p>s	  </body>s</html>r �
)rY�responseCode�flushLoggedErrorsr�NoSuchMethod�len�join�written)�ignored�errors�expectedrVr(s   ��r!�
cbRenderedz4DistribTests.test_connectionLost.<locals>.cbRendered2sl������W�1�1�3�7��+�+�B�O�O�<�F����S��[�!�,��H� 
���e�j�j��2�3�W�_�_�Er )r#r�Rootr:rrPrArrQrRrSr<rrrW)r(�
serverFactory�
serverPort�subscriptionr`r�rVs`     @r!�test_connectionLostz DistribTests.test_connectionLost"s����#2�"�'�'�)�"<�<���-�")�"3�"3�A�}�"E�E��
�Z�")�">�">���+�+�-�2�2�#
�	
���<���u�%���L�'�*��	F�2	
�
�
�j�!��r c�P�tjj|t�}t	j
t
��}tdg�}tj|�}|j|�|jdt|��|jd|dd�y)zQ
        When a request fails, the string form of the failure is logged.
        r r8zFailure instancerr�N)rr��createWithCleanupr	r
�Failurer,rr�Issue�failed�assertEqualsr�r�)r(r��frV�issues     r!�test_logFailedzDistribTests.test_logFailedNs���$�8�8�J�J��$�
��
�O�O�N�,�-����u�%���
�
�g�&��
���Q�����!�S��-�.��
�
�(�+�a�.��*F�Gr c�v�tjj|t�}t	�}tj|�}tjt��}|j|�|jt�|jdt|��|j|dd|�y)zf
        When L{twisted.web.distrib.Request}'s fail is called, the failure
        is logged.
        r8r�log_failureN)rr�r�r	r,r
r�r�Requestr�failr�r�r��assertIs)r(r��errr�r�s     r!�test_requestFailzDistribTests.test_requestFail]s���
$�8�8�J�J��$�
������O�O�C� ���o�o�l�n�-����������~�.����!�S��-�.��
�
�k�!�n�]�3�Q�7r )rrrrArDr<r:rFrarorur�r�r�r�r�r�r�r�r�rr r!r.r.=sZ���E��E�
�C�	
�B�'�*�("�6�&�: �D�*�*�"�*�X
H�8r r.c��eZdZd�Zd�Zd�Zy)�_PasswordDatabasec��||_yr%)�_users)r(�userss  r!�__init__z_PasswordDatabase.__init__os	����r c�,�t|j�Sr%)�iterr��r(s r!�getpwallz_PasswordDatabase.getpwallrs���D�K�K� � r c�P�|jD]}|d|k(s�|cSt��r1)r��KeyError)r(�username�users   r!�getpwnamz_PasswordDatabase.getpwnamus0���K�K�	�D��A�w�(�"���	��j�r N)rrrr�r�r�rr r!r�r�ns���!�r r�c�p�eZdZdZd�Zd�Zdeddfd�Zd�Zd	�Z	d
�Z
d�Zd�Zd
�Z
eed�d��Zy)�UserDirectoryTestszj
    Tests for L{UserDirectory}, a resource for listing all user resources
    available on a system.
    c��ddddd|j�df|_dddd	d
|j�df|_t|j|jg�|_tj|j�|_y)N�alice�x�{i�zAlice,,,z/bin/sh�bob��i7zBob,,,)�mktemprrr��databaser�
UserDirectory�	directoryr�s r!�setUpzUserDirectoryTests.setUp�si���s�C��j�$�+�+�-��S��
��3��S�(�D�K�K�M�9�M���)�4�:�:�t�x�x�*@�A��
� �.�.�t�}�}�=��r c�j�|jttj|j��y)zK
        L{UserDirectory} instances provide L{resource.IResource}.
        N)�
assertTruerr�	IResourcerr�s r!�test_interfacez!UserDirectoryTests.test_interface�s!��	
����X�%7�%7����H�Ir �name�returnNc���K�t|g�}|jj||�}t||�}|�d{���|j	|j
d�y7�!�w)zr
        Verify that requesting the C{name} child of C{self.directory} results
        in a 404 response.
        Ni�)rr�getChildrrYr�)r(rrVr�r`s     r!�_404TestzUserDirectoryTests._404Test�sX����
��v�&�����(�(��w�7���F�G�$���������-�-�s�3�	�s�:A �A�"A c��BK�|jd��d{���y7��w)z�
        L{UserDirectory.getChild} returns a resource which renders a 404
        response when passed a string which does not correspond to any known
        user.
        scarolN�rr�s r!�test_getInvalidUserz&UserDirectoryTests.test_getInvalidUser�������m�m�H�%�%�%�����c��BK�|jd��d{���y7��w)z�
        L{UserDirectory.getChild} returns a resource which renders a 404
        response when passed a string which corresponds to a known user who has
        neither a user directory nor a user distrib socket.
        saliceNrr�s r!�test_getUserWithoutResourcez.UserDirectoryTests.test_getUserWithoutResource�rrc�f�tj|jd�}|jd�}|j	�tdg�}|jjd|�}|j|tj�|j|j|j�y)z�
        L{UserDirectory.getChild} returns a L{static.File} instance when passed
        the name of a user with a home directory containing a I{public_html}
        directory.
        ����public_htmlrsbobN)
r�FilePathrrf�makedirsrrr�assertIsInstancer�FilerY�path)r(�homerrVr�s     r!�test_getPublicHTMLChildz*UserDirectoryTests.test_getPublicHTMLChild�s���� � ����"��.���j�j��/��������w�'�����(�(���9�����f�f�k�k�2�������k�&6�&6�7r c��tj|jd�}|j�|j	d�}tdg�}|jjd|�}|j|tj�|j|jd�|jt|j�|j�y)z�
        L{UserDirectory.getChild} returns a L{ResourceSubscription} instance
        when passed the name of a user suffixed with C{".twistd"} who has a
        home directory containing a I{.twistd-web-pb} socket.
        r�.twistd-web-pbrs
bob.twistd�unixN)rrrrrfrrrrrrQrYrerrSr!)r(r"�webrVr�s     r!�test_getDistribChildz'UserDirectoryTests.test_getDistribChild�s���� � ����"��.���
�
���j�j�)�*����w�'�����(�(���@�����f�g�&B�&B�C�������f�-��������-�s�x�x�8r c��tdg�}d|_|jtj|j
j|�y)zr
        L{UserDirectory.render} raises L{UnsupportedMethod} in response to a
        non-I{GET} request.
        r��POSTN)r�method�assertRaisesr�UnsupportedMethodrr�r�s  r!�test_invalidMethodz%UserDirectoryTests.test_invalidMethod�s9��
��t�$��������&�2�2�D�N�N�4I�4I�7�Sr c����tj�jd�jd�}|j	�tj�j
d�}|j	�|jd�j
d�tdg��t�j��}��fd�}|j|�|S)z}
        L{UserDirectory} renders a list of links to available user content
        in response to a I{GET} request.
        rrr%r r�c�z��tdj�j��}|jd�\}}�j	|j
jd��j	|j
jd�d��j	|j
j
jd��j	|j
jd��j	|j
jd�d��j	|j
j
jd�y)	Nr �li�a�hrefzalice/zAlice (file)zbob.twistd/zBob (twistd))	rr�r��getElementsByTagNamerY�
firstChild�tagName�getAttributer�)r��documentrrrVr(s    ��r!r�z2UserDirectoryTests.test_render.<locals>.cbRendered�s����"�3�8�8�G�O�O�#<�=�H�$�8�8��>�L�U�C����U�-�-�5�5�s�;����U�-�-�:�:�6�B�H�M����U�-�-�8�8�=�=�~�N����S�^�^�3�3�S�9����S�^�^�8�8��@�-�P����S�^�^�6�6�;�;�^�Lr )rrrrfrr�
setContentrrrrW)r(rr'r�r�rVs`    @r!�test_renderzUserDirectoryTests.test_render�s����
�'�'��
�
�2��7�=�=�m�L��������������-������	�	�	�"�#�.�.�s�3���t�$�������1��
	M�	���:�&��
r zpwd module requiredc�l�tj�}|j|jt�y)z�
        If L{UserDirectory} is instantiated with no arguments, it uses the
        L{pwd} module as its password database.
        N)rr�assertIdentical�_pwd�pwd)r(rs  r!�test_passwordDatabasez(UserDirectoryTests.test_passwordDatabase�s&���)�)�+�	����Y�^�^�S�1r )rrrr*r	r
�bytesrrrr#r(r.r:rr>r?rr r!r�r�|sc���
>�J�	4�5�	4�T�	4�&�&�8�
9�T��@��G�*�+�2�,�2r r�)1r*�os.pathr�xml.dom.minidomrr>r=�ImportError�unittestr�zope.interface.verifyr�twisted.internetrr�twisted.loggerr	�twisted.pythonr
r�twisted.spreadr�twisted.spread.bananar�twisted.testr�twisted.trial.unittestr�twisted.webrrrrr�twisted.web.http_headersr�twisted.web.test._utilr�twisted.web.test.requesthelperrrrNrr#�	Exceptionr,r.r�r�rr r!�<module>rRs�����'����C��.�+�-�,��,�&�+�A�A�,�*�E�	�V�[�[�	�
�b�(�(�
� �Y��n8�8�n8�b	��z2��z2��_��
�C��s�C�C�C

Zerion Mini Shell 1.0