
    \e*(                     "   d 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)	FORBIDDEN	NOT_FOUNDResource_UnsafeErrorPage_UnsafeForbiddenResource_UnsafeNoResourcegetChildForRequest)DummyRequestc                   N    e Zd ZdZeZeZeZ	d Z
d Zd Zd Zd Zd Zd Zd	 Zd
S )ErrorPageTestszb
    Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and
    L{_UnsafeForbiddenResource}.
    c                     ddl m} |                     || j                   |                                 \  }|                     |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
        imported.
        r   )	ErrorPagecategoryztwisted.web.pages.errorPagemessageN)twisted.web.resourcer   assertIs	errorPageflushWarningsassertEqualDeprecationWarningassertIn)selfr   warnings      >lib/python3.11/site-packages/twisted/web/test/test_resource.pytest_deprecatedErrorPagez'ErrorPageTests.test_deprecatedErrorPage!   s{     	322222i000&&((	,.@AAA3WY5GHHHHH    c                     ddl m} |                     || j                   |                                 \  }|                     |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
        imported.
        r   )
NoResourcer   ztwisted.web.pages.notFoundr   N)r   r!   r   
noResourcer   r   r   r   )r   r!   r   s      r   test_deprecatedNoResourcez(ErrorPageTests.test_deprecatedNoResource/   s{     	433333j$/222&&((	,.@AAA2GI4FGGGGGr   c                     ddl m} |                     || j                   |                                 \  }|                     |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
        imported.
        r   )ForbiddenResourcer   ztwisted.web.pages.forbiddenr   N)r   r%   r   forbiddenResourcer   r   r   r   )r   r%   r   s      r    test_deprecatedForbiddenResourcez/ErrorPageTests.test_deprecatedForbiddenResource=   s}     	;:::::')?@@@&&((	,.@AAA3WY5GHHHHHr   c                     |                      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_getChildK   sG    
 ~~c5%00T]]7FHH==tDDDDDr   c                 @   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   r/   codebriefdetailrequesttemplateexpecteds           r   _pageRenderingTestz!ErrorPageTests._pageRenderingTestS   s    u%% 	 tUE6::W--xw/G/GHHH-t444#_'B&CDEE	
 	
 	
 	
 	
r   c                 p    d}d}d}|                      |||          }|                     ||||           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   r6   r7   r8   r/   s        r   test_errorPageRenderingz&ErrorPageTests.test_errorPageRenderingg   sH     (1~~dE622dE6:::::r   c                 n    d}|                      |          }|                     |t          d|           dS )z@
        L{NoResource} sets the HTTP I{NOT FOUND} code.
        zlong messagezNo Such ResourceN)r"   r<   r   r   r8   r/   s      r   test_noResourceRenderingz'ErrorPageTests.test_noResourceRenderingt   s;      v&&i1CVLLLLLr   c                 n    d}|                      |          }|                     |t          d|           dS )zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)r&   r<   r   r@   s      r   test_forbiddenResourceRenderingz.ErrorPageTests.test_forbiddenResourceRendering|   s=     "%%f--i1EvNNNNNr   N)__name__
__module____qualname____doc__r   r   r!   r"   r%   r&   r   r#   r'   r0   r<   r>   rA   rC    r   r   r   r      s         
 IJ)I I IH H HI I IE E E
 
 
(; ; ;M M MO O O O Or   r   c                       e Zd ZdZd ZdS )DynamicChildzG
    A L{Resource} to be created on the fly by L{DynamicChildren}.
    c                 J    t          j        |            || _        || _        d S N)r   __init__pathr9   r   rN   r9   s      r   rM   zDynamicChild.__init__   s%    $	r   N)rD   rE   rF   rG   rM   rH   r   r   rJ   rJ      s-             r   rJ   c                       e Zd ZdZd ZdS )DynamicChildrenz.
    A L{Resource} with dynamic children.
    c                 "    t          ||          S rL   )rJ   rO   s      r   r-   zDynamicChildren.getChild   s    D'***r   N)rD   rE   rF   rG   r-   rH   r   r   rQ   rQ      s-         + + + + +r   rQ   c                       e Zd ZdZd Zd ZdS )BytesReturnedRenderablezG
    A L{Resource} with minimal capabilities to render a response.
    c                 <    t          j        |            || _        dS )zo
        @param response: A C{bytes} object giving the value to return from
            C{render_GET}.
        N)r   rM   	_response)r   responses     r   rM   z BytesReturnedRenderable.__init__   s     
 	$!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.
        )rV   r   r9   s     r   
render_GETz"BytesReturnedRenderable.render_GET   s    
 ~r   N)rD   rE   rF   rG   rM   rZ   rH   r   r   rT   rT      s<         " " "    r   rT   c                       e Zd ZdZd Zd ZdS )ImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    c                     d S rL   rH   rY   s     r   rZ   z!ImplicitAllowedMethods.render_GET       r   c                     d S rL   rH   rY   s     r   
render_PUTz!ImplicitAllowedMethods.render_PUT   r^   r   N)rD   rE   rF   rG   rZ   r`   rH   r   r   r\   r\      s<         
      r   r\   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	ResourceTestsz 
    Tests for L{Resource}.
    c           	          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.
        rd   N)	r   rQ   rg   assertIsInstancerJ   r   rN   r,   r9   )r   rN   r9   ri   rj   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	TypeErrorrf   rh   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   methodrT   r   r2   )r   r;   r9   ri   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.
        )   GETru      PUT	   FICTIONALN)	r   allowedMethodsr   rv   rq   r   r2   r   setr   r;   ri   r9   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}.
        >   ry   rz   ru   r{   N)	r\   r   rv   rq   r   r2   r   r}   r|   r~   s        r   test_implicitAllowedMethodsz)ResourceTests.test_implicitAllowedMethods   so     -,,)++r""% 18?GLL3s'9#:#:;;;;;r   N)
rD   rE   rF   rG   rl   ro   rs   rw   r   r   rH   r   r   rb   rb      s~         
 
 
5 5 5	G 	G 	G	= 	= 	=A A A< < < < <r   rb   c                   $    e Zd ZdZd Zd Zd ZdS )GetChildForRequestTestsz*
    Tests for L{getChildForRequest}.
    c                     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   r9   ri   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}.
        rd   re   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}.
        rd   re   TN)	r   r   r   rf   r,   r   r   prepathpostpath)r   r9   rootrj   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   N)rD   rE   rF   rG   r   r   r   rH   r   r   r   r     sK         / / /	/ 	/ 	/5 5 5 5 5r   r   N)rG   twisted.trial.unittestr   twisted.web.errorr   twisted.web.http_headersr   r   r   r   r   r	   r   r
   r%   r   r!   r   twisted.web.test.requesthelperr   r   rJ   rQ   rT   r\   rb   r   rH   r   r   <module>r      s    , + + + + + / / / / / / , , , , , ,                  8 7 7 7 7 7kO kO kO kO kOX kO kO kO\    8   + + + + +h + + +    h   *
 
 
 
 
X 
 
 
R< R< R< R< R<H R< R< R<j&5 &5 &5 &5 &5h &5 &5 &5 &5 &5r   