
    	Pj                        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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/m0Z0m1Z1m2Z2 d dl3m4Z4m5Z5  G d de1      Z6 G d de1      Z7 G d de1      Z8 G d de1      Z9 G d de1      Z: G d de1      Z; G d de1      Z< G d de1      Z= G d  d!e1      Z> G d" d#e1      Z? G d$ d%e1      Z@ G d& d'e1      ZA G d( d)e1      ZB G d* d+e1      ZC G d, d-e1      ZD G d. d/e1      ZE G d0 d1e1      ZF G d2 d3e)      ZG G d4 d5ej                        ZI G d6 d7ej                        ZJ G d8 d9ej                        ZK G d: d;ej                        ZL G d< d=ej                        ZM G d> d?ej                        ZNy)@    N)closing)BytesIO)utf8
native_str
to_unicodejson_encodejson_decode)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 Zy)HelloWorldHandlerc                 t    | j                  dd      }| j                  dd       | j                  d|z         y )NnameworldContent-Type
text/plainz	Hello %s!)get_argument
set_headerfinish)selfr#   s     O/root/.hermes/venv/lib/python3.12/site-packages/tornado/test/httpclient_test.pygetzHelloWorldHandler.get%   s3      15K$&'    N__name__
__module____qualname__r,    r-   r+   r!   r!   $   s    (r-   r!   c                       e Zd Zd Zy)PostHandlerc                 n    | j                  d| j                  d      d| j                  d             y )NzPost arg1: arg1z, arg2: arg2)r)   r'   r*   s    r+   postzPostHandler.post,   s/      ($*;*;F*CE	
r-   Nr/   r0   r1   r9   r2   r-   r+   r4   r4   +       
r-   r4   c                       e Zd Zd Zy)
PutHandlerc                 p    | j                  d       | j                  | j                  j                         y )Nz
Put body: writerequestbodyr8   s    r+   putzPutHandler.put4   s$    

< 

4<<$$%r-   N)r/   r0   r1   rC   r2   r-   r+   r=   r=   3       &r-   r=   c                       e Zd Zd Zy)RedirectHandlerc           	          | j                  d       | j                  | j                  d      t        | j                  dd                   y )Nzredirects can have bodies toor   status302)rH   )r@   redirectr'   intr8   s    r+   preparezRedirectHandler.prepare:   sB    

23e$S1B1B8U1S-T 	 	
r-   Nr/   r0   r1   rL   r2   r-   r+   rF   rF   9   r;   r-   rF   c                       e Zd Zd Zy)RedirectWithoutLocationHandlerc                 F    | j                  d       | j                          y )N-  )
set_statusr)   r8   s    r+   rL   z&RedirectWithoutLocationHandler.prepareB   s    r-   NrM   r2   r-   r+   rO   rO   A   s    r-   rO   c                   0    e Zd Zej                  d        Zy)ChunkHandlerc              #      K   | j                  d       | j                          t        j                  d       | j                  d       y w)Nasdf{Gz?qwer)r@   flushr
   sleepr8   s    r+   r,   zChunkHandler.getI   s5     

6

iio

6s   AAN)r/   r0   r1   r
   	coroutiner,   r2   r-   r+   rT   rT   H   s    ]] r-   rT   c                       e Zd Zd Zy)AuthHandlerc                 T    | j                  | j                  j                  d          y )NAuthorization)r)   rA   headersr8   s    r+   r,   zAuthHandler.getS   s    DLL((9:r-   Nr.   r2   r-   r+   r]   r]   R   s    ;r-   r]   c                       e Zd Zd Zy)CountdownHandlerc                     t        |      }|dkD  r%| j                  | j                  d|dz
               y | j                  d       y )Nr   	countdown   Zero)rK   rJ   reverse_urlr@   )r*   counts     r+   r,   zCountdownHandler.getX   s;    E
19MM$**;	BCJJvr-   Nr.   r2   r-   r+   rb   rb   W   s    r-   rb   c                       e Zd Zd Zy)EchoPostHandlerc                 N    | j                  | j                  j                         y Nr?   r8   s    r+   r9   zEchoPostHandler.posta   s    

4<<$$%r-   Nr:   r2   r-   r+   rj   rj   `   s    &r-   rj   c                       e Zd Zd Zy)UserAgentHandlerc                 n    | j                  | j                  j                  j                  dd             y )N
User-AgentzUser agent not set)r@   rA   r`   r,   r8   s    r+   r,   zUserAgentHandler.getf   s&    

4<<''++L:NOPr-   Nr.   r2   r-   r+   rn   rn   e   s    Qr-   rn   c                       e Zd Zd Zd Zy)ContentLength304Handlerc                 J    | j                  d       | j                  dd       y )N0  Content-Length*   )rR   r(   r8   s    r+   r,   zContentLength304Handler.getk   s    ("-r-   c                      y rl   r2   r8   s    r+   _clear_representation_headersz5ContentLength304Handler._clear_representation_headerso   s     	r-   N)r/   r0   r1   r,   rx   r2   r-   r+   rr   rr   j   s    .r-   rr   c                       e Zd Zd Zy)PatchHandlerc                 N    | j                  | j                  j                         y)z=Return the request payload - so we can check it is being keptNr?   r8   s    r+   patchzPatchHandler.patchv   s    

4<<$$%r-   N)r/   r0   r1   r|   r2   r-   r+   rz   rz   u   rD   r-   rz   c                   P    e Zd Zej                  dz   Zd ZexZxZxZxZ	xZ
xZxZZy)AllMethodsHandler)OTHERc                 ~    | j                   j                  J | j                  | j                   j                         y rl   )rA   methodr@   r8   s    r+   r   zAllMethodsHandler.method~   s.    ||""...

