U
    ‰d¹‡  ã                   @   s¼  d dl Z d dlZd dlmZ d dlZd dlZd dlZd dlZd dlm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZmZmZ d dlmZ d dlmZmZmZmZmZ d dlmZ d dlmZ d d	l m!Z! d d
l"m#Z#m$Z$ d dlm%Z% d dl&m'Z'm(Z(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/m0Z0 d dl1m2Z2m3Z3 G dd„ de/ƒZ4G dd„ de/ƒZ5G dd„ de/ƒZ6G dd„ de/ƒZ7G dd„ de/ƒZ8G dd„ de/ƒZ9G dd„ de/ƒZ:G dd„ de/ƒZ;G d d!„ d!e/ƒZ<G d"d#„ d#e/ƒZ=G d$d%„ d%e/ƒZ>G d&d'„ d'e/ƒZ?G d(d)„ d)e/ƒZ@G d*d+„ d+e/ƒZAG d,d-„ d-e/ƒZBG d.d/„ d/e/ƒZCG d0d1„ d1e'ƒZDG d2d3„ d3ejEƒZFG d4d5„ d5ejEƒZGG d6d7„ d7ejEƒZHG d8d9„ d9ejEƒZIG d:d;„ d;ejEƒZJG d<d=„ d=ejEƒZKdS )>é    N)Úclosing)ÚBytesIO)Úutf8Ú
native_strÚ
to_unicode)Úgen)ÚHTTPRequestÚHTTPResponseÚ_RequestProxyÚ	HTTPErrorÚ
HTTPClient)Ú
HTTPServer)ÚIOLoop)ÚIOStream)Úgen_logÚapp_log)Únetutil)ÚAsyncHTTPTestCaseÚbind_unused_portÚgen_testÚ	ExpectLog)ÚskipOnTravis)Ú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-fekwu36z/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+   r5   r%   r%   r%   r&   r0   3   s   r0   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)r7   )r2   Úredirectr!   Úintr-   r%   r%   r&   Úprepare:   s
    
 ÿzRedirectHandler.prepareN©r)   r*   r+   r:   r%   r%   r%   r&   r6   9   s   r6   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)r2   Ú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#   r3   Úheadersr-   r%   r%   r&   r'   S   s    zAuthHandler.getNr(   r%   r%   r%   r&   rE   R   s   rE   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)r9   r8   Zreverse_urlr2   )r$   Úcountr%   r%   r&   r'   X   s    zCountdownHandler.getNr(   r%   r%   r%   r&   rH   W   s   rH   c                   @   s   e Zd Zdd„ ZdS )ÚEchoPostHandlerc                 C   s   |   | jj¡ d S ©Nr1   r-   r%   r%   r&   r.   a   s    zEchoPostHandler.postNr/   r%   r%   r%   r&   rL   `   s   rL   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)r2   r3   rG   r'   r-   r%   r%   r&   r'   f   s    zUserAgentHandler.getNr(   r%   r%   r%   r&   rN   e   s   rN   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 rM   r%   r-   r%   r%   r&   Ú_clear_representation_headerso   s    z5ContentLength304Handler._clear_representation_headersN)r)   r*   r+   r'   rT   r%   r%   r%   r&   rP   j   s   rP   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 keptNr1   r-   r%   r%   r&   Úpatchv   s    zPatchHandler.patchN)r)   r*   r+   rV   r%   r%   r%   r&   rU   u   s   rU   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 rM   )r3   ÚmethodÚAssertionErrorr2   r-   r%   r%   r&   rY   ~   s    zAllMethodsHandler.methodN)r)   r*   r+   r   ZSUPPORTED_METHODSrY   r'   Úheadr.   r5   ÚdeleteÚoptionsrV   Úotherr%   r%   r%   r&   rW   {   s   
