U
    luf{"                     @   s<  d dl mZmZmZmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZ d dlZG dd de	ZG d	d
 d
eZi 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d ZedZed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 )!    )HTTPHeadersHTTPMessageDelegateHTTPServerConnectionDelegateResponseStartLine)HostMatchesPathMatchesReversibleRouterRouterRule
RuleRouter)AsyncHTTPTestCase)Application	HTTPErrorRequestHandler)WSGIContainerNc                   @   s   e Zd Zdd ZdS )BasicRouterc                 K   s   G dd dt }||jS )Nc                   @   s   e Zd Zdd Zdd ZdS )z1BasicRouter.find_handler.<locals>.MessageDelegatec                 S   s
   || _ d S N
connectionselfr    r   =/tmp/pip-unpacked-wheel-bmg6zs32/tornado/test/routing_test.py__init__%   s    z:BasicRouter.find_handler.<locals>.MessageDelegate.__init__c                 S   s.   | j tdddtddid | j   d S NHTTP/1.1   OKContent-Length2   OK)r   write_headersr   r   finishr   r   r   r   r"   (   s    

z8BasicRouter.find_handler.<locals>.MessageDelegate.finishN__name__
__module____qualname__r   r"   r   r   r   r   MessageDelegate$   s   r(   )r   r   )r   requestkwargsr(   r   r   r   find_handler#   s    zBasicRouter.find_handlerN)r%   r&   r'   r+   r   r   r   r   r   "   s   r   c                   @   s   e Zd Zdd Zdd ZdS )BasicRouterTestCasec                 C   s   t  S r   )r   r#   r   r   r   get_app4   s    zBasicRouterTestCase.get_appc                 C   s   |  d}| |jd d S )Nz/any_requestr    fetchassertEqualbodyr   responser   r   r   test_basic_router7   s    
z%BasicRouterTestCase.test_basic_routerN)r%   r&   r'   r-   r4   r   r   r   r   r,   3   s   r,   c                   @   s   e Zd Zdd ZdS )GetResourcec                 C   s"   |t krtd| t |  d S )N  )	resourcesr   r"   r   pathr   r   r   get@   s    zGetResource.getNr%   r&   r'   r:   r   r   r   r   r5   ?   s   r5   c                   @   s   e Zd Zdd ZdS )PostResourcec                 C   s   | j jt|< d S r   )r)   r1   r7   r8   r   r   r   postH   s    zPostResource.postN)r%   r&   r'   r=   r   r   r   r   r<   G   s   r<   c                   @   s   e Zd Zdd Zdd ZdS )HTTPMethodRouterc                 C   s
   || _ d S r   )app)r   r?   r   r   r   r   M   s    zHTTPMethodRouter.__init__c                 K   s(   |j dkrtnt}| jj|||jgdS )NGET)Z	path_args)methodr5   r<   r?   get_handler_delegater9   )r   r)   r*   handlerr   r   r   r+   P   s    zHTTPMethodRouter.find_handlerN)r%   r&   r'   r   r+   r   r   r   r   r>   L   s   r>   c                   @   s   e Zd Zdd Zdd ZdS )HTTPMethodRouterTestCasec                 C   s
   t t S r   )r>   r   r#   r   r   r   r-   V   s    z HTTPMethodRouterTestCase.get_appc                 C   s`   | j dddd}| |jd |  d}| |jd |  d}| |jd | |jd d S )	Nz/post_resourcePOSTdata)rA   r1   r   z/get_resourcer6   s   data)r/   r0   coder1   r2   r   r   r   test_http_method_routerY   s    

z0HTTPMethodRouterTestCase.test_http_method_routerN)r%   r&   r'   r-   rH   r   r   r   r   rD   U   s   rD   c                    s   G  fdddt }|S )Nc                       s   e Zd Z fddZdS )z#_get_named_handler.<locals>.Handlerc                    sD   | j jdd k	r(| | j jd d  |  d |    d S )Napp_namez: )Zapplicationsettingsr:   writer"   reverse_urlr   argsr*   handler_namer   r   r:   g   s    z'_get_named_handler.<locals>.Handler.getNr;   r   rO   r   r   Handlerf   s   rQ   )r   )rP   rQ   r   rO   r   _get_named_handlere   s    rR   first_handlersecond_handlerc                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	CustomRouterc                    s   t    i | _d S r   )superr   routesr#   	__class__r   r   r   u   s    
zCustomRouter.__init__c                 C   s   | j | d S r   )rW   update)r   rW   r   r   r   
add_routesy   s    zCustomRouter.add_routesc                 K   s,   |j | jkr(| j|j  \}}|||S d S r   )r9   rW   rB   )r   r)   r*   r?   rC   r   r   r   r+   |   s    zCustomRouter.find_handlerc                 G   s   d| }|| j kr|S d S )N/)rW   )r   namerN   Zhandler_pathr   r   r   rL      s    zCustomRouter.reverse_url)r%   r&   r'   r   r[   r+   rL   __classcell__r   r   rX   r   rU   t   s   rU   c                   @   s   e Zd Zdd Zdd ZdS )CustomRouterTestCasec                    sP   t   G  fdddt}|dd}|dd} |tf|tf|tfd  S )Nc                       s   e Zd Z fddZdS )z7CustomRouterTestCase.get_app.<locals>.CustomApplicationc                    s    j |f| S r   )rL   )r   r]   rN   routerr   r   rL      s    zCCustomRouterTestCase.get_app.<locals>.CustomApplication.reverse_urlN)r%   r&   r'   rL   r   r`   r   r   CustomApplication   s   rb   app1)rI   app2)/first_handler/second_handler/first_handler_second_app)rU   r   r[   FirstHandlerSecondHandler)r   rb   rc   rd   r   r`   r   r-      s    