4<<&&'r-   N)r/   r0   r1   r   SUPPORTED_METHODSr   r,   headr9   rC   deleteoptionsr|   otherr2   r-   r+   r~   r~   {   sD    &88:E( BHGCG$GGGfGwGr-   r~   c                       e Zd Zd Zy)SetHeaderHandlerc                     t        | j                  d      | j                  j                  d         D ]  \  }}| j	                  ||        y )Nkv)zipget_argumentsrA   	argumentsr(   )r*   r   r   s      r+   r,   zSetHeaderHandler.get   sF     **3/1G1G1LM 	"DAqOOAq!	"r-   Nr.   r2   r-   r+   r   r      s    "r-   r   c                       e Zd ZddZy)InvalidGzipHandlerNc                    | j                  dd       | j                  dd       dj                  d t        d      D              d d }t        j                  |j                         d	
      dz   }| j                  |       y )Nr%   r&   Content-Encodinggzip c              3   (   K   | ]
  }d | d  yw)zHello World 
Nr2   ).0is     r+   	<genexpr>z)InvalidGzipHandler.get.<locals>.<genexpr>   s     AaS+As   i(#  i;F    )compresslevel    )r(   joinranger   compressencoder@   )r*   textrB   s      r+   r,   zInvalidGzipHandler.get   sg    5*F3 wwAU4[AA'6J}}T[[]!<vE

4r-   )returnNr.   r2   r-   r+   r   r      s    	r-   r   c                       e Zd Zd Zy)HeaderEncodingHandlerc                 r    | j                  | j                  j                  d   j                  d             y )NFoo	ISO8859-1)r)   rA   r`   r   r8   s    r+   r,   zHeaderEncodingHandler.get   s(    DLL((/66{CDr-   Nr.   r2   r-   r+   r   r      s    Er-   r   c                       e Zd Zd Zy)EchoHeadersHandlerc                     | j                  t        t        | j                  j                  j                                            y rl   )r@   r   dictrA   r`   get_allr8   s    r+   r,   zEchoHeadersHandler.get   s+    

;tDLL$8$8$@$@$BCDEr-   Nr.   r2   r-   r+   r   r      s    Fr-   r   c                   z    e Zd Zd Z fdZdedefdZd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zed        Zd Zd Z ed        Z!d  Z"ed!        Z#ed"        Z$ed#        Z%ed$        Z&ed%        Z'd& Z(d' Z)d( Z*d) Z+d* Z,d+ Z-ed,        Z.d- Z/d. Z0 xZ1S )/HTTPClientCommonTestCasec                    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	
      t        d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$              gd      S )N/hello/postz/putz	/redirect/redirect_without_location/chunk/authz/countdown/([0-9]+)rd   )r#   	/echopost/user_agent/304_with_content_length/all_methods/patchz/set_header/invalid_gzip/header-encoding/echo_headersT)r   )r   r   r!   r4   r=   rF   rO   rT   r]   rb   rj   rn   rr   r~   rz   r   r   r   r   r8   s    r+   get_appz HTTPClientCommonTestCase.get_app   s    H/0G[)FJ'K102PQHl+G[))+;+NK1M#34.0GHN$56Hl+M#34O%78&(=>O%78#& )
 	
r-   c                     t         |           t               \  }}| j                  j	                  |       || _        y rl   )supersetUpr   http_server
add_socket _HTTPClientCommonTestCase__port2)r*   sockport	__class__s      r+   r   zHTTPClientCommonTestCase.setUp   s5     &'
d##D)r-   pathr   c                 D    | j                          d| j                   | S )Nz://127.0.0.1:)get_protocolr   r*   r   s     r+   get_url2z!HTTPClientCommonTestCase.get_url2   s%    ##%&mDLL>$HHr-   c                     d}| j                  dd|      }| j                  |j                  d       | j                  |j                  |       y )Ns   some patch datar   PATCHr   rB      fetchassertEqualcoderB   )r*   rB   responses      r+   test_patch_receives_payloadz4HTTPClientCommonTestCase.test_patch_receives_payload   sC    !::hwT:B,-r-   c                    | j                  d      }| j                  |j                  d       | j                  |j                  d   d       | j                  |j                  d       |j
                  J | j                  t        |j
                        d       | j                  d      }| j                  |j                  d       y )	Nr   r   r%   r&      Hello world!r   z/hello?name=Bens
   Hello Ben!)r   r   r   r`   rB   request_timerK   r*   r   s     r+   test_hello_worldz)HTTPClientCommonTestCase.test_hello_world   s    ::h',)).9<H8$$000X223Q7::/06r-   c                     g }| j                  d|j                        }| j                  |dg       | j                  |j                         y )Nr   streaming_callbackr   )r   appendr   assertFalserB   )r*   chunksr   s      r+   test_streaming_callbackz0HTTPClientCommonTestCase.test_streaming_callback   sB    ::h6==:I/!23'r-   c                     | j                  ddd      }| j                  |j                  d       | j                  |j                  d       y )Nr   POSTzarg1=foo&arg2=barr   r   s   Post arg1: foo, arg2: barr   r   s     r+   	test_postz"HTTPClientCommonTestCase.test_post   s@    ::gf;N:O,(DEr-   c                     | j                  d      }| j                  |j                  d       g }| j                  d|j                        }| j                  |ddg       | j	                  |j                         y )Nr      asdfqwerr      asdf   qwer)r   r   rB   r   r   )r*   r   r   s      r+   test_chunkedz%HTTPClientCommonTestCase.test_chunked   sf    ::h'4::h6==:I'7!34'r-   c                     t               \  }}t        |      5  t        j                   fd       }t	        j
                  ||        j                  d|z        }|j                           j                  |j                  d        j                  j                  |j                                d d d        y # 1 sw Y   y xY w)Nc              3      K   t        |       }|j                  d      }d|vrj                  d       |j                  dj	                  dd             |j                          y w)N   

   HTTP/1.requires HTTP/1.xs7   HTTP/1.1 200 OK
Transfer-Encoding: chunked

1
1
1
2
0

   
   
r   
read_untilskipTestr@   replacecloseconnaddressstreamrequest_datar*   s       r+   accept_callbackzDHTTPClientCommonTestCase.test_chunked_close.<locals>.accept_callback   sl      "$%+%6%6{%CC\1MM"56ll
 Gw     A(A+http://127.0.0.1:%d/s   12)r   r   r
   r[   r   add_accept_handlerr   rethrowr   rB   io_loopremove_handlerfilenor*   r   r   r   resps   `    r+   test_chunked_closez+HTTPClientCommonTestCase.test_chunked_close   s     &'
