U
    
W[È  ã                   @   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	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)ÚTestCase)ÚFilePath)Ú	NOT_FOUND)ÚResourceScriptDirectoryÚPythonScript)Ú_render)ÚDummyRequestc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚResourceScriptDirectoryTestsz/
    Tests for L{ResourceScriptDirectory}.
    c                    s<   t ˆ ¡ ƒ}tdgƒ‰ t|ˆ ƒ}‡ ‡fdd„}| |¡ |S )zh
        L{ResourceScriptDirectory.render} sets the HTTP response code to I{NOT
        FOUND}.
        ó    c                    s   ˆ  ˆ jt¡ d S ©N©ÚassertEqualZresponseCoder   ©Zignored©ZrequestÚself© ú>/usr/lib/python3/dist-packages/twisted/web/test/test_script.pyÚ
cbRendered   s    zDResourceScriptDirectoryTests.test_renderNotFound.<locals>.cbRendered)r   Úmktempr   r   ÚaddCallback©r   ÚresourceÚdr   r   r   r   Útest_renderNotFound   s    


z0ResourceScriptDirectoryTests.test_renderNotFoundc                    sV   ˆ  ¡ }t |¡ t|ƒ}tdgƒ‰ | dˆ ¡}t|ˆ ƒ}‡ ‡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fooc                    s   ˆ  ˆ jt¡ d S r   r   r   r   r   r   r   1   s    zCResourceScriptDirectoryTests.test_notFoundChild.<locals>.cbRendered)r   ÚosÚmakedirsr   r   ÚgetChildr   r   )r   Úpathr   Úchildr   r   r   r   r   Útest_notFoundChild$   s    



z/ResourceScriptDirectoryTests.test_notFoundChildc                    sl   t ˆ ¡ ƒ}| ¡  | d¡ d¡ t| ¡ ƒ}tdgƒ‰ | dˆ ¡}t	|ˆ ƒ}‡ ‡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.rpyc                    s   ˆ  d ˆ j¡d¡ d S )Nr
   s   ok)r   ÚjoinÚwrittenr   r   r   r   r   J   s    z<ResourceScriptDirectoryTests.test_render.<locals>.cbRendered)r   r   r   r   Ú
setContentr   Ú_asBytesPathr   r   r   r   )r   Útmpr   r   r   r   r   r   r   Útest_render7   s    


z(ResourceScriptDirectoryTests.test_renderN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r&   r   r   r   r   r	      s   r	   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚPythonScriptTestsz$
    Tests for L{PythonScript}.
    c                    s>   t ˆ ¡ dƒ}tdgƒ‰ t|ˆ ƒ}‡ ‡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
   c                    s   ˆ  ˆ jt¡ d S r   r   r   r   r   r   r   ]   s    z9PythonScriptTests.test_notFoundRender.<locals>.cbRendered)r   r   r   r   r   r   r   r   r   Útest_notFoundRenderU   s    


z%PythonScriptTests.test_notFoundRenderc                    sf   t ˆ ¡ ƒ}| ¡  | d¡}| d¡ t| ¡ dƒ}tdgƒ‰ t|ˆ ƒ}‡ ‡fdd„}| 	|¡ |S )r    ztest.epys   raise Exception("nooo")Nr
   c                    s   ˆ  dd ˆ j¡¡ d S )Ns   nooor
   )ZassertInr!   r"   r   r   r   r   r   p   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   Útest_renderExceptionc   s    




z&PythonScriptTests.test_renderExceptionN)r'   r(   r)   r*   r,   r-   r   r   r   r   r+   Q   s   r+   )r*   r   Ztwisted.trial.unittestr   Ztwisted.python.filepathr   Ztwisted.web.httpr   Ztwisted.web.scriptr   r   Ztwisted.web.test._utilr   Ztwisted.web.test.requesthelperr   r	   r+   r   r   r   r   Ú<module>   s   ?