U
    Â“«j(  ã                   @   sØ   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	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d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                   @   sˆ   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zee	e
e
ddœdd„Zddœdd„Zddœdd„Zddœdd„ZdS )ÚErrorPageTestszb
    Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and
    L{_UnsafeForbiddenResource}.
    N©Úreturnc                 C   s:   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.
        é{   Zonozono!Úcategoryztwisted.web.pages.errorPageÚmessageN)Ú	ErrorPageÚflushWarningsÚassertEqualÚDeprecationWarningÚassertIn©ÚselfÚ_Úwarning© r   úB/tmp/pip-unpacked-wheel-59kc99xd/twisted/web/test/test_resource.pyÚtest_deprecatedErrorPage   s    
z'ErrorPageTests.test_deprecatedErrorPagec                 C   s4   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   r   r   r   Útest_deprecatedNoResource)   s    
z(ErrorPageTests.test_deprecatedNoResourcec                 C   s4   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   r   r   r   Ú test_deprecatedForbiddenResource4   s    
z/ErrorPageTests.test_deprecatedForbiddenResourcec                 C   s&   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   Úpager   r   r   Útest_getChild?   s    zErrorPageTests.test_getChild)r*   ÚcodeÚbriefÚdetailr   c                 C   s`   t dgƒ}d}|||||f }|  | |¡| d¡¡ |  |j|¡ |  |jtddgiƒ¡ d S )Nó    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ÚencodeZresponseCodeZresponseHeadersr   )r   r*   r,   r-   r.   ÚrequestÚtemplateÚexpectedr   r   r   Ú_pageRenderingTestG   s    
ÿ
þz!ErrorPageTests._pageRenderingTestc                 C   s,   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   r5   )r   r,   r-   r.   r*   r   r   r   Útest_errorPageRendering]   s
    z&ErrorPageTests.test_errorPageRenderingc                 C   s    d}t |ƒ}|  |td|¡ dS )z@
        L{NoResource} sets the HTTP I{NOT FOUND} code.
        zlong messagezNo Such ResourceN)r    r5   r   ©r   r.   r*   r   r   r   Útest_noResourceRenderingj   s    z'ErrorPageTests.test_noResourceRenderingc                 C   s    d}t |ƒ}|  |td|¡ dS )zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)r"   r5   r   r7   r   r   r   Útest_forbiddenResourceRenderingr   s    z.ErrorPageTests.test_forbiddenResourceRendering)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r#   r+   r   ÚintÚstrr5   r6   r8   r9   r   r   r   r   r      s   	   þr   c                   @   s"   e Zd ZdZeeddœdd„ZdS )ÚDynamicChildzG
    A L{Resource} to be created on the fly by L{DynamicChildren}.
    N©Úpathr2   r   c                 C   s   t  | ¡ || _|| _d S ©N)r   Ú__init__rB   r2   ©r   rB   r2   r   r   r   rD   €   s    
zDynamicChild.__init__)r:   r;   r<   r=   Úbytesr   rD   r   r   r   r   r@   {   s   r@   c                   @   s"   e Zd ZdZeeedœdd„ZdS )ÚDynamicChildrenz.
    A L{Resource} with dynamic children.
    rA   c                 C   s
   t ||ƒS rC   )r@   rE   r   r   r   r(   ‹   s    zDynamicChildren.getChildN)r:   r;   r<   r=   rF   r   r@   r(   r   r   r   r   rG   †   s   rG   c                   @   s0   e Zd ZdZeddœdd„Zeedœdd„ZdS )	ÚBytesReturnedRenderablezG
    A L{Resource} with minimal capabilities to render a response.
    N)Úresponser   c                 C   s   t  | ¡ || _dS )zo
        @param response: A C{bytes} object giving the value to return from
            C{render_GET}.
        N)r   rD   Ú	_response)r   rI   r   r   r   rD   ”   s    
z BytesReturnedRenderable.__init__©r2   r   c                 C   s   | j S )z}
        Render a response to a I{GET} request by returning a short byte string
        to be written by the server.
        )rJ   ©r   r2   r   r   r   Ú
render_GETœ   s    z"BytesReturnedRenderable.render_GET)r:   r;   r<   r=   rF   rD   r)   rM   r   r   r   r   rH   �   s   rH   c                   @   s0   e Zd ZdZeddœdd„Zeddœdd„ZdS )ÚImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    NrK   c                 C   s   d S rC   r   rL   r   r   r   rM   ª   s    z!ImplicitAllowedMethods.render_GETc                 C   s   d S rC   r   rL   r   r   r   Ú
render_PUT­   s    z!ImplicitAllowedMethods.render_PUT)r:   r;   r<   r=   r)   rM   rO   r   r   r   r   rN   ¤   s   rN   c                   @   sd   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	dS )ÚResourceTestsz 
    Tests for L{Resource}.
    Nr   c                 C   sF   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ÚchildZsiblingr   r   r   Útest_staticChildren¶   s     ÿz!ResourceTests.test_staticChildrenc                 C   sJ   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.
        rQ   N)	r   rG   rT   ZassertIsInstancer@   r   rB   r'   r2   )r   rB   r2   rV   rW   r   r   r   Útest_dynamicChildrenÅ   s    z"ResourceTests.test_dynamicChildrenc                 C   s:   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Ú	TypeErrorrS   rU   r   r   r   Útest_staticChildPathTypeÒ   s
    z&ResourceTests.test_staticChildPathTypec                 C   s0   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   ÚmethodrH   r   r0   )r   r4   r2   rV   r   r   r   Útest_defaultHEADÝ   s
    zResourceTests.test_defaultHEADc                 C   sN   dddg}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^   rZ   r   r0   r   Úset©r   r4   rV   r2   Úexcr   r   r   Útest_explicitAllowedMethodsè   s    
z)ResourceTests.test_explicitAllowedMethodsc                 C   sD   dddh}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]   ra   rb   N)	rN   r   r^   rZ   r   r0   r   rd   rc   re   r   r   r   Útest_implicitAllowedMethodsö   s    
z)ResourceTests.test_implicitAllowedMethods)
r:   r;   r<   r=   rX   rY   r\   r_   rg   rh   r   r   r   r   rP   ±   s   rP   c                   @   s:   e Zd ZdZddœdd„Zddœdd„Zddœdd	„ZdS )
ÚGetChildForRequestTestsz*
    Tests for L{getChildForRequest}.
    Nr   c                 C   s(   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   r2   rV   Úresultr   r   r   Útest_exhaustedPostPath  s    
z.GetChildForRequestTests.test_exhaustedPostPathc                 C   s2   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}.
        rQ   rR   TN)r   r   ÚisLeafr   r'   rj   r   r   r   Útest_leafResource  s
    
z)GetChildForRequestTests.test_leafResourcec                 C   s`   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}.
        rQ   rR   TN)	r   r   rm   rS   r'   r   r   ZprepathZpostpath)r   r2   ÚrootrW   r   r   r   Útest_postPathToPrePath   s    z.GetChildForRequestTests.test_postPathToPrePath)r:   r;   r<   r=   rl   rn   rp   r   r   r   r   ri     s   
ri   N)r=   Ztwisted.trial.unittestr   Ztwisted.web.errorr   Ztwisted.web.http_headersr   Ztwisted.web.iwebr   Ztwisted.web.resourcer   r   r   r	   r   r
   r"   r   r    r   Ztwisted.web.test.requesthelperr   r   r@   rG   rH   rN   rP   ri   r   r   r   r   Ú<module>   s   $	c	U