Ë
    ojp/  ã                  ó^  — 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       ú;/usr/lib/python3/dist-packages/twisted/web/test/test_tap.pyÚ_pathOptionzServiceTests._pathOption-   sN   € ô ˜Ÿ™›Ó&ˆØ�‰ŒÜ“)ˆØ×Ñ˜h¨¯	©	Ð2Ô3Ø�v‰ˆØ�TˆzÐó    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Ø×Ñ˜d¤DÔ)Ø×Ñ˜Ÿ™ D§I¡IÕ.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ÑBÄDÔIØ×Ñ˜×)Ñ)¨!Ñ,×4Ñ4×=Ñ=×BÑBÀDÇIÁIÔNØ�‰œŸ™Ÿ™ tÓ,Ô-Ø�‰œŸ™¤b§g¡g¨d£m×&;Ñ&;Ó<Õ=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Ó×(Ñ(¨Ô-Ø×Ñ˜dŸm™m¨I°tÓ<¼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Ó×(Ñ(¨Ô-Ø×Ñ˜dŸm™m¨I°tÓ<¼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¨Ó.ˆØ×Ñ˜e¤TÔ*Ø×Ñ˜Ÿ™ 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Ñ0Ô2CÔDØ×Ñ˜]×/Ñ/×4Ñ4°dÕ;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Ó,Ô-Ø�‰œŸ™¤b§g¡g¨d£m×&;Ñ&;Ó<Õ=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˜^Ô,Ü�w‰w×!Ñ!¤"§'¡'§,¡,¨s´M×4PÑ4PÓ"QÓRˆØ×Ñ˜ Ñ)¨!Ñ,°°d°V¨nÕ=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Ô Ø×ÑÜ×"Ñ" 7¨7Ñ#3°AÑ#6¸Ó=¸b¸qÐAÐCXõ	
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Ø�‰�f˜g gÑ.¨qÑ1Ô2Ø�‰�f˜g gÑ.¨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Ñ#3´ZÓ@ÔAØ×Ñ˜T×.Ñ.´Ô<ð 	×Ñ˜×)Ñ)×1Ñ1Ô2ÜŒ\œ7Ó#×3Ñ3°IÔ>Ø�‰˜×(Ñ(×0Ñ0Ô1Ø×Ñ˜×)Ñ)×0Ñ0Ô1ÜŒ\œ7Ó#×3Ñ3°JÔ?Ø�‰˜×(Ñ(×/Ñ/Õ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°×0DÑ0DÀxÐQUÐFVÓWˆCØ×ÑœS ›XÐ)CÀDÀ8Ð'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°'×2FÑ2FÈÈÓXˆ	à×ÑÐ4°i·n±nÀQÑ6GÕ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˜_Ô-à�‰�e˜W WÑ-¨aÑ0Ô1Ø�‰�e˜W WÑ-¨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Ñ0Ô2DÔEØ×ÑØ�Q‰K˜	Ñ"Ð$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˜-Ó(°Ð4Ð6PÓQò	Ió Rð	Iñ ‰M˜-Ó(¨DÐ0Ð2PÓ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~   € ô
 'Ü�I‰I‹KÒCó
ˆô ˜rÓ"ˆØ×$Ñ$ R¨Ô1Ø×Ñ˜×0Ñ0×>Ñ>¸tÓDÀtÈTÀlÔSØ×Ñ˜×0Ñ0×>Ñ>¸tÓDÀtÀfÕ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+   