dT] 	7]] 0 &&t_=::4t;<DLLNTYY.LL''6?	7 	7 	7s   BB;;Cc                 `    | j                  | j                  ddd      j                  d       y )Nr   Aladdinopen sesameauth_usernameauth_password"   Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==r   r   rB   r8   s    r+   test_basic_authz(HTTPClientCommonTestCase.test_basic_auth  s0    JJy  d1		
r-   c                 b    | j                  | j                  dddd      j                  d       y )Nr   r  r  basic)r
  r  	auth_moder  r  r8   s    r+   test_basic_auth_explicit_modez6HTTPClientCommonTestCase.test_basic_auth_explicit_mode#  s7    JJ'+!	  
 d1	
r-   c                     | j                  | j                  ddd      j                  d       t        j                  dd      }| j                  | j                  d|d      j                  d	       y )
Nr   testu   123£r	  s   Basic dGVzdDoxMjPCow==NFDu   joséu   səcrəts   Basic am9zw6k6c8mZY3LJmXQ=)r   r   rB   unicodedata	normalize)r*   usernames     r+   test_basic_auth_unicodez0HTTPClientCommonTestCase.test_basic_auth_unicode.  sh    JJwfGJLQQ%	
 ((8JJwhjJQVV)	
r-   c           	          t        t        dd      5  | j                  t        t        f      5  | j                  ddddd	       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)
Nzuncaught exceptionFrequiredr   r  r  rV   T)r
  r  r  raise_error)r   r   assertRaises
ValueErrorr   r   r8   s    r+   test_unsupported_auth_modez3HTTPClientCommonTestCase.test_unsupported_auth_mode=  ss     w 4uE 	""J	#:; 

"+"/$ $  	 	 	 	s"   A#AA#A 	A##A,c                    | j                  dd      }| j                  d|j                         | j                  |j                  d   j                  d             | j                  d      }| j                  d|j                         | j                  |j                  j                  d             | j                  d	|j                         y )
Nz/countdown/2Ffollow_redirects.  Locationz/countdown/1r   z/countdown/0s   Zero)r   r   r   
assertTruer`   endswitheffective_urlrB   r   s     r+   test_follow_redirectz-HTTPClientCommonTestCase.test_follow_redirectK  s    ::nu:Ehmm,((4==nMN::n-hmm,..77GH(--0r-   c                 b    | j                  dd      }| j                  d|j                         y )Nr   Tr"  rQ   r   r   r   r   s     r+   test_redirect_without_locationz7HTTPClientCommonTestCase.test_redirect_without_locationU  s+    :::T:R
 	hmm,r-   c                 d    | j                  ddd      }| j                  |j                  d       y )N/redirect?url=/put&status=307PUThellor      Put body: hellor   r   rB   r   s     r+   test_redirect_put_with_bodyz4HTTPClientCommonTestCase.test_redirect_put_with_body]  s3    ::+E  
 	(:;r-   c                 d    | j                  ddd      }| j                  |j                  d       y )Nr.  r/  Tr   allow_nonstandard_methodss
   Put body: r2  r   s     r+   test_redirect_put_without_bodyz7HTTPClientCommonTestCase.test_redirect_put_without_bodyc  s4    ::+&*  

 	6r-   c                    dD ]  }d|z  }| j                  |dd      }| j                  d|j                         dD ]g  }| j                  ||d	      }|d
