o
    Ehȏ                     @   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, d dl-m.Z.m/Z/m0Z0 d dl1m2Z2m3Z3 G dd de/Z4G dd de/Z5G dd de/Z6G dd de/Z7G dd de/Z8G dd de/Z9G 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>G d&d' d'e/Z?G d(d) d)e/Z@G d*d+ d+e/ZAG d,d- d-e/ZBG d.d/ d/e/ZCG d0d1 d1e'ZDG d2d3 d3ejEZFG d4d5 d5ejEZGG d6d7 d7ejEZHG d8d9 d9ejEZIG d:d; d;ejEZJG d<d= d=ejEZKd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)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'   ]/var/www/html/Persson_Maskin/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: %sarg1arg2)r%   r#   r&   r'   r'   r(   post,   s
   zPostHandler.postNr+   r,   r-   r3   r'   r'   r'   r(   r/   +   r.   r/   c                   @   r   )
PutHandlerc                 C   s   |  d |  | jj d S )Nz
Put body: writerequestbodyr2   r'   r'   r(   put4   s   
zPutHandler.putN)r+   r,   r-   r:   r'   r'   r'   r(   r5   3   r.   r5   c                   @   r   )RedirectHandlerc                 C   s.   |  d | j| dt| ddd d S )Nzredirects can have bodies toor   status302)r<   )r7   redirectr#   intr2   r'   r'   r(   prepare:   s   

zRedirectHandler.prepareNr+   r,   r-   r@   r'   r'   r'   r(   r;   9   r.   r;   c                   @   r   )RedirectWithoutLocationHandlerc                 C   s   |  d |   d S )N-  )
set_statusr%   r2   r'   r'   r(   r@   B   s   
z&RedirectWithoutLocationHandler.prepareNrA   r'   r'   r'   r(   rB   A   r.   rB   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)r7   flushr   sleepr2   r'   r'   r(   r)   I   s
   
zChunkHandler.getN)r+   r,   r-   r   	coroutiner)   r'   r'   r'   r(   rE   H   s    rE   c                   @   r   )AuthHandlerc                 C   s   |  | jjd  d S )NAuthorization)r%   r8   headersr2   r'   r'   r(   r)   S   s   zAuthHandler.getNr*   r'   r'   r'   r(   rL   R   r.   rL   c                   @   r   )CountdownHandlerc                 C   s8   t |}|dkr| | d|d  d S | d d S )Nr   	countdown   Zero)r?   r>   reverse_urlr7   )r&   countr'   r'   r(   r)   X   s   zCountdownHandler.getNr*   r'   r'   r'   r(   rO   W   r.   rO   c                   @   r   )EchoPostHandlerc                 C   s   |  | jj d S Nr6   r2   r'   r'   r(   r3   a   s   zEchoPostHandler.postNr4   r'   r'   r'   r(   rU   `   r.   rU   c                   @   r   )UserAgentHandlerc                 C   s   |  | jjdd d S )N
User-AgentzUser agent not set)r7   r8   rN   r)   r2   r'   r'   r(   r)   f   s   zUserAgentHandler.getNr*   r'   r'   r'   r(   rW   e   r.   rW   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*   )rD   r$   r2   r'   r'   r(   r)   k   s   
zContentLength304Handler.getc                 C   s   d S rV   r'   r2   r'   r'   r(   _clear_representation_headerso   s   z5ContentLength304Handler._clear_representation_headersN)r+   r,   r-   r)   r]   r'   r'   r'   r(   rY   j   s    rY   c                   @   r   )PatchHandlerc                 C   s   |  | jj dS )z=Return the request payload - so we can check it is being keptNr6   r2   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 rV   )r8   methodr7   r2   r'   r'   r(   rb   ~   s   zAllMethodsHandler.methodN)r+   r,   r-   r   SUPPORTED_METHODSrb   r)   headr3   r:   deleteoptionsr_   otherr'   r'   r'   r(   r`   {   s    
$r`   c                   @   r   )SetHeaderHandlerc                 C   s2   t | d| jjd D ]
\}}| || qd S )Nkv)zipget_argumentsr8   	argumentsr$   )r&   ri   rj   r'   r'   r(   r)      s    zSetHeaderHandler.getNr*   r'   r'   r'   r(   rh      r.   rh   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 | dV  qdS )zHello World 
Nr'   ).0ir'   r'   r(   	<genexpr>   s    z)InvalidGzipHandler.get.<locals>.<genexpr>i(#  i;F    )compresslevel    )r$   joinrangerq   compressencoder7   )r&   textr9   r'   r'   r(   r)      s
    zInvalidGzipHandler.get)ro   Nr*   r'   r'   r'   r(   rn      s    rn   c                   @   r   )HeaderEncodingHandlerc                 C   s   |  | jjd d d S )NFoo	ISO8859-1)r%   r8   rN   r}   r2   r'   r'   r(   r)      s   zHeaderEncodingHandler.getNr*   r'   r'   r'   r(   r      r.   r   c                   @   st  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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/d0 Zd1d2 Zd3d4 Zed5d6 Zd7d8 Zed9d: Z ed;d< Z!ed=d> Z"ed?d@ Z#edAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*edOdP Z+dQdR Z,dSS )T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]+)rP   )r   	/echopost/user_agent/304_with_content_length/all_methods/patchz/set_header/invalid_gzip/header-encodingT)rq   )r   r   r   r/   r5   r;   rB   rE   rL   rO   rU   rW   rY   r`   r^   rh   rn   r   r2   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rb   r9      fetchassertEqualcoder9   )r&   r9   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   rN   r9   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   assertFalser9   )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   r9   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skipTestr7   replacecloseconnaddressstreamrequest_datar2   r'   r(   accept_callback   s   

