Ϫfp/^dZddlmZddlZddlZddlmZddlmZddl m Z m Z ddl m Z mZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZmZddl m!Z!ddl"m#Z#ddl$m%Z%m&Z&ddl'm(Z(m)Z)m*Z*m+Z+ddl,m-Z-ddl.m/Z/ddl0m1Z1e2Z3GddeZ4GddeZ5y)z Tests for L{twisted.web.tap}. ) annotationsN)cast)skipIf) endpointsreactor) IReactorCore IReactorUNIX)FilePath) requireModule) ThreadPool) UsageError)PBServerFactory)TestCase)demo)ResourcePublisher UserDirectory) PythonScript)Site)DataFile)Options_AddHeadersResourcemakePersonalServerFactory makeService) DummyRequest) CGIScript) WSGIResourceceZdZdZddZddZeeje  dddZ ddZ ddZ ddZ dd Zeeje  ddd Zeeje  ddd Zdd Zdd ZddZddZeeddudddZeeddudddZddZddZddZddZddZy) ServiceTestszD Tests for the service creation APIs in L{twisted.web.tap}. ct|j}|jt}|j d|j g|d}||fS)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. --pathroot)r mktempmakedirsr parseOptionspath)selfr&optionsr"s ;/usr/lib/python3/dist-packages/twisted/web/test/test_tap.py _pathOptionzServiceTests._pathOption-sN & )h 23vTzc|j\}}|j|t|j|j|jy)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_pathzServiceTests.test_path>s> %%' d dD) DII.r+z0The reactor does not support UNIX domain socketsct|j}|j|j}t}|j dd|zd|j gt |}|j|j|j|j|jdjjt|j|jdjjj |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. --portunix:r!rN)r r#r$rr%r&r startService addCleanup stopServicer-servicesfactoryresourcerr. assertTrueosexistsstatS_ISSOCKst_mode)r'r&portr(services r)test_pathServerzServiceTests.test_pathServerGs  & {{})h$$))LMg& ++, g..q199BBDI ))!,44==BBDIIN t,-  bggdm&;&;<=r+c|j\}}|jdjd|j|j ddt y)z The I{--path} option creates a root resource which serves a L{CGIScript} instance for any child with the C{".cgi"} extension. zfoo.cgir+N)r*child setContentr-getChildrr/s r)test_cgiProcessorzServiceTests.test_cgiProcessor]sH %%' d 9((- dmmItr?)r'r@r(rAs r)test_personalServerz ServiceTests.test_personalServers{{})h$ EFg& ++, t,-  bggdm&;&;<=r+ct}|jdgtjj tjj dt j}|j|ddd|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~portsrr3N) rr%r;r& expanduserjoinruserSocketNamer.)r'r(r&s r)test_defaultPersonalPathz%ServiceTests.test_defaultPersonalPathsd)l^,ww!!"'',,sM4P4P"QR )!,dVn=r+ct}|jg|jtj|ddddddy)zl If the I{--port} option is not specified, L{Options} defaults the port to C{8080}. rYrN)TCP)iN)rr%r.r _parseServerr'r(s r)test_defaultPortzServiceTests.test_defaultPortsK )R    " "77#3A#6 =bq ACX r+ct}|jgd|jd|dd|jd|ddy)zQ If the I{--http} option is given twice, there are two listeners )--listenztcp:8001reztcp:80028001rYr8002Nrr%assertInrbs r) test_twoPortszServiceTests.test_twoPortssJ)MN fgg.q12 fgg.q12r+ct}|jdtdzg|d}|j|t|j |j t|jt|jt|j |jt|j|jjtt tj#d|j|jj|j|jj$tt tj#d|j|jj$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"startupshutdownN)rr%__name__r:rrP_reactorr isinstance _threadpoolr _application application assertFalsestartedrrfireSystemEventjoined)r'r(r"s r) test_wsgizServiceTests.test_wsgis  )h>(ABCv l+ T]]G4  4#3#3Z@A T.. < ))112 \7#33I> ((001 ))001 \7#33J? ((//0r+ct}tdzdfD]C}|jt|jd|g}|j t |d|Ey)zn If I{--wsgi} is given an invalid name, L{Options.parseOptions} raises L{UsageError}. z .nosuchthingzfoo.rmzNo such WSGI application: N)rrp assertRaisesr r%r.str)r'r(nameexcs r)test_invalidApplicationz$ServiceTests.test_invalidApplicationsb ).7 ND##J0D0DxQUFVWC   SX)CD8'L M Nr+z OpenSSL.SSLNzSSL module is available.ct}|jt|jdg}|j d|j dy)zp An L{UsageError} is raised when C{https} is requested but there is no support for SSL. --https=443zSSL support not installedrN)rr|r r%r.args)r'r( exceptions r)test_HTTPSFailureOnMissingSSLz*ServiceTests.test_HTTPSFailureOnMissingSSLsA )%%j'2F2FX  4innQ6GHr+zSSL module is not available.ct}|jdg|jd|dd|jd|ddy)zM When SSL support is present, it accepts the --https option. rsslrYr443Nrirbs r) test_HTTPSAcceptedOnAvailableSSLz-ServiceTests.test_HTTPSAcceptedOnAvailableSSLsK )m_- eWW-a01 eWW-a01r+clt}|jgd|j|dddgy)zE When --add-header is specific, the value is parsed.  --add-headerzK1: V1rzK2: V2 extraHeadersK1V1K2V2N)rr%r.rbs r)test_add_header_parsingz$ServiceTests.test_add_header_parsings4)QR 0<2NOr+cVt}|jgdt|}|jdjj }|j |t|j|jddg|j |jtjy)zj When --add-header is specified, the resource is a composition that adds headers. rrrrN) rr%rr7r8r9r-rr._headers_originalResourcerTest)r'r(rAr9s r)test_add_header_resourcez%ServiceTests.test_add_header_resources )QRg&##A&..77 h(;< **\<,HI h88$))Dr+c(t}|jdgt||j|jg}|j |ddt |j |ddd|j t|dy)z7 Passing --notracebacks is deprecated. z--notracebacksrcategorymessagez/--notracebacks was deprecated in Twisted 19.7.0rhN)rr%r flushWarningstest_noTracebacksDeprecationr.DeprecationWarninglen)r'r(warningss r)rz)ServiceTests.test_noTracebacksDeprecations)./0G%%t'H'H&IJ !Z02DE  QK "$U  X*r+ct}|jdgt|}|j|jdj j y)zm Passing --display-tracebacks will enable traceback rendering on the generated Site. z--display-tracebacksrN)rr%rr:r7r8displayTracebacksr'r(rAs r)test_displayTracebacksz#ServiceTests.test_displayTracebackssJ )456g& ((+33EEFr+ct}|jgt|}|j|jdj j y)zt Not passing --display-tracebacks will leave traceback rendering on the generated Site off. rN)rr%rrvr7r8rrs r)test_displayTracebacksNotGivenz+ServiceTests.test_displayTracebacksNotGiven(sH )R g& ))!,44FFGr+)returnztuple[FilePath[str], File]rNone)rp __module__ __qualname____doc__r*r0rr providedByrrBrGrIrNrSrVr]rcrkrzrr rrrrrrrr+r)rr(s6"/ #L # #G ,,:> >$IL4 < #L # #G ,,: >  > #L # #G ,,: >  >  31,N M- ( 46PQ IR I M- (D 02PQ 2R 2P E +GHr+rceZdZddZy)AddHeadersResourceTestsc0ttjgd}tg}|j d||j |j jdddg|j |j jddgy) zc When getChildWithDefault is invoked, it adds the headers to the response. )rr)rV3rrrrrN)rrrrgetChildWithDefaultr.responseHeaders getRawHeaders)r'r9requests r)test_getChildWithDefaultz0AddHeadersResourceTests.test_getChildWithDefault4s~ ' IIKC r"$$R1 00>>tDtTlS 00>>tDtfMr+Nr)rprrrrr+r)rr3s Nr+r)6r __future__rr;r=typingrunittestrtwisted.internetrrtwisted.internet.interfacesrr twisted.python.filepathr twisted.python.reflectr twisted.python.threadpoolr twisted.python.usager twisted.spread.pbrtwisted.trial.unittestr twisted.webrtwisted.web.distribrrtwisted.web.scriptrtwisted.web.serverrtwisted.web.staticrrtwisted.web.taprrrrtwisted.web.test.requesthelperrtwisted.web.twcgirtwisted.web.wsgirobjectrurrrr+r)rs}# /B,00+-+@+#) 8')h HH8HHV Nh Nr+