U
    luf^‰  ã                   @   sÀ  d dl Z d dlZd dlmZ d dlZd dlZd dlZd dlZd dlm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZmZmZ d dlmZ d dlmZmZmZmZmZ d dlmZ d dlmZ d d	l m!Z! d d
l"m#Z#m$Z$ d dlm%Z% d dl&m'Z'm(Z(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4 G dd„ de0ƒZ5G dd„ de0ƒZ6G dd„ de0ƒZ7G dd„ de0ƒZ8G dd„ de0ƒZ9G dd„ de0ƒZ:G dd„ de0ƒZ;G dd„ de0ƒZ<G d d!„ d!e0ƒZ=G d"d#„ d#e0ƒZ>G d$d%„ d%e0ƒZ?G d&d'„ d'e0ƒZ@G d(d)„ d)e0ƒZAG d*d+„ d+e0ƒZBG d,d-„ d-e0ƒZCG d.d/„ d/e0ƒZDG d0d1„ d1e'ƒZEG d2d3„ d3ejFƒZGG d4d5„ d5ejFƒZHG d6d7„ d7ejFƒZIG d8d9„ d9ejFƒZJG d:d;„ d;ejFƒZKG d<d=„ d=ejFƒZLdS )>é    N)Úclosing)ÚBytesIO)Úutf8Ú
native_strÚ
to_unicode)Úgen)ÚHTTPRequestÚHTTPResponseÚ_RequestProxyÚ	HTTPErrorÚ
HTTPClient)Ú
HTTPServer)ÚIOLoop)ÚIOStream)Úgen_logÚapp_log)Únetutil)ÚAsyncHTTPTestCaseÚbind_unused_portÚgen_testÚ	ExpectLog)ÚskipOnTravisÚignore_deprecation)ÚApplicationÚRequestHandlerÚurl)Úformat_timestampÚHTTPHeadersc                   @   s   e Zd Zdd„ ZdS )ÚHelloWorldHandlerc                 C   s*   |   dd¡}|  dd¡ |  d| ¡ d S )NÚnameZworldúContent-Typeú
text/plainz	Hello %s!)Úget_argumentÚ
set_headerÚfinish)Úselfr   © r&   ú@/tmp/pip-unpacked-wheel-bmg6zs32/tornado/test/httpclient_test.pyÚget%   s    zHelloWorldHandler.getN©Ú__name__Ú
__module__Ú__qualname__r(   r&   r&   r&   r'   r   $   s   r   c                   @   s   e Zd Zdd„ ZdS )ÚPostHandlerc                 C   s"   |   d|  d¡|  d¡f ¡ d S )NzPost arg1: %s, arg2: %sZarg1Zarg2)r$   r"   ©r%   r&   r&   r'   Úpost,   s
    ÿÿzPostHandler.postN©r*   r+   r,   r/   r&   r&   r&   r'   r-   +   s   r-   c                   @   s   e Zd Zdd„ ZdS )Ú
PutHandlerc                 C   s   |   d¡ |   | jj¡ d S )Nz
Put body: ©ÚwriteÚrequestÚbodyr.   r&   r&   r'   Úput4   s    
zPutHandler.putN)r*   r+   r,   r6   r&   r&   r&   r'   r1   3   s   r1   c                   @   s   e Zd Zdd„ ZdS )ÚRedirectHandlerc                 C   s.   |   d¡ | j|  d¡t|  dd¡ƒd d S )Nzredirects can have bodies toor   ÚstatusZ302)r8   )r3   Úredirectr"   Úintr.   r&   r&   r'   Úprepare:   s
    
 ÿzRedirectHandler.prepareN©r*   r+   r,   r;   r&   r&   r&   r'   r7   9   s   r7   c                   @   s   e Zd Zdd„ ZdS )ÚRedirectWithoutLocationHandlerc                 C   s   |   d¡ |  ¡  d S )Né-  )Ú
