
    Vh(                        d 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mZmZmZmZmZ ddlmZ  G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)z$
Tests for L{twisted.web.resource}.
    )TestCase)UnsupportedMethod)Headers)IRequest)	FORBIDDEN	NOT_FOUNDResource_UnsafeErrorPage_UnsafeForbiddenResource_UnsafeNoResourcegetChildForRequest)DummyRequestc            
       d    e Zd ZdZddZddZddZddZded	e	d
e
de
ddf
dZddZddZddZy)ErrorPageTestszb
    Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and
    L{_UnsafeForbiddenResource}.
    returnNc                     t        ddd      }| j                         \  }| j                  |d   t               | j	                  d|d          y)z
        The public C{twisted.web.resource.ErrorPage} alias for the
        corresponding C{_Unsafe} class produces a deprecation warning when
        called.
        {   onozono!categoryztwisted.web.pages.errorPagemessageN)	ErrorPageflushWarningsassertEqualDeprecationWarningassertInself_warnings      N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/test/test_resource.pytest_deprecatedErrorPagez'ErrorPageTests.test_deprecatedErrorPage   sL     c5&)&&(	,.@A3WY5GH    c                     t               }| j                         \  }| j                  |d   t               | j	                  d|d          y)z
        The public C{twisted.web.resource.NoResource} alias for the
        corresponding C{_Unsafe} class produces a deprecation warning when
        called.
        r   ztwisted.web.pages.notFoundr   N)
NoResourcer   r   r   r   r   s      r    test_deprecatedNoResourcez(ErrorPageTests.test_deprecatedNoResource)   sE     L&&(	,.@A2GI4FGr"   c                     t               }| j                         \  }| j                  |d   t               | j	                  d|d          y)z
        The public C{twisted.web.resource.ForbiddenResource} alias for the
        corresponding C{_Unsafe} class produce a deprecation warning when
        called.
        r   ztwisted.web.pages.forbiddenr   N)ForbiddenResourcer   r   r   r   r   s      r     test_deprecatedForbiddenResourcez/ErrorPageTests.test_deprecatedForbiddenResource4   sF     &&(	,.@A3WY5GHr"   c                 r    t        ddd      }| j                  |j                  dt                     |       y)zj
        The C{getChild} method of L{ErrorPage} returns the L{ErrorPage} it is
        called on.
        A  foobars   nameN)r   assertIdenticalgetChildobject)r   pages     r    test_getChildzErrorPageTests.test_getChild?   s/    
 eU+T]]7FH=tDr"   r0   codebriefdetailc                    t        dg      }d}|||||fz  }| j                  |j                  |      |j                  d             | j                  |j                  |       | j                  |j
                  t        ddgi             y )Nr"   zg
<html>
  <head><title>%s - %s</title></head>
  <body>
    <h1>%s</h1>
    <p>%s</p>
  </body>
</html>
zutf-8s   content-types   text/html; charset=utf-8)r   r   renderencoderesponseCoderesponseHeadersr   )r   r0   r2   r3   r4   requesttemplateexpecteds           r    _pageRenderingTestz!ErrorPageTests._pageRenderingTestG   s     u% 	 tUE6::W-xw/GH--t4##_'B&CDE	
r"   c                 R    d}d}d}t        |||      }| 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*   zbrief description textzmuch longer text might go hereN)r   r=   )r   r2   r3   r4   r0   s        r    test_errorPageRenderingz&ErrorPageTests.test_errorPageRendering]   s5     (1uf-dE6:r"   c                 N    d}t        |      }| j                  |t        d|       y)z@
        L{NoResource} sets the HTTP I{NOT FOUND} code.
        zlong messagezNo Such ResourceN)r$   r=   r   r   r4   r0   s      r    test_noResourceRenderingz'ErrorPageTests.test_noResourceRenderingj   s(      &!i1CVLr"   c                 N    d}t        |      }| j                  |t        d|       y)zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)r'   r=   r   rA   s      r    test_forbiddenResourceRenderingz.ErrorPageTests.test_forbiddenResourceRenderingr   s(     " (i1EvNr"   r   N)__name__
__module____qualname____doc__r!   r%   r(   r1   r	   intstrr=   r?   rB   rD    r"   r    r   r      s]    
	I	H	IE

$'
03
=@
	
,;MOr"   r   c                   $    e Zd ZdZdededdfdZy)DynamicChildzG
    A L{Resource} to be created on the fly by L{DynamicChildren}.
    pathr:   r   Nc                 J    t        j                  |        || _        || _        y N)r	   __init__rO   r:   r   rO   r:   s      r    rR   zDynamicChild.__init__   s    $	r"   )rF   rG   rH   rI   bytesr   rR   rL   r"   r    rN   rN   {   s!    U X $ r"   rN   c                   $    e Zd ZdZdededefdZy)DynamicChildrenz.
    A L{Resource} with dynamic children.
    rO   r:   r   c                     t        ||      S rQ   )rN   rS   s      r    r.   zDynamicChildren.getChild   s    D'**r"   N)rF   rG   rH   rI   rT   r   rN   r.   rL   r"   r    rV   rV      s!    +U +X +, +r"   rV   c                   0    e Zd ZdZdeddfdZdedefdZy)BytesReturnedRenderablezG
    A L{Resource} with minimal capabilities to render a response.
    responser   Nc                 <    t        j                  |        || _        y)zo
        @param response: A C{bytes} object giving the value to return from
            C{render_GET}.
        N)r	   rR   	_response)r   rZ   s     r    rR   z BytesReturnedRenderable.__init__   s    
 	$!r"   r:   c                     | j                   S )z}
        Render a response to a I{GET} request by returning a short byte string
        to be written by the server.
        )r\   r   r:   s     r    
