%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_cgi.cpython-312.pyc

�

Ϫ�f;����dZddlZddlZddlZddlmZddlmZmZm	Z	m
Z
ddlmZddl
mZmZmZddlmZddlmZmZmZmZmZmZdd	lmZmZdd
lmZddlm Z m!Z!dZ"d
Z#dZ$dZ%dZ&dZ'dZ(dZ)dZ*Gd�dejV�Z,Gd�d�Z-Gd�de-ej\�Z/Gd�de-ej\�Z0Gd�dej\�Z1Gd�d ej\�Z2d!�Z3y)"z!
Tests for L{twisted.web.twcgi}.
�N)�BytesIO)�address�error�
interfaces�reactor)�ConnectionLost)�failure�log�util)�unittest)�client�http�http_headers�resource�server�twcgi)�INTERNAL_SERVER_ERROR�	NOT_FOUND)�_render)�DummyChannel�DummyRequestz2print("Header: OK")
print("")
print("cgi output")
zJprint("Header: spam")
print("Header: eggs")
print("")
print("cgi output")
z+print("XYZ")
print("")
print("cgi output")
zPprint("Server: monkeys")
print("Date: last year")
print("")
print("cgi output")
a&# This is an example of a correctly-written CGI script which reads a body
# from stdin, which only reads env['CONTENT_LENGTH'] bytes.

import os, sys

body_length = int(os.environ.get('CONTENT_LENGTH',0))
indata = sys.stdin.read(body_length)
print("Header: OK")
print("")
print("readinput ok")
a+# This is an example of the typical (incorrect) CGI script which expects
# the server to close stdin when the body of the request is complete.
# A correct CGI should only read env['CONTENT_LENGTH'] bytes.

import sys

indata = sys.stdin.read()
print("Header: OK")
print("")
print("readallinput ok")
zMprint("content-type: text/cgi-duplicate-test")
print("")
print("cgi output")
z�import json
import os
print("")
print("")
vals = {x:y for x,y in os.environ.items() if x.startswith("HTTP_")}
print(json.dumps(vals))
z]import os
param = str(os.environ['QUERY_STRING'])
print("Header: OK")
print("")
print(param)
c�$�eZdZejZy)�PythonScriptN)�__name__�
__module__�__qualname__�sys�
executable�filter���;/usr/lib/python3/dist-packages/twisted/web/test/test_cgi.pyrrbs��
�^�^�Fr!rc��eZdZd�Zd�Zd�Zy)�_StartServerAndTearDownMixinc�<�tj�}tjt|�}|jdt
|��tj|�}tjd|�|_|jj�jS)Nscgir)r�Resourcer�sibpath�__file__�putChildrr�Siter�	listenTCP�p�getHost�port)�self�cgi�root�cgipath�sites     r"�startServerz(_StartServerAndTearDownMixin.startServergsl��� � �"���,�,�x��-���
�
�f�l�7�3�4��{�{�4� ���"�"�1�d�+����v�v�~�~��$�$�$r!c�R�t|dd�r|jj�Sy)Nr,)�getattrr,�
stopListening)r/s r"�tearDownz%_StartServerAndTearDownMixin.tearDownos&���4��d�#��6�6�'�'�)�)�$r!c���tjj|j��}t	|d�5}|j|�ddd�|S#1swY|SxYw)N�wt)�os�path�abspath�mktemp�open�write)r/�source�cgiFilename�cgiFiles    r"�writeCGIz%_StartServerAndTearDownMixin.writeCGIssN���g�g�o�o�d�k�k�m�4��
�+�t�
$�	"���M�M�&�!�	"���	"���s�A�A N)rrrr4r8rDr r!r"r$r$fs��%�*�r!r$c���eZdZdZej
j
e�sdZd�Z	d�Z
d�Zd�Zd�Z
d�Zd	�Zd
�Zde_d�Zd
�Zde_d�Zd�Zde_d�Zd�Zy)�CGITestsz,
    Tests for L{twcgi.FilteredScript}.
    z5CGI tests require a functional reactor.spawnProcess()c�F�|jt�}|j|�}d|fz}|jd�}t	j