rW   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_argumentsr3   Ú	argumentsr"   )r$   r`   ra   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Úrangerg   ÚcompressÚencoder2   )r$   Útextr4   r%   r%   r&   r'   Ž   s
     zInvalidGzipHandler.getr(   r%   r%   r%   r&   rd      s   rd   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#   r3   rG   rs   r-   r%   r%   r&   r'   ›   s    zHeaderEncodingHandler.getNr(   r%   r%   r%   r&   ru   š   s   ru   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]+)rI   )r   ú	/echopostú/user_agentú/304_with_content_lengthú/all_methodsú/patchz/set_headerú/invalid_gzipú/header-encodingT)rg   )r   r   r   r,   r0   r6   r<   r?   rE   rH   rL   rN   rP   rW   rU   r_   rd   ru   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©rY   r4   éÈ   ©ÚfetchÚassertEqualÚcoder4   )r$   r4   Ú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 )	Nry   rˆ   r   r    ó   Hello world!r   z/hello?name=Bens
   Hello Ben!)rŠ   r‹   rŒ   rG   r4   Úrequest_timerZ   r9   ©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 )Nry   ©Ústreaming_callbackr   )rŠ   Úappendr‹   ÚassertFalser4   )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 )Nrz   Ú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‹   r4   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skipTestr2   Ú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   rD   r   Úadd_accept_handlerrŠ   Úrethrowr‹   r4   Ú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Š   r4   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Š   r4   Ú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¸   r@   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Œ   Ú
assertTruerG   ÚendswithÚeffective_urlr4   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‹   r4   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©rY   Ú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©rY   rˆ   )i3  i4  z%/redirect?url=/all_methods&status=307)rÞ   rß   r™   rÖ   rà   )rŠ   r‹   r4   r   ÚassertInrŒ   r   )r$   r7   r   rµ   rY   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Ú	b64encoder4   )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)rY   r4   rG   rR   Ú2Ú1Úfoo)rY   r4   rG   Ú
user_agent)ÚbinasciiÚa2b_hexrŠ   r‹   rG   r4   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 )Nry   r   )
rŠ   r‹   Útyper4   ÚbytesrG   ÚstrrŒ   r9   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-Encodingrg   )Zdecompress_responserG   rf   r›   é"   Úr)ÚmodeÚfileobj)
rŠ   r‹   rG   ZassertNotEqualr4   Úlenrg   Ú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Œ   r4   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
ú:rJ   )Ú
startswithr•   ÚsplitÚstripÚlower)Zheader_liner`   ra   )Ú
first_linerG   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—   rG   r$   r%   r&   r”   Ö  s    zIHTTPClientCommonTestCase.test_header_callback.<locals>.streaming_callbackr|   )r  r”   rJ   r   zHTTP/[0-9]\.[0-9] 200.*
rœ   r   )rŠ   r‹   rù   ÚassertRegex)r$   r  r”   r%   )r—   r  rG   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‹   r4   )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ó   MyUserAgentrO   r   ©rG   r  z#response=%r, value=%r, container=%r)r  r   rŠ   r‹   r4   )r$   ÚvalueÚ	containerrG   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   rD   r   r­   r¯   r°   r±   rŠ   r®   r‹   rG   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„   rv   u   bÃ¤rr  rw   )r  rŠ   rå   r‹   r4   rs   r‘   r%   r%   r&   Útest_header_encoding"  s     ÿþz-HTTPClientCommonTestCase.test_header_encodingc                 C   s.   |   d¡}|  |jd¡ |  |jd d¡ d S )Nr€   rQ   rR   Z42)rŠ   r‹   rŒ   rG   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 ©Nry   r   ©r  rŠ   rå   r‹   r4   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å   Ú	exceptionrZ   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‹   r3   r   rÏ   Ú
isinstancer   r4   )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 )Nry   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   r3   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á   rX   TrÚ   s   OTHER)rŠ   r‹   r4   r   )r$   rY   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)rY   rÉ   zmust not be NonerÚ   rˆ   r(  rœ   )rY   r4   rÉ   zmust be NonerÞ   )rY   r4   rÛ   rÉ   )rÊ   rË   rŠ   rã   ró   r  r‹   rŒ   )r$   rY   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‹   r4   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=%E9rv   ré   )rŠ   r®   r‹   rG   r   r‘   r%   r%   r&   Útest_non_ascii_header¨  s    
z.HTTPClientCommonTestCase.test_non_ascii_headerc                 C   sš   t   ¡ }|  d¡}| ¡  |  |jd¡ |  |jd¡ |jd k	sDt‚|  t|j| ƒd¡ |j	 
¡ D ]0\}}|  d|  ko‚dk n  d||f ¡ qdd S )Nry   r   g      ð?ztime_info[%s] out of bounds: %s)ÚtimerŠ   r®   ZassertGreaterEqualr   Z
assertLessÚ
start_timerZ   ÚabsZ	time_infoÚitemsrÏ   )r$   r/  r   r`   ra   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 )Nry   r   )Úconnect_timeoutrˆ   )Úrequest_timeout)r3  r4  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   rA   )r  rŠ   rå   rÏ   Úcancelr   r   rq   r   rC   Zlogged_stack)r$   ZfutÚelrk   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-  r2  r5  r9  r%   r%   r%   r&   rx   ¤   s`   
	%	
	$

 
		




0
rx   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‹   r@  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 rC  )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 rC  )r
   r   rD  rº   r<  r%   r%   r&   Útest_defaults_noneô  s    z#RequestProxyTest.test_defaults_noneN)	r)   r*   r+   r>  r?  rB  rE  rG  rH  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+   rL  r%   r%   r%   r&   rI  ù  s   rI  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 rM   )Úserver_ioloopZrun_syncÚstartr%   )rR  r$   r%   r&   rT    s    z'SyncHTTPClientTest.setUp.<locals>.start)Útarget)r   rS  Ú	threadingÚEventr   rD   ÚThreadÚserver_threadrT  Úwaitr   r  )r$   rT  r%   )rQ  rR  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é   )rO  Zclose_all_connectionsrq   rS  Ústop)rk   r-   r%   r&   Ú	slow_stop#  s    zCSyncHTTPClientTest.tearDown.<locals>.stop_server.<locals>.slow_stop)rO  r]  r   rD   rS  Úadd_callback)r^  r-   r%   r&   Ústop_server  s    
z0SyncHTTPClientTest.tearDown.<locals>.stop_serverT)Zall_fds)rS  r_  rY  rp   r  r¦   )r$   r`  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å   3  s    zSyncHTTPClientTest.get_urlc                 C   s$   | j  |  d¡¡}|  d|j¡ d S )NrN  r   r  r‘   r%   r%   r&   Útest_sync_client6  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[  ra  rå   rc  rd  r%   r%   r%   r&   rM    s
   rM  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()Tr\  )ÚstdoutÚstderrÚcheckÚtimeoutzSTDOUT:z%subprocess produced unexpected output)
