o
    h                     @   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nameworldContent-Type
text/plainz	Hello %s!)get_argument
set_headerfinish)selfr     r(   P/var/www/vscode/kcb/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: %sarg1arg2)r&   r$   r'   r(   r(   r)   post,   s
   zPostHandler.postNr,   r-   r.   r4   r(   r(   r(   r)   r0   +   r/   r0   c                   @   r   )
PutHandlerc                 C   s   |  d |  | jj d S )Nz
Put body: writerequestbodyr3   r(   r(   r)   put4   s   
zPutHandler.putN)r,   r-   r.   r;   r(   r(   r(   r)   r6   3   r/   r6   c                   @   r   )RedirectHandlerc                 C   s.   |  d | j| dt| ddd d S )Nzredirects can have bodies toor   status302)r=   )r8   redirectr$   intr3   r(   r(   r)   prepare:   s   

zRedirectHandler.prepareNr,   r-   r.   rA   r(   r(   r(   r)   r<   9   r/   r<   c                   @   r   )RedirectWithoutLocationHandlerc                 C   s   |  d |   d S )N-  )
set_statusr&   r3   r(   r(   r)   rA   B   s   
z&RedirectWithoutLocationHandler.prepareNrB   r(   r(   r(   r)   rC   A   r/   rC   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?qwer)r8   flushr   sleepr3   r(   r(   r)   r*   I   s
   
zChunkHandler.getN)r,   r-   r.   r   	coroutiner*   r(   r(   r(   r)   rF   H   s    rF   c                   @   r   )AuthHandlerc                 C   s   |  | jjd  d S )NAuthorization)r&   r9   headersr3   r(   r(   r)   r*   S   s   zAuthHandler.getNr+   r(   r(   r(   r)   rM   R   r/   rM   c                   @   r   )CountdownHandlerc                 C   s8   t |}|dkr| | d|d  d S | d d S )Nr   	countdown   Zero)r@   r?   reverse_urlr8   )r'   countr(   r(   r)   r*   X   s   zCountdownHandler.getNr+   r(   r(   r(   r)   rP   W   r/   rP   c                   @   r   )EchoPostHandlerc                 C   s   |  | jj d S Nr7   r3   r(   r(   r)   r4   a   s   zEchoPostHandler.postNr5   r(   r(   r(   r)   rV   `   r/   rV   c                   @   r   )UserAgentHandlerc                 C   s   |  | jjdd d S )N
User-AgentzUser agent not set)r8   r9   rO   r*   r3   r(   r(   r)   r*   f   s   zUserAgentHandler.getNr+   r(   r(   r(   r)   rX   e   r/   rX   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*   )rE   r%   r3   r(   r(   r)   r*   k   s   
zContentLength304Handler.getc                 C   s   d S rW   r(   r3   r(   r(   r)   _clear_representation_headerso   s   z5ContentLength304Handler._clear_representation_headersN)r,   r-   r.   r*   r^   r(   r(   r(   r)   rZ   j   s    rZ   c                   @   r   )PatchHandlerc                 C   s   |  | jj dS )z=Return the request payload - so we can check it is being keptNr7   r3   r(   r(   r)   patchv   s   zPatchHandler.patchN)r,   r-   r.   r`   r(   r(   r(   r)   r_   u   r/   r_   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 rW   )r9   methodr8   r3   r(   r(   r)   rc   ~      zAllMethodsHandler.methodN)r,   r-   r.   r   SUPPORTED_METHODSrc   r*   headr4   r;   deleteoptionsr`   otherr(   r(   r(   r)   ra   {   s    
$ra   c                   @   r   )SetHeaderHandlerc                 C   s2   t | d| jjd D ]
\}}| || qd S )Nkv)zipget_argumentsr9   	argumentsr%   )r'   rk   rl   r(   r(   r)   r*      s    zSetHeaderHandler.getNr+   r(   r(   r(   r)   rj      r/   rj   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rangers   compressencoder8   )r'   textr:   r(   r(   r)   r*      s
    zInvalidGzipHandler.get)rq   Nr+   r(   r(   r(   r)   rp      s    rp   c                   @   r   )HeaderEncodingHandlerc                 C   s   |  | jjd d d S )NFoo	ISO8859-1)r&   r9   rO   r   r3   r(   r(   r)   r*      s   zHeaderEncodingHandler.getNr+   r(   r(   r(   r)   r      r/   r   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]+)rQ   )r    	/echopost/user_agent/304_with_content_length/all_methods/patchz/set_header/invalid_gzip/header-encodingT)rs   )r   r   r   r0   r6   r<   rC   rF   rM   rP   rV   rX   rZ   ra   r_   rj   rp   r   r3   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rc   r:      fetchassertEqualcoder:   )r'   r:   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   rO   r:   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   assertFalser:   )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   r:   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   
