o
    )Zh                     @   s  d dl Z d dlZd dlmZ d dlZd dlZd dlZd dlZd dlm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZmZmZ d dlmZ d dlmZmZmZmZ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'm(Z(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4 G dd de0Z5G dd de0Z6G dd de0Z7G dd de0Z8G dd de0Z9G dd de0Z:G dd de0Z;G dd de0Z<G d d! d!e0Z=G d"d# d#e0Z>G d$d% d%e0Z?G d&d' d'e0Z@G d(d) d)e0ZAG d*d+ d+e0ZBG d,d- d-e0ZCG d.d/ d/e0ZDG d0d1 d1e'ZEG d2d3 d3ejFZGG d4d5 d5ejFZHG d6d7 d7ejFZIG d8d9 d9ejFZJG d:d; d;ejFZKG d<d= d=ejFZLdS )>    N)closing)BytesIO)utf8
native_str
to_unicode)gen)HTTPRequestHTTPResponse_RequestProxy	HTTPError
HTTPClient)
HTTPServer)IOLoop)IOStream)gen_logapp_log)netutil)AsyncHTTPTestCasebind_unused_portgen_test	ExpectLog)skipOnTravisignore_deprecation)ApplicationRequestHandlerurl)format_timestampHTTPHeadersc                   @      e Zd Zdd ZdS )HelloWorldHandlerc                 C   s*   |  dd}| dd | d|  d S )NnameZworldContent-Type
text/plainz	Hello %s!)get_argument
set_headerfinish)selfr     r'   S/var/www/html/lang_env/lib/python3.10/site-packages/tornado/test/httpclient_test.pyget%   s   zHelloWorldHandler.getN__name__
__module____qualname__r)   r'   r'   r'   r(   r   $       r   c                   @   r   )PostHandlerc                 C   s"   |  d| d| df  d S )NzPost arg1: %s, arg2: %sZarg1Zarg2)r%   r#   r&   r'   r'   r(   post,   s
   zPostHandler.postNr+   r,   r-   r1   r'   r'   r'   r(   r/   +   r.   r/   c                   @   r   )
PutHandlerc                 C   s   |  d |  | jj d S )Nz
Put body: writerequestbodyr0   r'   r'   r(   put4   s   
zPutHandler.putN)r+   r,   r-   r8   r'   r'   r'   r(   r3   3   r.   r3   c                   @   r   )RedirectHandlerc                 C   s.   |  d | j| dt| ddd d S )Nzredirects can have bodies toor   statusZ302)r:   )r5   redirectr#   intr0   r'   r'   r(   prepare:   s   

