
    tfp/                    r   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          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                     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dS ) ServiceTestszD
    Tests for the service creation APIs in L{twisted.web.tap}.
    returntuple[FilePath[str], File]c                    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$   s       X/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/web/test/test_tap.py_pathOptionzServiceTests._pathOption-   s]     &&))h	2333vTz    Nonec                    |                                  \  }}|                     |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$   s      r+   	test_pathzServiceTests.test_path>   sO    
 %%''
ddD)))DI.....r-   z0The reactor does not support UNIX domain socketsc                   t          |                                           }|                                 |                                 }t                      }|                    dd|z   d|j        g           t          |          }|                                 |                     |j	                   | 
                    |j        d         j        j        t                     |                     |j        d         j        j        j        |j                   |                     t"          j                            |                     |                     t'          j        t#          j        |          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stopServicer0   servicesfactoryresourcer   r1   
assertTrueosexistsstatS_ISSOCKst_mode)r)   r(   portr*   services        r+   test_pathServerzServiceTests.test_pathServerG   s-    &&{{}}))h$$)LMMMg&&+,,,g.q19BDIII)!,4=BDINNNt,,---bgdmm&;<<=====r-   c                    |                                  \  }}|                    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.cgir-   N)r,   child
setContentr0   getChildr   r2   s      r+   test_cgiProcessorzServiceTests.test_cgiProcessor]   sc    
 %%''
d

9((---dmmIt<<iHHHHHr-   c                    |                                  \  }}|                    d                              d           |                     |                    dd          t
                     dS )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,   rG   rH   r0   rI   r   r2   s      r+   test_epyProcessorzServiceTests.test_epyProcessorf   sc    
 %%''
d

9((---dmmIt<<lKKKKKr-   c                R   |                                  \  }}|                    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,   rG   rH   rI   r0   r   r1   datatype)r)   r(   r$   rG   s       r+   test_rpyProcessorzServiceTests.test_rpyProcessoro   s     %%''
d

9((;	
 	
 	
 i..eT***Y///]33333r-   c                   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   r0   r   r$   r   assertIdenticalsite)r)   rT   serverFactorys      r+   test_makePersonalServerFactoryz+ServiceTests.test_makePersonalServerFactory   sz     D\22331$77m_===m02CDDD]/4d;;;;;r-   c                   |                                  }t                      }|                    dd|z   dg           t          |          }|                                 |                     |j                   |                     t          j	        
                    |                     |                     t          j        t          j        |          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.
        r5   r6   
--personalN)r%   r   r'   r   r7   r8   r9   r=   r>   r(   r?   r@   rA   rB   )r)   rC   r*   rD   s       r+   test_personalServerz ServiceTests.test_personalServer   s     {{}}))h$EFFFg&&+,,,t,,---bgdmm&;<<=====r-   c                &   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.
        rX   ~portsr   r6   N)	r   r'   r>   r(   
expanduserjoinr   userSocketNamer1   )r)   r*   r(   s      r+   test_defaultPersonalPathz%ServiceTests.test_defaultPersonalPath   sw     ))l^,,,w!!"',,sM4P"Q"QRR)!,ndnn=====r-   c                    t                      }|                    g            |                     t          j        |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}.
        r\   r   N   )TCP)i  N)r   r'   r1   r   _parseServerr)   r*   s     r+   test_defaultPortzServiceTests.test_defaultPort   si    
 ))R   "77#3A#6==bqbACX	
 	
 	
 	
 	