set_statusr$   r.   r&   r&   r'   r;   B   s    
z&RedirectWithoutLocationHandler.prepareNr<   r&   r&   r&   r'   r=   A   s   r=   c                   @   s   e Zd Zejdd„ ƒZdS )ÚChunkHandlerc                 c   s,   |   d¡ |  ¡  t d¡V  |   d¡ d S )NÚasdfç{®Gáz„?Zqwer)r3   Úflushr   Úsleepr.   r&   r&   r'   r(   I   s    
zChunkHandler.getN)r*   r+   r,   r   Ú	coroutiner(   r&   r&   r&   r'   r@   H   s   r@   c                   @   s   e Zd Zdd„ ZdS )ÚAuthHandlerc                 C   s   |   | jjd ¡ d S )NÚAuthorization)r$   r4   Úheadersr.   r&   r&   r'   r(   S   s    zAuthHandler.getNr)   r&   r&   r&   r'   rF   R   s   rF   c                   @   s   e Zd Zdd„ ZdS )ÚCountdownHandlerc                 C   s6   t |ƒ}|dkr(|  |  d|d ¡¡ n
|  d¡ d S )Nr   Ú	countdowné   ZZero)r:   r9   Zreverse_urlr3   )r%   Úcountr&   r&   r'   r(   X   s    zCountdownHandler.getNr)   r&   r&   r&   r'   rI   W   s   rI   c                   @   s   e Zd Zdd„ ZdS )ÚEchoPostHandlerc                 C   s   |   | jj¡ d S ©Nr2   r.   r&   r&   r'   r/   a   s    zEchoPostHandler.postNr0   r&   r&   r&   r'   rM   `   s   rM   c                   @   s   e Zd Zdd„ ZdS )ÚUserAgentHandlerc                 C   s   |   | jj dd¡¡ d S )Nú
User-AgentzUser agent not set)r3   r4   rH   r(   r.   r&   r&   r'   r(   f   s    zUserAgentHandler.getNr)   r&   r&   r&   r'   rO   e   s   rO   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚContentLength304Handlerc                 C   s   |   d¡ |  dd¡ d S )Né0  úContent-Lengthé*   )r?   r#   r.   r&   r&   r'   r(   k   s    
zContentLength304Handler.getc                 C   s   d S rN   r&   r.   r&   r&   r'   Ú_clear_representation_headerso   s    z5ContentLength304Handler._clear_representation_headersN)r*   r+   r,   r(   rU   r&   r&   r&   r'   rQ   j   s   rQ   c                   @   s   e Zd Zdd„ ZdS )ÚPatchHandlerc                 C   s   |   | jj¡ dS )z=Return the request payload - so we can check it is being keptNr2   r.   r&   r&   r'   Úpatchv   s    zPatchHandler.patchN)r*   r+   r,   rW   r&   r&   r&   r'   rV   u   s   rV   c                   @   s>   e Zd Zejd Zdd„ Ze Z Z Z Z	 Z
 Z ZZdS )ÚAllMethodsHandler)ÚOTHERc                 C   s"   | j jd k	st‚|  | j j¡ d S rN   )r4   ÚmethodÚAssertionErrorr3   r.   r&   r&   r'   rZ   ~   s    zAllMethodsHandler.methodN)r*   r+   r,   r   ZSUPPORTED_METHODSrZ   r(   Úheadr/   r6   ÚdeleteÚoptionsrW   Úotherr&   r&   r&   r'   rX   {   s   
rX   c                   @   s   e Zd Zdd„ ZdS )ÚSetHeaderHandlerc                 C   s2   t |  d¡| jjd ƒD ]\}}|  ||¡ qd S )NÚkÚv)ÚzipZget_argumentsr4   Ú	argumentsr#   )r%   ra   rb   r&   r&   r'   r(   †   s     zSetHeaderHandler.getNr)   r&   r&   r&   r'   r`   …   s   r`   c                   @   s   e Zd Zddœdd„ZdS )ÚInvalidGzipHandlerN)Úreturnc                 C   s\   |   dd¡ |   dd¡ d dd„ tdƒD ƒ¡d d	… }tj| ¡ d
dd }|  |¡ d S )Nr    r!   úContent-EncodingÚgzipÚ c                 s   s   | ]}d   |¡V  qdS )zHello World {}
N)Úformat)Ú.0Úir&   r&   r'   Ú	<genexpr>•   s     z)InvalidGzipHandler.get.<locals>.<genexpr>i(#  i;F é   )Úcompressleveló    )r#   ÚjoinÚrangerh   ÚcompressÚencoder3   )r%   Útextr5   r&   r&   r'   r(   Ž   s
     zInvalidGzipHandler.getr)   r&   r&   r&   r'   re      s   re   c                   @   s   e Zd Zdd„ ZdS )ÚHeaderEncodingHandlerc                 C   s   |   | jjd  d¡¡ d S )NÚFooú	ISO8859-1)r$   r4   rH   rt   r.   r&   r&   r'   r(   ›   s    zHeaderEncodingHandler.getNr)   r&   r&   r&   r'   rv   š   s   rv   c                   @   sh  e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zed-d.„ ƒZd/d0„ Zd1d2„ Zed3d4„ ƒZd5d6„ Zed7d8„ ƒZ ed9d:„ ƒZ!ed;d<„ ƒZ"ed=d>„ ƒZ#ed?d@„ ƒZ$dAdB„ Z%dCdD„ Z&dEdF„ Z'dGdH„ Z(dIdJ„ Z)dKdL„ Z*edMdN„ ƒZ+dOS )PÚHTTPClientCommonTestCasec                 C   s   t tdtƒtdtƒtdtƒtdtƒtdtƒtdtƒtdtƒtdt	d	d
tdt
ƒtdtƒtdtƒtdtƒtdtƒtdtƒtdtƒtdtƒgddS )Nú/helloú/postz/putz	/redirectú/redirect_without_locationú/chunkú/authz/countdown/([0-9]+)rJ   )r   ú	/echopostú/user_agentú/304_with_content_lengthú/all_methodsú/patchz/set_headerú/invalid_gzipú/header-encodingT)rh   )r   r   r   r-   r1   r7   r=   r@   rF   rI   rM   rO   rQ   rX   rV   r`   re   rv   r.   r&   r&   r'   Úget_app¥   s(    ðíz HTTPClientCommonTestCase.get_appc                 C   s4   d}| j dd|d}|  |jd¡ |  |j|¡ d S )Ns   some patch datarƒ   ÚPATCH©rZ   r5   éÈ   ©ÚfetchÚassertEqualÚcoder5   )r%   r5   Úresponser&   r&   r'   Útest_patch_receives_payload¼   s    z4HTTPClientCommonTestCase.test_patch_receives_payloadc                 C   st   |   d¡}|  |jd¡ |  |jd d¡ |  |jd¡ |jd k	sFt‚|  t|jƒd¡ |   d¡}|  |jd¡ d S )	Nrz   r‰   r    r!   ó   Hello world!r   z/hello?name=Bens
   Hello Ben!)r‹   rŒ   r   rH   r5   Úrequest_timer[   r:   ©r%   rŽ   r&   r&   r'   Útest_hello_worldÂ   s    

z)HTTPClientCommonTestCase.test_hello_worldc                 C   s2   g }| j d|jd}|  |dg¡ |  |j¡ d S )Nrz   ©Ústreaming_callbackr   )r‹   ÚappendrŒ   ÚassertFalser5   )r%   ÚchunksrŽ   r&   r&   r'   Útest_streaming_callbackÎ   s    z0HTTPClientCommonTestCase.test_streaming_callbackc                 C   s0   | j dddd}|  |jd¡ |  |jd¡ d S )Nr{   ÚPOSTzarg1=foo&arg2=barrˆ   r‰   s   Post arg1: foo, arg2: barrŠ   r’   r&   r&   r'   Ú	test_postÖ   s    z"HTTPClientCommonTestCase.test_postc                 C   sL   |   d¡}|  |jd¡ g }| j d|jd}|  |ddg¡ |  |j¡ d S )Nr}   ó   asdfqwerr”   ó   asdfó   qwer)r‹   rŒ   r5   r–   r—   )r%   rŽ   r˜   r&   r&   r'   Útest_chunkedÛ   s    
z%HTTPClientCommonTestCase.test_chunkedc              	      st   t ƒ \}}t|ƒX tj‡ fdd„ƒ}t ||¡ ˆ  d| ¡}| ¡  ˆ  |j	d¡ ˆ j
 | ¡ ¡ W 5 Q R X d S )Nc                 3   sF   t | ƒ}| d¡V }d|kr&ˆ  d¡ | d dd¡¡V  | ¡  d S )Nó   