zDHTTPClientCommonTestCase.test_chunked_close.<locals>.accept_callbackhttp://127.0.0.1:%d/s   12)r   r   r   rK   r   add_accept_handlerr   rethrowr   r9   io_loopremove_handlerfilenor&   sockportr   respr'   r2   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   r9   r2   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   r2   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   r9   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   rF   T)r   r   r   raise_error)r   r   assertRaises
ValueErrorr   r   r2   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   
assertTruerN   endswitheffective_urlr9   r   r'   r'   r(   test_follow_redirect9  s   
z-HTTPClientCommonTestCase.test_follow_redirectc                 C   s    | j ddd}| d|j d S )Nr   Tr   rC   r   r   r   r   r'   r'   r(   test_redirect_without_locationC  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   r9   r   r'   r'   r(   test_redirect_put_with_bodyK  s   z4HTTPClientCommonTestCase.test_redirect_put_with_bodyc                 C   r   )Nr   r   Trb   allow_nonstandard_methodss
   Put body: r   r   r'   r'   r(   test_redirect_put_without_bodyQ  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)rC   r   i/  z$/redirect?url=/all_methods&status=%dr       r   s   GET)GETOPTIONSr   DELETETr   )rC   r   HEADrb   r   )i3  i4  z%/redirect?url=/all_methods&status=307)r   r   r   r   r   )r   r   r9   r   assertInr   r   )r&   r<   r   r   rb   r'   r'   r(   test_method_after_redirectZ  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	b64encoder9   )r&   r   r   r'   r'   r(   test_credentials_in_urlx  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)rb   r9   rN   r[   21foo)rb   r9   rN   
user_agent)binasciia2b_hexr   r   rN   r9   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   typer9   bytesrN   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-Encodingrq   )decompress_responserN   rp   r   "   r)modefileobj)
r   r   rN   assertNotEqualr9   lenrq   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..*Malformed HTTP message.*unconsumed gzip dataFr   r   r      s   Hello World 0
)r   r   r   r   r   r9   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 101HTTP/z
:rQ   )
startswithr   splitstriplower)header_lineri   rj   )
first_linerN   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   rN   r&   r'   r(   r     s   zIHTTPClientCommonTestCase.test_header_callback.<locals>.streaming_callbackr   )r   r   rQ   r   zHTTP/[0-9]\.[0-9] 200.*
r   r   )r   r   r  assertRegex)r&   r   r   r'   )r   r  rN   r&   r(   test_header_callback  s   
z-HTTPClientCommonTestCase.test_header_callbackc                    sN   t    fdd}| jd|d   D ]\}}| || k||f qd S )Nc                    s   |  drd S  |  d S )Nr  )r  
parse_line)linerN   r'   r(   r     s   
zTHTTPClientCommonTestCase.test_header_callback_to_parse_line.<locals>.header_callbackr   )r   )r   r   get_allr   r  )r&   r   ri   rj   r'   r&  r(   "test_header_callback_to_parse_line  s   z;HTTPClientCommonTestCase.test_header_callback_to_parse_linec                 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   r9   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   MyUserAgentrX   r   r&  r3  z#response=%r, value=%r, container=%r)r-  r   r   r   r9   )r&   value	containerrN   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   r2   r'   r(   r     s   

zIHTTPClientCommonTestCase.test_multi_line_headers.<locals>.accept_callbackr   zX-XSS-Protectionz1; mode=block)r   r   r   rK   r   r   r   r   r   rN   r   r   r   r   r'   r2   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ärr&  r   )r.  r   r   r   r9   r}   r   r'   r'   r(   test_header_encoding2  s   z-HTTPClientCommonTestCase.test_header_encodingc                 C   s.   |  d}| |jd | |jd d d S )Nr   rZ   r[   42)r   r   r   rN   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   r9   r   r'   r'   r(   test_future_interfaceE  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_errorJ  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_raiseS  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   r8   r   r   
isinstancer   r9   )r&   r   r   	response2r'   r'   r(    test_reuse_request_from_responseZ  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  rA  )r&   r   r8   r   rB  r'   r'   r(   test_bind_source_ipg  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   ra   Tr   s   OTHER)r   r   r9   r   )r&   rb   r   r'   r'   r(   test_all_methodss  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 )NrL  r   T)rb   r   zmust not be Noner   r   rK  r   )rb   r9   r   zmust be Noner   )rb   r9   r   r   )r   r   r   r   r  rA  r   r   )r&   rb   rB  r   r'   r'   r(   test_body_sanity_checks  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   r9   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   rN   r   r   r'   r'   r(   test_non_ascii_header  s   
z.HTTPClientCommonTestCase.test_non_ascii_headerc                 C   s   t   }| d}|  | |j |jd usJ | |jd | |jd | |j |jd us5J | t|j| d |j	
 D ]\}}| d|  koTdk n  d| d|  qEd S )Nr   r   g      ?z
