o
    ¯b°#  ã                   @   sÌ   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„ 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)Ú	FORBIDDENÚ	NOT_FOUNDÚ	ErrorPageÚForbiddenResourceÚ
NoResourceÚResourceÚgetChildForRequest)Ú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  ÚfooÚ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ÚencodeÚresponseCodeÚ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_errorPageRendering<   s
   z&ErrorPageTests.test_errorPageRenderingc                 C   ó"   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_noResourceRenderingI   ó   
z'ErrorPageTests.test_noResourceRenderingc                 C   r(   )zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)ÚforbiddenResourcer&   r   r*   r   r   r   Útest_forbiddenResourceRenderingQ   r,   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                   @   ó   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   r7   r#   r   r   r   r6   _   s   

zDynamicChild.__init__N)r/   r0   r1   r2   r6   r   r   r   r   r4   Z   ó    r4   c                   @   r3   )ÚDynamicChildrenz.
    A L{Resource} with dynamic children.
    c                 C   s
   t ||ƒS r5   )r4   r8   r   r   r   r   j   s   
zDynamicChildren.getChildN)r/   r0   r1   r2   r   r   r   r   r   r:   e   r9   r:   c                   @   ó    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
   r6   Ú	_response)r   Úresponser   r   r   r6   s   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.
        )r=   ©r   r#   r   r   r   Ú
render_GET{   s   z"BytesReturnedRenderable.render_GETN)r/   r0   r1   r2   r6   r@   r   r   r   r   r<   n   s    r<   c                   @   r;   )ÚImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    c                 C   ó   d S r5   r   r?   r   r   r   r@   ‰   ó   z!ImplicitAllowedMethods.render_GETc                 C   rB   r5   r   r?   r   r   r   Ú
render_PUTŒ   rC   z!ImplicitAllowedMethods.render_PUTN)r/   r0   r1   r2   r@   rD   r   r   r   r   rA   ƒ   s    rA   c                   @   s@   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}.
    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ÚchildÚ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.
        rF   N)	r   r:   rI   ÚassertIsInstancer4   r   r7   r   r#   )r   r7   r#   rJ   rK   r   r   r   Útest_dynamicChildren¤   s   z"ResourceTests.test_dynamicChildrenc                 C   sª   t ƒ }t ƒ }t ƒ }| d|¡ |  | jg¡}|  t|ƒd¡ |  d|d d ¡ |  | dt	g ƒ¡t
¡ | d|¡ |  | jg¡}|  t|ƒd¡ |  d|d d ¡ dS )zv
        Test that passing the wrong type to putChild results in a warning,
        and a failure in Python 3
        r   é   zPath segment must be bytesr   ÚmessagerF   N)r
   rH   ÚflushWarningsÚtest_staticChildPathTyper   ÚlenÚassertInrN   rI   r   r   )r   rJ   rK   rL   Úwarningsr   r   r   rS   ±   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   Úmethodr<   r   r   )r   r%   r#   rJ   r   r   r   Útest_defaultHEADÇ   s
   zResourceTests.test_defaultHEADc                 C   sL   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.
        )ó   GETrW   ó   PUTó	   FICTIONALN)	r
   ÚallowedMethodsr   rX   ÚassertRaisesr   r   r   Úset©r   r%   rJ   r#   Úexcr   r   r   Útest_explicitAllowedMethodsÒ   s   z)ResourceTests.test_explicitAllowedMethodsc                 C   sB   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}.
        >   rZ   r[   rW   r\   N)	rA   r   rX   r^   r   r   r   r_   r]   r`   r   r   r   Útest_implicitAllowedMethodsà   s   z)ResourceTests.test_implicitAllowedMethodsN)
r/   r0   r1   r2   rM   rO   rS   rY   rb   rc   r   r   r   r   rE   �   s    rE   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#   rJ   Ú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}.
        rF   rG   TN)r   r
   ÚisLeafr   r   re   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}.
        rF   rG   TN)	r   r
   rh   rH   r   r   r   ÚprepathÚpostpath)r   r#   ÚrootrK   r   r   r   Útest_postPathToPrePath
  s   z.GetChildForRequestTests.test_postPathToPrePathN)r/   r0   r1   r2   rg   ri   rm   r   r   r   r   rd   ð   s
    
rd   N)r2   Útwisted.trial.unittestr   Útwisted.web.errorr   Útwisted.web.http_headersr   Útwisted.web.resourcer   r   r   r   r	   r
   r   Útwisted.web.test.requesthelperr   r   r4   r:   r<   rA   rE   rd   r   r   r   r   Ú<module>   s   $	C	`