o
    ¼>hp/  ã                   @  sJ  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 e2ƒ Z3G dd„ deƒZ4G dd„ deƒZ5dS )z
Tests for L{twisted.web.tap}.
é    )ÚannotationsN)Úcast)ÚskipIf)Ú	endpointsÚreactor)ÚIReactorCoreÚIReactorUNIX)ÚFilePath)ÚrequireModule)Ú
ThreadPool)Ú
UsageError)ÚPBServerFactory)ÚTestCase)Údemo)ÚResourcePublisherÚUserDirectory)ÚPythonScript)ÚSite)ÚDataÚFile)ÚOptionsÚ_AddHeadersResourceÚmakePersonalServerFactoryÚmakeService)ÚDummyRequest)Ú	CGIScript)ÚWSGIResourcec                   @  s2  e Zd ZdZd2dd„Zd3dd„Zee e	¡ d	ƒd3d
d„ƒZ
d3dd„Zd3dd„Zd3dd„Zd3dd„Zee e	¡ d	ƒd3dd„ƒZee e	¡ d	ƒd3dd„ƒZd3dd„Zd3dd„Zd3dd„Zd3dd„Zeed ƒd!ud"ƒd3d#d$„ƒZeed ƒd!u d%ƒd3d&d'„ƒZd3d(d)„Zd3d*d+„Zd3d,d-„Zd3d.d/„Zd3d0d1„Zd!S )4ÚServiceTestszD
    Tests for the service creation APIs in L{twisted.web.tap}.
    Úreturnútuple[FilePath[str], File]c                 C  s:   t |  ¡ ƒ}| ¡  tƒ }| d|jg¡ |d }||fS )aƒ  
        Helper for the I{--path} tests which creates a directory and creates
        an L{Options} object which uses that directory as its static
        filesystem root.

        @return: A two-tuple of a L{FilePath} referring to the directory and
            the value associated with the C{'root'} key in the L{Options}
            instance after parsing a I{--path} option.
        ú--pathÚroot)r	   ÚmktempÚmakedirsr   ÚparseOptionsÚpath)Úselfr%   Úoptionsr!   © r(   úy/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/web/test/test_tap.pyÚ_pathOption-   s   
zServiceTests._pathOptionÚNonec                 C  s,   |   ¡ \}}|  |t¡ |  |j|j¡ dS )zŠ
        The I{--path} option causes L{Options} to create a root resource
        which serves responses from the specified path.
        N)r*   ÚassertIsInstancer   ÚassertEqualr%   ©r&   r%   r!   r(   r(   r)   Ú	test_path>   s   zServiceTests.test_pathz0The reactor does not support UNIX domain socketsc                 C  s´   t |  ¡ ƒ}| ¡  |  ¡ }tƒ }| dd| d|jg¡ t|ƒ}| ¡  |  |j	¡ |  
|jd jjt¡ |  |jd jjj|j¡ |  tj |¡¡ |  t t |¡j¡¡ dS )z¥
        The I{--path} option to L{makeService} causes it to return a service
        which will listen on the server address given by the I{--port} option.
        ú--portúunix:r    r   N)r	   r"   r#   r   r$   r%   r   ÚstartServiceÚ
addCleanupÚstopServicer,   ÚservicesÚfactoryÚresourcer   r-   Ú
assertTrueÚosÚexistsÚstatÚS_ISSOCKÚst_mode)r&   r%   Úportr'   Úservicer(   r(   r)   Útest_pathServerG   s   	zServiceTests.test_pathServerc                 C  ó4   |   ¡ \}}| d¡ d¡ |  | dd¡t¡ dS )z—
        The I{--path} option creates a root resource which serves a
        L{CGIScript} instance for any child with the C{".cgi"} extension.
        zfoo.cgió    N)r*   ÚchildÚ
setContentr,   ÚgetChildr   r.   r(   r(   r)   Útest_cgiProcessor]   ó   zServiceTests.test_cgiProcessorc                 C  rA   )zš
        The I{--path} option creates a root resource which serves a
        L{PythonScript} instance for any child with the C{".epy"} extension.
        zfoo.epyrB   N)r*   rC   rD   r,   rE   r   r.   r(   r(   r)   Útest_epyProcessorf   rG   zServiceTests.test_epyProcessorc                 C  sT   |   ¡ \}}| d¡ d¡ | dd¡}|  |t¡ |  |jd¡ |  |jd¡ dS )zº
        The I{--path} option creates a root resource which serves the
        C{resource} global defined by the Python source in any child with
        the C{".rpy"} extension.
        zfoo.rpysN   from twisted.web.static import Data