time_info[z] out of bounds: )timer   r   assertIsNotNoner   assertGreaterEqual
assertLess
start_timeabs	time_infoitemsr   )r&   rU  r   ri   rj   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)rZ  r[  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   rG   )r.  r   r   r   cancelr   r   r{   r   rJ   logged_stack)r&   futelru   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:crlfr4  )r   positionr   r   r&  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   r1  r6  r7  r8  r:  r=  rC  rD  rG  rJ  rM  rN  rO  rP  rY  r\  rb  rj  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 )Nrl  r   rH  )r
   r   r-  r   rI  rm  r'   r'   r(   test_default_set  rp  z!RequestProxyTest.test_default_setc                 C   s*   t tdddtdd}| |jd d S )Nrl  r   )
proxy_hostbar)r
   r   r-  r   rr  rm  r'   r'   r(   test_both_set  s   zRequestProxyTest.test_both_setc                 C   s    t tdt }| |j d S Nrl  )r
   r   r-  assertIsNoner   rm  r'   r'   r(   test_neither_set  s   z!RequestProxyTest.test_neither_setc                 C   sF   t tdt }| t |j W d    d S 1 sw   Y  d S ru  )r
   r   r-  r   AttributeErrorr   rm  r'   r'   r(   test_bad_attribute  s   "z#RequestProxyTest.test_bad_attributec                 C   s   t tdd }| |j d S ru  )r
   r   rv  r   rm  r'   r'   r(   test_defaults_none  s   z#RequestProxyTest.test_defaults_noneN)	r+   r,   r-   ro  rq  rt  rw  ry  rz  r'   r'   r'   r(   rk    s    rk  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-   r~  r'   r'   r'   r(   r{    r.   r{  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 rV   )server_iolooprun_syncstartr'   )r  r&   r'   r(   r  3  s   z'SyncHTTPClientTest.setUp.<locals>.start)target)r   r  	threadingEventr   rK   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)ru   r2   r'   r(   	slow_stopF  s
   zCSyncHTTPClientTest.tearDown.<locals>.stop_server.<locals>.slow_stop)r  r  r   rK   r  add_callback)r  r2   r'   r(   stop_server>  s   
z0SyncHTTPClientTest.tearDown.<locals>.stop_serverT)all_fds)r  r  r  rz   r.  r   )r&   r  r'   r2   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   V  s   zSyncHTTPClientTest.get_urlc                 C   s$   | j | d}| d|j d S )Nr  r   r<  r   r'   r'   r(   test_sync_clientY  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   rA  r   )r&   	assertionr'   r'   r(   test_sync_client_error]  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_logf  s    z0SyncHTTPClientSubprocessTest.test_destructor_logN)r+   r,   r-   r  r'   r'   r'   r(   r  e  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 )Nr|  r   rs  r&  )r   r   rN   r&   r8   r'   r'   r(   test_headers  s   z HTTPRequestTestCase.test_headersc                 C   s(   t d}ddi|_| |jddi d S )Nr|  rs  bazr   rN   r   r  r'   r'   r(   test_headers_setter  s   
z'HTTPRequestTestCase.test_headers_setterc                 C   s    t d}d |_| |ji  d S )Nr|  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 )Nr|  r   )r9   )r   r   r9   r   r  r'   r'   r(   	test_body  s   zHTTPRequestTestCase.test_bodyc                 C   s$   t d}d|_| |jtd d S )Nr|  r   )r   r9   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 Nr|  )if_modified_sincezIf-Modified-Since)datetimenowtimezoneutcr   r   rN   r   r&   	http_dater8   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   rN   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    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 )Nrl  r  r  )	r	   r   r   r   r   rA  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  )Lr   r  
contextlibr   r  rq   r  r  ior   r  r  rQ  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   tornado.webr   r   r   tornado.httputilr   r   r   r/   r5   r;   rB   rE   rL   rO   rU   rW   rY   r^   r`   rh   rn   r   r   TestCaserk  r{  r  r  r  r  r'   r'   r'   r(   <module>   sj    
	

    [!
?(