
    tf(                     .   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dS )z$
Tests for L{twisted.web.resource}.
    )TestCase)UnsupportedMethod)Headers)IRequest)	FORBIDDEN	NOT_FOUNDResource_UnsafeErrorPage_UnsafeForbiddenResource_UnsafeNoResourcegetChildForRequest)DummyRequestc            
       f    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dS )ErrorPageTestszb
    Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and
    L{_UnsafeForbiddenResource}.
    returnNc                     t          ddd          }|                                 \  }|                     |d         t                     |                     d|d                    dS )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      ]/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/web/test/test_resource.pytest_deprecatedErrorPagez'ErrorPageTests.test_deprecatedErrorPage   sd     c5&))&&((	,.@AAA3WY5GHHHHH    c                     t                      }|                                 \  }|                     |d         t                     |                     d|d                    dS )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)   s\     LL&&((	,.@AAA2GI4FGGGGGr"   c                     t                      }|                                 \  }|                     |d         t                     |                     d|d                    dS )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   s^     &&((	,.@AAA3WY5GHHHHHr"   c                     t          ddd          }|                     |                    dt                                |           dS )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?   sC    
 eU++T]]7FHH==tDDDDDr"   r0   codebriefdetailc                 @   t          dg          }d}|||||fz  }|                     |                    |          |                    d                     |                     |j        |           |                     |j        t          ddgi                     d S )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/GHHH-t444#_'B&CDEE	
 	
 	
 	
 	
r"   c                 d    d}d}d}t          |||          }|                     ||||           dS )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]   sD     (1uf--dE6:::::r"   c                 b    d}t          |          }|                     |t          d|           dS )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   s7      &!!i1CVLLLLLr"   c                 b    d}t          |          }|                     |t          d|           dS )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   s7     " ((i1EvNNNNNr"   r   N)__name__
__module____qualname____doc__r!   r%   r(   r1   r	   intstrr=   r?   rB   rD    r"   r    r   r      s         
	I 	I 	I 	I	H 	H 	H 	H	I 	I 	I 	IE E E E

$'
03
=@
	
 
 
 
,; ; ; ;M M M MO O O O O Or"   r   c                   &    e Zd ZdZdededdfdZdS )DynamicChildzG
    A L{Resource} to be created on the fly by L{DynamicChildren}.
    pathr:   r   Nc                 J    t          j        |            || _        || _        d S 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   {   sG         U X $      r"   rN   c                   &    e Zd ZdZdededefdZdS )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      sG         +U +X +, + + + + + +r"   rV   c                   2    e Zd ZdZdeddfdZdedefdZdS )BytesReturnedRenderablezG
    A L{Resource} with minimal capabilities to render a response.
    responser   Nc                 <    t          j        |            || _        dS )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      sb         " "4 " " " "& U      r"   rY   c                   2    e Zd ZdZdeddfdZdeddfdZdS )ImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    r:   r   Nc                     d S rQ   rL   r^   s     r    r_   z!ImplicitAllowedMethods.render_GET       r"   c                     d S rQ   rL   r^   s     r    
render_PUTz!ImplicitAllowedMethods.render_PUT   rc   r"   )rF   rG   rH   rI   r/   r_   re   rL   r"   r    ra   ra      sb         
& T    & T      r"   ra   c                   B    e Zd ZdZd
dZd
dZd
dZd
dZd
dZd
d	Z	dS )ResourceTestsz 
    Tests for L{Resource}.
    r   Nc           	          t                      }t                      }t                      }|                    d|           |                    d|           |                     ||                    dt	          g                                dS )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//R8H8HII	
 	
 	
 	
 	
r"   c                    d}t          g           }t                      }|                    ||          }|                     |t                     |                     |j        |           |                     |j        |           dS )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   s    
 r"""$$,,T7;;e\222T***U]G44444r"   c                     t                      }t                      }t                      }|                     t          |j        d|           |                     t          |j        d|           dS )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   s_    
 ::

**)X%6uEEE)X%6gFFFFFr"   c                     d}t          g           }d|_        t          |          }|                     ||                    |                     dS )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   sR    
 +r"" *8448??7#;#;<<<<<r"   c                    g d}t                      }||_        t          g           }d|_        |                     t
          |j        |          }|                     t          |          t          |j                             dS )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   s{     -,,::"*r""% 18?GLLXC,>(?(?@@@@@r"   c                     h d}t                      }t          g           }d|_        |                     t          |j        |          }|                     |t          |j                             dS )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   so     -,,)++r""% 18?GLL3s'9#:#:;;;;;r"   rE   )
rF   rG   rH   rI   rq   rt   rx   r|   r   r   rL   r"   r    rg   rg      s         
 
 
 
5 5 5 5	G 	G 	G 	G	= 	= 	= 	=A A A A< < < < < <r"   rg   c                   *    e Zd ZdZddZddZddZdS )GetChildForRequestTestsz*
    Tests for L{getChildForRequest}.
    r   Nc                     t          g           }t                      }t          ||          }|                     ||           dS )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  sE    
 r""::#Hg66Xv.....r"   c                     t          ddg          }t                      }d|_        t          ||          }|                     ||           dS )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  sQ    
 /00::#Hg66Xv.....r"   c                 R   t          ddg          }t                      }t                      }d|_        |                    d|           |                     |t          ||                     |                     |j        dg           |                     |j        dg           dS )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0zz

fe$$$U$6tW$E$EFFF6(333)F844444r"   rE   )rF   rG   rH   rI   r   r   r   rL   r"   r    r   r     sZ         / / / /	/ 	/ 	/ 	/5 5 5 5 5 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 7 7 7 7 7`O `O `O `O `OX `O `O `OF    8   + + + + +h + + +    h   *
 
 
 
 
X 
 
 
R< R< R< R< R<H R< R< R<j&5 &5 &5 &5 &5h &5 &5 &5 &5 &5r"   