Ϫfy)dZddlmZddlmZddlmZddlmZddl m Z m Z m Z m ZmZmZmZddlmZGdd eZGd d e ZGd d e ZGdde ZGdde ZGddeZGddeZy)z$ Tests for L{twisted.web.resource}. )TestCase)UnsupportedMethod)Headers)IRequest) FORBIDDEN NOT_FOUNDResource_UnsafeErrorPage_UnsafeForbiddenResource_UnsafeNoResourcegetChildForRequest) DummyRequestc peZdZdZeZeZeZ ddZ ddZ ddZ ddZ ded ed ed eddf d Zdd ZddZddZy)ErrorPageTestszb Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and L{_UnsafeForbiddenResource}. returnNcddlm}|j||j|j \}|j |dt |jd|dy)z The public C{twisted.web.resource.ErrorPage} alias for the corresponding C{_Unsafe} class produces a deprecation warning when imported. r) ErrorPagecategoryztwisted.web.pages.errorPagemessageN)twisted.web.resourcerassertIs errorPage flushWarnings assertEqualDeprecationWarningassertIn)selfrwarnings @/usr/lib/python3/dist-packages/twisted/web/test/test_resource.pytest_deprecatedErrorPagez'ErrorPageTests.test_deprecatedErrorPage"sS 3 i0&&(  ,.@A 3WY5GHcddlm}|j||j|j \}|j |dt |jd|dy)z The public C{twisted.web.resource.NoResource} alias for the corresponding C{_Unsafe} class produces a deprecation warning when imported. r) NoResourcerztwisted.web.pages.notFoundrN)rr#r noResourcerrrr)rr#rs rtest_deprecatedNoResourcez(ErrorPageTests.test_deprecatedNoResource0sS 4 j$//2&&(  ,.@A 2GI4FGr!cddlm}|j||j|j \}|j |dt |jd|dy)z The public C{twisted.web.resource.ForbiddenResource} alias for the corresponding C{_Unsafe} class produce a deprecation warning when imported. r)ForbiddenResourcerztwisted.web.pages.forbiddenrN)rr'rforbiddenResourcerrrr)rr'rs r test_deprecatedForbiddenResourcez/ErrorPageTests.test_deprecatedForbiddenResource>sV ; ')?)?@&&(  ,.@A 3WY5GHr!c~|jddd}|j|jdt|y)zj The C{getChild} method of L{ErrorPage} returns the L{ErrorPage} it is called on. AfoobarsnameN)rassertIdenticalgetChildobject)rpages r test_getChildzErrorPageTests.test_getChildLs3 ~~c5%0 T]]7FH=tDr!r1codebriefdetailctdg}d}|||||fz}|j|j||jd|j|j||j|j t ddgiy)Nr!zg %s - %s

%s

%s