r-   c                    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:8001rh   ztcp:80028001r\   r   8002   Nr   r'   assertInre   s     r+   test_twoPortszServiceTests.test_twoPorts   sj     ))MMMNNNfgg.q1222fgg.q122222r-   c                ,   t                      }|                    dt          dz   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__r=   r   rS   _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CCvl+++T]G444
4#3Z@@AAAT.<<< 	)1222\7##33I>>>(0111)0111\7##33J???(/00000r-   c                    t                      }t          dz   dfD ]K}|                     t          |j        d|g          }|                     t          |          d|           LdS )zn
        If I{--wsgi} is given an invalid name, L{Options.parseOptions}
        raises L{UsageError}.
        z.nosuchthingzfoo.rp   zNo such WSGI application: N)r   rs   assertRaisesr   r'   r1   str)r)   r*   nameexcs       r+   test_invalidApplicationz$ServiceTests.test_invalidApplication   s|    
 )).7 	N 	ND##J0DxQUFVWWCSXX'LD'L'LMMMM	N 	Nr-   zOpenSSL.SSLNzSSL module is available.c                    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   r   r   r'   r1   args)r)   r*   	exceptions      r+   test_HTTPSFailureOnMissingSSLz*ServiceTests.test_HTTPSFailureOnMissingSSL   sM     ))%%j'2FXX	4inQ6GHHHHHr-   zSSL module is not available.c                    t                      }|                    dg           |                     d|d         d                    |                     d|d         d                    dS )zM
        When SSL support is present, it accepts the --https option.
        r   sslr\   r   443Nrl   re   s     r+    test_HTTPSAcceptedOnAvailableSSLz-ServiceTests.test_HTTPSAcceptedOnAvailableSSL   se    
 ))m_---eWW-a0111eWW-a011111r-   c                    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'   r1   re   s     r+   test_add_header_parsingz$ServiceTests.test_add_header_parsing   sN     ))QQQRRR0<2NOOOOOr-   c                V   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   r:   r;   r<   r0   r   r1   _headers_originalResourcer   Test)r)   r*   rD   r<   s       r+   test_add_header_resourcez%ServiceTests.test_add_header_resource  s    
 ))QQQRRRg&&#A&.7h(;<<<*\<,HIIIh8$)DDDDDr-   c                z   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.0rk   N)r   r'   r   flushWarningstest_noTracebacksDeprecationr1   DeprecationWarninglen)r)   r*   warningss      r+   r   z)ServiceTests.test_noTracebacksDeprecation  s     ))./000G%%t'H&IJJ!Z02DEEEQK	"$U	
 	
 	
 	X*****r-   c                    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   r=   r:   r;   displayTracebacksr)   r*   rD   s      r+   test_displayTracebacksz#ServiceTests.test_displayTracebacks  sX    
 ))45666g&&(+3EFFFFFr-   c                    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   ry   r:   r;   r   r   s      r+   test_displayTracebacksNotGivenz+ServiceTests.test_displayTracebacksNotGiven(  sV    
 ))R   g&&)!,4FGGGGGr-   )r    r!   r    r.   )rs   
__module____qualname____doc__r,   r3   r   r	   
providedByr   rE   rJ   rL   rQ   rV   rY   r`   rf   rn   r}   r   r   r   r   r   r   r   r   r    r-   r+   r   r   (   s           "/ / / / V#L#G,,,: > > >	 >$I I I IL L L L4 4 4 4 < < < < V#L#G,,,: > > >	 > V#L#G,,,: 	> 	> 	>	 	>	
 	
 	
 	
3 3 3 31 1 1 1,N N N N VMM-((46PQQ	I 	I 	I RQ	I VMM-((D02PQQ	2 	2 	2 RQ	2P P P PE E E E+ + + +G G G GH H H H H Hr-   r   c                      e Zd ZddZdS )AddHeadersResourceTestsr    r.   c                T   t          t          j                    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   getChildWithDefaultr1   responseHeadersgetRawHeaders)r)   r<   requests      r+   test_getChildWithDefaultz0AddHeadersResourceTests.test_getChildWithDefault4  s    
 'IKKCCC
 
 r""$$R1110>>tDDtTlSSS0>>tDDtfMMMMMr-   Nr   )rs   r   r   r   r   r-   r+   r   r   3  s.        N N N N N 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   objectrx   r   r   r   r-   r+   <module>r      sS    # " " " " " 				              / / / / / / / / B B B B B B B B , , , , , , 0 0 0 0 0 0 0 0 0 0 0 0 + + + + + + - - - - - - + + + + + +       @ @ @ @ @ @ @ @ + + + + + + # # # # # # ) ) ) ) ) ) ) )            8 7 7 7 7 7 ' ' ' ' ' ' ) ) ) ) ) )fhhHH HH HH HH HH8 HH HH HHVN N N N Nh N N N N Nr-   