o
    ¼>hŸ  ã                   @   sˆ   d Z ddl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 ddlmZ dd	lmZ G d
d„ deƒZG dd„ deƒZdS )z"
Tests for L{twisted.web.script}.
é    N)Údefer)ÚFilePath)ÚTestCase)Ú	NOT_FOUND)ÚPythonScriptÚResourceScriptDirectory)Ú_render)ÚDummyRequestc                   @   sL   e Zd ZdZdejd fdd„Zdejd fdd„Zdejd fdd	„ZdS )
ÚResourceScriptDirectoryTestsz/
    Tests for L{ResourceScriptDirectory}.
    ÚreturnNc                    sB   t ˆ ¡ ƒ}tdgƒ‰ t|ˆ ƒ}dtddf‡ ‡fdd„}| |¡S )zh
        L{ResourceScriptDirectory.render} sets the HTTP response code to I{NOT
        FOUND}.
        ó    Úignoredr   Nc                    ó   ˆ  ˆ jt¡ d S ©N©ÚassertEqualÚresponseCoder   ©r   ©ÚrequestÚself© ú|/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/web/test/test_script.pyÚ
cbRendered!   ó   zDResourceScriptDirectoryTests.test_renderNotFound.<locals>.cbRendered)r   Úmktempr	   r   ÚobjectÚaddCallback©r   ÚresourceÚdr   r   r   r   Útest_renderNotFound   s
   


z0ResourceScriptDirectoryTests.test_renderNotFoundc                    s\   ˆ  ¡ }t |¡ t|ƒ}tdgƒ‰ | dˆ ¡}t|ˆ ƒ}dtddf‡ ‡fdd„}| |¡S )a  
        L{ResourceScriptDirectory.getChild} returns a resource which renders an
        response with the HTTP I{NOT FOUND} status code if the indicated child
        does not exist as an entry in the directory used to initialized the
        L{ResourceScriptDirectory}.
        s   fooÚfoor   r   Nc                    r   r   r   r   r   r   r   r   4   r   zCResourceScriptDirectoryTests.test_notFoundChild.<locals>.cbRendered)	r   ÚosÚmakedirsr   r	   ÚgetChildr   r   r   )r   Úpathr   Úchildr    r   r   r   r   Útest_notFoundChild&   s   



z/ResourceScriptDirectoryTests.test_notFoundChildc                    sr   t ˆ ¡ ƒ}| ¡  | d¡ d¡ t| ¡ ƒ}tdgƒ‰ | dˆ ¡}t	|ˆ ƒ}dt
ddf‡ ‡fdd	„}| |¡S )
ú¿
        L{ResourceScriptDirectory.getChild} returns a resource which renders a
        response with the HTTP 200 status code and the content of the rpy's
        C{request} global.
        ztest.rpys¬   
from twisted.web.resource import Resource
class TestResource(Resource):
    isLeaf = True
    def render_GET(self, request):
        return b'ok'
resource = TestResource()r   s   test.rpyr   r   Nc                    s   ˆ  d ˆ j¡d¡ d S )Nr   s   ok)r   ÚjoinÚwrittenr   r   r   r   r   O   ó   z<ResourceScriptDirectoryTests.test_render.<locals>.cbRendered)r   r   r$   r'   Ú
setContentr   Ú_asBytesPathr	   r%   r   r   r   )r   Útmpr   r'   r    r   r   r   r   Útest_render9   s   
ÿ	


z(ResourceScriptDirectoryTests.test_render)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚDeferredr!   r(   r0   r   r   r   r   r
      s
    r
   c                   @   s8   e Zd ZdZdejd fdd„Zdejd fdd„ZdS )ÚPythonScriptTestsz$
    Tests for L{PythonScript}.
    r   Nc                    sD   t ˆ ¡ dƒ}tdgƒ‰ t|ˆ ƒ}dtddf‡ ‡fdd„}| |¡S )z¥
        If the source file a L{PythonScript} is initialized with doesn't exist,
        L{PythonScript.render} sets the HTTP response code to I{NOT FOUND}.
        Nr   r   r   c                    r   r   r   r   r   r   r   r   c   r   z9PythonScriptTests.test_notFoundRender.<locals>.cbRendered)r   r   r	   r   r   r   r   r   r   r   Útest_notFoundRenderZ   s
   


z%PythonScriptTests.test_notFoundRenderc                    sl   t ˆ ¡ ƒ}| ¡  | d¡}| d¡ t| ¡ dƒ}tdgƒ‰ t|ˆ ƒ}dt	ddf‡ ‡fdd„}| 
|¡S )	r)   ztest.epys   raise Exception("nooo")Nr   r   r   c                    s   ˆ  dd ˆ j¡¡ d S )Ns   nooor   )ÚassertInr*   r+   r   r   r   r   r   v   r,   z:PythonScriptTests.test_renderException.<locals>.cbRendered)r   r   r$   r'   r-   r   r.   r	   r   r   r   )r   r/   r'   r   r    r   r   r   r   Útest_renderExceptionh   s   




z&PythonScriptTests.test_renderException)r1   r2   r3   r4   r   r5   r7   r9   r   r   r   r   r6   U   s    r6   )r4   r#   Útwisted.internetr   Útwisted.python.filepathr   Útwisted.trial.unittestr   Útwisted.web.httpr   Útwisted.web.scriptr   r   Útwisted.web.test._utilr   Útwisted.web.test.requesthelperr	   r
   r6   r   r   r   r   Ú<module>   s   B