zCustomRouterTestCase.get_appc                 C   sL   |  d}| |jd |  d}| |jd |  d}| |jd d S )Nre   s#   app1: first_handler: /first_handlerrf   s%   app2: second_handler: /second_handlerrg   s#   app2: first_handler: /first_handlerr.   r2   r   r   r   test_custom_router   s    


z'CustomRouterTestCase.test_custom_routerN)r%   r&   r'   r-   rj   r   r   r   r   r_      s   r_   c                   @   s   e Zd Zdd ZdS )ConnectionDelegatec                 C   s   G dd dt }||S )Nc                   @   s   e Zd Zdd Zdd ZdS )z9ConnectionDelegate.start_request.<locals>.MessageDelegatec                 S   s
   || _ d S r   r   r   r   r   r   r      s    zBConnectionDelegate.start_request.<locals>.MessageDelegate.__init__c                 S   sD   d}| j tdddtdtt|i | j | | j   d S )Nr    r   r   r   r   )r   r!   r   r   strlenrK   r"   )r   Zresponse_bodyr   r   r   r"      s    
z@ConnectionDelegate.start_request.<locals>.MessageDelegate.finishNr$   r   r   r   r   r(      s   r(   )r   )r   Zserver_connZrequest_connr(   r   r   r   start_request   s    z ConnectionDelegate.start_requestN)r%   r&   r'   rn   r   r   r   r   rk      s   rk   c                   @   s   e Zd Zdd Zdd ZdS )RuleRouterTestc              
   C   s   t  }dd }t }|d|tdfi |dtdtddi d	fgfttd
|ttdtddttd|dt	 fg |S )Nc                 S   s8   | j tdddtddi | j d | j   d S r   )r   r!   r   r   rK   r"   )r)   r   r   r   request_callable   s    

z0RuleRouterTest.get_app.<locals>.request_callable/nested_handlerZnested_handlerz.*www.example.comre   z'tornado.test.routing_test.SecondHandlerrT   z
/.*handlerrS   )r]   /request_callable/connection_delegate)
r   rU   r[   rR   add_handlersr   r   r
   rh   rk   )r   r?   rp   ra   r   r   r   r-      s.    zRuleRouterTest.get_appc                 C   s   |  d}| |jd | j dddid}| |jd |  d}| |jd |  d	}| |jd
 |  d}| |jd |  d}| |jd |  d}| |jd
 d S )Nre   s   first_handler: /first_handlerHostrr   )headerss   second_handler: /first_handlerrq   s   nested_handler: /nested_handlerz/nested_not_found_handlerr6   rt   r    rs   /404)r/   r0   r1   rG   r2   r   r   r   test_rule_based_router   s    





z%RuleRouterTest.test_rule_based_routerN)r%   r&   r'   r-   ry   r   r   r   r   ro      s   'ro   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
WSGIContainerTestCasec                 C   sD   t | j}G dd dt}ttdtd|i dfgftd|fgS )Nc                   @   s   e Zd Zdd ZdS )z.WSGIContainerTestCase.get_app.<locals>.Handlerc                 _   s   |  | d d S )Ntornado)r"   rL   rM   r   r   r   r:      s    z2WSGIContainerTestCase.get_app.<locals>.Handler.getNr;   r   r   r   r   rQ      s   rQ   z
/tornado.*/tornado/testr{   /wsgi)r   wsgi_appr   r   r   r   )r   r~   rQ   r   r   r   r-      s    

zWSGIContainerTestCase.get_appc                 C   s   |dg  dgS )Nz200 OK   WSGIr   )r   environZstart_responser   r   r   r~     s    
zWSGIContainerTestCase.wsgi_appc                 C   s4   |  d}| |jd |  d}| |jd d S )Nr|   s   /tornado/testr}   r   r.   r2   r   r   r   test_wsgi_container  s    

z)WSGIContainerTestCase.test_wsgi_containerc                 C   s   |  d}| |jd d S )Nrx   r6   )r/   r0   rG   r2   r   r   r   test_delegate_not_found  s    
z-WSGIContainerTestCase.test_delegate_not_foundN)r%   r&   r'   r-   r~   r   r   r   r   r   r   rz      s   rz   )$Ztornado.httputilr   r   r   r   Ztornado.routingr   r   r   r	   r
   r   Ztornado.testingr   Ztornado.webr   r   r   Ztornado.wsgir   typingr   r,   r7   r5   r<   r>   rD   rR   rh   ri   rU   r_   rk   ro   rz   r   r   r   r   <module>   s(    		?