o
    ¯bB.  ã                   @   s.  d Z ddlZddl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 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l+m,Z, e-ƒ Z.G dd„ deƒZ/G dd„ deƒZ0dS )z
Tests for L{twisted.web.tap}.
é    N)ÚskipIf)Ú	endpointsÚreactor)ÚIReactorUNIX)ÚFilePath)ÚrequireModule)Ú
ThreadPool)Ú
UsageError)ÚPBServerFactory)ÚTestCase)Údemo)ÚResourcePublisherÚUserDirectory)ÚPythonScript)ÚSite)ÚDataÚFile)ÚOptionsÚ_AddHeadersResourceÚmakePersonalServerFactoryÚmakeService)ÚDummyRequest)Ú	CGIScript)ÚWSGIResourcec                   @   s
  e Zd ZdZdd„ Zdd„ Zee e	¡ dƒdd„ ƒZ
d	d
„ Zdd„ Zdd„ Zdd„ Zee e	¡ dƒdd„ ƒZee e	¡ dƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zeedƒdudƒd d!„ ƒZeedƒdu d"ƒd#d$„ ƒZd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ ZdS )/ÚServiceTestszD
    Tests for the service creation APIs in L{twisted.web.tap}.
    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#   ú;/usr/lib/python3/dist-packages/twisted/web/test/test_tap.pyÚ_pathOption,   s   
zServiceTests._pathOptionc                 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_pathServerF   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   r;   )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.epyr<   N)r%   r=   r>   r&   r?   r   r(   r#   r#   r$   Útest_epyProcessore   rA   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%   r=   r>   r?   r&   r   r'   ÚdataÚtype)r!   r    r   r=   r#   r#   r$   Útest_rpyProcessorn   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!   rH   Ú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.
        r*   r+   ú
--personalN)r   r   r   r   r,   r-   r.   r2   r3   r    r4   r5   r6   r7   )r!   r8   r"   r9   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.
        rK   ú~Úportsr   r+   N)	r   r   r3   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}.
        rN   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:8001rX   ztcp:8002Ú8001rN   r   Ú8002é   N©r   r   ÚassertInrV   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 d¡ |  |j	j¡ |  |j	j¡ 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__r2   r   rG   Ú_reactorr   Ú
isinstanceÚ_threadpoolr   Ú_applicationÚapplicationÚassertFalseÚstartedÚ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.r_   zNo such WSGI application: N)r   rb   Ú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   rm   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.
        rr   ÚsslrN   r   Ú443Nr\   rV   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: V1rz   zK2: V2ÚextraHeaders©ÚK1ÚV1©ÚK2ÚV2N)r   r   r'   rV   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.
        ry   r   r|   r   N)r   r   r   r/   r0   r1   r&   r   r'   Ú_headersÚ_originalResourcer   ÚTest)r!   r"   r9   r1   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.0r[   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   r2   r/   r0   ÚdisplayTracebacks©r!   r"   r9   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   rh   r/   r0   rŽ   r�   r#   r#   r$   Útest_displayTracebacksNotGiven'  s   
z+ServiceTests.test_displayTracebacksNotGiven)rb   Ú
__module__Ú__qualname__Ú__doc__r%   r)   r   r   Ú
providedByr   r:   r@   rB   rF   rJ   rL   rR   rW   r^   rl   rq   r   ru   rx   r‚   r†   rŠ   r�   r‘   r#   r#   r#   r$   r   '   sH    	
þ
		
þ

þ
	



r   c                   @   s   e Zd Zdd„ ZdS )ÚAddHeadersResourceTestsc                 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!   r1   Úrequestr#   r#   r$   Útest_getChildWithDefault3  s   ÿz0AddHeadersResourceTests.test_getChildWithDefaultN)rb   r’   r“   r�   r#   r#   r#   r$   r–   2  s    r–   )1r”   r3   r5   Úunittestr   Útwisted.internetr   r   Útwisted.internet.interfacesr   Ú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   Úobjectrg   r   r–   r#   r#   r#   r$   Ú<module>   s4     