
    g                        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e0      Z5 G d de0      Z6 G d de0      Z7 G d de0      Z8 G d de0      Z9 G d de0      Z: G d de0      Z; G d de0      Z< G d  d!e0      Z= G d" d#e0      Z> G d$ d%e0      Z? G d& d'e0      Z@ G d( d)e0      ZA G d* d+e0      ZB G d, d-e0      ZC G d. d/e0      ZD G d0 d1e'      ZE G d2 d3ej                        ZG G d4 d5ej                        ZH G d6 d7ej                        ZI G d8 d9ej                        ZJ G d: d;ej                        ZK G d< d=ej                        ZLy)>    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 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     Q/var/www/openai/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.   r/   r0   r8   r1   r,   r*   r3   r3   +       
r,   r3   c                       e Zd Zd Zy)
PutHandlerc                 p    | j                  d       | j                  | j                  j                         y )Nz
Put body: writerequestbodyr7   s    r*   putzPutHandler.put4   s$    

< 

4<<$$%r,   N)r.   r/   r0   rB   r1   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)rG   )r?   redirectr&   intr7   s    r*   preparezRedirectHandler.prepare:   sB    

23e$S1B1B8U1S-T 	 	
r,   Nr.   r/   r0   rK   r1   r,   r*   rE   rE   9   r:   r,   rE   c                       e Zd Zd Zy)RedirectWithoutLocationHandlerc                 F    | j                  d       | j                          y )N-  )
set_statusr(   r7   s    r*   rK   z&RedirectWithoutLocationHandler.prepareB   s    r,   NrL   r1   r,   r*   rN   rN   A   s    r,   rN   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   sleepr7   s    r*   r+   zChunkHandler.getI   s5     

6

iio

6s   AAN)r.   r/   r0   r   	coroutiner+   r1   r,   r*   rS   rS   H   s    ]] r,   rS   c                       e Zd Zd Zy)AuthHandlerc                 T    | j                  | j                  j                  d          y )NAuthorization)r(   r@   headersr7   s    r*   r+   zAuthHandler.getS   s    DLL((9:r,   Nr-   r1   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)rJ   rI   reverse_urlr?   )r)   counts     r*   r+   zCountdownHandler.getX   s;    E
19MM$**;	BCJJvr,   Nr-   r1   r,   r*   ra   ra   W   s    r,   ra   c                       e Zd Zd Zy)EchoPostHandlerc                 N    | j                  | j                  j                         y Nr>   r7   s    r*   r8   zEchoPostHandler.posta   s    

