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	`