
    Vhp/                    ^   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       Z3 G d de      Z4 G d de      Z5y)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                     e Zd ZdZddZddZ e ej                  e	       d      dd       Z
ddZddZddZdd	Z e ej                  e	       d      dd
       Z e ej                  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y)ServiceTestszD
    Tests for the service creation APIs in L{twisted.web.tap}.
    c                    t        | j                               }|j                          t               }|j	                  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"   s       I/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/test/test_tap.py_pathOptionzServiceTests._pathOption-   sN     &)h		23vTz    c                    | j                         \  }}| j                  |t               | j                  |j                  |j                         y)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"   s      r)   	test_pathzServiceTests.test_path>   s>    
 %%'
ddD)DII.r+   z0The reactor does not support UNIX domain socketsc                   t        | j                               }|j                          | j                         }t               }|j	                  dd|z   d|j
                  g       t        |      }|j                          | j                  |j                         | j                  |j                  d   j                  j                  t               | j                  |j                  d   j                  j                  j
                  |j
                         | j!                  t"        j
                  j%                  |             | j!                  t'        j(                  t#        j&                  |      j*                               y)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(   services        r)   test_pathServerzServiceTests.test_pathServerG   s
    &{{})h$$))LMg&++,g..q199BBDI))!,44==BBDIINt,-bggdm&;&;<=r+   c                    | j                         \  }}|j                  d      j                  d       | j                  |j	                  dd      t
               y)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.cgir+   N)r*   child
setContentr-   getChildr   r/   s      r)   test_cgiProcessorzServiceTests.test_cgiProcessor]   sH    
 %%'
d

9((-dmmIt<iHr+   c                    | j                         \  }}|j                  d      j                  d       | j                  |j	                  dd      t
               y)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*   rD   rE   r-   rF   r   r/   s      r)   test_epyProcessorzServiceTests.test_epyProcessorf   sH    
 %%'
d

9((-dmmIt<lKr+   c                *   | j                         \  }}|j                  d      j                  d       |j                  dd      }| j	                  |t
               | j                  |j                  d       | j                  |j                  d       y)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*   rD   rE   rF   r-   r   r.   datatype)r'   r&   r"   rD   s       r)   test_rpyProcessorzServiceTests.test_rpyProcessoro   s|     %%'
d

9((;	
 i.eT*Y/]3r+   c                    t        t        dd            }t        |      }| j                  |t               | j                  |j
                  t               | j                  |j
                  j                  |       y)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'   rQ   serverFactorys      r)   test_makePersonalServerFactoryz+ServiceTests.test_makePersonalServerFactory   sb     D\231$7m_=m002CD]//44d;r+   c                   | j                         }t               }|j                  dd|z   dg       t        |      }|j	                          | j                  |j                         | j                  t        j                  j                  |             | j                  t        j                  t        j                  |      j                               y)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.
        r2   r3   
--personalN)r#   r   r%   r   r4   r5   r6   r:   r;   r&   r<   r=   r>   r?   )r'   r@   r(   rA   s       r)   test_personalServerz ServiceTests.test_personalServer   s     {{})h$EFg&++,t,-bggdm&;&;<=r+   c                   t               }|j                  dg       t        j                  j	                  t        j                  j                  dt        j                              }| j                  |d   d   d|        y)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.
        rU   ~portsr   r3   N)	r   r%   r;   r&   
expanduserjoinr   userSocketNamer.   )r'   r(   r&   s      r)   test_defaultPersonalPathz%ServiceTests.test_defaultPersonalPath   sd     )l^,ww!!"'',,sM4P4P"QR)!,dVn=r+   c                    t               }|j                  g        | j                  t        j                  |d   d   d      dd d       y)zl
        If the I{--port} option is not specified, L{Options} defaults the port
        to C{8080}.
        rY   r   N   )TCP)i  N)r   r%   r.   r   _parseServerr'   r(   s     r)   test_defaultPortzServiceTests.test_defaultPort   sK    
 )R ""77#3A#6=bqACX	
r+   c                    t               }|j                  g d       | j                  d|d   d          | j                  d|d   d          y)zQ
        If the I{--http} option is given twice, there are two listeners
        )--listenztcp:8001re   ztcp:80028001rY   r   8002   Nr   r%   assertInrb   s     r)   test_twoPortszServiceTests.test_twoPorts   sJ     )MNfgg.q12fgg.q12r+   c                   t               }|j                  dt        dz   g       |d   }| j                  |t               | j                  |j                  t               | j                  t        |j                  t                     | j                  |j                  t               | j                  |j                  j                         t        t         t              j#                  d       | j                  |j                  j                         | j                  |j                  j$                         t        t         t              j#                  d       | j                  |j                  j$                         y)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__r:   r   rP   _reactorr   