4<<$$%r,   Nr9   r1   r,   r*   ri   ri   `   s    &r,   ri   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?   r@   r_   r+   r7   s    r*   r+   zUserAgentHandler.getf   s&    

4<<''++L:NOPr,   Nr-   r1   r,   r*   rm   rm   e   s    Qr,   rm   c                       e Zd Zd Zd Zy)ContentLength304Handlerc                 J    | j                  d       | j                  dd       y )N0  Content-Length*   )rQ   r'   r7   s    r*   r+   zContentLength304Handler.getk   s    ("-r,   c                      y rk   r1   r7   s    r*   _clear_representation_headersz5ContentLength304Handler._clear_representation_headerso   s     	r,   N)r.   r/   r0   r+   rw   r1   r,   r*   rq   rq   j   s    .r,   rq   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>   r7   s    r*   patchzPatchHandler.patchv   s    

4<<$$%r,   N)r.   r/   r0   r{   r1   r,   r*   ry   ry   u   rC   r,   ry   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 rk   )r@   methodr?   r7   s    r*   r   zAllMethodsHandler.method~   s.    ||""...

4<<&&'r,   N)r.   r/   r0   r   SUPPORTED_METHODSr   r+   headr8   rB   deleteoptionsr{   otherr1   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_argumentsr@   	argumentsr'   )r)   r   r   s      r*   r+   zSetHeaderHandler.get   sB     **3/1G1G1LMDAqOOAq! Nr,   Nr-   r1   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 j                  |        yw)zHello World {}
N)format).0is     r*   	<genexpr>z)InvalidGzipHandler.get.<locals>.<genexpr>   s     I[)003[s   i(#  i;F    )compresslevel    )r'   joinranger   compressencoder?   )r)   textrA   s      r*   r+   zInvalidGzipHandler.get   sg    5*F3 wwIU4[II'6R}}T[[]!<vE

4r,   )returnNr-   r1   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(   r@   r_   r   r7   s    r*   r+   zHeaderEncodingHandler.get   s(    DLL((/66{CDr,   Nr-   r1   r,   r*   r   r      s    Er,   r   c                   V   e Zd Zd Zd Ze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,y))*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"              gd      S )N/hello/postz/putz	/redirect/redirect_without_location/chunk/authz/countdown/([0-9]+)rc   )r"   	/echopost/user_agent/304_with_content_length/all_methods/patchz/set_header/invalid_gzip/header-encodingT)r   )r   r   r    r3   r<   rE   rN   rS   r\   ra   ri   rm   rq   r}   ry   r   r   r   r7   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&(=>!$ '
 	
r,   c                     d}| j                  dd|      }| j                  |j                  d       | j                  |j                  |       y )Ns   some patch datar   PATCHr   rA      fetchassertEqualcoderA   )r)   rA   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_   rA   request_timerJ   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   assertFalserA   )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   rA   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   rZ   r   add_accept_handlerr   rethrowr   rA   io_loopremove_handlerfilenor)   sockportr   resps   `    r*   test_chunked_closez+HTTPClientCommonTestCase.test_chunked_close   s     &'
dT]]] 0 &&t_=::4t;<DLLNTYY.LL''6? ]]s   BB;;Cc                 `    | j                  | j                  ddd      j                  d       y )Nr   Aladdinopen sesameauth_usernameauth_password"   Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==r   r   rA   r7   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   r7   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   rA   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   rU   T)r   r   r   raise_error)r   r   assertRaises
ValueErrorr   r   r7   s    r*   test_unsupported_auth_modez3HTTPClientCommonTestCase.test_unsupported_auth_mode,  sc     w 4uE""J	#:;

"+"/$ $   < FE;; FEs"   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_urlrA   r   s     r*   test_follow_redirectz-HTTPClientCommonTestCase.test_follow_redirect:  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  rP   r   r   r   r   s     r*   test_redirect_without_locationz7HTTPClientCommonTestCase.test_redirect_without_locationD  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   rA   r   s     r*   test_redirect_put_with_bodyz4HTTPClientCommonTestCase.test_redirect_put_with_bodyL  s3    ::+E  
 	(:;r,   c                 d    | j                  ddd      }| j                  |j                  d       y )Nr  r  Tr   allow_nonstandard_methodss
   Put body: r  r   s     r*   test_redirect_put_without_bodyz7HTTPClientCommonTestCase.test_redirect_put_without_bodyR  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)rP   r  i/  z$/redirect?url=/all_methods&status=%dr   r,   r   s   GET)GETOPTIONSr  DELETETr"  )rP   r  HEADr   r   )i3  i4  z%/redirect?url=/all_methods&status=307)r&  r'  r   r  r(  )r   r   rA   r   assertInr   r   )r)   rG   r   r   r   s        r*   test_method_after_redirectz3HTTPClientCommonTestCase.test_method_after_redirect[  s\   %F86AC::c&s:;DVTYY/ >zz#fPTzUZ'$$T&\499=MM$))d6lF-CD > ::c&:1DS$)),S$)),! && !F9CEzz#fPTzU  DII)>? F ::c&:1DS$)),S$)), !r,   c                     | j                  d      j                  dd      }| j                  |      }| j                  dt	        j
                  d      z   |j                         y )Nr   zhttp://zhttp://me:secret@s   Basic s	   me:secret)get_urlr   r   r   base64	b64encoderA   )r)   r   r   s      r*   test_credentials_in_urlz0HTTPClientCommonTestCase.test_credentials_in_urly  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   rA   r_   rt   21foo)r   rA   r_   
user_agent)binasciia2b_hexr   r   r_   rA   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   typerA   bytesr_   strr   rJ   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_   assertNotEqualrA   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*(Uncaught exception|Exception in callback)Fr  r   r      s   Hello World 0
)r   r   r   r   r   rA   r   r   s     r*   test_invalid_gzipz*HTTPClientCommonTestCase.test_invalid_gzip  s     AE
::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 101zHTTP/z
:rd   )
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   )r[  r   rd   r   zHTTP/[0-9]\.[0-9] 200.*
r   r   )r   r   rI  assertRegex)r)   r[  r   r   rZ  r_   s   `  @@@r*   test_header_callbackz-HTTPClientCommonTestCase.test_header_callback  sz    
	/	! 	