v r&| j                  t        |      |j                         A| j	                  |j                  t        |      dg       i | j                  |d      }| j                  d|j
                         | j                  d|j                          dD ]  }d}dD ];  }| j                  ||d	      }| j                  |t        |j                               = | j                  |d      }| j                  d|j
                         | j                  d|j                          y )N)rQ   r$  i/  z$/redirect?url=/all_methods&status=%dr   r-   r   s   GET)GETOPTIONSr/  DELETETr5  )rQ   r$  HEADr   r   )i3  i4  z%/redirect?url=/all_methods&status=307)r9  r:  r   r/  r;  )r   r   rB   r   assertInr   r   )r*   rH   r   r  r   s        r+   test_method_after_redirectz3HTTPClientCommonTestCase.test_method_after_redirectl  ss   % 	-F86AC::c&s:;DVTYY/ > Ezz#fPTzUZ'$$T&\499=MM$))d6lF-CDE ::c&:1DS$)),S$)),!	-& ! 	-F9CE @zz#fPTzU  DII)>?@ ::c&:1DS$)),S$)),	-r-   c                     | j                  d      j                  dd      }| j                  |      }| j                  dt	        j
                  d      z   |j                         y )Nr   http://zhttp://me:secret@s   Basic s	   me:secret)get_urlr   r   r   base64	b64encoderB   )r*   r   r   s      r+   test_credentials_in_urlz0HTTPClientCommonTestCase.test_credentials_in_url  sO    ll7#++I7JK::c?V%5%5l%CCX]]Sr-   c                 2   d}t        j                  d      }| j                  dd|ddi      }| j                  |j                  d   d	       | j                  |j
                  t        |             | j                  dd|ddi      }| j                  |j                  d   d
       | j                  |j
                  |       | j                  dd|ddid      }| j                  |j                  d   d
       | j                  |j
                  |       y )N   és   e9r   r   r%   zapplication/blah)r   rB   r`   ru   21foo)r   rB   r`   
user_agent)binasciia2b_hexr   r   r`   rB   r   )r*   unicode_body	byte_bodyr   s       r+   test_body_encodingz+HTTPClientCommonTestCase.test_body_encoding  s*   $$U+	 ::#%78	  
 	))*:;SA\(:; ::#%78	  
 	))*:;SA	2 ::#%78  
 	))*:;SA	2r-   c                 t   | j                  d      }| j                  t        |j                        t               | j                  t        |j
                  d         t               | j                  t        |j                        t               | j                  t        |j                        t               y )Nr   r%   )
r   r   typerB   bytesr`   strr   rK   r(  r   s     r+   
test_typesz#HTTPClientCommonTestCase.test_types  s}    ::h'hmm,e4h..~>?Ehmm,c2h445s;r-   c                 r   | j                  ddddi      }| j                  |j                  d   d       | j                  |j                  d       | j                  t        |j                        d       t        j                  d	|j                  
      }| j                  |j                         d       y )Nr   FzAccept-Encodingr   )decompress_responser`   r   r   "   r)modefileobj)
r   r   r`   assertNotEqualrB   lenr   GzipFilebufferread)r*   r   fs      r+   	test_gzipz"HTTPClientCommonTestCase.test_gzip  s     ::%:KV9T  
 	))*<=vFHMM;7 	X]]+R0MMsHOO<;/r-   c                 
   t        t        dd      5  	 | j                  d      }| j                  |j                  d       | j                  |j
                  d d d       d d d        y # t        $ r Y w xY w# 1 sw Y   y xY w)Nz..*Malformed HTTP message.*unconsumed gzip dataFr  r   r      s   Hello World 0
)r   r   r   r   r   rB   r   r   s     r+   test_invalid_gzipz*HTTPClientCommonTestCase.test_invalid_gzip  s     EPU
 	::o6  4  s!35GH	 	  	 	s)   A9AA**	A63A95A66A99Bc                      g i g fd} fd} j                  d||        j                  t              d        j                  d   d        j                  dd	g       y )
Nc                     | j                  d      ry | j                  d      rj                  |        y | dk7  r7| j                  dd      \  }}|j                         |j	                         <   y y )NzHTTP/1.1 101HTTP/z
:re   )
startswithr   splitstriplower)header_liner   r   
first_liner`   s      r+   header_callbackzFHTTPClientCommonTestCase.test_header_callback.<locals>.header_callback  si    %%n5''0!!+.&"((a01%&WWY	" 'r-   c                 R    j                  d   d       j                  |        y )Nzcontent-typeztext/html; charset=UTF-8)r   r   )chunkr   r`   r*   s    r+   r   zIHTTPClientCommonTestCase.test_header_callback.<locals>.streaming_callback  s'     W^46PQMM% r-   r   )rp  r   re   r   zHTTP/[0-9]\.[0-9] 200.*
r   r   )r   r   r]  assertRegex)r*   rp  r   r   ro  r`   s   `  @@@r+   test_header_callbackz-HTTPClientCommonTestCase.test_header_callback  sz    
	/	! 	

+1 	 	

 	Z!Z8A(FG'7!34r-   c                     t               fd}| j                  d|       j                         D ]*  \  }}| j                  ||j	                         k(  ||f       , y )Nc                 L    | j                  d      ry j                  |        y )Nrh  )rj  
parse_line)liner`   s    r+   rp  zTHTTPClientCommonTestCase.test_header_callback_to_parse_line.<locals>.header_callback  s     w't$r-   r   )rp  )r   r   r   r&  rl  )r*   rp  r   r   r`   s       @r+   "test_header_callback_to_parse_linez;HTTPClientCommonTestCase.test_header_callback_to_parse_line  sY    
 -	% 	