zRedirectHandler.prepareNr+   r,   r-   r=   r'   r'   r'   r(   r9   9   r.   r9   c                   @   r   )RedirectWithoutLocationHandlerc                 C   s   |  d |   d S )N-  )
set_statusr%   r0   r'   r'   r(   r=   B   s   
z&RedirectWithoutLocationHandler.prepareNr>   r'   r'   r'   r(   r?   A   r.   r?   c                   @   s   e Zd Zejdd ZdS )ChunkHandlerc                 c   s.    |  d |   tdV  |  d d S )Nasdf{Gz?Zqwer)r5   flushr   sleepr0   r'   r'   r(   r)   I   s
   
zChunkHandler.getN)r+   r,   r-   r   	coroutiner)   r'   r'   r'   r(   rB   H   s    rB   c                   @   r   )AuthHandlerc                 C   s   |  | jjd  d S )NAuthorization)r%   r6   headersr0   r'   r'   r(   r)   S   s   zAuthHandler.getNr*   r'   r'   r'   r(   rH   R   r.   rH   c                   @   r   )CountdownHandlerc                 C   s8   t |}|dkr| | d|d  d S | d d S )Nr   	countdown   ZZero)r<   r;   Zreverse_urlr5   )r&   countr'   r'   r(   r)   X   s   zCountdownHandler.getNr*   r'   r'   r'   r(   rK   W   r.   rK   c                   @   r   )EchoPostHandlerc                 C   s   |  | jj d S Nr4   r0   r'   r'   r(   r1   a   s   zEchoPostHandler.postNr2   r'   r'   r'   r(   rO   `   r.   rO   c                   @   r   )UserAgentHandlerc                 C   s   |  | jjdd d S )N
User-AgentzUser agent not set)r5   r6   rJ   r)   r0   r'   r'   r(   r)   f   s   zUserAgentHandler.getNr*   r'   r'   r'   r(   rQ   e   r.   rQ   c                   @   s   e Zd Zdd Zdd ZdS )ContentLength304Handlerc                 C   s   |  d | dd d S )N0  Content-Length*   )rA   r$   r0   r'   r'   r(   r)   k   s   
zContentLength304Handler.getc                 C   s   d S rP   r'   r0   r'   r'   r(   _clear_representation_headerso   s   z5ContentLength304Handler._clear_representation_headersN)r+   r,   r-   r)   rW   r'   r'   r'   r(   rS   j   s    rS   c                   @   r   )PatchHandlerc                 C   s   |  | jj dS )z=Return the request payload - so we can check it is being keptNr4   r0   r'   r'   r(   patchv   s   zPatchHandler.patchN)r+   r,   r-   rY   r'   r'   r'   r(   rX   u   r.   rX   c                   @   s>   e Zd Zejd Zdd Ze Z Z Z Z	 Z
 Z ZZdS )AllMethodsHandler)OTHERc                 C   s"   | j jd usJ | | j j d S rP   )r6   methodr5   r0   r'   r'   r(   r\   ~      zAllMethodsHandler.methodN)r+   r,   r-   r   ZSUPPORTED_METHODSr\   r)   headr1   r8   deleteoptionsrY   otherr'   r'   r'   r(   rZ   {   s    
$rZ   c                   @   r   )SetHeaderHandlerc                 C   s2   t | d| jjd D ]
\}}| || qd S )Nkv)zipZget_argumentsr6   	argumentsr$   )r&   rc   rd   r'   r'   r(   r)      s    zSetHeaderHandler.getNr*   r'   r'   r'   r(   rb      r.   rb   c                   @   s   e Zd ZdddZdS )InvalidGzipHandlerreturnNc                 C   s\   |  dd |  dd ddd tdD d d	 }tj| d
dd }| | d S )Nr!   r"   Content-Encodinggzip c                 s   s    | ]}d  |V  qdS )zHello World {}
N)format).0ir'   r'   r(   	<genexpr>   s    z)InvalidGzipHandler.get.<locals>.<genexpr>i(#  i;F    )compresslevel    )r$   joinrangerj   compressencoder5   )r&   textr7   r'   r'   r(   r)      s
    zInvalidGzipHandler.get)rh   Nr*   r'   r'   r'   r(   rg      s    rg   c                   @   r   )HeaderEncodingHandlerc                 C   s   |  | jjd d d S )NFoo	ISO8859-1)r%   r6   rJ   rv   r0   r'   r'   r(   r)      s   zHeaderEncodingHandler.getNr*   r'   r'   r'   r(   rx      r.   rx   c                   @   sp  e Zd Zdd Zdd Zedd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zed-d. Zd/d0 Zd1d2 Zed3d4 Zd5d6 Zed7d8 Z ed9d: Z!ed;d< Z"ed=d> Z#ed?d@ Z$dAdB Z%dCdD Z&dEdF Z'dGdH Z(dIdJ Z)dKdL Z*edMdN Z+dOdP Z,dQS )RHTTPClientCommonTestCasec                 C   s   t tdttdttdttdttdttdttdttdt	d	d
tdt
tdttdttdttdttdttdttdtgddS )N/hello/postz/putz	/redirect/redirect_without_location/chunk/authz/countdown/([0-9]+)rL   )r    	/echopost/user_agent/304_with_content_length/all_methods/patchz/set_header/invalid_gzip/header-encodingT)rj   )r   r   r   r/   r3   r9   r?   rB   rH   rK   rO   rQ   rS   rZ   rX   rb   rg   rx   r0   r'   r'   r(   get_app   s(   z HTTPClientCommonTestCase.get_appc                 C   s4   d}| j dd|d}| |jd | |j| d S )Ns   some patch datar   PATCHr\   r7      fetchassertEqualcoder7   )r&   r7   responser'   r'   r(   test_patch_receives_payload   s   z4HTTPClientCommonTestCase.test_patch_receives_payloadc                 C   st   |  d}| |jd | |jd d | |jd |jd us#J | t|jd |  d}| |jd d S )	Nr|   r   r!   r"      Hello world!r   z/hello?name=Bens
   Hello Ben!)r   r   r   rJ   r7   request_timer<   r&   r   r'   r'   r(   test_hello_world   s   

z)HTTPClientCommonTestCase.test_hello_worldc                 C   s2   g }| j d|jd}| |dg | |j d S )Nr|   streaming_callbackr   )r   appendr   assertFalser7   )r&   chunksr   r'   r'   r(   test_streaming_callback   s   z0HTTPClientCommonTestCase.test_streaming_callbackc                 C   s0   | j dddd}| |jd | |jd d S )Nr}   POSTzarg1=foo&arg2=barr   r   s   Post arg1: foo, arg2: barr   r   r'   r'   r(   	test_post   s   z"HTTPClientCommonTestCase.test_postc                 C   sL   |  d}| |jd g }| j d|jd}| |ddg | |j d S )Nr      asdfqwerr      asdf   qwer)r   r   r7   r   r   )r&   r   r   r'   r'   r(   test_chunked   s   
z%HTTPClientCommonTestCase.test_chunkedc                    s   t  \}}t|2 tj fdd}t||  d| }|   |j	d  j
|  W d    d S 1 s>w   Y  d S )Nc                 3   H    t | }|dV }d|vr d |dddV  |  d S )N   

   HTTP/1.requires HTTP/1.xs7   HTTP/1.1 200 OK
Transfer-Encoding: chunked

1
1
1
2
0

   
   
r   Z
read_untilZskipTestr5   replacecloseconnaddressstreamZrequest_datar0   r'   r(   accept_callback   s   

zDHTTPClientCommonTestCase.test_chunked_close.<locals>.accept_callbackhttp://127.0.0.1:%d/s   12)r   r   r   rG   r   add_accept_handlerr   rethrowr   r7   io_loopremove_handlerfilenor&   sockportr   respr'   r0   r(   test_chunked_close   s   

"z+HTTPClientCommonTestCase.test_chunked_closec                 C   s   |  | jddddjd d S )Nr   Aladdinopen sesameauth_usernameauth_password"   Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==r   r   r7   r0   r'   r'   r(   test_basic_auth	  s   z(HTTPClientCommonTestCase.test_basic_authc                 C   s    |  | jdddddjd d S )Nr   r   r   basic)r   r   	auth_moder   r   r0   r'   r'   r(   test_basic_auth_explicit_mode  s   z6HTTPClientCommonTestCase.test_basic_auth_explicit_modec                 C   sD   |  | jddddjd tdd}|  | jd|ddjd	 d S )
Nr   testu   123£r   s   Basic dGVzdDoxMjPCow==ZNFDu   joséu   səcrəts   Basic am9zw6k6c8mZY3LJmXQ=)r   r   r7   unicodedata	normalize)r&   usernamer'   r'   r(   test_basic_auth_unicode  s   z0HTTPClientCommonTestCase.test_basic_auth_unicodec              	   C   s   t tddd2 | ttf | jdddddd	 W d    n1 s$w   Y  W d    d S W d    d S 1 s<w   Y  d S )
Nzuncaught exceptionFrequiredr   r   r   rC   T)r   r   r   raise_error)r   r   assertRaises
ValueErrorr   r   r0   r'   r'   r(   test_unsupported_auth_mode,  s   "z3HTTPClientCommonTestCase.test_unsupported_auth_modec                 C   sn   | j ddd}| d|j | |jd d |  d}| d|j | |jd | d	|j d S )
Nz/countdown/2FZfollow_redirects.  ZLocationz/countdown/1r   z/countdown/0s   Zero)r   r   r   
assertTruerJ   endswitheffective_urlr7   r   r'   r'   r(   test_follow_redirect:  s   
z-HTTPClientCommonTestCase.test_follow_redirectc                 C   s    | j ddd}| d|j d S )Nr~   Tr   r@   r   r   r   r   r'   r'   r(   test_redirect_without_locationD  s   z7HTTPClientCommonTestCase.test_redirect_without_locationc                 C   "   | j dddd}| |jd d S )N/redirect?url=/put&status=307PUTZhellor      Put body: hellor   r   r7   r   r'   r'   r(   test_redirect_put_with_bodyL  s   z4HTTPClientCommonTestCase.test_redirect_put_with_bodyc                 C   r   )Nr   r   Tr\   allow_nonstandard_methodss
   Put body: r   r   r'   r'   r(   test_redirect_put_without_bodyR  s   z7HTTPClientCommonTestCase.test_redirect_put_without_bodyc                 C   s  dD ]P}d| }| j |ddd}| d|j dD ]#}| j ||dd	}|d
v r1| t||j q| |jt|dg q| j |dd}| d|j | d|j qdD ]/}d}dD ]}| j ||dd	}| |t|j q[| j |dd}| d|j | d|j qUd S )N)r@   r   i/  z$/redirect?url=/all_methods&status=%dr       r   s   GET)GETOPTIONSr   DELETETr   )r@   r   HEADr\   r   )i3  i4  z%/redirect?url=/all_methods&status=307)r   r   r   r   r   )r   r   r7   r   assertInr   r   )r&   r:   r   r   r\   r'   r'   r(   test_method_after_redirect[  s*   z3HTTPClientCommonTestCase.test_method_after_redirectc                 C   s8   |  ddd}| |}| dtd |j d S )Nr   zhttp://zhttp://me:secret@s   Basic s	   me:secret)get_urlr   r   r   base64	b64encoder7   )r&   r   r   r'   r'   r(   test_credentials_in_urly  s   
z0HTTPClientCommonTestCase.test_credentials_in_urlc                 C   s   d}t d}| jdd|ddid}| |jd d	 | |jt| | jdd|ddid}| |jd d
 | |j| | jdd|ddidd}| |jd d
 | |j| d S )N   és   e9r   r   r!   zapplication/blah)r\   r7   rJ   rU   21foo)r\   r7   rJ   
user_agent)binasciia2b_hexr   r   rJ   r7   r   )r&   Zunicode_bodyZ	byte_bodyr   r'   r'   r(   test_body_encoding~  s6   
z+HTTPClientCommonTestCase.test_body_encodingc                 C   sZ   |  d}| t|jt | t|jd t | t|jt | t|j	t d S )Nr|   r!   )
r   r   typer7   bytesrJ   strr   r<   r   r   r'   r'   r(   
test_types  s
   
z#HTTPClientCommonTestCase.test_typesc                 C   sj   | j ddddid}| |jd d | |jd | t|jd tjd	|jd
}| |	 d d S )Nr   FzAccept-Encodingrj   )Zdecompress_responserJ   ri   r   "   r)modefileobj)
r   r   rJ   ZassertNotEqualr7   lenrj   GzipFilebufferread)r&   r   fr'   r'   r(   	test_gzip  s   
z"HTTPClientCommonTestCase.test_gzipc              	   C   s   t tddd4 z| d}| |jd | |jd d d W n	 ty*   Y n	w W d    d S W d    d S 1 s>w   Y  d S )Nz*(Uncaught exception|Exception in callback)Fr   r   r      s   Hello World 0
)r   r   r   r   r   r7   r   r   r'   r'   r(   test_invalid_gzip  s   
"z*HTTPClientCommonTestCase.test_invalid_gzipc                    sp   g i g  fdd} fdd}j d||d td d d	  d
dg d S )Nc                    sV   |  drd S |  dr |  d S | dkr)| dd\}}| | < d S d S )NzHTTP/1.1 101zHTTP/z
:rM   )
startswithr   splitstriplower)Zheader_linerc   rd   )
first_linerJ   r'   r(   header_callback  s   

zFHTTPClientCommonTestCase.test_header_callback.<locals>.header_callbackc                    s    d d  |  d S )Nzcontent-typeztext/html; charset=UTF-8)r   r   )chunk)r   rJ   r&   r'   r(   r     s   zIHTTPClientCommonTestCase.test_header_callback.<locals>.streaming_callbackr   )r  r   rM   r   zHTTP/[0-9]\.[0-9] 200.*
r   r   )r   r   r   assertRegex)r&   r  r   r'   )r   r  rJ   r&   r(   test_header_callback  s   
z-HTTPClientCommonTestCase.test_header_callbackc                 c   sX    t ddd}| jjd|d}z|| dV }| |jd W |  d S |  w )NZTestDefaultUserAgentF)r   Z
allow_ipv6T)Zforce_instancedefaultsr   s   TestDefaultUserAgent)dicthttp_client	__class__r   r   r   r7   r   )r&   r  clientr   r'   r'   r(   test_configure_defaults  s   z0HTTPClientCommonTestCase.test_configure_defaultsc              
   C   sT   dD ]%}t tfD ]}| }||d< | jd|d}| |jdd|j||f  qqd S )N)ZMyUserAgent   MyUserAgentrR   r   rJ   r  z#response=%r, value=%r, container=%r)r  r   r   r   r7   )r&   value	containerrJ   r   r'   r'   r(   test_header_types  s   
z*HTTPClientCommonTestCase.test_header_typesc                    s   t  \}}t|@ tj fdd}t|| z d| }|   |j	d d W  j
|  n	 j
|  w W d    d S 1 sLw   Y  d S )Nc                 3   r   )Nr   r   r   s2   HTTP/1.1 200 OK
X-XSS-Protection: 1;
	mode=block

r   r   r   r   r0   r'   r(   r     s   

zIHTTPClientCommonTestCase.test_multi_line_headers.<locals>.accept_callbackr   zX-XSS-Protectionz1; mode=block)r   r   r   rG   r   r   r   r   r   rJ   r   r   r   r   r'   r0   r(   test_multi_line_headers  s   

&"z0HTTPClientCommonTestCase.test_multi_line_headersc                 c   s6    | j j| dddidV }| |jdd d S )Nr   ry   u   bärr  rz   )r  r   r   r   r7   rv   r   r'   r'   r(   test_header_encoding"  s   z-HTTPClientCommonTestCase.test_header_encodingc                 C   s.   |  d}| |jd | |jd d d S )Nr   rT   rU   Z42)r   r   r   rJ   r   r'   r'   r(   test_304_with_content_length,  s   
z5HTTPClientCommonTestCase.test_304_with_content_lengthc                 c   s(    | j | dV }| |jd d S Nr|   r   r  r   r   r   r7   r   r'   r'   r(   test_future_interface5  s   z.HTTPClientCommonTestCase.test_future_interfacec                 c   s    |  t}| j| dV  W d    n1 sw   Y  |jd us'J |jjd us/J | |jjd | |jjjd d S N	/notfound  )	r   r   r  r   r   	exceptionr   r   r   )r&   contextr'   r'   r(   test_future_http_error:  s   z/HTTPClientCommonTestCase.test_future_http_errorc                 c   s,    | j j| dddV }| |jd d S )Nr"  F)r   r#  )r  r   r   r   r   r   r'   r'   r(   test_future_http_error_no_raiseC  s
   
z8HTTPClientCommonTestCase.test_future_http_error_no_raisec                 c   s^    |  d}| j|V }| |jj| | t|jt | j|jV }| |j	d d S r  )
r   r  r   r   r6   r   r   
isinstancer   r7   )r&   r   r   Z	response2r'   r'   r(    test_reuse_request_from_responseJ  s   
z9HTTPClientCommonTestCase.test_reuse_request_from_responsec                 c   s    |  d}t|dd}| j|V }| |jd | ttf}t|dd}| j|V  W d    n1 s9w   Y  | 	dt
|j d S )Nr|   z	127.0.0.1network_interfacer   znot-interface-or-ip)r   r   r  r   r   r   r   r   r   r   r   r$  )r&   r   r6   r   r%  r'   r'   r(   test_bind_source_ipW  s   
z,HTTPClientCommonTestCase.test_bind_source_ipc                 C   s   dD ]}| j d|d}| |jt| qdD ]}| j d|dd}| |jt| q| j ddd}| |jd | j ddd	d
}| |jd d S )Nr   r   r   r   r   r   r   r   r   r   r   r[   Tr   s   OTHER)r   r   r7   r   )r&   r\   r   r'   r'   r(   test_all_methodsc  s   z)HTTPClientCommonTestCase.test_all_methodsc              	   C   s   dD ]7}|  t}| jd|dd W d    n1 sw   Y  | dt|j | jd|dd}| |jd qdD ]>}|  t}| jd|d	dd
 W d    n1 sWw   Y  | dt|j |dkrz| jd|d	ddd | |jd q<d S )Nr.  r   T)r\   r   zmust not be Noner   r   r-  r   )r\   r7   r   zmust be Noner   )r\   r7   r   r   )r   r   r   r   r   r$  r   r   )r&   r\   r%  r   r'   r'   r(   test_body_sanity_checksq  s6   z0HTTPClientCommonTestCase.test_body_sanity_checksc                 C   s*   | j dddd}|  | |jd d S )Nz/redirect?status=307&url=/putr   s   hellor   r   )r   r   r   r7   r   r'   r'   r(   test_put_307  s
   z%HTTPClientCommonTestCase.test_put_307c                 C   s,   |  d}|  | |jd td d S )Nz/set_header?k=foo&v=%E9ry   r   )r   r   r   rJ   r   r   r'   r'   r(   test_non_ascii_header  s   
z.HTTPClientCommonTestCase.test_non_ascii_headerc                 C   s   t   }| d}|  |jd usJ | |jd | |jd |jd us)J | t|j| d |j	 D ]\}}| 
d|  koHdk n  d||f  q9d S )Nr|   r   g      ?ztime_info[%s] out of bounds: %s)timer   r   r   ZassertGreaterEqualZ
assertLess
start_timeabsZ	time_infoitemsr   )r&   r4  r   rc   rd   r'   r'   r(   test_response_times  s   
*z,HTTPClientCommonTestCase.test_response_timesc                 C   sZ   | j ddd}| |jd | j ddd}| |jd | j dddd}| |jd d S )Nr|   r   )connect_timeoutr   )request_timeout)r8  r9  r   r   r'   r'   r(   test_zero_timeout  s   z*HTTPClientCommonTestCase.test_zero_timeoutc                 c   s    | j | d}| |  ttd#}tdD ]}t	dV  |j
r( n	qW d    d S W d    d S 1 s<w   Y  d S )Nz/404z$Exception after Future was cancelledd   rD   )r  r   r   r   cancelr   r   rt   r   rF   Zlogged_stack)r&   Zfutelrn   r'   r'   r(   test_error_after_cancel  s   "z0HTTPClientCommonTestCase.test_error_after_cancelc              
   C   s   dD ]n\}}| j |dd& | t | jdd|id W d    n1 s'w   Y  W d    n1 s6w   Y  | j |dd& | t | jd|did W d    n1 s\w   Y  W d    n1 skw   Y  qd S )N))zfoobar:cr)zfoo
bar:lf)z	foo
bar:Zcrlfr  )r    positionr|   r   r  key)ZsubTestr   r   r   )r&   headerr    r'   r'   r(   test_header_crlf  s   z)HTTPClientCommonTestCase.test_header_crlfN)-r+   r,   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r  r  r  r  r  r   r&  r'  r)  r,  r/  r0  r1  r2  r7  r:  r>  rD  r'   r'   r'   r(   r{      sd    
	%	
	$

 
		




0

r{   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )RequestProxyTestc                 C   s&   t tdddt }| |jd d S )Nhttp://example.com/r   )r   )r
   r   r  r   r   r&   proxyr'   r'   r(   test_request_set     z!RequestProxyTest.test_request_setc                 C   s&   t tdtdd}| |jd d S )NrF  r   r*  )r
   r   r  r   r+  rG  r'   r'   r(   test_default_set  rJ  z!RequestProxyTest.test_default_setc                 C   s*   t tdddtdd}| |jd d S )NrF  r   )
proxy_hostbar)r
   r   r  r   rL  rG  r'   r'   r(   test_both_set  s   zRequestProxyTest.test_both_setc                 C   s"   t tdt }| |jd  d S NrF  )r
   r   r  assertIsr   rG  r'   r'   r(   test_neither_set  r]   z!RequestProxyTest.test_neither_setc                 C   sF   t tdt }| t |j W d    d S 1 sw   Y  d S rO  )r
   r   r  r   AttributeErrorr   rG  r'   r'   r(   test_bad_attribute   s   "z#RequestProxyTest.test_bad_attributec                 C   s    t tdd }| |jd  d S rO  )r
   r   rP  r   rG  r'   r'   r(   test_defaults_none  s   z#RequestProxyTest.test_defaults_noneN)	r+   r,   r-   rI  rK  rN  rQ  rS  rT  r'   r'   r'   r(   rE    s    rE  c                   @   r   )HTTPResponseTestCasec                 C   s<   t tddt d}t|}| |d | d| d S )Nhttp://example.comr   )r   zHTTPResponse(zcode=200)r	   r   r   r   r   r  r   )r&   r   sr'   r'   r(   test_str  s   zHTTPResponseTestCase.test_strN)r+   r,   r-   rX  r'   r'   r'   r(   rU  
  r.   rU  c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )SyncHTTPClientTestc                    sb   t dd_t  tj fddfdd}tj|d_j   	  t
 _d S )NF)Zmake_currentc                     s<   t  \} _tdtfg}t|_j|     d S )N/)r   r   r   r   r   serverZ
add_socketset)r   Zapp)eventr&   r'   r(   init_server  s
   
z-SyncHTTPClientTest.setUp.<locals>.init_serverc                      s   j   j   d S rP   )server_ioloopZrun_syncstartr'   )r^  r&   r'   r(   r`  !  s   z'SyncHTTPClientTest.setUp.<locals>.start)target)r   r_  	threadingEventr   rG   Threadserver_threadr`  waitr   r  )r&   r`  r'   )r]  r^  r&   r(   setUp  s   
zSyncHTTPClientTest.setUpc                    s>    fdd} j |  j   j   j jdd d S )Nc                     s,    j   tj fdd}  j|  d S )Nc                  3   s0     j  V  tdD ]} d V  q j  d S )N   )r[  Zclose_all_connectionsrt   r_  stop)rn   r0   r'   r(   	slow_stop4  s
   zCSyncHTTPClientTest.tearDown.<locals>.stop_server.<locals>.slow_stop)r[  ri  r   rG   r_  add_callback)rj  r0   r'   r(   stop_server,  s   
