
    \eB.                     V   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e          Z/ G 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                      e Zd ZdZd Zd Z e ej        e	           d          d             Z
d Zd Zd Zd	 Z e ej        e	           d          d
             Z e ej        e	           d          d             Zd Zd Zd Zd Z e ed          dud          d             Z e ed          du d          d             Zd Zd Zd Zd Zd ZdS )ServiceTestszD
    Tests for the service creation APIs in L{twisted.web.tap}.
    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       9lib/python3.11/site-packages/twisted/web/test/test_tap.py_pathOptionzServiceTests._pathOption,   s]     &&))h	2333vTz    c                     |                                  \  }}|                     |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stopServicer*   servicesfactoryresourcer   r+   
assertTrueosexistsstatS_ISSOCKst_mode)r$   r#   portr%   services        r&   test_pathServerzServiceTests.test_pathServerF   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
setContentr*   getChildr   r,   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'   rA   rB   r*   rC   r   r,   s      r&   test_epyProcessorzServiceTests.test_epyProcessore   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'   rA   rB   rC   r*   r   r+   datatype)r$   r#   r   rA   s       r&   test_rpyProcessorzServiceTests.test_rpyProcessorn   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   r*   r   r   r   assertIdenticalsite)r$   rN   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.
        r/   r0   
--personalN)r    r   r"   r   r1   r2   r3   r7   r8   r#   r9   r:   r;   r<   )r$   r=   r%   r>   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.
        rR   ~portsr   r0   N)	r   r"   r8   r#   
expanduserjoinr   userSocketNamer+   )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}.
        rV   r   N   )TCP)i  N)r   r"   r+   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:8001rb   ztcp:80028001rV   r   8002   Nr   r"   assertInr_   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          j        d           |                     |j	        j                   |                     |j	        j                   t          j        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__r7   r   rM   _reactorr   
isinstance_threadpoolr	   _applicationapplicationassertFalsestarted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	***(0111)0111
+++(/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.rj   zNo such WSGI application: N)r   rm   assertRaisesr
   r"   r+   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   ry   r
   r"   r+   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   sslrV   r   443Nrf   r_   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"   r+   r_   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   r4   r5   r6   r*   r   r+   _headers_originalResourcer   Test)r$   r%   r>   r6   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.0re   N)r   r"   r   flushWarningstest_noTracebacksDeprecationr+   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   r7   r4   r5   displayTracebacksr$   r%   r>   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   rs   r4   r5   r   r   s      r&   test_displayTracebacksNotGivenz+ServiceTests.test_displayTracebacksNotGiven'  sV    
 ))R   g&&)!,4FGGGGGr(   )rm   
__module____qualname____doc__r'   r-   r   r   
providedByr   r?   rD   rF   rK   rP   rS   rZ   r`   rh   rw   r}   r   r   r   r   r   r   r   r    r(   r&   r   r   '   sK          "/ / / V#L#G,,,: > >	 >$I I IL L L4 4 4 < < < V#L#G,,,: > >	 > V#L#G,,,: 	> 	>	 	>	
 	
 	
3 3 31 1 1,N N N VMM-((46PQQ	I 	I RQ	I VMM-((D02PQQ	2 	2 RQ	2P P PE E E+ + +G G GH H H H Hr(   r   c                       e Zd Zd ZdS )AddHeadersResourceTestsc                 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   getChildWithDefaultr+   responseHeadersgetRawHeaders)r$   r6   requests      r&   test_getChildWithDefaultz0AddHeadersResourceTests.test_getChildWithDefault3  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(   N)rm   r   r   r   r   r(   r&   r   r   2  s(        N N N N Nr(   r   )1r   r8   r:   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   objectrr   r   r   r   r(   r&   <module>r      s)   
 
			        / / / / / / / / 4 4 4 4 4 4 , , , , , , 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(   