8_
=OO% 	4DAqOOANQF3	4r-   c              #   (  K   t        dd      }| j                  j                  d|      }	 |j                  | j	                  d            }| j                  |j                  d       |j                          y # |j                          w xY ww)NTestDefaultUserAgentF)rK  
allow_ipv6T)force_instancedefaultsr   s   TestDefaultUserAgent)r   http_clientr   r   rB  r   rB   r   )r*   r~  clientr   s       r+   test_configure_defaultsz0HTTPClientCommonTestCase.test_configure_defaults  st     #9eL!!++4(+S	#\\$,,}*EFFHX]],CDLLNFLLNs   +B>A= ,B=BBc                     dD ]b  }t         t        fD ]Q  } |       }||d<   | j                  d|      }| j                  |j                  dd|j                  d|d|       S d y )	N)MyUserAgent   MyUserAgentrp   r   r`   r  z	response=z, value=z, container=)r   r   r   r   rB   )r*   value	containerr`   r  s        r+   test_header_typesz*HTTPClientCommonTestCase.test_header_types  sr    
 5 
	E"K0 		#+(-%zz-zA  II"yy%4		
	r-   c                     t               \  }}t        |      5  t        j                   fd       }t	        j
                  ||       	  j                  d|z        }|j                           j                  |j                  d   d        j                  j                  |j                                	 d d d        y #  j                  j                  |j                                w xY w# 1 sw Y   y xY w)Nc              3      K   t        |       }|j                  d      }d|vrj                  d       |j                  dj	                  dd             |j                          y w)Nr   r   r   s2   HTTP/1.1 200 OK
X-XSS-Protection: 1;
	mode=block

r   r   r   r   s       r+   r   zIHTTPClientCommonTestCase.test_multi_line_headers.<locals>.accept_callback*  sj     !$%+%6%6{%CC\1MM"56ll
 Gw	 	 r   r   zX-XSS-Protectionz1; mode=block)r   r   r
   r[   r   r   r   r   r   r`   r   r  r  r  s   `    r+   test_multi_line_headersz0HTTPClientCommonTestCase.test_multi_line_headers$  s     &'
dT] 	;]] " &&t_=;zz"84"?@  .@!A?S++DKKM:5	; 	;4 ++DKKM:5	; 	;s$   /C.
AC )C. +C++C..C7c              #      K   | j                   j                  | j                  d      ddi      }| j                  |j                  dj                  d             y w)Nr   r   u   bärr  r   )r  r   rB  r   rB   r   r   s     r+   test_header_encodingz-HTTPClientCommonTestCase.test_header_encodingD  s\     ))//LL+,x 0 
 
 	(DEs   AAc                     | j                  d      }| j                  |j                  d       | j                  |j                  d   d       y )Nr   rt   ru   42)r   r   r   r`   r   s     r+   test_304_with_content_lengthz5HTTPClientCommonTestCase.test_304_with_content_lengthN  sC    
 ::89,))*:;TBr-   c              #      K   | j                   j                  | j                  d            }| j                  |j                  d       y wNr   r   r  r   rB  r   rB   r   s     r+   test_future_interfacez.HTTPClientCommonTestCase.test_future_interfaceW  s<     ))//X0FGG8s   A
Ac              #     K   | j                  t              5 }| j                  j                  | j	                  d             d d d        j
                  J |j
                  j                  J | j                  |j
                  j                  d       | j                  |j
                  j                  j                  d       y # 1 sw Y   xY wwN	/notfound  )	r  r   r  r   rB  	exceptionr   r   r   )r*   contexts     r+   test_future_http_errorz/HTTPClientCommonTestCase.test_future_http_error\  s     y) 	DW""((k)BCC	D  ,,,  ))555**//5**3388#>	D 	Ds   C-C
BC
CCc              #      K   | j                   j                  | j                  d      d      }| j                  |j                  d       y w)Nr  F)r  r  )r  r   rB  r   r   r   s     r+   test_future_http_error_no_raisez8HTTPClientCommonTestCase.test_future_http_error_no_raisee  sJ     ))//LL%5 0 
 
 	,s   AAc              #     K   | j                  d      }| j                  j                  |      }| j                  |j                  j
                  |       | j                  t        |j                  t                     | j                  j                  |j                        }| j                  |j                  d       y wr  )
rB  r  r   r   rA   r   r&  
isinstancer   rB   )r*   r   r   	response2s       r+    test_reuse_request_from_responsez9HTTPClientCommonTestCase.test_reuse_request_from_responsel  s      ll8$))//44))--s3
8#3#3[AB**001A1ABB	9s   CCc              #     K   | j                  d      }t        |d      }| j                  j                  |      }| j	                  |j
                  d       | j                  t        t        f      5 }t        |d      }| j                  j                  |       d d d        | j                  dt        j                        v xs dt        |j                        v        y # 1 sw Y   IxY ww)Nr   z	127.0.0.1network_interfacer   znot-interface-or-ipz#Failed binding local connection end)rB  r   r  r   r   r   r  r  r   r&  rT  r  )r*   r   rA   r   r  s        r+   test_bind_source_ipz,HTTPClientCommonTestCase.test_bind_source_ipy  s     ll8$c[A))//88,
I67 	27!#9NOG""((11	2 	!S):):%;; O4G<M<M8NN	
	2 	2s   A3C45+C( AC4(C1-C4c                    dD ]:  }| j                  d|      }| j                  |j                  t        |             < dD ];  }| j                  d|d      }| j                  |j                  t        |             = | j                  dd      }| j                  |j                  d       | j                  ddd	
      }| j                  |j                  d       y )Nr9  r;  r:  r   r=  r   r/  r   r-   r   r<  r   Tr5  s   OTHER)r   r   rB   r   )r*   r   r   s      r+   test_all_methodsz)HTTPClientCommonTestCase.test_all_methods  s    2 	:Fzz.z@HX]]DL9	: / 	:Fzz.czJHX]]DL9	: ::nV:<,::7d  
 	1r-   c                 ~   dD ]  }| j                  t              5 }| j                  d|d       d d d        | j                  dt	        j
                               | j                  d|d      }| j                  |j                  d        dD ]  }| j                  t              5 }| j                  d|d	d
       d d d        | j                  dt	        j
                               |dk7  sa| j                  d|d	dd       | j                  j                  d        y # 1 sw Y   xY w# 1 sw Y   uxY w)Nr  r   T)r   r  zmust not be Noner5  r   r  r   )r   rB   r  zmust be Noner9  )r   rB   r6  r  )r  r  r   r>  rT  r  r   r   )r*   r   r  r  s       r+   test_body_sanity_checksz0HTTPClientCommonTestCase.test_body_sanity_checks  s@   . 	-F"":. L'

>&d
KLMM,c'2C2C.DE::v  D TYY,	- 3 	1F"":. '

"6T   MM.#g.?.?*@A 

"! .2 $     C0#	1L L s   D')D3'D0	3D<	c                     | j                  ddd      }|j                          | j                  |j                  d       y )Nz/redirect?status=307&url=/putr/  s   hellor   r1  )r   r   r   rB   r   s     r+   test_put_307z%HTTPClientCommonTestCase.test_put_307  s?    ::+E  
 	(:;r-   c                     | j                  d      }|j                          | j                  |j                  d   t	        d             y )Nz/set_header?k=foo&v=%E9r   rG  )r   r   r   r`   r   r   s     r+   test_non_ascii_headerz.HTTPClientCommonTestCase.test_non_ascii_header  s=    ::78))%0*X2FGr-   c                 b   t        j                          }| j                  d      }|j                          | j                  |j                         |j                  J | j                  |j                  d       | j                  |j                  d       | j                  |j                         |j                  J | j                  t        |j                  |z
        d       |j                  j                         D ]+  \  }}| j                  d|cxk  xr dk  nc d| d|        - y )Nr   r   g      ?z