z0SyncHTTPClientTest.tearDown.<locals>.stop_serverT)Zall_fds)r_  rk  re  rs   r  r   )r&   rl  r'   r0   r(   tearDown+  s
   

zSyncHTTPClientTest.tearDownc                 C   s   d| j |f S )Nzhttp://127.0.0.1:%d%s)r   )r&   pathr'   r'   r(   r   D  s   zSyncHTTPClientTest.get_urlc                 C   s$   | j | d}| d|j d S )NrZ  r   r  r   r'   r'   r(   test_sync_clientG  s   z#SyncHTTPClientTest.test_sync_clientc                 C   sP   |  t}| j| d W d    n1 sw   Y  | |jjd d S r!  )r   r   r  r   r   r   r$  r   )r&   Z	assertionr'   r'   r(   test_sync_client_errorK  s   z)SyncHTTPClientTest.test_sync_client_errorN)r+   r,   r-   rg  rm  r   ro  rp  r'   r'   r'   r(   rY    s    rY  c                   @   r   )SyncHTTPClientSubprocessTestc                 C   sT   t jtjddgt jt jddd}|jrtd tt|j |jr(| 	d d S d S )Nz-czMfrom tornado.httpclient import HTTPClient; f = lambda: None; c = HTTPClient()T   )stdoutstderrchecktimeoutzSTDOUT:z%subprocess produced unexpected output)