ó   HTTP/1.úrequires HTTP/1.xs7   HTTP/1.1 200 OK
Transfer-Encoding: chunked

1
1
1
2
0

ó   
ó   
©r   Z
read_untilZskipTestr3   ÚreplaceÚclose©ÚconnÚaddressÚstreamZrequest_datar.   r&   r'   Úaccept_callbackê   s    
 õÿzDHTTPClientCommonTestCase.test_chunked_close.<locals>.accept_callbackúhttp://127.0.0.1:%d/s   12)r   r   r   rE   r   Úadd_accept_handlerr‹   ÚrethrowrŒ   r5   Úio_loopÚremove_handlerÚfileno©r%   ÚsockÚportr¬   Úrespr&   r.   r'   Útest_chunked_closeä   s    

z+HTTPClientCommonTestCase.test_chunked_closec                 C   s   |   | jddddjd¡ d S )Nr~   ÚAladdinúopen sesame©Úauth_usernameÚauth_passwordó"   Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==©rŒ   r‹   r5   r.   r&   r&   r'   Útest_basic_auth	  s      ÿüz(HTTPClientCommonTestCase.test_basic_authc                 C   s    |   | jdddddjd¡ d S )Nr~   r¸   r¹   Úbasic)r»   r¼   Ú	auth_moder½   r¾   r.   r&   r&   r'   Útest_basic_auth_explicit_mode  s    üùz6HTTPClientCommonTestCase.test_basic_auth_explicit_modec                 C   sD   |   | jddddjd¡ t dd¡}|   | jd|ddjd	¡ d S )
Nr~   Útestu   123Â£rº   s   Basic dGVzdDoxMjPCow==ZNFDu   josÃ©u   sÉ™crÉ™ts   Basic am9zw6k6c8mZY3LJmXQ=)rŒ   r‹   r5   ÚunicodedataÚ	normalize)r%   Úusernamer&   r&   r'   Útest_basic_auth_unicode  s    þþz0HTTPClientCommonTestCase.test_basic_auth_unicodec              
   C   sL   t tddd4 |  ttf¡ | jdddddd	 W 5 Q R X W 5 Q R X d S )