t�jd|�}|jtj�|j|j�|S)N�http://localhost:%d/cgi�ascii�GET)rD�	DUMMY_CGIr4�encoder
�Agentr�request�addCallback�readBody�
_testCGI_1)r/rB�portnum�url�ds     r"�test_CGIzCGITests.test_CGI�sz���m�m�I�.���"�"�;�/��'�7�*�4���j�j��!���L�L��!�)�)�&�#�6��	�
�
�f�o�o�&�	�
�
�d�o�o�&��r!c�h�|j|dtjjd�z�y)Ns
cgi outputrI)�assertEqualr;�lineseprL)r/�ress  r"rQzCGITests._testCGI_1�s&������m�b�j�j�.?�.?��.H�H�Ir!c�.���jt�}�j|�}d|fz}|jd�}t	j
t�}|jd|�}|jt��fd�}|j|�|S)zc
        If the CGI script emits a I{Server} or I{Date} header, these are
        ignored.
        rHrIrJc����jd|jjd���jd|jjd��y)N�monkeysrz	last year�date)�assertNotIn�headers�
getRawHeaders��responser/s �r"�
checkResponsez;CGITests.test_protectedServerAndDate.<locals>.checkResponse�sE������Y��(8�(8�(F�(F�x�(P�Q����[�(�*:�*:�*H�*H��*P�Qr!)
rD�SPECIAL_HEADER_CGIr4rLr
rMrrNrO�discardBody�r/rBrRrS�agentrTrcs`      r"�test_protectedServerAndDatez$CGITests.test_protectedServerAndDate�s����
�m�m�$6�7���"�"�;�/��'�7�*�4���j�j��!�����W�%���M�M�&�#�&��	�
�
�k�"�	R�	
�
�
�m�$��r!c�.���jt�}�j|�}d|fz}|jd�}t	j
t�}|jd|�}|jt��fd�}|j|�|S)z�
        If the CGI script emits a I{content-type} header, make sure that the
        server doesn't add an additional (duplicate) one, as per ticket 4786.
        rHrIrJc�`���j|jjd�dg�|S)Nzcontent-typeztext/cgi-duplicate-test�rWr_r`ras �r"rczBCGITests.test_noDuplicateContentTypeHeaders.<locals>.checkResponse�s2������� � �.�.�~�>�*�+�
��Or!)
rD�$NO_DUPLICATE_CONTENT_TYPE_HEADER_CGIr4rLr
rMrrNrOrerfs`      r"�"test_noDuplicateContentTypeHeadersz+CGITests.test_noDuplicateContentTypeHeaders�s����
�m�m�$H�I���"�"�;�/��'�7�*�4���j�j��!�����W�%���M�M�&�#�&��	�
�
�k�"�	�	
�
�
�m�$��r!c�z���jt�}�j|�}d|fz}|jd�}t	j
t�}tjdgdgd��}|jd||��}�fd�}|jtj�|j|�|S)	zV
        The CGI script is never called with the Proxy header passed through.
        rHrIsfoosbar)sProxysX-Innocent-HeaderrJ)r_c���tj|jd��}�jt	|j��hd��y)NrI>�	HTTP_HOST�HTTP_CONNECTION�HTTP_X_INNOCENT_HEADER)�json�loads�decoderW�set�keys)rbr_r/s  �r"rcz7CGITests.test_noProxyPassthrough.<locals>.checkResponse�s9����j�j�����!9�:�G�����G�L�L�N�#�J�
r!)rD�HEADER_OUTPUT_CGIr4rLr
rMrr�HeadersrNrOrP)r/rBrRrSrgr_rTrcs`       r"�test_noProxyPassthroughz CGITests.test_noProxyPassthrough�s�����m�m�$5�6���"�"�;�/��'�7�*�4���j�j��!�����W�%���&�&��x��x�@�
��
�M�M�&�#�w�M�7��	�	
�
�
�f�o�o�&�	�
�
�m�$��r!c�.���jt�}�j|�}d|fz}|jd�}t	j
t�}|jd|�}|jt��fd�}|j|�|S)zp
        If a CGI script emits two instances of the same header, both are sent
        in the response.
        rHrIrJc�`���j|jjd�ddg�y)N�header�spam�eggsrkras �r"rcz7CGITests.test_duplicateHeaderCGI.<locals>.checkResponse�s*������X�-�-�;�;�H�E��PV�GW�Xr!)