time_info[z] out of bounds: )timer   r   assertIsNotNoner   assertGreaterEqual
assertLess
start_timeabs	time_infoitemsr&  )r*   r  r   r   r   s        r+   test_response_timesz,HTTPClientCommonTestCase.test_response_times  s    YY[
::h'X223$$000 5 5q9--s3 	X001""...H//*<=sC&&,,. 	PDAqOOALSLJqc9J1#*NO	Pr-   c                     | j                  dd      }| j                  |j                  d       | j                  dd      }| j                  |j                  d       | j                  ddd      }| j                  |j                  d       y )Nr   r   )connect_timeoutr   )request_timeout)r  r  r+  r   s     r+   test_zero_timeoutz*HTTPClientCommonTestCase.test_zero_timeout  sv    ::h::,::h::,::h1:M,r-   c              #   R  K   | j                   j                  | j                  d            }| j                  |j	                                t        t        d      5 }t        d      D ]'  }t        j                  d       |j                  s' n d d d        y # 1 sw Y   y xY ww)Nz/404z$Exception after Future was cancelledd   rW   )r  r   rB  r&  cancelr   r   r   r
   rZ   logged_stack)r*   futelr   s       r+   test_error_after_cancelz0HTTPClientCommonTestCase.test_error_after_cancel  s     $$T\\&%9:

%w FG 	2
 3Z iio%??	 	 	s$   AB'2BB	B'B$ B'c                    dD ]  \  }}| j                  |d      5  | j                  t              5  | j                  dd|i       d d d        d d d        | j                  |d      5  | j                  t              5  | j                  d|di       d d d        d d d         y # 1 sw Y   cxY w# 1 sw Y   gxY w# 1 sw Y   ,xY w# 1 sw Y   xY w)N))zfoobar:cr)zfoo
bar:lf)z	foo
bar:crlfr  )r#   positionr   rJ  r  key)subTestr  r  r   )r*   headerr#   s      r+   test_header_crlfz)HTTPClientCommonTestCase.test_header_crlf  s    
 
	BLFD
 4': B&&z2 BJJx%JABB 4%8 B&&z2 BJJx&%JABB B
	BB BB BB BB BsF   B6B*B6+CCC*B3/B66B?	CCC	c           	         dt        ddi      dfdt               dfdt        d	d
      dfdt        ddi      dfg}|D ]  \  }}}| j                  |d      5  | j                  d| j                  d      z        }|r |j                  dd|z        } | j
                  di t        |      |z  }|j                          t        |j                        }| j                  d|       | j                  d|       | j                  d|       d d d        | j                  |d      5  | j                  d| j                  d      z        }|r |j                  dd|z        } | j
                  di t        |      |z  }|j                          t        |j                        }| j                  d|       | j                  d|v xs d|v        d d d         y # 1 sw Y   xY w# 1 sw Y   xY w)Nzmanual auth headerr_   secretr  r   zcredentials in URLz	me:secretzauth parametersmer	  zmanual cookie headerCookie	different)r#   originz/redirect?url=%s&status=302r   rA  z