+1 	 	

 	Z!Z8A(FG'7!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)r7  
allow_ipv6T)force_instancedefaultsr   s   TestDefaultUserAgent)dicthttp_client	__class__r   r.  r   rA   r   )r)   rd  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   MyUserAgentro   r   r_   rl  z	response=z, value=z, container=)re  r   r   r   rA   )r)   value	containerr_   r   s        r*   test_header_typesz*HTTPClientCommonTestCase.test_header_types  sh    
 5E"K0	#+(-%zz-zA  II"yy%4	 1 5r,   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   rZ   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ärrm  r   )rf  r   r.  r   rA   r   r   s     r*   test_header_encodingz-HTTPClientCommonTestCase.test_header_encoding"  s\     ))//LL+,x 0 
 
 	(DEs   AAc                     | j                  d      }| j                  |j                  d       | j                  |j                  d   d       y )Nr   rs   rt   42)r   r   r   r_   r   s     r*   test_304_with_content_lengthz5HTTPClientCommonTestCase.test_304_with_content_length,  sC    
 ::89,))*:;TBr,   c              #      K   | j                   j                  | j                  d            }| j                  |j                  d       y wNr   r   rf  r   r.  r   rA   r   s     r*   test_future_interfacez.HTTPClientCommonTestCase.test_future_interface5  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   rf  r   r.  	exceptionr   r   r   )r)   contexts     r*   test_future_http_errorz/HTTPClientCommonTestCase.test_future_http_error:  s     y)W""((k)BCC *  ,,,  ))555**//5**3388#> *)s   C-C
BC
CCc              #      K   | j                   j                  | j                  d      d      }| j                  |j                  d       y w)Nr  F)r
  r  )rf  r   r.  r   r   r   s     r*   test_future_http_error_no_raisez8HTTPClientCommonTestCase.test_future_http_error_no_raiseC  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rz  )
r.  rf  r   r   r@   r   r  
isinstancer	   rA   )r)   r   r   	response2s       r*    test_reuse_request_from_responsez9HTTPClientCommonTestCase.test_reuse_request_from_responseJ  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                               y # 1 sw Y   /xY ww)Nr   z	127.0.0.1network_interfacer   znot-interface-or-ip)r.  r	   rf  r   r   r   r  r  r   r+  r@  r  )r)   r   r@   r   r  s        r*   test_bind_source_ipz,HTTPClientCommonTestCase.test_bind_source_ipW  s     ll8$c[A))//88,
I677!#9NOG""((11 8 	+S1B1B-CD 87s   A3C5+C .CCC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r&  r(  r'  r   r*  r   r  r   r,   r   r)  r~   Tr"  s   OTHER)r   r   rA   r   )r)   r   r   s      r*   test_all_methodsz)HTTPClientCommonTestCase.test_all_methodsc  s    2Fzz.z@HX]]DL9 3 /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 Noner"  r   r  r   )r   rA   r
  zmust be Noner&  )r   rA   r#  r
  )r  r  r   r+  r@  r  r   r   )r)   r   r  r   s       r*   test_body_sanity_checksz0HTTPClientCommonTestCase.test_body_sanity_checksq  s$   .F"":.'

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

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

"! .2 $     C0# 3 /. /.s   D')D3'D0	3D<	c                     | j                  ddd      }|j                          | j                  |j                  d       y )Nz/redirect?status=307&url=/putr  s   hellor   r  )r   r   r   rA   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   r3  )r   r   r   r_   r   r   s     r*   test_non_ascii_headerz.HTTPClientCommonTestCase.test_non_ascii_header  s=    ::78))%0*X2FGr,   c                    t        j                          }| j                  d      }|j                          |j                  J | j	                  |j                  d       | j                  |j                  d       |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   r   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'$$000 5 5q9--s3 ""...H//*<=sC&&,,.DAqOOALSLqRS*TU /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   rV   )rf  r   r.  r  cancelr   r   r   r   rY   logged_stack)r)   futelr   s       r*   test_error_after_cancelz0HTTPClientCommonTestCase.test_error_after_cancel  sx     $$T\\&%9:

%w FG2
 3Ziio%??   HGGs$   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:crlfrn  )r"   positionr   r6  rm  key)subTestr  r  r   )r)   headerr"   s      r*   test_header_crlfz)HTTPClientCommonTestCase.test_header_crlf  s    
LFD
 4':&&z2JJx%JA 3 ; 4%8&&z2JJx&%JA 3 98
 32 ;: 32 98sF   B6B*B6+CCC*B3/B66B?	CCC	N)-r.   r/   r0   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r$  r,  r1  r<  rA  rN  rQ  r_  r   ri  rp  rs  ru  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r1   r,   r*   r   r      s_   
