o
    >h(                     @   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.
        {   onozono!categoryztwisted.web.pages.errorPagemessageN)	ErrorPageflushWarningsassertEqualDeprecationWarningassertInself_warning r   ~/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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encoderesponseCoderesponseHeadersr   )r   r,   r.   r/   r0   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   r9   )r   r.   r/   r0   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!   r9   r   r   r0   r,   r   r   r   test_noResourceRenderingj      z'ErrorPageTests.test_noResourceRenderingc                 C   r;   )zG
        L{ForbiddenResource} sets the HTTP I{FORBIDDEN} code.
        zlonger messagezForbidden ResourceN)r$   r9   r   r<   r   r   r   test_forbiddenResourceRenderingr   r>   z.ErrorPageTests.test_forbiddenResourceRenderingr   N)__name__
__module____qualname____doc__r   r"   r%   r-   r   intstrr9   r:   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}.
    pathr6   r   Nc                 C   s   t |  || _|| _d S N)r   __init__rH   r6   r   rH   r6   r   r   r   rJ      s   

zDynamicChild.__init__)rA   rB   rC   rD   bytesr   rJ   r   r   r   r   rG   {       rG   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.
    rH   r6   r   c                 C   s
   t ||S rI   )rG   rK   r   r   r   r*      s   
zDynamicChildren.getChildN)rA   rB   rC   rD   rL   r   rG   r*   r   r   r   r   rN      rM   rN   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   rJ   	_response)r   rP   r   r   r   rJ      s   

z BytesReturnedRenderable.__init__r6   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.
        )rQ   r   r6   r   r   r   
render_GET   s   z"BytesReturnedRenderable.render_GET)rA   rB   rC   rD   rL   rJ   r+   rS   r   r   r   r   rO      s    rO   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.
    r6   r   Nc                 C      d S rI   r   rR   r   r   r   rS         z!ImplicitAllowedMethods.render_GETc                 C   rU   rI   r   rR   r   r   r   
render_PUT   rV   z!ImplicitAllowedMethods.render_PUT)rA   rB   rC   rD   r+   rS   rW   r   r   r   r   rT      s    rT   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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.
        rY   N)	r   rN   r\   assertIsInstancerG   r   rH   r)   r6   )r   rH   r6   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	TypeErrorr[   r]   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   methodrO   r   r2   )r   r8   r6   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.
        )   GETrg      PUT	   FICTIONALN)	r   allowedMethodsr   rh   rd   r   r2   r   setr   r8   r^   r6   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}.
        >   rj   rk   rg   rl   N)	rT   r   rh   rd   r   r2   r   rn   rm   ro   r   r   r   test_implicitAllowedMethods   s   z)ResourceTests.test_implicitAllowedMethodsr@   )
rA   rB   rC   rD   ra   rc   rf   ri   rq   rr   r   r   r   r   rX      s    




rX   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   r6   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}.
        rY   rZ   TN)r   r   isLeafr   r)   rt   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}.
        rY   rZ   TN)	r   r   rw   r[   r)   r   r   prepathpostpath)r   r6   rootr_   r   r   r   test_postPathToPrePath   s   z.GetChildForRequestTests.test_postPathToPrePathr@   )rA   rB   rC   rD   rv   rx   r|   r   r   r   r   rs     s
    


rs   N)rD   twisted.trial.unittestr   twisted.web.errorr   twisted.web.http_headersr   twisted.web.iwebr   twisted.web.resourcer   r   r   r	   r   r
   r$   r   r!   r   twisted.web.test.requesthelperr   r   rG   rN   rO   rT   rX   rs   r   r   r   r   <module>   s   $	c	U