render_GETz"BytesReturnedRenderable.render_GET   s    
 ~~r"   )rF   rG   rH   rI   rT   rR   r/   r_   rL   r"   r    rY   rY      s-    " "4 "& U r"   rY   c                   0    e Zd ZdZdeddfdZdeddfdZy)ImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    r:   r   Nc                      y rQ   rL   r^   s     r    r_   z!ImplicitAllowedMethods.render_GET       r"   c                      y rQ   rL   r^   s     r    
render_PUTz!ImplicitAllowedMethods.render_PUT   rc   r"   )rF   rG   rH   rI   r/   r_   re   rL   r"   r    ra   ra      s-    
& T & T r"   ra   c                   @    e Zd ZdZd	dZd	dZd	dZd	dZd	dZd	dZ	y)
ResourceTestsz 
    Tests for L{Resource}.
    Nc           	          t               }t               }t               }|j                  d|       |j                  d|       | j                  ||j                  dt	        g                    y)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.
           foo   barN)r	   putChildr-   getChildWithDefaultr   r   resourcechildsiblings       r    test_staticChildrenz!ResourceTests.test_staticChildren   s^     :
*&%(&'*8//R8HI	
r"   c                     d}t        g       }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.
        ri   N)	r   rV   rl   assertIsInstancerN   r   rO   r-   r:   )r   rO   r:   rn   ro   s        r    test_dynamicChildrenz"ResourceTests.test_dynamicChildren   se    
 r""$,,T7;e\2T*U]]G4r"   c                     t               }t               }t               }| j                  t        |j                  d|       | j                  t        |j                  d|       y)zv
        Test that passing the wrong type to putChild results in a warning,
        and a failure in Python 3
        r+   N)r	   assertRaises	TypeErrorrk   rm   s       r    test_staticChildPathTypez&ResourceTests.test_staticChildPathType   sM    
 :
*)X%6%6uE)X%6%6gFr"   c                     d}t        g       }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.
        s   insert response here   HEADN)r   methodrY   r   r6   )r   r<   r:   rn   s       r    test_defaultHEADzResourceTests.test_defaultHEAD   s=    
 +r" *848??7#;<r"   c                     g d}t               }||_        t        g       }d|_        | j	                  t
        |j                  |      }| j                  t        |      t        |j                               y)z
        The L{UnsupportedMethod} raised by L{Resource.render} for an unsupported
        request method has a C{allowedMethods} attribute set to the value of the
        C{allowedMethods} attribute of the L{Resource}, if it has one.
        )   GETrz      PUT	   FICTIONALN)	r	   allowedMethodsr   r{   rv   r   r6   r   setr   r<   rn   r:   excs        r    test_explicitAllowedMethodsz)ResourceTests.test_explicitAllowedMethods   sa     -:"*r"% 18??GLXC,>,>(?@r"   c                     h d}t               }t        g       }d|_        | j                  t        |j
                  |      }| j                  |t        |j                               y)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}.
        >   r~   r   rz   r   N)	ra   r   r{   rv   r   r6   r   r   r   r   s        r    test_implicitAllowedMethodsz)ResourceTests.test_implicitAllowedMethods   sV     -)+r"% 18??GL3s'9'9#:;r"   rE   )
rF   rG   rH   rI   rq   rt   rx   r|   r   r   rL   r"   r    rg   rg      s'    
5	G	=A<r"   rg   c                   (    e Zd ZdZddZddZddZy)GetChildForRequestTestsz*
    Tests for L{getChildForRequest}.
    Nc                 j    t        g       }t               }t        ||      }| j                  ||       y)z
        L{getChildForRequest} returns whatever resource has been reached by the
        time the request's C{postpath} is empty.
        N)r   r	   r   r-   r   r:   rn   results       r    test_exhaustedPostPathz.GetChildForRequestTests.test_exhaustedPostPath  s1    
 r":#Hg6Xv.r"   c                 |    t        ddg      }t               }d|_        t        ||      }| j	                  ||       y)z
        L{getChildForRequest} returns the first resource it encounters with a
        C{isLeaf} attribute set to C{True}.
        ri   rj   TN)r   r	   isLeafr   r-   r   s       r    test_leafResourcez)GetChildForRequestTests.test_leafResource  s=    
 /0:#Hg6Xv.r"   c                 $   t        ddg      }t               }t               }d|_        |j                  d|       | j	                  |t        ||             | j                  |j                  dg       | j                  |j                  dg       y)z
        As path segments from the request are traversed, they are taken from
        C{postpath} and put into C{prepath}.
        ri   rj   TN)	r   r	   r   rk   r-   r   r   prepathpostpath)r   r:   rootro   s       r    test_postPathToPrePathz.GetChildForRequestTests.test_postPathToPrePath   s|    
 /0z
fe$U$6tW$EF6(3))F84r"   rE   )rF   rG   rH   rI   r   r   r   rL   r"   r    r   r     s    /	/5r"   r   N)rI   twisted.trial.unittestr   twisted.web.errorr   twisted.web.http_headersr   twisted.web.iwebr   twisted.web.resourcer   r   r	   r
   r   r   r'   r   r$   r   twisted.web.test.requesthelperr   r   rN   rV   rY   ra   rg   r   rL   r"   r    <module>r      s    , / , %   8`OX `OF8 +h +h *
X 
R<H R<j&5h &5r"   