resource = Data('content', 'major/minor')
NÚcontentzmajor/minor)	r*   rC   rD   rE   r,   r   r-   ÚdataÚtype)r&   r%   r!   rC   r(   r(   r)   Útest_rpyProcessoro   s   
ÿzServiceTests.test_rpyProcessorc                 C  sD   t tddƒƒ}t|ƒ}|  |t¡ |  |jt¡ |  |jj|¡ dS )z‡
        L{makePersonalServerFactory} returns a PB server factory which has
        as its root object a L{ResourcePublisher}.
        s   foo barz
text/plainN)	r   r   r   r,   r   r!   r   ÚassertIdenticalÚsite)r&   rN   ÚserverFactoryr(   r(   r)   Útest_makePersonalServerFactory   s
   z+ServiceTests.test_makePersonalServerFactoryc                 C  sl   |   ¡ }tƒ }| dd| dg¡ t|ƒ}| ¡  |  |j¡ |  tj	 
|¡¡ |  t t |¡j¡¡ dS )z±
        The I{--personal} option to L{makeService} causes it to return a
        service which will listen on the server address given by the I{--port}
        option.
        r0   r1   ú
--personalN)r"   r   r$   r   r2   r3   r4   r8   r9   r%   r:   r;   r<   r=   )r&   r>   r'   r?   r(   r(   r)   Útest_personalServerŒ   s   
z ServiceTests.test_personalServerc                 C  sH   t ƒ }| dg¡ tj tj dtj¡¡}|  |d d d|› ¡ dS )zÆ
        If the I{--port} option not specified but the I{--personal} option is,
        L{Options} defaults the port to C{UserDirectory.userSocketName} in the
        user's home directory.
        rQ   ú~Úportsr   r1   N)	r   r$   r9   r%   Ú
expanduserÚjoinr   ÚuserSocketNamer-   )r&   r'   r%   r(   r(   r)   Útest_defaultPersonalPathŸ   s   
z%ServiceTests.test_defaultPersonalPathc                 C  s8   t ƒ }| g ¡ |  t |d d d¡dd… d¡ dS )zl
        If the I{--port} option is not specified, L{Options} defaults the port
        to C{8080}.
        rT   r   Né   )ÚTCP)i  N)r   r$   r-   r   Ú_parseServer©r&   r'   r(   r(   r)   Útest_defaultPort®   s
   
ÿzServiceTests.test_defaultPortc                 C  s@   t ƒ }| g d¢¡ |  d|d d ¡ |  d|d d ¡ dS )zQ
        If the I{--http} option is given twice, there are two listeners
        )ú--listenztcp:8001r^   ztcp:8002Ú8001rT   r   Ú8002é   N©r   r$   ÚassertInr\   r(   r(   r)   Útest_twoPorts¹   s   zServiceTests.test_twoPortsc                 C  s¶   t ƒ }| dtd g¡ |d }|  |t¡ |  |jt¡ |  t|j	t
ƒ¡ |  |jt¡ |  |j	j¡ tttƒ d¡ |  |j	j¡ |  |j	j¡ tttƒ d¡ |  |j	j¡ dS )zÁ
        The I{--wsgi} option takes the fully-qualifed Python name of a WSGI
        application object and creates a L{WSGIResource} at the root which
        serves that application.
        ú--wsgiz.applicationr!   ÚstartupÚshutdownN)r   r$   Ú__name__r8   r   rM   Ú_reactorr   Ú
isinstanceÚ_threadpoolr   Ú_applicationÚapplicationÚassertFalseÚstartedr   r   ÚfireSystemEventÚjoined)r&   r'   r!   r(   r(   r)   Ú	test_wsgiÂ   s   zServiceTests.test_wsgic                 C  sF   t ƒ }td dfD ]}|  t|jd|g¡}|  t|ƒd|›¡ q	dS )zn
        If I{--wsgi} is given an invalid name, L{Options.parseOptions}
        raises L{UsageError}.
        z.nosuchthingzfoo.re   zNo such WSGI application: N)r   rh   ÚassertRaisesr   r$   r-   Ústr)r&   r'   ÚnameÚexcr(   r(   r)   Útest_invalidApplicationØ   s
   þz$ServiceTests.test_invalidApplicationzOpenSSL.SSLNzSSL module is available.c                 C  s.   t ƒ }|  t|jdg¡}|  d|jd ¡ dS )zp
        An L{UsageError} is raised when C{https} is requested but there is no
        support for SSL.
        ú--https=443zSSL support not installedr   N)r   rs   r   r$   r-   Úargs)r&   r'   Ú	exceptionr(   r(   r)   Útest_HTTPSFailureOnMissingSSLâ   s   z*ServiceTests.test_HTTPSFailureOnMissingSSLzSSL module is not available.c                 C  s>   t ƒ }| dg¡ |  d|d d ¡ |  d|d d ¡ dS )zM
        When SSL support is present, it accepts the --https option.
        rx   ÚsslrT   r   Ú443Nrb   r\   r(   r(   r)   Ú test_HTTPSAcceptedOnAvailableSSLî   s   z-ServiceTests.test_HTTPSAcceptedOnAvailableSSLc                 C  s,   t ƒ }| g d¢¡ |  |d ddg¡ dS )zE
        When --add-header is specific, the value is parsed.
        ©ú--add-headerzK1: V1r€   zK2: V2ÚextraHeaders©ÚK1ÚV1©ÚK2ÚV2N)r   r$   r-   r\   r(   r(   r)   Útest_add_header_parsingú   s   z$ServiceTests.test_add_header_parsingc                 C  s\   t ƒ }| g d¢¡ t|ƒ}|jd jj}|  |t¡ |  |j	ddg¡ |  |j