Nzuncaught exceptionF©Úrequiredr~   r¸   r¹   rA   T)r»   r¼   rÁ   Úraise_error)r   r   ÚassertRaisesÚ
ValueErrorr   r‹   r.   r&   r&   r'   Útest_unsupported_auth_mode,  s    ûz3HTTPClientCommonTestCase.test_unsupported_auth_modec                 C   sn   | j ddd}|  d|j¡ |  |jd  d¡¡ |   d¡}|  d|j¡ |  |j d¡¡ |  d	|j¡ d S )
Nz/countdown/2F©Zfollow_redirectsé.  ZLocationz/countdown/1r‰   z/countdown/0s   Zero)r‹   rŒ   r   Ú
assertTruerH   ÚendswithÚeffective_urlr5   r’   r&   r&   r'   Útest_follow_redirect:  s    
z-HTTPClientCommonTestCase.test_follow_redirectc                 C   s    | j ddd}|  d|j¡ d S )Nr|   TrÎ   r>   ©r‹   rŒ   r   r’   r&   r&   r'   Útest_redirect_without_locationD  s    z7HTTPClientCommonTestCase.test_redirect_without_locationc                 C   s"   | j dddd}|  |jd¡ d S )Nú/redirect?url=/put&status=307ÚPUTZhellorˆ   ó   Put body: hello©r‹   rŒ   r5   r’   r&   r&   r'   Útest_redirect_put_with_bodyL  s      ÿz4HTTPClientCommonTestCase.test_redirect_put_with_bodyc                 C   s"   | j dddd}|  |jd¡ d S )NrÖ   r×   T©rZ   Úallow_nonstandard_methodss
   Put body: rÙ   r’   r&   r&   r'   Útest_redirect_put_without_bodyR  s    ýz7HTTPClientCommonTestCase.test_redirect_put_without_bodyc                 C   s  dD ] }d| }| j |ddd}|  d|j¡ dD ]F}| j ||dd	}|d
krb|  t|ƒ|j¡ q2|  |jt|ƒdg¡ q2| j |dd}|  d|j¡ |  d|j¡ qdD ]^}d}dD ]&}| j ||dd	}|  |t|jƒ¡ q¶| j |dd}|  d|j¡ |  d|j¡ qªd S )N)r>   rÏ   i/  z$/redirect?url=/all_methods&status=%drš   ó    rˆ   s   GET)ÚGETÚOPTIONSr×   ÚDELETETrÛ   )r>   rÏ   ÚHEAD©rZ   r‰   )i3  i4  z%/redirect?url=/all_methods&status=307)rß   rà   rš   r×   rá   )r‹   rŒ   r5   r   ÚassertInr   r   )r%   r8   r   r¶   rZ   r&   r&   r'   Útest_method_after_redirect[  s(    z3HTTPClientCommonTestCase.test_method_after_redirectc                 C   s8   |   d¡ dd¡}|  |¡}|  dt d¡ |j¡ d S )Nr~   zhttp://zhttp://me:secret@s   Basic s	   me:secret)Úget_urlr¦   r‹   rŒ   Úbase64Ú	b64encoder5   )r%   r   rŽ   r&   r&   r'   Útest_credentials_in_urly  s    
z0HTTPClientCommonTestCase.test_credentials_in_urlc                 C   sº   d}t  d¡}| jdd|ddid}|  |jd d	¡ |  |jt|ƒ¡ | jdd|ddid}|  |jd d
¡ |  |j|¡ | jdd|ddidd}|  |jd d
¡ |  |j|¡ d S )Nõ   Ã©s   e9r   rš   r    zapplication/blah)rZ   r5   rH   rS   Ú2Ú1Úfoo)rZ   r5   rH   Ú
user_agent)ÚbinasciiÚa2b_hexr‹   rŒ   rH   r5   r   )r%   Zunicode_bodyZ	byte_bodyrŽ   r&   r&   r'   Útest_body_encoding~  s6    
üüûz+HTTPClientCommonTestCase.test_body_encodingc                 C   sZ   |   d¡}|  t|jƒt¡ |  t|jd ƒt¡ |  t|jƒt¡ |  t|j	ƒt¡ d S )Nrz   r    )
r‹   rŒ   Útyper5   ÚbytesrH   Ústrr   r:   rÒ   r’   r&   r&   r'   Ú
test_types¢  s
    
z#HTTPClientCommonTestCase.test_typesc                 C   sj   | j ddddid}|  |jd d¡ |  |jd¡ |  t|jƒd¡ tjd	|jd
}|  | 	¡ d¡ d S )Nr}   FzAccept-Encodingrh   )Zdecompress_responserH   rg   rœ   é"   Úr)ÚmodeÚfileobj)
r‹   rŒ   rH   ZassertNotEqualr5   Úlenrh   ÚGzipFileÚbufferÚread)r%   rŽ   Úfr&   r&   r'   Ú	test_gzip©  s      ÿz"HTTPClientCommonTestCase.test_gzipc              	   C   sf   t tdddN z2|  d¡}|  |jd¡ |  |jd d… d¡ W n tk
rV   Y nX W 5 Q R X d S )Nz*(Uncaught exception|Exception in callback)FrÈ   r„   r‰   é   s   Hello World 0
)r   r   r‹   rŒ   r   r5   r   r’   r&   r&   r'   Útest_invalid_gzipº  s      ÿ
z*HTTPClientCommonTestCase.test_invalid_gzipc                    sp   g ‰i ‰g ‰ ‡‡fdd„}‡ ‡‡fdd„}ˆj d||d ˆ tˆƒdˆ¡ ˆ ˆd d	¡ ˆ ˆ d
dg¡ d S )Nc                    sN   |   d¡rn>|   d¡r"ˆ  | ¡ n(| dkrJ|  dd¡\}}| ¡ ˆ| ¡ < d S )NzHTTP/1.1 101zHTTP/z
ú:rK   )Ú
startswithr–   ÚsplitÚstripÚlower)Zheader_linera   rb   )Ú
first_linerH   r&   r'   Úheader_callbackÌ  s    