rD�DUAL_HEADER_CGIr4rLr
rMrrNrOrerfs`      r"�test_duplicateHeaderCGIz CGITests.test_duplicateHeaderCGI�s����
�m�m�O�4���"�"�;�/��'�7�*�4���j�j��!�����W�%���M�M�&�#�&��	�
�
�k�"�	Y�	
�
�
�m�$��r!c�����jt�}�j|�}d|fz}|jd�}t	j
t�}|jd|�}|jt�g��fd�}tj|��jtj|���fd�}|j|�|S)zF
        Check for the error message in the duplicated header
        rHrIrJc�N���jtj|��y�N)�appendr
�textFromEventDict)�	eventDict�loggedMessagess �r"�
addMessagez4CGITests.test_malformedHeaderCGI.<locals>.addMessage�s����!�!�#�"7�"7�	�"B�Cr!c�B���jdtd�z��y)Nzignoring malformed CGI header: sXYZ)�assertIn�repr)�ignoredr�r/s ��r"rcz7CGITests.test_malformedHeaderCGI.<locals>.checkResponses����M�M�1�D��L�@�.�
r!)rD�BROKEN_HEADER_CGIr4rLr
rMrrNrOrer
�addObserver�
addCleanup�removeObserver)	r/rBrRrSrgrTr�rcr�s	`       @r"�test_malformedHeaderCGIz CGITests.test_malformedHeaderCGI�s�����m�m�$5�6���"�"�;�/��'�7�*�4���j�j��!�����W�%���M�M�&�#�&��	�
�
�k�"���	D�	���
�#�����*�*�J�7�	�
	
�
�
�m�$��r!c���tjj|j��}t	|d�5}|jt�ddd�|j|�}tjt�}d|fz}|jd�}|jd|�}|jtj�|j|j�|S#1swY��xYw)Nr:rHrIrJ)r;r<r=r>r?r@�
READINPUT_CGIr4r
rMrrLrNrOrP�_test_ReadEmptyInput_1�r/rBrCrRrgrSrTs       r"�test_ReadEmptyInputzCGITests.test_ReadEmptyInput	s����g�g�o�o�d�k�k�m�4��
�+�t�
$�	)���M�M�-�(�	)��"�"�;�/�����W�%��'�7�*�4���j�j��!���M�M�&�#�&��	�
�
�f�o�o�&�	�
�
�d�1�1�2���	)�	)�s�C'�'C0�c�p�dtj��}|jd�}|j||�y�Nzreadinput okrI�r;rXrLrW�r/rY�expecteds   r"r�zCGITests._test_ReadEmptyInput_1�0��!�"�*�*��.���?�?�7�+������h�'r!c	�"�tjj|j��}t	|d�5}|jt�ddd�|j|�}tjt�}d|fz}|jd�}|j|dtjtd����}|jtj �|j|j"�|S#1swY��xYw�Nr:rHrIsPOSTsHere is your stdin)�uri�method�bodyProducer)r;r<r=r>r?r@r�r4r
rMrrLrN�FileBodyProducerrrOrP�_test_ReadInput_1r�s       r"�test_ReadInputzCGITests.test_ReadInputs����g�g�o�o�d�k�k�m�4��
�+�t�
$�	)���M�M�-�(�	)��"�"�;�/�����W�%��'�7�*�4���j�j��!���M�M����0�0��9N�1O�P�
�
��
	
�
�
�f�o�o�&�	�
�
�d�,�,�-���	)�	)�s�D�Dc�p�dtj��}|jd�}|j||�yr�r�r�s   r"r�zCGITests._test_ReadInput_12r�r!c	��tjj|j��}t	|d�5}|jt�ddd�|j|�}d|fz}|jd�}tjt�j|dtjtd����}|jtj �|j|j"�|S#1swY��xYwr�)r;r<r=r>r?r@�READALLINPUT_CGIr4rLr
rMrrNr�rrOrP�_test_ReadAllInput_1)r/rBrCrRrSrTs      r"�test_ReadAllInputzCGITests.test_ReadAllInput7s����g�g�o�o�d�k�k�m�4��
�+�t�
$�	,���M�M�*�+�	,��"�"�;�/��'�7�*�4���j�j��!���L�L��!�)�)����0�0��9N�1O�P�
*�
��
	