zutf-8s content-typestext/html; charset=utf-8)rrrenderencode responseCoderesponseHeadersr)rr1r3r4r5requesttemplateexpecteds r_pageRenderingTestz!ErrorPageTests._pageRenderingTestTsu%  tUE6:: W-xw/GH --t4   # # _'B&CD E r!c^d}d}d}|j|||}|j||||y)a L{ErrorPage.render} returns a C{bytes} describing the error defined by the response code and message passed to L{ErrorPage.__init__}. It also uses that response code to set the response code on the L{Request} passed in. r+zbrief description textzmuch longer text might go hereN)rr>)rr3r4r5r1s rtest_errorPageRenderingz&ErrorPageTests.test_errorPageRenderingjs9(1~~dE62 dE6:r!cZd}|j|}|j|td|y)z@ L{NoResource} sets the HTTP I{NOT FOUND} code. z long messagezNo Such ResourceN)r$r>rrr5r1s rtest_noResourceRenderingz'ErrorPageTests.test_noResourceRenderingws, v& i1CVLr!cZd}|j|}|j|td|y)zG L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code. zlonger messagezForbidden ResourceN)r(r>rrBs rtest_forbiddenResourceRenderingz.ErrorPageTests.test_forbiddenResourceRenderings."%%f- i1EvNr!rN)__name__ __module__ __qualname____doc__rrr#r$r'r(r r%r)r2r intstrr>r@rCrEr!rrrso IJ) I H IE  $' 03 =@  , ;MOr!rc$eZdZdZdededdfdZy) DynamicChildzG A L{Resource} to be created on the fly by L{DynamicChildren}. pathr;rNcJtj|||_||_yN)r __init__rPr;rrPr;s rrSzDynamicChild.__init__s$  r!)rGrHrIrJbytesrrSrMr!rrOrOs!UX$r!rOc$eZdZdZdededefdZy)DynamicChildrenz. A L{Resource} with dynamic children. rPr;rct||SrR)rOrTs rr/zDynamicChildren.getChildsD'**r!N)rGrHrIrJrUrrOr/rMr!rrWrWs!+U+X+,+r!rWc0eZdZdZdeddfdZdedefdZy)BytesReturnedRenderablezG A L{Resource} with minimal capabilities to render a response. responserNc<tj|||_y)zo @param response: A C{bytes} object giving the value to return from C{render_GET}. N)r rS _response)rr[s rrSz BytesReturnedRenderable.__init__s $!r!r;c|jS)z} Render a response to a I{GET} request by returning a short byte string to be written by the server. )r]rr;s r render_GETz"BytesReturnedRenderable.render_GETs ~~r!)rGrHrIrJrUrSr0r`rMr!rrZrZs-""4"&Ur!rZc0eZdZdZdeddfdZdeddfdZy)ImplicitAllowedMethodszn A L{Resource} which implicitly defines its allowed methods by defining renderers to handle them. r;rNcyrRrMr_s rr`z!ImplicitAllowedMethods.render_GET r!cyrRrMr_s r render_PUTz!ImplicitAllowedMethods.render_PUTrdr!)rGrHrIrJr0r`rfrMr!rrbrbs-  & T  & T r!rbc@eZdZdZd dZd dZd dZd dZd dZd dZ y) ResourceTestsz Tests for L{Resource}. Nc t}t}t}|jd||jd||j||jdt gy)z L{Resource.putChild} adds a I{static} child to the resource. That child is returned from any call to L{Resource.getChildWithDefault} for the child's path. foobarN)r putChildr.getChildWithDefaultrrresourcechildsiblings rtest_staticChildrenz!ResourceTests.test_staticChildrens^ : *&%(&'*  8// R8HI r!cd}tg}t}|j||}|j|t|j |j ||j|j|y)z L{Resource.getChildWithDefault} delegates to L{Resource.getChild} when the requested path is not associated with any static child. rjN) rrWrmassertIsInstancerOrrPr.r;)rrPr;rorps rtest_dynamicChildrenz"ResourceTests.test_dynamicChildrense r""$,,T7; e\2 T* U]]G4r!ct}t}t}|jt|jd||jt|jd|y)zv Test that passing the wrong type to putChild results in a warning, and a failure in Python 3 r,N)r assertRaises TypeErrorrlrns rtest_staticChildPathTypez&ResourceTests.test_staticChildPathTypesM : * )X%6%6uE )X%6%6gFr!cd}tg}d|_t|}|j||j |y)z When not otherwise overridden, L{Resource.render} treats a I{HEAD} request as if it were a I{GET} request. sinsert response hereHEADN)rmethodrZrr7)rr=r;ros rtest_defaultHEADzResourceTests.test_defaultHEADs= +r" *84 8??7#;,>(?@r!chd}t}tg}d|_|jt|j |}|j |t|jy)al The L{UnsupportedMethod} raised by L{Resource.render} for an unsupported request method has a C{allowedMethods} attribute set to a list of the methods supported by the L{Resource}, as determined by the I{render_}-prefixed methods which it defines, if C{allowedMethods} is not explicitly defined by the L{Resource}. >rrr{rN) rbrr|rwrr7rrrrs rtest_implicitAllowedMethodsz)ResourceTests.test_implicitAllowedMethodssV-)+r"% 18??GL 3s'9'9#:;r!rF) rGrHrIrJrrruryr}rrrMr!rrhrhs'   5 G = A rs,/,%8mOXmO`8+h+h*  X  R