zFHTTPClientCommonTestCase.test_header_callback.<locals>.header_callbackc                    s   ˆ  ˆd d¡ ˆ  | ¡ d S )Nzcontent-typeztext/html; charset=UTF-8)rŒ   r–   )Úchunk)r˜   rH   r%   r&   r'   r•   Ö  s    zIHTTPClientCommonTestCase.test_header_callback.<locals>.streaming_callbackr}   )r  r•   rK   r   zHTTP/[0-9]\.[0-9] 200.*
r   rž   )r‹   rŒ   rú   ÚassertRegex)r%   r  r•   r&   )r˜   r  rH   r%   r'   Útest_header_callbackÇ  s    
ýz-HTTPClientCommonTestCase.test_header_callbackc                 c   sP   t ddd}| jjd|d}z$| |  d¡¡V }|  |jd¡ W 5 | ¡  X d S )NZTestDefaultUserAgentF)rî   Z
allow_ipv6T)Zforce_instanceÚdefaultsr€   s   TestDefaultUserAgent)ÚdictÚhttp_clientÚ	__class__r§   r‹   ræ   rŒ   r5   )r%   r  ÚclientrŽ   r&   r&   r'   Útest_configure_defaultsæ  s    z0HTTPClientCommonTestCase.test_configure_defaultsc              
   C   sT   dD ]J}t tfD ]<}|ƒ }||d< | jd|d}|  |jdd|j||f ¡ qqd S )N)ZMyUserAgentó   MyUserAgentrP   r€   ©rH   r  z#response=%r, value=%r, container=%r)r  r   r‹   rŒ   r5   )r%   ÚvalueÚ	containerrH   r¶   r&   r&   r'   Útest_header_typesñ  s    
ÿýz*HTTPClientCommonTestCase.test_header_typesc                    s€   t ƒ \}}t|ƒd tj‡ fdd„ƒ}t ||¡ z,ˆ  	d| ¡}| 
¡  ˆ  |jd d¡ W 5 ˆ j | ¡ ¡ X W 5 Q R X d S )Nc                 3   sF   t | ƒ}| d¡V }d|kr&ˆ  d¡ | d dd¡¡V  | ¡  d S )Nr    r¡   r¢   s2   HTTP/1.1 200 OK
X-XSS-Protection: 1;
	mode=block

r£   r¤   r¥   r¨   r.   r&   r'   r¬     s    
 úÿ
zIHTTPClientCommonTestCase.test_multi_line_headers.<locals>.accept_callbackr­   zX-XSS-Protectionz1; mode=block)r   r   r   rE   r   r®   r°   r±   r²   r‹   r¯   rŒ   rH   r³   r&   r.   r'   Útest_multi_line_headers  s    

z0HTTPClientCommonTestCase.test_multi_line_headersc                 c   s4   | j j|  d¡ddidV }|  |jd d¡¡ d S )Nr…   rw   u   bÃ¤rr  rx   )r  r‹   ræ   rŒ   r5   rt   r’   r&   r&   r'   Útest_header_encoding"  s     ÿþz-HTTPClientCommonTestCase.test_header_encodingc                 C   s.   |   d¡}|  |jd¡ |  |jd d¡ d S )Nr   rR   rS   Z42)r‹   rŒ   r   rH   r’   r&   r&   r'   Útest_304_with_content_length,  s    
z5HTTPClientCommonTestCase.test_304_with_content_lengthc                 c   s&   | j  |  d¡¡V }|  |jd¡ d S ©Nrz   r   ©r  r‹   ræ   rŒ   r5   r’   r&   r&   r'   Útest_future_interface5  s    z.HTTPClientCommonTestCase.test_future_interfacec              	   c   sn   |   t¡}| j |  d¡¡V  W 5 Q R X |jd k	s8t‚|jjd k	sHt‚|  |jj	d¡ |  |jjj	d¡ d S ©Nú	/notfoundé”  )
rË   r   r  r‹   ræ   Ú	exceptionr[   rŽ   rŒ   r   )r%   Úcontextr&   r&   r'   Útest_future_http_error:  s    z/HTTPClientCommonTestCase.test_future_http_errorc                 c   s*   | j j|  d¡ddV }|  |jd¡ d S )Nr  F)rÊ   r  )r  r‹   ræ   rŒ   r   r’   r&   r&   r'   Útest_future_http_error_no_raiseC  s
     ÿz8HTTPClientCommonTestCase.test_future_http_error_no_raisec                 c   s\   |   d¡}| j |¡V }|  |jj|¡ |  t|jtƒ¡ | j |j¡V }|  |j	d¡ d S r  )