.. 	7 	7(F
(#7J
	

1-<7-<T
"3H<0"5>  ";@ F FC 9 9 ? ? - - 
: 
: 	E 	E21`<HV$-  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/r6  )r7  )r   r	   re  r   r7  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  r6  r  )r   r	   re  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  r6  )
proxy_hostbar)r   r	   re  r   r  r  s     r*   test_both_setzRequestProxyTest.test_both_set  s7    -%@$RWBX
 	))51r,   c                 v    t        t        d      t                     }| j                  |j                  d        y Nr  )r   r	   re  assertIsr   r  s     r*   test_neither_setz!RequestProxyTest.test_neither_set  s*    k*?@$&Ie))40r,   c                     t        t        d      t                     }| j                  t              5  |j
                   d d d        y # 1 sw Y   y xY wr  )r   r	   re  r  AttributeErrorr6  r  s     r*   test_bad_attributez#RequestProxyTest.test_bad_attribute   s7    k*?@$&I~.II /..s   A		Ac                 f    t        t        d      d       }| j                  |j                  d        y r  )r   r	   r  r   r  s     r*   test_defaults_nonez#RequestProxyTest.test_defaults_none  s(    k*?@$Ge))40r,   N)	r.   r/   r0   r  r  r  r  r  r  r1   r,   r*   r  r    s     2921
1r,   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   )rK  zHTTPResponse(zcode=200)r
   r	   r   r@  r  rU  r+  )r)   r   ss      r*   test_strzHTTPResponseTestCase.test_str  sI    ,-s79
 M_56j!$r,   N)r.   r/   r0   r  r1   r,   r*   r  r  
  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   server
add_socketset)r   appeventr)   s     r*   init_serverz-SyncHTTPClientTest.setUp.<locals>.init_server  sJ    .0OD$)%6789C$S/DKKK""4(IIKr,   c                  p    j                   j                          j                   j                          y rk   )server_iolooprun_syncstart)r  r)   s   r*   r  z'SyncHTTPClientTest.setUp.<locals>.start!  s)    ''4$$&r,   )target)r   r  	threadingEventr   rZ   Threadserver_threadr  waitr   rf  )r)   r  r  r  s   ` @@r*   setUpzSyncHTTPClientTest.setUp  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_stop4  s?     kk7799 qA """'')s   AA)r  r  r   rZ   r  add_callback)r  r)   s    r*   stop_serverz0SyncHTTPClientTest.tearDown.<locals>.stop_server,  sA    KK ]]* * ++I6r,   T)all_fds)r  r  r  r   rf  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)   paths     r*   r.  zSyncHTTPClientTest.get_urlD  s    &$))T):::r,   c                     | j                   j                  | j                  d            }| j                  d|j                         y )Nr  r   r{  r   s     r*   test_sync_clientz#SyncHTTPClientTest.test_sync_clientG  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   rf  r   r.  r   r  r   )r)   	assertions     r*   test_sync_client_errorz)SyncHTTPClientTest.test_sync_client_errorK  s[     y)Y""4<<#<= *,,1137 *)s   +A00A9N)r.   r/   r0   r  r  r.  r  r  r1   r,   r*   r  r    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_logT  sw     ~~_
 ??$$

 ;;)*T[[)*;;II=> r,   N)r.   r/   r0   r  r1   r,   r*   r  r  S  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  r6  r  rm  )r	   r   r_   r)   r@   s     r*   test_headersz HTTPRequestTestCase.test_headersr  s+    2UENK5%.9r,   c                 h    t        d      }ddi|_        | j                  |j                  ddi       y )Nr  r  bazr	   r_   r   r  s     r*   test_headers_setterz'HTTPRequestTestCase.test_headers_setterv  s0    23 %.5%.9r,   c                 `    t        d      }d |_        | j                  |j                  i        y )Nr  r  r  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  r6  )rA   )r	   r   rA   r   r  s     r*   	test_bodyzHTTPRequestTestCase.test_body  s'    2?tE{3r,   c                 r    t        d      }d|_        | j                  |j                  t        d             y )Nr  r6  )r	   rA   r   r   r  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_dater@   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   r  utcnowr	   r   r_   r   r"  s      r*   'test_if_modified_since_naive_deprecatedz;HTTPRequestTestCase.test_if_modified_since_naive_deprecated  sZ    ! ))002I "2iPOO13CI3NO	
 "!s   A''A0N)
r.   r/   r0   r  r  r  r  r  r$  r'  r1   r,   r*   r  r  q  s%    ::
.
44


r,   r  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 )Nr+  HTTP 403: Forbidden)r   r   r@  repr)r)   r.  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  r+  r2  )	r
   r	   r  r   r   r  r   r@  r3  )r)   r   cmr.  s       r*   test_error_with_responsez*HTTPErrorTestCase.test_error_with_response  sn    K(=>Dy)RLLN *LLQ!67a"78	 *)s   BBN)r.   r/   r0   r0  r4  r7  r1   r,   r*   r)  r)    s    *9
9r,   r)  )Mr/  r8  
contextlibr   r,  r   r  r  ior   r  r  r  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    r3   r<   rE   rN   rS   r\   ra   ri   rm   rq   ry   r}   r   r   r   r   TestCaser  r  r  r  r  r)  r1   r,   r*   <module>rH     s            
     7 7   * ! % (  T T > 8 8 :( (
. 
& &
n 
^ > ;. ;
~ &n &
Q~ Q
n &> &H H"~ "
 
EN EB	B0 B	BJ1x(( 1B%8,, %<8** <8~?8#4#4 ?<%
(++ %
P9)) 9r,   