http://%s@)r   rp   samer2   )r   r  rB  r   r   r   r   r	   rB   r>  assertNotInr&  )r*   
test_casesr#   kwargs	url_credsr   r   echoed_headerss           r+   test_strip_headers_on_redirectz7HTTPClientCommonTestCase.test_strip_headers_on_redirect  s    "4(0K#LbQ!46;74x PRTU#T8X2F%GL	3

 (2 	#D&)4< ;ll1DMM/4RR %#++i	1IJC%4::@#(?@  "!,X]]!;lN;  .A  >:; 47 ll1DLL4QQ %#++i	1IJC%4::@#(?@  "!,X]]!;lN; #~5S^9S 	; ; s   B6G#,B+G/#G,	/G9	)2r/   r0   r1   r   r   rT  r   r   r   r   r   r   r  r  r  r  r   r)  r,  r3  r7  r?  rE  rP  rU  rb  re  rt  ry  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __classcell__)r   s   @r+   r   r      st   
0IS IS I.	7(F
(#7J
	

1-<7-<T
"3H<0"5>4"  ";@ F FC 9 9 ? ? - - 
: 
: 
 
21`<HP(-  B 'r-   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)RequestProxyTestc                 z    t        t        dd      t                     }| j                  |j                  d       y )Nhttp://example.com/rJ  )rK  )r   r   r   r   rK  r*   proxys     r+   test_request_setz!RequestProxyTest.test_request_set:  s3    -%@$&
 	))51r-   c                 z    t        t        d      t        d            }| j                  |j                  d       y )Nr  rJ  r  )r   r   r   r   r  r  s     r+   test_default_setz!RequestProxyTest.test_default_set@  s4    -.u0M
 	00%8r-   c                 ~    t        t        dd      t        d            }| j                  |j                  d       y )Nr  rJ  )
proxy_hostbar)r   r   r   r   r  r  s     r+   test_both_setzRequestProxyTest.test_both_setF  s7    -%@$RWBX
 	))51r-   c                 t    t        t        d      t                     }| j                  |j                         y Nr  )r   r   r   assertIsNoner
  r  s     r+   test_neither_setz!RequestProxyTest.test_neither_setL  s*    k*?@$&I%--.r-   c                     t        t        d      t                     }| j                  t              5  |j
                   d d d        y # 1 sw Y   y xY wr  )r   r   r   r  AttributeErrorrJ  r  s     r+   test_bad_attributez#RequestProxyTest.test_bad_attributeP  s@    k*?@$&I~. 	II	 	 	s   A		Ac                 d    t        t        d      d       }| j                  |j                         y r  )r   r   r  r
  r  s     r+   test_defaults_nonez#RequestProxyTest.test_defaults_noneU  s(    k*?@$G%--.r-   N)	r/   r0   r1   r  r  r  r  r  r  r2   r-   r+   r  r  9  s     292/
/r-   r  c                       e Zd Zd Zy)HTTPResponseTestCasec                     t        t        d      dt                     }t        |      }| j	                  |j                  d             | j                  d|       y )Nhttp://example.comr   )r_  zHTTPResponse(zcode=200)r   r   r   rT  r&  rj  r>  )r*   r   ss      r+   test_strzHTTPResponseTestCase.test_str[  sI    ,-s79
 M_56j!$r-   N)r/   r0   r1   r  r2   r-   r+   r  r  Z  s    %r-   r  c                   *    e Zd Zd Zd Zd Zd Zd Zy)SyncHTTPClientTestc                 :    t        d       _        t        j                         t        j
                   fd        fd}t        j                  |       _         j                  j                          j                          t                _        y )NF)make_currentc                      t               \  } _        t        dt        fg      }t	        |      _        j
                  j                  |        j                          y )N/)r   r   r   r!   r   serverr   set)r   appeventr*   s     r+   init_serverz-SyncHTTPClientTest.setUp.<locals>.init_serveri  sJ    .0OD$)%6789C$S/DKKK""4(IIKr-   c                  p    j                   j                          j                   j                          y rl   )server_iolooprun_syncstart)r  r*   s   r+   r  z'SyncHTTPClientTest.setUp.<locals>.startq  s)    ''4$$&r-   )target)r   r  	threadingEventr
   r[   Threadserver_threadr  waitr   r  )r*   r  r  r  s   ` @@r+   r   zSyncHTTPClientTest.setUpe  sq    #7!		 
		' '--U;  "

%<r-   c                       fd} j                   j                  |        j                  j                           j                  j                           j                   j                  d       y )Nc                      j                   j                          t        j                  fd       } j                  j                  |        y )Nc               3      K   j                   j                          t        d      D ]  } d   j                  j	                          y w)N   )r  close_all_connectionsr   r  stop)r   r*   s    r+   	slow_stopzCSyncHTTPClientTest.tearDown.<locals>.stop_server.<locals>.slow_stop  sD     kk7799 q A""'')s   AA)r  r  r
   r[   r  add_callback)r  r*   s    r+   stop_serverz0SyncHTTPClientTest.tearDown.<locals>.stop_server|  sA    KK ]]* * ++I6r-   T)all_fds)r  r  r  r   r  r   )r*   r  s   ` r+   tearDownzSyncHTTPClientTest.tearDown{  sY    	7& 	''4!    .r-   c                 $    d| j                   |fz  S )Nzhttp://127.0.0.1:%d%s)r   r   s     r+   rB  zSyncHTTPClientTest.get_url  s    &$))T):::r-   c                     | j                   j                  | j                  d            }| j                  d|j                         y )Nr  r   r  r   s     r+   test_sync_clientz#SyncHTTPClientTest.test_sync_client  s5    ##))$,,s*;<(--8r-   c                     | j                  t              5 }| j                  j                  | j	                  d             d d d        | j                  j                  j                  d       y # 1 sw Y   0xY wr  )r  r   r  r   rB  r   r  r   )r*   	assertions     r+   test_sync_client_errorz)SyncHTTPClientTest.test_sync_client_error  sb     y) 	>Y""4<<#<=	>,,1137	> 	>s   +A00A9N)r/   r0   r1   r   r  rB  r  r  r2   r-   r+   r  r  d  s    (,/2;98r-   r  c                       e Zd Zd Zy)SyncHTTPClientSubprocessTestc                 8   t        j                  t        j                  ddgt         j                  t         j
                  dd      }|j                  r)t        d       t        t        |j                               |j                  r| j                  d       y y )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*   procs     r+   test_destructor_logz0SyncHTTPClientSubprocessTest.test_destructor_log  sw     ~~_
 ??$$

 ;;)*T[[)*;;II=> r-   N)r/   r0   r1   r/  r2   r-   r+   r  r    s    ?r-   r  c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	HTTPRequestTestCasec                 ^    t        dddi      }| j                  |j                  ddi       y )Nr  rJ  r  r  )r   r   r`   r*   rA   s     r+   test_headersz HTTPRequestTestCase.test_headers  s+    2UENK5%.9r-   c                 h    t        d      }ddi|_        | j                  |j                  ddi       y )Nr  r  bazr   r`   r   r3  s     r+   test_headers_setterz'HTTPRequestTestCase.test_headers_setter  s0    23 %.5%.9r-   c                 `    t        d      }d |_        | j                  |j                  i        y )Nr  r7  r3  s     r+   test_null_headers_setterz,HTTPRequestTestCase.test_null_headers_setter  s(    23"-r-   c                 h    t        dd      }| j                  |j                  t        d             y )Nr  rJ  )rB   )r   r   rB   r   r3  s     r+   	test_bodyzHTTPRequestTestCase.test_body  s'    2?tE{3r-   c                 r    t        d      }d|_        | j                  |j                  t        d             y )Nr  rJ  )r   rB   r   r   r3  s     r+   test_body_setterz$HTTPRequestTestCase.test_body_setter  s,    23tE{3r-   c                     t         j                   j                  t         j                  j                        }t	        d|      }| j                  |j                  dt        |      i       y Nr  )if_modified_sincezIf-Modified-Since)datetimenowtimezoneutcr   r   r`   r   r*   	http_daterA   s      r+   test_if_modified_sincez*HTTPRequestTestCase.test_if_modified_since  sT    %%))(*;*;*?*?@	2iPOO13CI3NO	
r-   c                     t               5  t        j                  j                         }d d d        t        d      }| j	                  |j
                  dt        |      i       y # 1 sw Y   >xY wr@  )r   rB  utcnowr   r   r`   r   rF  s      r+   'test_if_modified_since_naive_deprecatedz;HTTPRequestTestCase.test_if_modified_since_naive_deprecated  sa    ! 	3 ))002I	32iPOO13CI3NO	
	3 	3s   A''A0N)