subprocessrunsys
executablePIPESTDOUTrs  printr   Zfail)r&   procr'   r'   r(   test_destructor_logT  s    z0SyncHTTPClientSubprocessTest.test_destructor_logN)r+   r,   r-   r  r'   r'   r'   r(   rq  S  r.   rq  c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )HTTPRequestTestCasec                 C   s&   t dddid}| |jddi d S )NrV  r   rM  r  )r   r   rJ   r&   r6   r'   r'   r(   test_headersr  s   z HTTPRequestTestCase.test_headersc                 C   s(   t d}ddi|_| |jddi d S )NrV  rM  Zbazr   rJ   r   r  r'   r'   r(   test_headers_setterv  s   
z'HTTPRequestTestCase.test_headers_setterc                 C   s    t d}d |_| |ji  d S )NrV  r  r  r'   r'   r(   test_null_headers_setter{  s   z,HTTPRequestTestCase.test_null_headers_setterc                 C   s"   t ddd}| |jtd d S )NrV  r   )r7   )r   r   r7   r   r  r'   r'   r(   	test_body  s   zHTTPRequestTestCase.test_bodyc                 C   s$   t d}d|_| |jtd d S )NrV  r   )r   r7   r   r   r  r'   r'   r(   test_body_setter  s   z$HTTPRequestTestCase.test_body_setterc                 C   s6   t j t jj}td|d}| |jdt|i d S NrV  )Zif_modified_sincezIf-Modified-Since)datetimenowtimezoneutcr   r   rJ   r   r&   Z	http_dater6   r'   r'   r(   test_if_modified_since  s
   z*HTTPRequestTestCase.test_if_modified_sincec                 C   sV   t   tj }W d    n1 sw   Y  td|d}| |jdt|i d S r  )r   r  utcnowr   r   rJ   r   r  r'   r'   r(   'test_if_modified_since_naive_deprecated  s   z;HTTPRequestTestCase.test_if_modified_since_naive_deprecatedN)
r+   r,   r-   r  r  r  r  r  r  r  r'   r'   r'   r(   r  q  s    r  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )HTTPErrorTestCasec                 C   s2   t d}t|}| || | |j|j d S )N  )r   copyZassertIsNotr   r   )r&   ee2r'   r'   r(   	test_copy  s   
zHTTPErrorTestCase.test_copyc                 C   s,   t d}| t|d | t|d d S )Nr  HTTP 403: Forbidden)r   r   r   repr)r&   r  r'   r'   r(   test_plain_error  s   z"HTTPErrorTestCase.test_plain_errorc                 C   sj   t tdd}| t}|  W d    n1 sw   Y  |j}| t|d | t|d d S )NrF  r  r  )	r	   r   r   r   r   r$  r   r   r  )r&   r   cmr  r'   r'   r(   test_error_with_response  s   
z*HTTPErrorTestCase.test_error_with_responseN)r+   r,   r-   r  r  r  r'   r'   r'   r(   r    s    r  )Mr   r   
contextlibr   r  rj   rb  r  ior   rw  ry  r3  typingr   ZunittestZtornado.escaper   r   r   tornador   Ztornado.httpclientr   r	   r
   r   r   Ztornado.httpserverr   Ztornado.ioloopr   Ztornado.iostreamr   Ztornado.logr   r   r   Ztornado.testingr   r   r   r   Ztornado.test.utilr   r   Ztornado.webr   r   r   Ztornado.httputilr   r   r   r/   r3   r9   r?   rB   rH   rK   rO   rQ   rS   rX   rZ   rb   rg   rx   r{   ZTestCaserE  rU  rY  rq  r  r  r'   r'   r'   r(   <module>   sj    
	

    I!
?(