read_untilskipTestr8   replacecloseconnaddressstreamrequest_datar3   r(   r)   accept_callback   s   

zDHTTPClientCommonTestCase.test_chunked_close.<locals>.accept_callbackhttp://127.0.0.1:%d/s   12)r   r   r   rL   r   add_accept_handlerr   rethrowr   r:   io_loopremove_handlerfilenor'   sockportr   respr(   r3   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   r:   r3   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   r3   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==NFDu   joséu   səcrəts   Basic am9zw6k6c8mZY3LJmXQ=)r   r   r:   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   rG   T)r   r   r   raise_error)r   r   assertRaises
ValueErrorr   r   r3   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follow_redirects.  Locationz/countdown/1r   z/countdown/0s   Zero)r   r   r   
assertTruerO   endswitheffective_urlr:   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   rD   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PUThellor      Put body: hellor   r   r:   r   r(   r(   r)   test_redirect_put_with_bodyL  s   z4HTTPClientCommonTestCase.test_redirect_put_with_bodyc                 C   r   )Nr   r   Trc   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)rD   r   i/  z$/redirect?url=/all_methods&status=%dr       r   s   GET)GETOPTIONSr   DELETETr   )rD   r   HEADrc   r   )i3  i4  z%/redirect?url=/all_methods&status=307)r   r   r   r   r   )r   r   r:   r   assertInr   r   )r'   r=   r   r   rc   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	b64encoder:   )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)rc   r:   rO   r\   21foo)rc   r:   rO   
user_agent)binasciia2b_hexr   r   rO   r:   r   )r'   unicode_body	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   typer:   bytesrO   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-Encodingrs   )decompress_responserO   rr   r   "   r)modefileobj)
r   r   rO   assertNotEqualr:   lenrs   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   r:   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
:rR   )
startswithr   splitstriplower)header_linerk   rl   )
first_linerO   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   rO   r'   r(   r)   r     s   zIHTTPClientCommonTestCase.test_header_callback.<locals>.streaming_callbackr   )r!  r   rR   r   zHTTP/[0-9]\.[0-9] 200.*
r   r   )r   r   r  assertRegex)r'   r!  r   r(   )r   r   rO   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 )NTestDefaultUserAgentF)r  
allow_ipv6T)force_instancedefaultsr   s   TestDefaultUserAgent)dicthttp_client	__class__r   r   r   r:   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)MyUserAgent   MyUserAgentrY   r   rO   r/  z#response=%r, value=%r, container=%r)r)  r   r   r   r:   )r'   value	containerrO   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   r3   r(   r)   r     s   

zIHTTPClientCommonTestCase.test_multi_line_headers.<locals>.accept_callbackr   zX-XSS-Protectionz1; mode=block)r   r   r   rL   r   r   r   r   r   rO   r   r   r   r   r(   r3   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   r   u   bärr0  r   )r*  r   r   r   r:   r   r   r(   r(   r)   test_header_encoding"  s   z-HTTPClientCommonTestCase.test_header_encodingc                 C   s.   |  d}| |jd | |jd d d S )Nr   r[   r\   42)r   r   r   rO   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   r:   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 r8  )
r   r*  r   r   r9   r   r   
isinstancer   r:   )r'   r   r   	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   r9   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   rb   Tr   s   OTHER)r   r   r:   r   )r'   rc   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 )NrI  r   T)rc   r   zmust not be Noner   r   rH  r   )rc   r:   r   zmust be Noner   )rc   r:   r   r   )r   r   r   r   r
  r>  r   r   )r'   rc   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   r:   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=%E9r   r   )r   r   r   rO   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   assertGreaterEqual