r/   r0   r1   r4  r8  r:  r<  r>  rH  rK  r2   r-   r+   r1  r1    s%    ::
.
44


r-   r1  c                       e Zd Zd Zd Zd Zy)HTTPErrorTestCasec                     t        d      }t        j                  |      }| j                  ||       | j                  |j                  |j                         y )N  )r   copyassertIsNotr   r   )r*   ee2s      r+   	test_copyzHTTPErrorTestCase.test_copy  s@    cNYYq\B)r-   c                     t        d      }| j                  t        |      d       | j                  t        |      d       y )NrO  HTTP 403: Forbidden)r   r   rT  repr)r*   rR  s     r+   test_plain_errorz"HTTPErrorTestCase.test_plain_error  s5    cNQ!67a"78r-   c                 &   t        t        d      d      }| j                  t              5 }|j	                          d d d        j
                  }| j                  t        |      d       | j                  t        |      d       y # 1 sw Y   LxY w)Nr  rO  rV  )	r   r   r  r   r   r  r   rT  rW  )r*   r  cmrR  s       r+   test_error_with_responsez*HTTPErrorTestCase.test_error_with_response  su    K(=>Dy) 	RLLN	LLQ!67a"78		 	s   BBN)r/   r0   r1   rT  rX  r[  r2   r-   r+   rM  rM    s    *9
9r-   rM  )OrC  rL  
contextlibr   rP  r   r  rB  ior   r&  r(  r  typingr  unittesttornado.escaper   r   r   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!   r4   r=   rF   rO   rT   r]   rb   rj   rn   rr   rz   r~   r   r   r   r   r   TestCaser  r  r  r  r1  rM  r2   r-   r+   <module>rl     s            
     Q Q   * ! % (  T T 0 8 8 :( (
. 
& &
n 
^ > ;. ;
~ &n &
Q~ Q
n &> &H H"~ "
 
EN E
F FM
0 M
`/x(( /B%8,, %<8** <8~?8#4#4 ?<%
(++ %
P9)) 9r-   