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     