ræ   r  r‹   rŒ   r4   r   rÐ   Ú
isinstancer   r5   )r%   r   rŽ   Z	response2r&   r&   r'   Ú test_reuse_request_from_responseJ  s    
z9HTTPClientCommonTestCase.test_reuse_request_from_responsec              	   c   s|   |   d¡}t|dd}| j |¡V }|  |jd¡ |  ttf¡ }t|dd}| j |¡V  W 5 Q R X |  	dt
|jƒ¡ d S )Nrz   z	127.0.0.1©Únetwork_interfacer‰   znot-interface-or-ip)ræ   r   r  r‹   rŒ   r   rË   rÌ   r   rä   rô   r   )r%   r   r4   rŽ   r!  r&   r&   r'   Útest_bind_source_ipW  s    
z,HTTPClientCommonTestCase.test_bind_source_ipc                 C   s”   dD ]$}| j d|d}|  |jt|ƒ¡ qdD ]&}| j d|dd}|  |jt|ƒ¡ q.| j ddd}|  |jd¡ | j ddd	d
}|  |jd¡ d S )N©rß   rá   rà   r‚   rã   ©rš   r×   r‡   rÞ   rˆ   râ   rY   TrÛ   s   OTHER)r‹   rŒ   r5   r   )r%   rZ   rŽ   r&   r&   r'   Útest_all_methodsc  s      ÿz)HTTPClientCommonTestCase.test_all_methodsc              
   C   sÒ   dD ]Z}|   t¡}| jd|dd W 5 Q R X |  dt|jƒ¡ | jd|dd}|  |jd¡ qdD ]h}|   t¡}| jd|d	dd
 W 5 Q R X |  dt|jƒ¡ |dkrd| jd|d	ddd |  |jd¡ qdd S )Nr*  r‚   T)rZ   rÊ   zmust not be NonerÛ   r‰   r)  r   )rZ   r5   rÊ   zmust be Nonerß   )rZ   r5   rÜ   rÊ   )rË   rÌ   r‹   rä   rô   r   rŒ   r   )r%   rZ   r!  r¶   r&   r&   r'   Útest_body_sanity_checksq  s8      ÿ   ÿûz0HTTPClientCommonTestCase.test_body_sanity_checksc                 C   s*   | j dddd}| ¡  |  |jd¡ d S )Nz/redirect?status=307&url=/putr×   s   hellorˆ   rØ   )r‹   r¯   rŒ   r5   r’   r&   r&   r'   Útest_put_307¡  s      ÿz%HTTPClientCommonTestCase.test_put_307c                 C   s,   |   d¡}| ¡  |  |jd tdƒ¡ d S )Nz/set_header?k=foo&v=%E9rw   rê   )r‹   r¯   rŒ   rH   r   r’   r&   r&   r'   Útest_non_ascii_header¨  s    