�
�
�f�o�o�&�	�
�
�d�/�/�0���	,�	,�s�D�Dc�p�dtj��}|jd�}|j||�y)Nzreadallinput okrIr�r�s   r"r�zCGITests._test_ReadAllInput_1Js0��$�R�Z�Z�L�1���?�?�7�+������h�'r!c���Gd�d�}|�}tddg�}tjddd�|_t	j
d|�	�}t
||�|j|j�y
)zw
        L{twcgi.FilteredScript.runProcess} uses the reactor passed as an
        argument to the constructor.
        c��eZdZdZdZd�Zy)�5CGITests.test_useReactorArgument.<locals>.FakeReactorzR
            A fake reactor recording whether spawnProcess is called.
            Fc��d|_y)z�
                Set the C{called} flag to C{True} if C{spawnProcess} is called.

                @param args: Positional arguments.
                @param kwargs: Keyword arguments.
                TN)�called)r/�args�kwargss   r"�spawnProcesszBCGITests.test_useReactorArgument.<locals>.FakeReactor.spawnProcess\s��#��r!N)rrr�__doc__r�r�r r!r"�FakeReactorr�Us��
��F�
#r!r��a�b�TCP�	127.0.0.1�90z
dummy-file�rN)	rr�IPv4Addressr
r�FilteredScriptr�
assertTruer�)r/r��fakeReactorrNrs     r"�test_useReactorArgumentz CGITests.test_useReactorArgumentOsg��	#�	#� "�m����S�z�*�� �,�,�U�K��G����'�'��k�J����'�"�����*�*�+r!N)rrrr�r�IReactorProcess�
providedByr�skiprUrQrhrmrzr�r�r��timeoutr�r�r�r�r�r�r r!r"rFrFzs�����%�%�0�0��9�F��	�J��*�0�8�(�8�#$���(�
�$�N��(�
�"!"���(�
,r!rFc�"�eZdZdZd�Zd�Zd�Zy)�CGIScriptTestsz'
    Tests for L{twcgi.CGIScript}.
    c�(�|jt�}|j|�}d|fz}tjt
�}|j
d|�}|jtj�|j|j�|S)zk
        If the CGI script is passed URL parameters, do not fall over,
        as per ticket 9887.
        s"http://localhost:%d/cgi?param=1234rJ)
rD�URL_PARAMETER_CGIr4r
rMrrNrOrP�_test_urlParameters_1)r/rBrRrSrgrTs      r"�test_urlParametersz!CGIScriptTests.test_urlParameterssst��
�m�m�$5�6���"�"�;�/��3�w�j�@�����W�%���M�M�&�#�&��	�
�
�f�o�o�&�	�
�
�d�0�0�1��r!c�p�dtj��}|jd�}|j||�y)Nz
param=1234rIr�r�s   r"r�z$CGIScriptTests._test_urlParameters_1�s0����
�
�|�,���?�?�7�+������h�'r!c��Gd�d�}|�}tj|j�|��}tddg�}t	j
ddd�|_t||�|j|jd	d
�y)zt
        L{twcgi.CGIScript.render} sets the process environment
        I{PATH_INFO} from the request path.
        c��eZdZdZd�Zy)�1CGIScriptTests.test_pathInfo.<locals>.FakeReactorzZ
            A fake reactor recording the environment passed to spawnProcess.
            c��||_y)a9
                Store the C{env} L{dict} to an instance attribute.

                @param process: Ignored
                @param filename: Ignored
                @param args: Ignored
                @param env: The environment L{dict} which will be stored
                @param wdir: Ignored
                N)�process_env)r/�process�filenamer��env�wdirs      r"r�z>CGIScriptTests.test_pathInfo.<locals>.FakeReactor.spawnProcess�s��$'�� r!N)rrrr�r�r r!r"r�r��s��
�

'r!r�r�r�r�r�r�r��	PATH_INFOz/a/bN)
r�	CGIScriptr>rrr�r
rrWr�)r/r��_reactorrrNs     r"�
test_pathInfozCGIScriptTests.test_pathInfo�st��	'�	'�"�=���?�?�4�;�;�=�(�C����S�z�*�� �,�,�U�K��G�����'�"�����-�-�k�:�F�Cr!N)rrrr�r�r�r�r r!r"r�r�ns����(�
Dr!r�c��eZdZdZd�Zd�Zy)�CGIDirectoryTestsz*
    Tests for L{twcgi.CGIDirectory}.
    c����tj�j��}tdg��t	|��}��fd�}|j|�|S)zc
        L{twcgi.CGIDirectory.render} sets the HTTP response code to I{NOT
        FOUND}.
        �c�F���j�jt�yr��rW�responseCoder�r�rNr/s ��r"�
cbRenderedz1CGIDirectoryTests.test_render.<locals>.cbRendered��������W�1�1�9�=r!)r�CGIDirectoryr>rrrO)r/rrTr�rNs`   @r"�test_renderzCGIDirectoryTests.test_render�sK���
�%�%�d�k�k�m�4����t�$���H�g�&��	>�	
�
�
�j�!��r!c�����j�}tj|�tj|�}tdg��|jd��}t|��}��fd�}|j|�|S)a
        L{twcgi.CGIDirectory.getChild} returns a resource which renders an
        response with the HTTP I{NOT FOUND} status code if the indicated child
        does not exist as an entry in the directory used to initialized the
        L{twcgi.CGIDirectory}.
        �fooc�F���j�jt�yr�r�r�s ��r"r�z8CGIDirectoryTests.test_notFoundChild.<locals>.cbRendered�r�r!)	r>r;�makedirsrr�r�getChildrrO)r/r<r�childrTr�rNs`     @r"�test_notFoundChildz$CGIDirectoryTests.test_notFoundChild�sp����{�{�}��
���D���%�%�d�+����w�'��!��!�!�%��1���E�7�#��	>�	
�
�
�j�!��r!N)rrrr�r�r�r r!r"r�r��s���
�r!r�c��eZdZdZd�Zd�Zy)�CGIProcessProtocolTestsz0
    Tests for L{twcgi.CGIProcessProtocol}.
    c���tdg�}tj|�}|jt	j
t
j���|j|jt�y)z�
        If the process communicating with L{CGIProcessProtocol} ends before
        finishing writing out headers, the response has I{INTERNAL SERVER
        ERROR} as its status code.
        r�N)rr�CGIProcessProtocol�processEndedr	�Failurer�ProcessTerminatedrWr�r)r/rN�protocols   r"�test_prematureEndOfHeadersz2CGIProcessProtocolTests.test_prematureEndOfHeaders�sW����t�$���+�+�G�4�����g�o�o�e�.E�.E�.G�H�I�����-�-�/D�Er!c�4�t�}tj|d�}tj|�}|jt
jtd���|jt
jtj���y)zg
        Ensure that the CGI process ends cleanly when the request connection
        is lost.
        TzConnection doneN)rr�Requestrr��connectionLostr	r�rr�rr�)r/rTrNr�s    r"�test_connectionLostz+CGIProcessProtocolTests.test_connectionLost�sh��

�N���,�,�q�$�'���+�+�G�4�����w���~�>O�/P�Q�R����g�o�o�e�.E�.E�.G�H�Ir!N)rrrr�r�r�r r!r"r�r��s���	F�	Jr!r�c�R��tj��j�fd��S)zn
    Discard the body of a HTTP response.

    @param response: The response.

    @return: The response.
    c����Sr�r )�_rbs �r"�<lambda>zdiscardBody.<locals>.<lambda>�s���8�r!)r
rPrO)rbs`r"rere�s ����?�?�8�$�0�0�1C�D�Dr!)4r�rsr;r�ior�twisted.internetrrrr�twisted.internet.errorr�twisted.pythonr	r
r�
twisted.trialr�twisted.webr
rrrrr�twisted.web.httprr�twisted.web.test._utilr�twisted.web.test.requesthelperrrrKr�r�rdr�r�rlrxr�r�rr$�TestCaserFr�r�r�rer r!r"�<module>rs�����	�
��@�@�1�-�-�"�K�K�=�*�E�
�	��������
���(�$������5�'�'����(q,�+�X�->�->�q,�h5D�1�8�3D�3D�5D�p&��)�)�&�RJ�h�/�/�J�8Er!

Zerion Mini Shell 1.0