isinstance_threadpoolr   _applicationapplicationassertFalsestartedr   r   fireSystemEventjoined)r'   r(   r"   s      r)   	test_wsgizServiceTests.test_wsgi   s    )h>(ABCvl+T]]G4
4#3#3Z@AT..< 	))112\7#33I>((001))001\7#33J?((//0r+   c                    t               }t        dz   dfD ]C  }| j                  t        |j                  d|g      }| j                  t        |      d|       E y)zn
        If I{--wsgi} is given an invalid name, L{Options.parseOptions}
        raises L{UsageError}.
        z.nosuchthingzfoo.rm   zNo such WSGI application: N)r   rp   assertRaisesr   r%   r.   str)r'   r(   nameexcs       r)   test_invalidApplicationz$ServiceTests.test_invalidApplication   sb    
 ).7 	ND##J0D0DxQUFVWCSX)CD8'LM	Nr+   zOpenSSL.SSLNzSSL module is available.c                    t               }| j                  t        |j                  dg      }| j	                  d|j
                  d          y)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   r|   r   r%   r.   args)r'   r(   	exceptions      r)   test_HTTPSFailureOnMissingSSLz*ServiceTests.test_HTTPSFailureOnMissingSSL   sA     )%%j'2F2FX	4innQ6GHr+   zSSL module is not available.c                    t               }|j                  dg       | j                  d|d   d          | j                  d|d   d          y)zM
        When SSL support is present, it accepts the --https option.
        r   sslrY   r   443Nri   rb   s     r)    test_HTTPSAcceptedOnAvailableSSLz-ServiceTests.test_HTTPSAcceptedOnAvailableSSL   sK    
 )m_-eWW-a01eWW-a01r+   c                l    t               }|j                  g d       | j                  |d   ddg       y)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.   rb   s     r)   test_add_header_parsingz$ServiceTests.test_add_header_parsing   s4     )QR0<2NOr+   c                V   t               }|j                  g d       t        |      }|j                  d   j                  j
                  }| j                  |t               | j                  |j                  ddg       | j                  |j                  t        j                         y)zj
        When --add-header is specified, the resource is a composition that adds
        headers.
        r   r   r   r   N)r   r%   r   r7   r8   r9   r-   r   r.   _headers_originalResourcer   Test)r'   r(   rA   r9   s       r)   test_add_header_resourcez%ServiceTests.test_add_header_resource  s    
 )QRg&##A&..77h(;<**\<,HIh88$))Dr+   c                (   t               }|j                  dg       t        |       | j                  | j                  g      }| j                  |d   d   t               | j                  |d   d   d       | j                  t        |      d       y)z7
        Passing --notracebacks is deprecated.
        z--notracebacksr   categorymessagez/--notracebacks was deprecated in Twisted 19.7.0rh   N)r   r%   r   flushWarningstest_noTracebacksDeprecationr.   DeprecationWarninglen)r'   r(   warningss      r)   r   z)ServiceTests.test_noTracebacksDeprecation  s     )./0G%%t'H'H&IJ!Z02DEQK	"$U	
 	X*r+   c                    t               }|j                  dg       t        |      }| j                  |j                  d   j
                  j                         y)zm
        Passing --display-tracebacks will enable traceback rendering on the
        generated Site.
        z--display-tracebacksr   N)r   r%   r   r:   r7   r8   displayTracebacksr'   r(   rA   s      r)   test_displayTracebacksz#ServiceTests.test_displayTracebacks  sJ    
 )456g&((+33EEFr+   c                    t               }|j                  g        t        |      }| j                  |j                  d   j
                  j                         y)zt
        Not passing --display-tracebacks will leave traceback rendering on the
        generated Site off.
        r   N)r   r%   r   rv   r7   r8   r   r   s      r)   test_displayTracebacksNotGivenz+ServiceTests.test_displayTracebacksNotGiven(  sH    
 )R g&))!,44FFGr+   )returnztuple[FilePath[str], File]r   None)rp   
__module____qualname____doc__r*   r0   r   r	   
providedByr   rB   rG   rI   rN   rS   rV   r]   rc   rk   rz   r   r   r   r   r   r   r   r   r    r+   r)   r   r   (   s6   "/ #L##G,,:>	>$IL4 < #L##G,,:>	> #L##G,,:	>		>	
31,N M-(46PQ	I R	I M-(D02PQ	2 R	2PE+GHr+   r   c                      e Zd ZddZy)AddHeadersResourceTestsc                0   t        t        j                         g d      }t        g       }|j	                  d|       | j                  |j                  j                  d      ddg       | j                  |j                  j                  d      dg       y)	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'   r9   requests      r)   test_getChildWithDefaultz0AddHeadersResourceTests.test_getChildWithDefault4  s~    
 'IIKC
 r"$$R100>>tDtTlS00>>tDtfMr+   Nr   )rp   r   r   r   r   r+   r)   r   r   3  s    Nr+   r   )6r   
__future__r   r;   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   objectru   r   r   r   r+   r)   <module>r      s}    # 	    / B , 0 0 + - +  @ + # )  8 ' )hHH8 HHVNh Nr+   