assertLess
start_timeabs	time_infoitemsr   )r'   rQ  r   rk   rl   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)rV  rW  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   rH   )r*  r   r   r   cancelr   r   r}   r   rK   logged_stack)r'   futelrw   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:crlfr1  )r    positionr   r  r0  key)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-  r3  r4  r5  r7  r:  r@  rA  rD  rG  rJ  rK  rL  rM  rU  rX  r^  rf  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 )Nrh  r  rE  )r
   r   r)  r   rF  ri  r(   r(   r)   test_default_set  rl  z!RequestProxyTest.test_default_setc                 C   s*   t tdddtdd}| |jd d S )Nrh  r  )
proxy_hostbar)r
   r   r)  r   rn  ri  r(   r(   r)   test_both_set  s   zRequestProxyTest.test_both_setc                 C   s"   t tdt }| |jd  d S Nrh  )r
   r   r)  assertIsr   ri  r(   r(   r)   test_neither_set  rd   z!RequestProxyTest.test_neither_setc                 C   sF   t tdt }| t |j W d    d S 1 sw   Y  d S rq  )r
   r   r)  r   AttributeErrorr  ri  r(   r(   r)   test_bad_attribute   s   "z#RequestProxyTest.test_bad_attributec                 C   s    t tdd }| |jd  d S rq  )r
   r   rr  r   ri  r(   r(   r)   test_defaults_none  s   z#RequestProxyTest.test_defaults_noneN)	r,   r-   r.   rk  rm  rp  rs  ru  rv  r(   r(   r(   r)   rg    s    rg  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.   rz  r(   r(   r(   r)   rw  
  r/   rw  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)make_currentc                     s<   t  \} _tdtfg}t|_j|     d S )N/)r   r   r   r   r   server
add_socketset)r   app)eventr'   r(   r)   init_server  s
   
z-SyncHTTPClientTest.setUp.<locals>.init_serverc                      s   j   j   d S rW   )server_iolooprun_syncstartr(   )r  r'   r(   r)   r  !  s   z'SyncHTTPClientTest.setUp.<locals>.start)target)r   r  	threadingEventr   rL   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~  close_all_connectionsr}   r  stop)rw   r3   r(   r)   	slow_stop4  s
   zCSyncHTTPClientTest.tearDown.<locals>.stop_server.<locals>.slow_stop)r~  r  r   rL   r  add_callback)r  r3   r(   r)   stop_server,  s   
z0SyncHTTPClientTest.tearDown.<locals>.stop_serverT)all_fds)r  r  r  r|   r*  r   )r'   r  r(   r3   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 )Nr}  r   r9  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'   	assertionr(   r(   r)   test_sync_client_errorK  s   z)SyncHTTPClientTest.test_sync_client_errorN)r,   r-   r.   r  r  r   r  r  r(   r(   r(   r)   r{    s    r{  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STDOUTr  printr   fail)r'   procr(   r(   r)   test_destructor_logT  s    z0SyncHTTPClientSubprocessTest.test_destructor_logN)r,   r-   r.   r  r(   r(   r(   r)   r  S  r/   r  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 )Nrx  r  ro  r0  )r   r   rO   r'   r9   r(   r(   r)   test_headersr  s   z HTTPRequestTestCase.test_headersc                 C   s(   t d}ddi|_| |jddi d S )Nrx  ro  bazr   rO   r   r  r(   r(   r)   test_headers_setterv  s   
z'HTTPRequestTestCase.test_headers_setterc                 C   s    t d}d |_| |ji  d S )Nrx  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 )Nrx  r  )r:   )r   r   r:   r   r  r(   r(   r)   	test_body  s   zHTTPRequestTestCase.test_bodyc                 C   s$   t d}d|_| |jtd d S )Nrx  r  )r   r:   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 Nrx  )if_modified_sincezIf-Modified-Since)datetimenowtimezoneutcr   r   rO   r   r'   	http_dater9   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   rO   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   copy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 )Nrh  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  rs   r  r  ior   r  r  rN  typingr   unittesttornado.escaper   r   r   tornador   tornado.httpclientr   r	   r
   r   r   tornado.httpserverr   tornado.ioloopr   tornado.iostreamr   tornado.logr   r   r   tornado.testingr   r   r   r   tornado.test.utilr   r   tornado.webr   r   r   tornado.httputilr   r   r   r0   r6   r<   rC   rF   rM   rP   rV   rX   rZ   r_   ra   rj   rp   r   r   TestCaserg  rw  r{  r  r  r  r(   r(   r(   r)   <module>   sj    
	

    I!
?(