z.HTTPClientCommonTestCase.test_non_ascii_headerc                 C   s¨   t   ¡ }|  d¡}| ¡  |jd k	s(t‚|  |jd¡ |  |jd¡ |jd k	sRt‚|  t|j| ƒd¡ |j	 
¡ D ]0\}}|  d|  kodk n  d||f ¡ qrd S )Nrz   r   g      ð?ztime_info[%s] out of bounds: %s)Útimer‹   r¯   r‘   r[   ZassertGreaterEqualZ
assertLessÚ
start_timeÚabsZ	time_infoÚitemsrÐ   )r%   r0  rŽ   ra   rb   r&   r&   r'   Útest_response_times®  s    
z,HTTPClientCommonTestCase.test_response_timesc                 C   sZ   | j ddd}|  |jd¡ | j ddd}|  |jd¡ | j dddd}|  |jd¡ d S )Nrz   r   )Úconnect_timeoutr‰   )Úrequest_timeout)r4  r5  rÔ   r’   r&   r&   r'   Útest_zero_timeoutÀ  s    z*HTTPClientCommonTestCase.test_zero_timeoutc              	   c   s^   | j  |  d¡¡}|  | ¡ ¡ ttdƒ*}tdƒD ]}t 	d¡V  |j
r4 qPq4W 5 Q R X d S )Nz/404z$Exception after Future was cancelledéd   rB   )r  r‹   ræ   rÐ   Úcancelr   r   rr   r   rD   Zlogged_stack)r%   ZfutÚelrl   r&   r&   r'   Útest_error_after_cancelÊ  s    z0HTTPClientCommonTestCase.test_error_after_cancelN),r*   r+   r,   r†   r   r   r“   r™   r›   rŸ   r·   r¿   rÂ   rÇ   rÍ   rÓ   rÕ   rÚ   rÝ   rå   ré   rñ   rõ   rÿ   r  r  r   r  r  r  r  r  r  r"  r#  r%  r(  r+  r,  r-  r.  r3  r6  r:  r&   r&   r&   r'   ry   ¤   s`   
	%	
	$

 
		




0
ry   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚRequestProxyTestc                 C   s&   t tdddtƒ ƒ}|  |jd¡ d S )Núhttp://example.com/rí   )rî   )r
   r   r  rŒ   rî   ©r%   Úproxyr&   r&   r'   Útest_request_setÚ  s
    
 ÿz!RequestProxyTest.test_request_setc                 C   s&   t tdƒtddƒ}|  |jd¡ d S )Nr<  rí   r&  )r
   r   r  rŒ   r'  r=  r&   r&   r'   Útest_default_setà  s
     ÿz!RequestProxyTest.test_default_setc                 C   s*   t tdddtddƒ}|  |jd¡ d S )Nr<  rí   )Ú
proxy_hostÚbar)r
   r   r  rŒ   rA  r=  r&   r&   r'   Útest_both_setæ  s
    
 ÿzRequestProxyTest.test_both_setc                 C   s"   t tdƒtƒ ƒ}|  |jd ¡ d S ©Nr<  )r
   r   r  ÚassertIsr»   r=  r&   r&   r'   Útest_neither_setì  s    z!RequestProxyTest.test_neither_setc              	   C   s0   t tdƒtƒ ƒ}|  t¡ |j W 5 Q R X d S rD  )r
   r   r  rË   ÚAttributeErrorrí   r=  r&   r&   r'   Útest_bad_attributeð  s    z#RequestProxyTest.test_bad_attributec                 C   s    t tdƒd ƒ}|  |jd ¡ d S rD  )r
   r   rE  r»   r=  r&   r&   r'   Útest_defaults_noneõ  s    z#RequestProxyTest.test_defaults_noneN)	r*   r+   r,   r?  r@  rC  rF  rH  rI  r&   r&   r&   r'   r;  Ù  s   r;  c                   @   s   e Zd Zdd„ ZdS )ÚHTTPResponseTestCasec                 C   s<   t tdƒdtƒ d}t|ƒ}|  | d¡¡ |  d|¡ d S )Núhttp://example.comr‰   )rü   zHTTPResponse(zcode=200)r	   r   r   rô   rÐ   r  rä   )r%   rŽ   Úsr&   r&   r'   Útest_strû  s      ÿzHTTPResponseTestCase.test_strN)r*   r+   r,   rM  r&   r&   r&   r'   rJ  ú  s   rJ  c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚSyncHTTPClientTestc                    sb   t ddˆ_t ¡ ‰ tj‡ ‡fdd„ƒ‰‡‡fdd„}tj|dˆ_ˆj ¡  ˆ  	¡  t
ƒ ˆ_d S )NF)Zmake_currentc                     s<   t ƒ \} ˆ_tdtfgƒ}t|ƒˆ_ˆj | ¡ ˆ  ¡  d S )Nú/)r   rµ   r   r   r   ÚserverZ
add_socketÚset)r´   Zapp)Úeventr%   r&   r'   Úinit_server	  s
    
z-SyncHTTPClientTest.setUp.<locals>.init_serverc                      s   ˆj  ˆ ¡ ˆj  ¡  d S rN   )Úserver_ioloopZrun_syncÚstartr&   )rS  r%   r&   r'   rU    s    z'SyncHTTPClientTest.setUp.<locals>.start)Útarget)r   rT  Ú	threadingÚEventr   rE   ÚThreadÚserver_threadrU  Úwaitr   r  )r%   rU  r&   )rR  rS  r%   r'   ÚsetUp  s    
zSyncHTTPClientTest.setUpc                    s>   ‡ fdd„}ˆ j  |¡ ˆ j ¡  ˆ j ¡  ˆ j jdd d S )Nc                     s,   ˆ j  ¡  tj‡ fdd„ƒ} ˆ j | ¡ d S )Nc                  3   s.   ˆ j  ¡ V  tdƒD ]
} d V  qˆ j ¡  d S )Né   )rP  Zclose_all_connectionsrr   rT  Ústop)rl   r.   r&   r'   Ú	slow_stop$  s    zCSyncHTTPClientTest.tearDown.<locals>.stop_server.<locals>.slow_stop)rP  r^  r   rE   rT  Úadd_callback)r_  r.   r&   r'   Ústop_server  s    
z0SyncHTTPClientTest.tearDown.<locals>.stop_serverT)Zall_fds)rT  r`  rZ  rq   r  r§   )r%   ra  r&   r.   r'   ÚtearDown  s
    

zSyncHTTPClientTest.tearDownc                 C   s   d| j |f S )Nzhttp://127.0.0.1:%d%s)rµ   )r%   Úpathr&   r&   r'   ræ   4  s    zSyncHTTPClientTest.get_urlc                 C   s$   | j  |  d¡¡}|  d|j¡ d S )NrO  r   r  r’   r&   r&   r'   Útest_sync_client7  s    z#SyncHTTPClientTest.test_sync_clientc              	   C   s<   |   t¡}| j |  d¡¡ W 5 Q R X |  |jjd¡ d S r  )rË   r   r  r‹   ræ   rŒ   r   r   )r%   Z	assertionr&   r&   r'   Útest_sync_client_error;  s    z)SyncHTTPClientTest.test_sync_client_errorN)r*   r+   r,   r\  rb  ræ   rd  re  r&   r&   r&   r'   rN    s
   rN  c                   @   s   e Zd Zdd„ ZdS )ÚSyncHTTPClientSubprocessTestc                 C   sP   t jtjddgt jt jddd}|jr<tdƒ tt|jƒƒ |jrL|  	d¡ d S )Nz-czMfrom tornado.httpclient import HTTPClient; f = lambda: None; c = HTTPClient()Té   )ÚstdoutÚstderrÚcheckÚtimeoutzSTDOUT:z%subprocess produced unexpected output)
