U
    c6{iŸ  ã                   @   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ejd dœdd„Zejd dœdd„Zejd dœdd	„ZdS )
ÚResourceScriptDirectoryTestsz/
    Tests for L{ResourceScriptDirectory}.
    N©Úreturnc                    s@   t ˆ ¡ ƒ}tdgƒ‰ t|ˆ ƒ}tddœ‡ ‡fdd„}| |¡S )zh
        L{ResourceScriptDirectory.render} sets the HTTP response code to I{NOT
        FOUND}.
        ó    N©Úignoredr   c                    s   ˆ  ˆ jt¡ d S ©N©ÚassertEqualZresponseCoder   ©r   ©ÚrequestÚself© ú@/tmp/pip-unpacked-wheel-hfbvu0bz/twisted/web/test/test_script.pyÚ
cbRendered!   s    zDResourceScriptDirectoryTests.test_renderNotFound.<locals>.cbRendered)r   Úmktempr	   r   ÚobjectÚaddCallback©r   ÚresourceÚdr   r   r   r   Útest_renderNotFound   s
    

z0ResourceScriptDirectoryTests.test_renderNotFoundc                    sZ   ˆ  ¡ }t |¡ t|ƒ}tdgƒ‰ | dˆ ¡}t|ˆ ƒ}tddœ‡ ‡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   fooZfooNr   c                    s   ˆ  ˆ jt¡ d S r   r   r   r   r   r   r   4   s    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                    sp   t ˆ ¡ ƒ}| ¡  | d¡ d¡ t| ¡ ƒ}tdgƒ‰ | dˆ ¡}t	|ˆ ƒ}t
ddœ‡ ‡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.rpyNr   c                    s   ˆ  d ˆ j¡d¡ d S )Nr   s   ok)r   ÚjoinÚwrittenr   r   r   r   r   O   s    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&   r-   r   r   r   r   r
      s   r
   c                   @   s8   e Zd ZdZejd dœdd„Zejd dœdd„ZdS )ÚPythonScriptTestsz$
    Tests for L{PythonScript}.
    Nr   c                    sB   t ˆ ¡ dƒ}tdgƒ‰ t|ˆ ƒ}tddœ‡ ‡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   c                    s   ˆ  ˆ jt¡ d S r   r   r   r   r   r   r   c   s    z9PythonScriptTests.test_notFoundRender.<locals>.cbRendered)r   r   r	   r   r   r   r   r   r   r   Útest_notFoundRenderZ   s
    

z%PythonScriptTests.test_notFoundRenderc                    sj   t ˆ ¡ ƒ}| ¡  | d¡}| d¡ t| ¡ dƒ}tdgƒ‰ t|ˆ ƒ}t	ddœ‡ ‡fdd„}| 
|¡S )r'   ztest.epys   raise Exception("nooo")Nr   r   c                    s   ˆ  dd ˆ j¡¡ d S )Ns   nooor   )ZassertInr(   r)   r   r   r   r   r   v   s    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)r.   r/   r0   r1   r   r2   r4   r5   r   r   r   r   r3   U   s   r3   )r1   r!   Ztwisted.internetr   Ztwisted.python.filepathr   Ztwisted.trial.unittestr   Ztwisted.web.httpr   Ztwisted.web.scriptr   r   Ztwisted.web.test._utilr   Ztwisted.web.test.requesthelperr	   r
   r3   r   r   r   r   Ú<module>   s   B