o
    Óó·i(  ã                   @   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                
   @   st   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zdede	de
de
ddf
dd„Zddd„Zddd„Zddd„ZdS )ÚErrorPageTestszb
    Tests for L{_UnafeErrorPage}, L{_UnsafeNoResource}, and
    L{_UnsafeForbiddenResource}.
    ÚreturnNc                 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   údC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/web/test/test_resource.pyÚtest_deprecatedErrorPage   s   
z'ErrorPageTests.test_deprecatedErrorPagec                 C   ó4   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)   ó   
z(ErrorPageTests.test_deprecatedNoResourcec                 C   r   )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   r"   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_getChildr+   ÚcodeÚbriefÚdetailc                 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   r6   )r   r-   r.   r/   r+   r   r   r   Útest_errorPageRendering]   s
   z&ErrorPageTests.test_errorPageRenderingc                 C   ó    d}t |ƒ}|  |td|¡ dS )z@
        L{NoResource} sets the HTTP I{NOT FOUND} code.
        zlong messagezNo Such ResourceN)r    r6   r   ©r   r/   r+   r   r   r   Útest_noResourceRenderingj   ó   z'ErrorPageTests.test_noResourceRenderingc                 C   r8   )zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)r#   r6   r   r9   r   r   r   Útest_forbiddenResourceRenderingr   r;   z.ErrorPageTests.test_forbiddenResourceRendering©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r$   r,   r   ÚintÚstrr6   r7   r:   r<   r   r   r   r   r      s(    



ÿÿÿÿ
þ

r   c                   @   s&   e Zd ZdZdededdfdd„ZdS )ÚDynamicChildzG
    A L{Resource} to be created on the fly by L{DynamicChildren}.
    Úpathr3   r   Nc                 C   s   t  | ¡ || _|| _d S ©N)r   Ú__init__rE   r3   ©r   rE   r3   r   r   r   rG   €   s   

zDynamicChild.__init__)r>   r?   r@   rA   Úbytesr   rG   r   r   r   r   rD   {   ó    rD   c                   @   s&   e Zd ZdZdededefdd„ZdS )ÚDynamicChildrenz.
    A L{Resource} with dynamic children.
    rE   r3   r   c                 C   s
   t ||ƒS rF   )rD   rH   r   r   r   r)   ‹   s   
zDynamicChildren.getChildN)r>   r?   r@   rA   rI   r   rD   r)   r   r   r   r   rK   †   rJ   rK   c                   @   s4   e Zd ZdZdeddfdd„Zdedefdd	„ZdS )
ÚBytesReturnedRenderablezG
    A L{Resource} with minimal capabilities to render a response.
    Úresponser   Nc                 C   s   t  | ¡ || _dS )zo
        @param response: A C{bytes} object giving the value to return from
            C{render_GET}.
        N)r   rG   Ú	_response)r   rM   r   r   r   rG   ”   s   

z BytesReturnedRenderable.__init__r3   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.
        N)rN   ©r   r3   r   r   r   Ú
render_GETœ   s   z"BytesReturnedRenderable.render_GET)r>   r?   r@   rA   rI   rG   r*   rP   r   r   r   r   rL   �   s    rL   c                   @   s4   e Zd ZdZdeddfdd„Zdeddfdd„ZdS )	ÚImplicitAllowedMethodszn
    A L{Resource} which implicitly defines its allowed methods by defining
    renderers to handle them.
    r3   r   Nc                 C   ó   d S rF   r   rO   r   r   r   rP   ª   ó   z!ImplicitAllowedMethods.render_GETc                 C   rR   rF   r   rO   r   r   r   Ú
render_PUT­   rS   z!ImplicitAllowedMethods.render_PUT)r>   r?   r@   rA   r*   rP   rT   r   r   r   r   rQ   ¤   s    rQ   c                   @   sL   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	dS )ÚResourceTestsz 
    Tests for L{Resource}.
    r   Nc                 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.
        rV   N)	r   rK   rY   ZassertIsInstancerD   r   rE   r(   r3   )r   rE   r3   r[   r\   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Ú	TypeErrorrX   rZ   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   ÚmethodrL   r   r1   )r   r5   r3   r[   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.
        )ó   GETrb   ó   PUTó	   FICTIONALN)	r   ÚallowedMethodsr   rc   r_   r   r1   r   Úset©r   r5   r[   r3   Ú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}.
        >   re   rb   rf   rg   N)	rQ   r   rc   r_   r   r1   r   ri   rh   rj   r   r   r   Útest_implicitAllowedMethodsö   s   z)ResourceTests.test_implicitAllowedMethodsr=   )
r>   r?   r@   rA   r]   r^   ra   rd   rl   rm   r   r   r   r   rU   ±   s    




rU   c                   @   s.   e Zd ZdZd
dd„Zd
dd„Zd
dd	„ZdS )ÚGetChildForRequestTestsz*
    Tests for L{getChildForRequest}.
    r   Nc                 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   r3   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}.
        rV   rW   TN)r   r   ÚisLeafr   r(   ro   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}.
        rV   rW   TN)	r   r   rr   rX   r(   r   r   ZprepathZpostpath)r   r3   Úrootr\   r   r   r   Útest_postPathToPrePath   s   z.GetChildForRequestTests.test_postPathToPrePathr=   )r>   r?   r@   rA   rq   rs   ru   r   r   r   r   rn     s
    


rn   N)rA   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   rD   rK   rL   rQ   rU   rn   r   r   r   r   Ú<module>   s   $	c	U