
    Ϫfy)                        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            
       p    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y)ErrorPageTestszb
    Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and
    L{_UnsafeForbiddenResource}.
    returnNc                     ddl m} | j                  || j                         | 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
        imported.
        r   )	ErrorPagecategoryztwisted.web.pages.errorPagemessageN)twisted.web.resourcer   assertIs	errorPageflushWarningsassertEqualDeprecationWarningassertIn)selfr   warnings      @/usr/lib/python3/dist-packages/twisted/web/test/test_resource.pytest_deprecatedErrorPagez'ErrorPageTests.test_deprecatedErrorPage"   sS     	3i0&&(	,.@A3WY5GH    c                     ddl m} | j                  || j                         | 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
        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   sS     	4j$//2&&(	,.@A2GI4FGr!   c                     ddl m} | j                  || j                         | 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
        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>   sV     	;')?)?@&&(	,.@A3WY5GHr!   c                 ~    | j                  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_getChildL   s3    
 ~~c5%0T]]7FH=tDr!   r1   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   r1   r3   r4   r5   requesttemplateexpecteds           r   _pageRenderingTestz!ErrorPageTests._pageRenderingTestT   s     u% 	 tUE6::W-xw/GH--t4##_'B&CD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+   zbrief description textzmuch longer text might go hereN)r   r>   )r   r3   r4   r5   r1   s        r   test_errorPageRenderingz&ErrorPageTests.test_errorPageRenderingj   s9     (1~~dE62dE6:r!   c                 Z    d}| j                  |      }| j                  |t        d|       y)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Lr!   c                 Z    d}| j                  |      }| j                  |t        d|       y)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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      so    
 IJ)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__rP   r;   r   rP   r;   s      r   rS   zDynamicChild.__init__   s    $	r!   )rG   rH   rI   rJ   bytesr   rS   rM   r!   r   rO   rO      s!    U X $ r!   rO   c                   $    e Zd ZdZdededefdZy)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      s!    +U +X +, +r!   rW   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	   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      s-    " "4 "& U r!   rZ   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 rR   rM   r_   s     r   r`   z!ImplicitAllowedMethods.render_GET       r!   c                      y rR   rM   r_   s     r   
render_PUTz!ImplicitAllowedMethods.render_PUT   rd   r!   )rG   rH   rI   rJ   r0   r`   rf   rM   r!   r   rb   rb      s-    
& T & T r!   rb   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.
        rj   N)	r   rW   rm   assertIsInstancerO   r   rP   r.   r;   )r   rP   r;   ro   rp   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	TypeErrorrl   rn   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   methodrZ   r   r7   )r   r=   r;   ro   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.
        )   GETr{      PUT	   FICTIONALN)	r	   allowedMethodsr   r|   rw   r   r7   r   setr   r=   ro   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   r{   r   N)	rb   r   r|   rw   r   r7   r   r   r   r   s        r   test_implicitAllowedMethodsz)ResourceTests.test_implicitAllowedMethods  sV     -)+r"% 18??GL3s'9'9#:;r!   rF   )
rG   rH   rI   rJ   rr   ru   ry   r}   r   r   rM   r!   r   rh   rh      s'    
5	G	=A<r!   rh   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;   ro   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}.
        rj   rk   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}.
        rj   rk   TN)	r   r	   r   rl   r.   r   r   prepathpostpath)r   r;   rootrp   s       r   test_postPathToPrePathz.GetChildForRequestTests.test_postPathToPrePath-  s|    
 /0z
fe$U$6tW$EF6(3))F84r!   rF   )rG   rH   rI   rJ   r   r   r   rM   r!   r   r   r     s    /	/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mOX mO`8 +h +h *
X 
R<H R<j&5h &5r!   