tj¡ dS )zj
        When --add-header is specified, the resource is a composition that adds
        headers.
        r   r   r‚   r…   N)r   r$   r   r5   r6   r7   r,   r   r-   Ú_headersÚ_originalResourcer   ÚTest)r&   r'   r?   r7   r(   r(   r)   Útest_add_header_resource  s   z%ServiceTests.test_add_header_resourcec                 C  sd   t ƒ }| dg¡ t|ƒ |  | jg¡}|  |d d t¡ |  |d d d¡ |  t|ƒd¡ dS )z7
        Passing --notracebacks is deprecated.
        z--notracebacksr   ÚcategoryÚmessagez/--notracebacks was deprecated in Twisted 19.7.0ra   N)r   r$   r   ÚflushWarningsÚtest_noTracebacksDeprecationr-   ÚDeprecationWarningÚlen)r&   r'   Úwarningsr(   r(   r)   r     s   ÿz)ServiceTests.test_noTracebacksDeprecationc                 C  s2   t ƒ }| dg¡ t|ƒ}|  |jd jj¡ dS )zm
        Passing --display-tracebacks will enable traceback rendering on the
        generated Site.
        z--display-tracebacksr   N)r   r$   r   r8   r5   r6   ÚdisplayTracebacks©r&   r'   r?   r(   r(   r)   Útest_displayTracebacks  s   z#ServiceTests.test_displayTracebacksc                 C  s0   t ƒ }| g ¡ t|ƒ}|  |jd jj¡ dS )zt
        Not passing --display-tracebacks will leave traceback rendering on the
        generated Site off.
        r   N)r   r$   r   rn   r5   r6   r”   r•   r(   r(   r)   Útest_displayTracebacksNotGiven(  s   
z+ServiceTests.test_displayTracebacksNotGiven)r   r   ©r   r+   )rh   Ú
__module__Ú__qualname__Ú__doc__r*   r/   r   r   Ú
providedByr   r@   rF   rH   rL   rP   rR   rX   r]   rd   rr   rw   r
   r{   r~   rˆ   rŒ   r   r–   r—   r(   r(   r(   r)   r   (   sH    

	
þ

	
	

þ
þ


	






r   c                   @  s   e Zd Zddd„ZdS )ÚAddHeadersResourceTestsr   r+   c                 C  sX   t t ¡ g d¢ƒ}tg ƒ}| d|¡ |  |j d¡ddg¡ |  |j d¡dg¡ dS )	zc
        When getChildWithDefault is invoked, it adds the headers to the
        response.
        )r‚   r…   )rƒ   ÚV3Ú rƒ   r„   rž   r†   r‡   N)r   r   r‹   r   ÚgetChildWithDefaultr-   ÚresponseHeadersÚgetRawHeaders)r&   r7   Úrequestr(   r(   r)   Útest_getChildWithDefault4  s   ÿz0AddHeadersResourceTests.test_getChildWithDefaultNr˜   )rh   r™   rš   r¤   r(   r(   r(   r)   r   3  s    r   )6r›   Ú
__future__r   r9   r;   Útypingr   Úunittestr   Útwisted.internetr   r   Útwisted.internet.interfacesr   r   Útwisted.python.filepathr	   Útwisted.python.reflectr
   Útwisted.python.threadpoolr   Útwisted.python.usager   Útwisted.spread.pbr   Útwisted.trial.unittestr   Útwisted.webr   Útwisted.web.distribr   r   Útwisted.web.scriptr   Útwisted.web.serverr   Útwisted.web.staticr   r   Útwisted.web.tapr   r   r   r   Útwisted.web.test.requesthelperr   Útwisted.web.twcgir   Útwisted.web.wsgir   Úobjectrm   r   r   r(   r(   r(   r)   Ú<module>   s8     