U
    
W[   ã                   @   sÌ   d 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 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)Ú	NOT_FOUNDÚ	FORBIDDENÚResourceÚ	ErrorPageÚ
NoResourceÚForbiddenResourceÚgetChildForRequest)ÚHeaders)ÚDummyRequestc                   @   sD   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
d„ ZdS )ÚErrorPageTestszJ
    Tests for L{ErrorPage}, L{NoResource}, and L{ForbiddenResource}.
    c                 C   s(   |   ddd¡}|  | dtƒ ¡|¡ dS )zj
        The C{getChild} method of L{ErrorPage} returns the L{ErrorPage} it is
        called on.
        éA  ZfooZbars   nameN)Ú	errorPageÚassertIdenticalÚgetChildÚobject)ÚselfÚpage© r   ú@/usr/lib/python3/dist-packages/twisted/web/test/test_resource.pyÚtest_getChild   s    zErrorPageTests.test_getChildc                 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   ÚassertEqualÚrenderÚencodeZresponseCodeZresponseHeadersr   )r   r   ÚcodeÚbriefÚdetailÚrequestÚtemplateÚexpectedr   r   r   Ú_pageRenderingTest$   s    
ÿ	 ÿþz!ErrorPageTests._pageRenderingTestc                 C   s.   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   r   r   r   r   r   r   r   Útest_errorPageRendering8   s
    z&ErrorPageTests.test_errorPageRenderingc                 C   s"   d}|   |¡}|  |td|¡ dS )z@
        L{NoResource} sets the HTTP I{NOT FOUND} code.
        zlong messagezNo Such ResourceN)Ú
noResourcer"   r   ©r   r   r   r   r   r   Útest_noResourceRenderingF   s    
z'ErrorPageTests.test_noResourceRenderingc                 C   s"   d}|   |¡}|  |td|¡ dS )zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)ÚforbiddenResourcer"   r   r%   r   r   r   Útest_forbiddenResourceRenderingO   s    
z.ErrorPageTests.test_forbiddenResourceRenderingN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r$   r	   r'   r   r"   r#   r&   r(   r   r   r   r   r      s   		r   c                   @   s   e Zd ZdZdd„ ZdS )ÚDynamicChildzG
    A L{Resource} to be created on the fly by L{DynamicChildren}.
    c                 C   s   t  | ¡ || _|| _d S ©N)r   Ú__init__Úpathr   ©r   r0   r   r   r   r   r/   ]   s    
zDynamicChild.__init__N)r)   r*   r+   r,   r/   r   r   r   r   r-   Y   s   r-   c                   @   s   e Zd ZdZdd„ ZdS )ÚDynamicChildrenz.
    A L{Resource} with dynamic children.
    c                 C   s
   t ||ƒS r.   )r-   r1   r   r   r   r   h   s    zDynamicChildren.getChildN)r)   r*   r+   r,   r   r   r   r   r   r2   d   s   r2   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚBytesReturnedRenderablezG
    A L{Resource} with minimal capabilities to render a response.
    c                 C   s   t  | ¡ || _dS )zo
        @param response: A C{bytes} object giving the value to return from
            C{render_GET}.
        N)r   r/   Ú	_response)r   Zresponser   r   r   r/   q   s    
z BytesReturnedRenderable.__init__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.
        )r4   ©r   r   r   r   r   Ú
render_GETz   s    z"BytesReturnedRenderable.render_GETN)r)   r*   r+   r,   r/   r6   r   r   r   r   r3   m   s   	r3   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    c                 C   s   d S r.   r   r5   r   r   r   r6   ˆ   s    z!ImplicitAllowedMethods.render_GETc                 C   s   d S r.   r   r5   r   r   r   Ú
render_PUTŒ   s    z!ImplicitAllowedMethods.render_PUTN)r)   r*   r+   r,   r6   r8   r   r   r   r   r7   ƒ   s   r7   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚResourceTestsz 
    Tests for L{Resource}.
    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.
        r:   N)	r   r2   r=   ZassertIsInstancer-   r   r0   r   r   )r   r0   r   r>   r?   r   r   r   Útest_dynamicChildren¤   s    z"ResourceTests.test_dynamicChildrenc                 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   Úmethodr3   r   r   )r   r!   r   r>   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.
        ó   GETrB   ó   PUTó	   FICTIONALN)	r   ÚallowedMethodsr   rC   ÚassertRaisesr   r   r   Úset©r   r!   r>   r   Úexcr   r   r   Útest_explicitAllowedMethods¾   s    
z)ResourceTests.test_explicitAllowedMethodsc                 C   sH   t dddgƒ}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}.
        rE   rB   rF   rG   N)	rJ   r7   r   rC   rI   r   r   r   rH   rK   r   r   r   Útest_implicitAllowedMethodsÍ   s    z)ResourceTests.test_implicitAllowedMethodsN)	r)   r*   r+   r,   r@   rA   rD   rM   rN   r   r   r   r   r9   ‘   s   r9   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚGetChildForRequestTestsz*
    Tests for L{getChildForRequest}.
    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   r   r>   Ú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}.
        r:   r;   TN)r   r   ÚisLeafr
   r   rP   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}.
        r:   r;   TN)	r   r   rS   r<   r   r
   r   ZprepathZpostpath)r   r   Úrootr?   r   r   r   Útest_postPathToPrePathú   s    z.GetChildForRequestTests.test_postPathToPrePathN)r)   r*   r+   r,   rR   rT   rV   r   r   r   r   rO   ß   s   rO   N)r,   Ztwisted.trial.unittestr   Ztwisted.web.errorr   Ztwisted.web.resourcer   r   r   r   r   r	   r
   Ztwisted.web.http_headersr   Ztwisted.web.test.requesthelperr   r   r-   r2   r3   r7   r9   rO   r   r   r   r   Ú<module>   s   $G	N