Ú
subprocessÚrunÚsysÚ
executableÚPIPEÚSTDOUTrh  Úprintr   Zfail)r%   Úprocr&   r&   r'   Útest_destructor_logD  s    ý÷z0SyncHTTPClientSubprocessTest.test_destructor_logN)r*   r+   r,   rt  r&   r&   r&   r'   rf  C  s   rf  c                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚHTTPRequestTestCasec                 C   s&   t dddid}|  |jddi¡ d S )NrK  rí   rB  r  )r   rŒ   rH   ©r%   r4   r&   r&   r'   Útest_headersb  s    z HTTPRequestTestCase.test_headersc                 C   s(   t dƒ}ddi|_|  |jddi¡ d S )NrK  rB  Zbaz©r   rH   rŒ   rv  r&   r&   r'   Útest_headers_setterf  s    
z'HTTPRequestTestCase.test_headers_setterc                 C   s    t dƒ}d |_|  |ji ¡ d S )NrK  rx  rv  r&   r&   r'   Útest_null_headers_setterk  s    z,HTTPRequestTestCase.test_null_headers_setterc                 C   s"   t ddd}|  |jtdƒ¡ d S )NrK  rí   )r5   )r   rŒ   r5   r   rv  r&   r&   r'   Ú	test_bodyp  s    zHTTPRequestTestCase.test_bodyc                 C   s$   t dƒ}d|_|  |jtdƒ¡ d S )NrK  rí   )r   r5   rŒ   r   rv  r&   r&   r'   Útest_body_settert  s    z$HTTPRequestTestCase.test_body_setterc                 C   s6   t j  t jj¡}td|d}|  |jdt|ƒi¡ d S ©NrK  )Zif_modified_sincezIf-Modified-Since)ÚdatetimeÚnowÚtimezoneÚutcr   rŒ   rH   r   ©r%   Z	http_dater4   r&   r&   r'   Útest_if_modified_sincey  s     
ÿz*HTTPRequestTestCase.test_if_modified_sincec              	   C   sB   t ƒ  tj ¡ }W 5 Q R X td|d}|  |jdt|ƒi¡ d S r}  )r   r~  Úutcnowr   rŒ   rH   r   r‚  r&   r&   r'   Ú'test_if_modified_since_naive_deprecated€  s     
ÿz;HTTPRequestTestCase.test_if_modified_since_naive_deprecatedN)
r*   r+   r,   rw  ry  rz  r{  r|  rƒ  r…  r&   r&   r&   r'   ru  a  s   ru  c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚHTTPErrorTestCasec                 C   s2   t dƒ}t |¡}|  ||¡ |  |j|j¡ d S )Né“  )r   ÚcopyZassertIsNotrŒ   r   )r%   ÚeÚe2r&   r&   r'   Ú	test_copyŠ  s    
zHTTPErrorTestCase.test_copyc                 C   s,   t dƒ}|  t|ƒd¡ |  t|ƒd¡ d S )Nr‡  úHTTP 403: Forbidden)r   rŒ   rô   Úrepr)r%   r‰  r&   r&   r'   Útest_plain_error  s    z"HTTPErrorTestCase.test_plain_errorc              	   C   sV   t tdƒdƒ}|  t¡}| ¡  W 5 Q R X |j}|  t|ƒd¡ |  t|ƒd¡ d S )Nr<  r‡  rŒ  )	r	   r   rË   r   r¯   r   rŒ   rô   r  )r%   r¶   Úcmr‰  r&   r&   r'   Útest_error_with_response•  s    z*HTTPErrorTestCase.test_error_with_responseN)r*   r+   r,   r‹  rŽ  r  r&   r&   r&   r'   r†  ‰  s   r†  )Mrç   rï   Ú
contextlibr   rˆ  rh   rW  r~  Úior   rl  rn  r/  ÚtypingrÄ   ZunittestZtornado.escaper   r   r   Ztornador   Ztornado.httpclientr   r	   r
   r   r   Ztornado.httpserverr   Ztornado.ioloopr   Ztornado.iostreamr   Ztornado.logr   r   r   Ztornado.testingr   r   r   r   Ztornado.test.utilr   r   Ztornado.webr   r   r   Ztornado.httputilr   r   r   r-   r1   r7   r=   r@   rF   rI   rM   rO   rQ   rV   rX   r`   re   rv   ry   ZTestCaser;  rJ  rN  rf  ru  r†  r&   r&   r&   r'   Ú<module>   sh   
	

    9!
?(