
    /Jfy)                     .   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            
       r    e Zd ZdZeZeZe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                     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      a/home/alex/cs2snipeproduction/venv/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_deprecatedNoResource0   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_getChildL   sG    
 ~~c5%00T]]7FHH==tDDDDDr!   r1   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   r1   r3   r4   r5   requesttemplateexpecteds           r   _pageRenderingTestz!ErrorPageTests._pageRenderingTestT   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   r3   r4   r5   r1   s        r   test_errorPageRenderingz&ErrorPageTests.test_errorPageRenderingj   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   r5   r1   s      r   test_noResourceRenderingz'ErrorPageTests.test_noResourceRenderingw   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   rB   s      r   test_forbiddenResourceRenderingz.ErrorPageTests.test_forbiddenResourceRendering   s=     "%%f--i1EvNNNNNr!   r   N)__name__
__module____qualname____doc__r   r   r#   r$   r'   r(   r    r%   r)   r2   r	   intstrr>   r@   rC   rE    r!   r   r   r      s        
 IJ)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__rP   r;   r   rP   r;   s      r   rS   zDynamicChild.__init__   s%    $	r!   )rG   rH   rI   rJ   bytesr   rS   rM   r!   r   rO   rO      sG         U X $      r!   rO   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.
    rP   r;   r   c                 "    t          ||          S rR   )rO   rT   s      r   r/   zDynamicChildren.getChild   s    D'***r!   N)rG   rH   rI   rJ   rU   r   rO   r/   rM   r!   r   rW   rW      sG         +U +X +, + + + + + +r!   rW   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	   rS   	_response)r   r[   s     r   rS   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!   )rG   rH   rI   rJ   rU   rS   r0   r`   rM   r!   r   rZ   rZ      sb         " "4 " " " "& U      r!   rZ   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 rR   rM   r_   s     r   r`   z!ImplicitAllowedMethods.render_GET       r!   c                     d S rR   rM   r_   s     r   
render_PUTz!ImplicitAllowedMethods.render_PUT   rd   r!   )rG   rH   rI   rJ   r0   r`   rf   rM   r!   r   rb   rb      sb         
& T    & T      r!   rb   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.
        rj   N)	r   rW   rm   assertIsInstancerO   r   rP   r.   r;   )r   rP   r;   ro   rp   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	TypeErrorrl   rn   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   methodrZ   r   r7   )r   r=   r;   ro   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.
        )   GETr{      PUT	   FICTIONALN)	r	   allowedMethodsr   r|   rw   r   r7   r   setr   r=   ro   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   r{   r   N)	rb   r   r|   rw   r   r7   r   r   r   r   s        r   test_implicitAllowedMethodsz)ResourceTests.test_implicitAllowedMethods  so     -,,)++r""% 18?GLL3s'9#:#:;;;;;r!   rF   )
rG   rH   rI   rJ   rr   ru   ry   r}   r   r   rM   r!   r   rh   rh      s         
 
 
 
5 5 5 5	G 	G 	G 	G	= 	= 	= 	=A A A A< < < < < <r!   rh   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;   ro   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}.
        rj   rk   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}.
        rj   rk   TN)	r   r	   r   rl   r.   r   r   prepathpostpath)r   r;   rootrp   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!   rF   )rG   rH   rI   rJ   r   r   r   rM   r!   r   r   r     sZ         / / / /	/ 	/ 	/ 	/5 5 5 5 5 5r!   r   N)rJ   twisted.trial.unittestr   twisted.web.errorr   twisted.web.http_headersr   twisted.web.iwebr   r   r   r   r	   r
   r   r   r'   r   r#   r   twisted.web.test.requesthelperr   r   rO   rW   rZ   rb   rh   r   rM   r!   r   <module>r      s    , + + + + + / / / / / / , , , , , , % % % % % %                  8 7 7 7 7 7mO mO mO mO mOX mO mO m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!   