Ú
subprocessÚrunÚsysÚ
executableÚPIPEÚSTDOUTrf  Úprintr   Zfail)r$   Úprocr%   r%   r&   Útest_destructor_logC  s    ý÷z0SyncHTTPClientSubprocessTest.test_destructor_logN)r)   r*   r+   rr  r%   r%   r%   r&   re  B  s   re  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 )ÚHTTPRequestTestCasec                 C   s&   t dddid}|  |jddi¡ d S )NrJ  rì   rA  r  )r   r‹   rG   ©r$   r3   r%   r%   r&   Útest_headersa  s    z HTTPRequestTestCase.test_headersc                 C   s(   t dƒ}ddi|_|  |jddi¡ d S )NrJ  rA  Zbaz©r   rG   r‹   rt  r%   r%   r&   Útest_headers_settere  s    
z'HTTPRequestTestCase.test_headers_setterc                 C   s    t dƒ}d |_|  |ji ¡ d S )NrJ  rv  rt  r%   r%   r&   Útest_null_headers_setterj  s    z,HTTPRequestTestCase.test_null_headers_setterc                 C   s"   t ddd}|  |jtdƒ¡ d S )NrJ  rì   )r4   )r   r‹   r4   r   rt  r%   r%   r&   Ú	test_bodyo  s    zHTTPRequestTestCase.test_bodyc                 C   s$   t dƒ}d|_|  |jtdƒ¡ d S )NrJ  rì   )r   r4   r‹   r   rt  r%   r%   r&   Útest_body_setters  s    z$HTTPRequestTestCase.test_body_setterc                 C   s0   t j  ¡ }td|d}|  |jdt|ƒi¡ d S )NrJ  )Zif_modified_sincezIf-Modified-Since)ÚdatetimeÚutcnowr   r‹   rG   r   )r$   Z	http_dater3   r%   r%   r&   Útest_if_modified_sincex  s    
 
ÿz*HTTPRequestTestCase.test_if_modified_sinceN)	r)   r*   r+   ru  rw  rx  ry  rz  r}  r%   r%   r%   r&   rs  `  s   rs  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~  )Lræ   rî   Ú
contextlibr   r€  rg   rV  r{  Úior   rj  rl  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   Ztornado.webr   r   r   Ztornado.httputilr   r   r   r,   r0   r6   r<   r?   rE   rH   rL   rN   rP   rU   rW   r_   rd   ru   rx   ZTestCaser:  rI  rM  re  rs  r~  r%   r%   r%   r&   Ú<module>   sh   
	

    8!
? 