o
    ~îjÜÐ  ã                   @   sö  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 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Zd dlmZmZmZmZmZ d dlmZ d dlmZmZmZmZm Z  d dl!m"Z" d dl#m$Z$ d d	l%m&Z& d d
l'm(Z(m)Z) d dlm*Z* d dl+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1m2Z2 d dl3m4Z4m5Z5m6Z6 d dl7m8Z8m9Z9 zd dl:Z:W n e;yÅ   dZ:Y nw e <e:du d¡Z=G dd„ de5ƒZ>G dd„ de5ƒZ?G dd„ de5ƒZ@G dd„ de5ƒZAG dd„ de5ƒZBG dd„ de5ƒZCG dd„ de5ƒZDG dd „ d e5ƒZEG d!d"„ d"e5ƒZFG d#d$„ d$e5ƒZGG d%d&„ d&e5ƒZHG d'd(„ d(e5ƒZIG d)d*„ d*e5ƒZJG d+d,„ d,e5ƒZKG d-d.„ d.e5ƒZLG d/d0„ d0e5ƒZMG d1d2„ d2e5ƒZNd3ZOd4ZPejQdd5�eOfd6eRd7eSfd8d9„ƒZTd4ZUe  Vd:¡ZWejQdd5�d7eSfd;d<„ƒZXG d=d>„ d>e5ƒZYG d?d@„ d@e5ƒZZG dAdB„ dBe5ƒZ[G dCdD„ dDe,ƒZ\G dEdF„ dFej]ƒZ^G dGdH„ dHej]ƒZ_G dIdJ„ dJej]ƒZ`G dKdL„ dLej]ƒZaG dMdN„ dNej]ƒZbG dOdP„ dPej]ƒZcdQZdG dRdS„ dSe5ƒZeG dTdU„ dUe5ƒZfG dVdW„ dWe5ƒZge1G dXdY„ dYe,ƒƒZhdS )Zé    N)Úclosing)ÚBytesIO)Úutf8Ú
native_strÚ
to_unicodeÚjson_encodeÚjson_decode)Úgen)ÚHTTPRequestÚHTTPResponseÚ_RequestProxyÚ	HTTPErrorÚ
HTTPClient)Ú
HTTPServer)ÚIOLoop)ÚIOStream)Úgen_logÚapp_log)Únetutil)ÚAsyncHTTPTestCaseÚbind_unused_portÚgen_testÚ	ExpectLog)Úabstract_base_testÚignore_deprecation)ÚApplicationÚRequestHandlerÚurl)Úformat_timestampÚHTTPHeadersztracemalloc module not presentc                   @   ó   e Zd Zdd„ ZdS )ÚHelloWorldHandlerc                 C   s*   |   dd¡}|  dd¡ |  d| ¡ d S )NÚnameÚworldúContent-Typeú
text/plainz	Hello %s!)Úget_argumentÚ
set_headerÚfinish)Úselfr"   © r*   úf/raid/bulk_storage/M2HATS/real-image-venv/lib/python3.10/site-packages/tornado/test/httpclient_test.pyÚget3   s   zHelloWorldHandler.getN©Ú__name__Ú
__module__Ú__qualname__r,   r*   r*   r*   r+   r!   2   ó    r!   c                   @   r    )ÚPostHandlerc                 C   s"   |   d|  d¡|  d¡f ¡ d S )NzPost arg1: %s, arg2: %sÚarg1Úarg2)r(   r&   ©r)   r*   r*   r+   Úpost:   s
   ÿÿzPostHandler.postN©r.   r/   r0   r6   r*   r*   r*   r+   r2   9   r1   r2   c                   @   r    )Ú
PutHandlerc                 C   s   |   d¡ |   | jj¡ d S )Nz
Put body: ©ÚwriteÚrequestÚbodyr5   r*   r*   r+   ÚputB   s   
zPutHandler.putN)r.   r/   r0   r=   r*   r*   r*   r+   r8   A   r1   r8   c                   @   r    )ÚRedirectHandlerc                 C   s.   |   d¡ | j|  d¡t|  dd¡ƒd� d S )Nzredirects can have bodies toor   ÚstatusÚ302)r?   )r:   Úredirectr&   Úintr5   r*   r*   r+   ÚprepareH   s   

ÿzRedirectHandler.prepareN©r.   r/   r0   rC   r*   r*   r*   r+   r>   G   r1   r>   c                   @   r    )ÚRedirectWithoutLocationHandlerc                 C   s   |   d¡ |  ¡  d S )Né-  )Ú
set_statusr(   r5   r*   r*   r+   rC   P   s   
z&RedirectWithoutLocationHandler.prepareNrD   r*   r*   r*   r+   rE   O   r1   rE   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„?Úqwer)r:   Úflushr	   Úsleepr5   r*   r*   r+   r,   W   s
   €
zChunkHandler.getN)r.   r/   r0   r	   Ú	coroutiner,   r*   r*   r*   r+   rH   V   s    rH   c                   @   r    )ÚAuthHandlerc                 C   s   |   | jjd ¡ d S )NÚAuthorization)r(   r;   Úheadersr5   r*   r*   r+   r,   a   s   zAuthHandler.getNr-   r*   r*   r*   r+   rO   `   r1   rO   c                   @   r    )ÚCountdownHandlerc                 C   s8   t |ƒ}|dkr|  |  d|d ¡¡ d S |  d¡ d S )Nr   Ú	countdowné   ÚZero)rB   rA   Úreverse_urlr:   )r)   Úcountr*   r*   r+   r,   f   s   zCountdownHandler.getNr-   r*   r*   r*   r+   rR   e   r1   rR   c                   @   r    )ÚEchoPostHandlerc                 C   s   |   | jj¡ d S ©Nr9   r5   r*   r*   r+   r6   o   s   zEchoPostHandler.postNr7   r*   r*   r*   r+   rX   n   r1   rX   c                   @   r    )ÚUserAgentHandlerc                 C   s   |   | jj dd¡¡ d S )Nú
User-AgentzUser agent not set)r:   r;   rQ   r,   r5   r*   r*   r+   r,   t   s   zUserAgentHandler.getNr-   r*   r*   r*   r+   rZ   s   r1   rZ   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é*   )rG   r'   r5   r*   r*   r+   r,   y   s   
zContentLength304Handler.getc                 C   s   d S rY   r*   r5   r*   r*   r+   Ú_clear_representation_headers}   s   z5ContentLength304Handler._clear_representation_headersN)r.   r/   r0   r,   r`   r*   r*   r*   r+   r\   x   s    r\   c                   @   r    )ÚPatchHandlerc                 C   s   |   | jj¡ dS )z=Return the request payload - so we can check it is being keptNr9   r5   r*   r*   r+   Úpatch„   ó   zPatchHandler.patchN)r.   r/   r0   rb   r*   r*   r*   r+   ra   ƒ   r1   ra   c                   @   s>   e Zd Zejd Zdd„ Ze Z Z Z Z	 Z
 Z ZZdS )ÚAllMethodsHandler)ÚOTHERc                 C   s"   | j jd usJ ‚|  | j j¡ d S rY   )r;   Úmethodr:   r5   r*   r*   r+   rf   Œ   s   zAllMethodsHandler.methodN)r.   r/   r0   r   ÚSUPPORTED_METHODSrf   r,   Úheadr6   r=   ÚdeleteÚoptionsrb   Úotherr*   r*   r*   r+   rd   ‰   s    
$rd   c                   @   r    )ÚSetHeaderHandlerc                 C   s2   t |  d¡| jjd ƒD ]
\}}|  ||¡ qd S )NÚkÚv)ÚzipÚget_argumentsr;   Ú	argumentsr'   )r)   rm   rn   r*   r*   r+   r,   ”   s    ÿzSetHeaderHandler.getNr-   r*   r*   r*   r+   rl   “   r1   rl   c                   @   s   e Zd Zddd„ZdS )ÚInvalidGzipHandlerÚreturnNc                 C   s\   |   dd¡ |   dd¡ d dd„ tdƒD ƒ¡d d	… }tj| ¡ d
d�d }|  |¡ d S )Nr$   r%   úContent-EncodingÚgzipÚ c                 s   s   � | ]	}d |› d�V  qdS )zHello World Ú
Nr*   ©Ú.0Úir*   r*   r+   Ú	<genexpr>£   s   € z)InvalidGzipHandler.get.<locals>.<genexpr>i(#  i;F é   )Úcompressleveló    )r'   ÚjoinÚrangeru   ÚcompressÚencoder:   )r)   Útextr<   r*   r*   r+   r,   œ   s
    zInvalidGzipHandler.get)rs   Nr-   r*   r*   r*   r+   rr   ›   s    rr   c                   @   r    )ÚHeaderEncodingHandlerc                 C   s   |   | jjd  d¡¡ d S )NÚFooú	ISO8859-1)r(   r;   rQ   r‚   r5   r*   r*   r+   r,   ©   s   zHeaderEncodingHandler.getNr-   r*   r*   r*   r+   r„   ¨   r1   r„   c                   @   r    )ÚEchoHeadersHandlerc                 C   s   |   tt| jj ¡ ƒƒ¡ d S rY   )r:   r   Údictr;   rQ   Úget_allr5   r*   r*   r+   r,   ®   s   zEchoHeadersHandler.getNr-   r*   r*   r*   r+   r‡   ­   r1   r‡   i   i   )ÚmaxsizeÚsizers   c                    sV   t  dt jdt j ¡‰dd ‰ ‡ ‡fdd„t| tˆ ƒ ƒD ƒ}| ˆ ¡ ¡ d |¡S )z¦Returns a small gzip stream that expands to ``size`` bytes.

    Compressed incrementally so that building it does not itself allocate
    the decompressed size.
    é	   é   r~   i   c                    s   g | ]}ˆ  ˆ ¡‘qS r*   )r�   )ry   Ú_©ÚblockÚ
compressorr*   r+   Ú
<listcomp>Ê   s    zgzip_bomb.<locals>.<listcomp>ó    )	ÚzlibÚcompressobjÚDEFLATEDÚ	MAX_WBITSr€   ÚlenÚappendrL   r   )r‹   Úpiecesr*   r�   r+   Ú	gzip_bombÁ   s
    
r›   a4  y///P/gnAOKxQCD3/o///3/wTwDEYRGA7v0f////4J8AiMMCAN37P/7//8E/ARCHBQC693/8//+DfwIgDgsAdO//+P//B/8EQBwWAOje//H//w/+CYA4LADQvf/j//8f/BMAcVgAoHv/x///P/gnAOKwAED3/o///3/wTwDEYQGA7v0f////4J8AiMMCAN37P/7//8E/ARCHBQC693/8//+DfwIgDgsAdO//+P//B/8EQBwWAOje//H//w/+CYA4LADQvf/j//8f/BMAcVgAoHv/x///P/gnAOKwAED3/o///3/wTwDEYQGA7v0f////4J8AiMMCAN37P/7//8E/ARCHBQC693/8//+DfwIgDgsAdO//+P//B/8EQBwWAOje//H//w/+CYA4LADQvf/j//8f/BMAcVgAoHv/x///P/gnAOKwAED3/o///3/wTwDEYQGA7v0f////4J8AiMMCAN37P/7//8E/ARCHBQC693/8//+DfwIgDgsAdO//+P//B/8EQBwWAOje//H//w/+CYA4LADQvf/j//8f/BMAcVgAoHv/Nw==c                   C   s   d  dd„ tdƒD ƒ¡S )zÂReturns a body big enough to be delivered in several chunks.

    The contents are non-repeating so that a chunk which was dropped,
    duplicated or delivered out of order can be detected.
    r“   c                 s   s   � | ]}d | V  qdS )s   %09d
Nr*   rx   r*   r*   r+   r{   ê   s   € zlarge_body.<locals>.<genexpr>i † )r   r€   r*   r*   r*   r+   Ú
large_bodyã   s   rœ   c                   @   ó   e Zd ZdZdd„ ZdS )ÚGzipBombHandlerz=Sends a small response that decompresses to a very large one.c                 C   s2   t |  dttƒ¡ƒ}|  dd¡ |  t|ƒ¡ d S )Nr‹   rt   ru   )rB   r&   ÚstrÚGZIP_BOMB_SIZEr'   r:   r›   )r)   r‹   r*   r*   r+   r,   ð   s   zGzipBombHandler.getN©r.   r/   r0   Ú__doc__r,   r*   r*   r*   r+   rž   í   s    rž   c                   @   r�   )ÚBrotliBombHandlera!  Sends a brotli-encoded bomb that the client never asked for.

    No client implementation requests brotli, but libcurl builds its decoder
    from the ``Content-Encoding`` of the response without checking it against
    what was requested, so ``curl_httpclient`` decodes this anyway.
    c                 C   s   |   dd¡ |  t¡ d S )Nrt   Úbr)r'   r:   ÚBROTLI_BOMBr5   r*   r*   r+   r,   ÿ   ó   zBrotliBombHandler.getNr¡   r*   r*   r*   r+   r£   ÷   s    r£   c                   @   r    )ÚLargeBodyHandlerc                 C   s   |   dd¡ |  tƒ ¡ d S )Nr$   úapplication/octet-stream)r'   r:   rœ   r5   r*   r*   r+   r,     s   zLargeBodyHandler.getNr-   r*   r*   r*   r+   r§     r1   r§   c                       sÎ  e Zd ZdZdd„ Z‡ fdd„Zdedefdd	„Zd
d„ Zdd„ Z	dd„ Z
dd„ Zedd„ ƒZe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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"e#d>d?„ ƒZ$d@dA„ Z%dBdC„ Z&e#dDdE„ ƒZ'dFdG„ Z(e#dHdI„ ƒZ)e#dJdK„ ƒZ*e#dLdM„ ƒZ+e#dNdO„ ƒZ,e#dPdQ„ ƒZ-dRdS„ Z.dTdU„ Z/dVdW„ Z0dXdY„ Z1dZd[„ Z2d\d]„ Z3e#d^d_„ ƒZ4d`da„ Z5dbdc„ Z6‡  Z7S )dÚHTTPClientCommonTestCaseFc                 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ƒ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]+)rS   )r"   ú	/echopostú/user_agentú/304_with_content_lengthú/all_methodsú/patchz/set_headerú/invalid_gzipú/header-encodingú/echo_headersú
/gzip_bombú/brotli_bombú/large_bodyT)ru   )r   r   r!   r2   r8   r>   rE   rH   rO   rR   rX   rZ   r\   rd   ra   rl   rr   r„   r‡   rž   r£   r§   r5   r*   r*   r+   Úget_app  s0   ìéz HTTPClientCommonTestCase.get_appc                    s*   t ƒ  ¡  tƒ \}}| j |¡ || _d S rY   )ÚsuperÚsetUpr   Úhttp_serverÚ
add_socketÚ _HTTPClientCommonTestCase__port2)r)   ÚsockÚport©Ú	__class__r*   r+   r¼   3  s   


zHTTPClientCommonTestCase.setUpÚpathrs   c                 C   s   |   ¡ › d| j› |› �S )Nz://127.0.0.1:)Úget_protocolr¿   ©r)   rÄ   r*   r*   r+   Úget_url2<  s   z!HTTPClientCommonTestCase.get_url2c                 C   s4   d}| j dd|d�}|  |jd¡ |  |j|¡ d S )Ns   some patch datar³   ÚPATCH©rf   r<   éÈ   ©ÚfetchÚassertEqualÚcoder<   )r)   r<   Úresponser*   r*   r+   Útest_patch_receives_payload?  s   z4HTTPClientCommonTestCase.test_patch_receives_payloadc                 C   st   |   d¡}|  |jd¡ |  |jd d¡ |  |jd¡ |jd us#J ‚|  t|jƒd¡ |   d¡}|  |jd¡ d S )	Nrª   rÊ   r$   r%   ó   Hello world!r   z/hello?name=Bens
   Hello Ben!)rÌ   rÍ   rÎ   rQ   r<   Úrequest_timerB   ©r)   rÏ   r*   r*   r+   Útest_hello_worldE  s   

z)HTTPClientCommonTestCase.test_hello_worldc                 C   s2   g }| j d|jd�}|  |dg¡ |  |j¡ d S )Nrª   ©Ústreaming_callbackrÑ   )rÌ   r™   rÍ   ÚassertFalser<   ©r)   ÚchunksrÏ   r*   r*   r+   Útest_streaming_callbackP  s   z0HTTPClientCommonTestCase.test_streaming_callbackc                 C   sF   g }| j d|jd�}|  |jd¡ |  d |¡tƒ ¡ |  |j¡ d S )Nr¹   rÕ   rÊ   r“   )rÌ   r™   rÍ   rÎ   r   rœ   r×   r<   rØ   r*   r*   r+   Ú"test_streaming_callback_large_bodyX  s
   z;HTTPClientCommonTestCase.test_streaming_callback_large_bodyc                    sŽ   t ƒ  d‰ ‡ fdd„}t ¡  zt ¡  | jd|d�}t ¡ d }W t ¡  nt ¡  w |  |jd¡ |  ˆ t	¡ |  
|j¡ |  |d¡ d S )	Nr   c                    ó   ˆ t | ƒ7 ‰ d S rY   ©r˜   ©Úchunk©Úreceivedr*   r+   rÖ   j  ó   zVHTTPClientCommonTestCase.test_streaming_decompression_bomb.<locals>.streaming_callbackr·   rÕ   rT   rÊ   é   )r›   ÚtracemallocÚstartÚ
reset_peakrÌ   Úget_traced_memoryÚstoprÍ   rÎ   r    r×   r<   Ú
assertLess)r)   rÖ   rÏ   Úpeakr*   rà   r+   Ú!test_streaming_decompression_bomba  s   z:HTTPClientCommonTestCase.test_streaming_decompression_bombc              	   C   s¶   t tƒ t ¡  zCt ¡  |  t¡�& ttdƒ� | j	dt dd� W d   ƒ n1 s,w   Y  W d   ƒ n1 s;w   Y  t 
¡ d }W t ¡  nt ¡  w |  |td ¡ d S )Nz.*decompressed body too largez/gzip_bomb?size=%dT©Úraise_errorrT   é   )r›   ÚUNBUFFERABLE_BOMB_SIZErä   rå   ræ   ÚassertRaisesr   r   r   rÌ   rç   rè   ré   )r)   rê   r*   r*   r+   Ú2test_decompression_bomb_without_streaming_callback{  s"   ÿþÿü€ zKHTTPClientCommonTestCase.test_decompression_bomb_without_streaming_callbackc                    sš   | j s|  d¡ d‰ ‡ fdd„}t ¡  z#t ¡  z	| jd|d� W n	 ty+   Y nw t ¡ d }W t ¡  nt ¡  w |  	ˆ t
tƒ¡ |  |d¡ d S )	Nz/client does not decompress Content-Encoding: brr   c                    rÜ   rY   rÝ   rÞ   rà   r*   r+   rÖ   Ÿ  râ   z[HTTPClientCommonTestCase.test_streaming_unsolicited_brotli_bomb.<locals>.streaming_callbackr¸   rÕ   rT   rã   )Údecompresses_brotliÚskipTesträ   rå   ræ   rÌ   r   rç   rè   ÚassertGreaterr˜   r¥   ré   )r)   rÖ   rê   r*   rà   r+   Ú&test_streaming_unsolicited_brotli_bomb”  s    
ÿz?HTTPClientCommonTestCase.test_streaming_unsolicited_brotli_bombc                 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Í   r<   r™   r×   )r)   rÏ   rÙ   r*   r*   r+   Útest_chunked¸  s   
z%HTTPClientCommonTestCase.test_chunkedc                    sŠ   t ƒ \}}t|ƒ�2 tj‡ fdd„ƒ}t ||¡ ˆ  d| ¡}| ¡  ˆ  |j	d¡ ˆ j
 | ¡ ¡ W d   ƒ d S 1 s>w   Y  d S )Nc                 3   óH   � t | ƒ}| d¡V }d|vrˆ  d¡ | d dd¡¡V  | ¡  d S )Nó   

ó   HTTP/1.úrequires HTTP/1.xs7   HTTP/1.1 200 OK
Transfer-Encoding: chunked

1
1
1
2
0

ó   
ó   
©r   Ú
read_untilró   r:   ÚreplaceÚclose©ÚconnÚaddressÚstreamÚrequest_datar5   r*   r+   Úaccept_callbackÇ  s   €

ÿõzDHTTPClientCommonTestCase.test_chunked_close.<locals>.accept_callbackúhttp://127.0.0.1:%d/s   12)r   r   r	   rN   r   Úadd_accept_handlerrÌ   ÚrethrowrÍ   r<   Úio_loopÚremove_handlerÚfileno©r)   rÀ   rÁ   r  Úrespr*   r5   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Ì   r<   r5   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  r5   r*   r*   r+   Útest_basic_auth_explicit_modeï  s   üùz6HTTPClientCommonTestCase.test_basic_auth_explicit_modec                 C   sD   |   | jdddd�jd¡ t dd¡}|   | jd|dd�jd	¡ d S )
Nr®   Útestu   123Â£r  s   Basic dGVzdDoxMjPCow==ÚNFDu   josÃ©u   sÉ™crÉ™ts   Basic am9zw6k6c8mZY3LJmXQ=)rÍ   rÌ   r<   ÚunicodedataÚ	normalize)r)   Úusernamer*   r*   r+   Útest_basic_auth_unicodeú  s   þþz0HTTPClientCommonTestCase.test_basic_auth_unicodec              	   C   s†   t tddd��2 |  ttf¡� | jdddddd	� W d   ƒ n1 s$w   Y  W d   ƒ d S W d   ƒ d S 1 s<w   Y  d S )
Nzuncaught exceptionF©Úrequiredr®   r  r  rI   T)r  r  r  rí   )r   r   rð   Ú
ValueErrorr   rÌ   r5   r*   r*   r+   Útest_unsupported_auth_mode	  s   ûÿÿ"ÿz3HTTPClientCommonTestCase.test_unsupported_auth_modec                 C   sn   | j ddd�}|  d|j¡ |  |jd  d¡¡ |   d¡}|  d|j¡ |  |j d¡¡ |  d	|j¡ d S )
Nz/countdown/2F©Úfollow_redirectsé.  ÚLocationz/countdown/1rÊ   z/countdown/0s   Zero)rÌ   rÍ   rÎ   Ú
assertTruerQ   ÚendswithÚeffective_urlr<   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*  rF   ©rÌ   rÍ   rÎ   rÓ   r*   r*   r+   Útest_redirect_without_location!  s   z7HTTPClientCommonTestCase.test_redirect_without_locationc                 C   ó"   | j dddd�}|  |jd¡ d S )Nú/redirect?url=/put&status=307ÚPUTÚhellorÉ   ó   Put body: hello©rÌ   rÍ   r<   rÓ   r*   r*   r+   Útest_redirect_put_with_body)  s   ÿz4HTTPClientCommonTestCase.test_redirect_put_with_bodyc                 C   r4  )Nr5  r6  T©rf   Úallow_nonstandard_methodss
   Put body: r9  rÓ   r*   r*   r+   Útest_redirect_put_without_body/  s   ýz7HTTPClientCommonTestCase.test_redirect_put_without_bodyc                 C   s  dD ]P}d| }| j |ddd�}|  d|j¡ dD ]#}| j ||dd	�}|d
v r1|  t|ƒ|j¡ q|  |jt|ƒdg¡ q| j |dd�}|  d|j¡ |  d|j¡ qdD ]/}d}dD ]}| j ||dd	�}|  |t|jƒ¡ q[| j |dd�}|  d|j¡ |  d|j¡ qUd S )N)rF   r,  i/  z$/redirect?url=/all_methods&status=%drö   r“   rÉ   s   GET)ÚGETÚOPTIONSr6  ÚDELETETr;  )rF   r,  ÚHEAD©rf   rÊ   )i3  i4  z%/redirect?url=/all_methods&status=307)r>  r?  rö   r6  r@  )rÌ   rÍ   r<   r   ÚassertInrÎ   r   )r)   r?   r   r  rf   r*   r*   r+   Útest_method_after_redirect8  s*   ùz3HTTPClientCommonTestCase.test_method_after_redirectc                 C   s8   |   d¡ dd¡}|  |¡}|  dt d¡ |j¡ d S )Nr®   úhttp://zhttp://me:secret@s   Basic s	   me:secret)Úget_urlr  rÌ   rÍ   Úbase64Ú	b64encoder<   )r)   r   rÏ   r*   r*   r+   Útest_credentials_in_urlV  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)rf   r<   rQ   r^   Ú2Ú1Úfoo)rf   r<   rQ   Ú
user_agent)ÚbinasciiÚa2b_hexrÌ   rÍ   rQ   r<   r   )r)   Úunicode_bodyÚ	byte_bodyrÏ   r*   r*   r+   Útest_body_encoding[  s6   
üüûz+HTTPClientCommonTestCase.test_body_encodingc                 C   sZ   |   d¡}|  t|jƒt¡ |  t|jd ƒt¡ |  t|jƒt¡ |  t|j	ƒt¡ d S )Nrª   r$   )
rÌ   rÍ   Útyper<   ÚbytesrQ   rŸ   rÎ   rB   r0  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-Encodingru   )Údecompress_responserQ   rt   rø   é"   Úr)ÚmodeÚfileobj)
rÌ   rÍ   rQ   ÚassertNotEqualr<   r˜   ru   ÚGzipFileÚbufferÚread)r)   rÏ   Úfr*   r*   r+   Ú	test_gzip†  s   
ÿz"HTTPClientCommonTestCase.test_gzipc              	   C   sŠ   t tddd��4 z|  d¡}|  |jd¡ |  |jd d… d¡ W n	 ty*   Y n	w W d   ƒ d S W d   ƒ d S 1 s>w   Y  d S )Nz..*Malformed HTTP message.*unconsumed gzip dataFr&  r´   rÊ   é   s   Hello World 0
)r   r   rÌ   rÍ   rÎ   r<   r   rÓ   r*   r*   r+   Útest_invalid_gzip—  s   ÿ
ÿÿú"øz*HTTPClientCommonTestCase.test_invalid_gzipc                    sp   g ‰i ‰g ‰ ‡‡fdd„}‡ ‡‡fdd„}ˆj d||d� ˆ tˆƒdˆ¡ ˆ ˆd d	¡ ˆ ˆ d
dg¡ d S )Nc                    sV   |   d¡rd S |   d¡rˆ  | ¡ d S | dkr)|  dd¡\}}| ¡ ˆ| ¡ < d S d S )NzHTTP/1.1 101úHTTP/z
ú:rT   )Ú
startswithr™   ÚsplitÚstripÚlower)Úheader_linerm   rn   )Ú
first_linerQ   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™   rÞ   )rÙ   rQ   r)   r*   r+   rÖ   ³  s   zIHTTPClientCommonTestCase.test_header_callback.<locals>.streaming_callbackr­   )rl  rÖ   rT   r   zHTTP/[0-9]\.[0-9] 200.*
rù   rú   )rÌ   rÍ   r˜   ÚassertRegex)r)   rl  rÖ   r*   )rÙ   rk  rQ   r)   r+   Útest_header_callback¤  s   
ýz-HTTPClientCommonTestCase.test_header_callbackc                    sN   t ƒ ‰ ‡ fdd„}| jd|d� ˆ  ¡ D ]\}}|  || ¡ k||f¡ qd S )Nc                    s   |   d¡rd S ˆ  | ¡ d S )Nrd  )rf  Ú
parse_line)Úline©rQ   r*   r+   rl  Ê  s   
zTHTTPClientCommonTestCase.test_header_callback_to_parse_line.<locals>.header_callbackrª   )rl  )r   rÌ   r‰   r.  rh  )r)   rl  rm   rn   r*   rq  r+   Ú"test_header_callback_to_parse_lineÃ  s   ÿz;HTTPClientCommonTestCase.test_header_callback_to_parse_linec                 c   sX   � t ddd�}| jjd|d�}z| |  d¡¡V }|  |jd¡ W | ¡  d S | ¡  w )NÚTestDefaultUserAgentF)rN  Ú
allow_ipv6T)Úforce_instanceÚdefaultsr°   s   TestDefaultUserAgent)rˆ   Úhttp_clientrÃ   rÌ   rF  rÍ   r<   r  )r)   rv  ÚclientrÏ   r*   r*   r+   Útest_configure_defaultsÔ  s   €z0HTTPClientCommonTestCase.test_configure_defaultsc              
   C   sT   dD ]%}t tfD ]}|ƒ }||d< | jd|d�}|  |jdd|j||f ¡ qqd S )N)ÚMyUserAgentó   MyUserAgentr[   r°   rq  r{  z#response=%r, value=%r, container=%r)rˆ   r   rÌ   rÍ   r<   )r)   ÚvalueÚ	containerrQ   r  r*   r*   r+   Útest_header_typesß  s   
ÿýüÿz*HTTPClientCommonTestCase.test_header_typesc                    s¦   t ƒ \}}t|ƒ�@ tj‡ fdd„ƒ}t ||¡ zˆ  d| ¡}| ¡  ˆ  |j	d d¡ W ˆ j
 | ¡ ¡ n	ˆ j
 | ¡ ¡ w W d   ƒ d S 1 sLw   Y  d S )Nc                 3   rü   )Nrý   rþ   rÿ   s2   HTTP/1.1 200 OK
X-XSS-Protection: 1;
	mode=block

r   r  r  r  r5   r*   r+   r  ö  s   €
ÿú
zIHTTPClientCommonTestCase.test_multi_line_headers.<locals>.accept_callbackr  zX-XSS-Protectionz1; mode=block)r   r   r	   rN   r   r  rÌ   r  rÍ   rQ   r  r  r  r  r*   r5   r+   Útest_multi_line_headersð  s   

&"æz0HTTPClientCommonTestCase.test_multi_line_headersc                 c   s6   � | j j|  d¡ddid�V }|  |jd d¡¡ d S )Nrµ   r…   u   bÃ¤rrq  r†   )rw  rÌ   rF  rÍ   r<   r‚   rÓ   r*   r*   r+   Útest_header_encoding  s   €ÿþz-HTTPClientCommonTestCase.test_header_encodingc                 C   s.   |   d¡}|  |jd¡ |  |jd d¡ d S )Nr±   r]   r^   Ú42)rÌ   rÍ   rÎ   rQ   rÓ   r*   r*   r+   Útest_304_with_content_length  s   
z5HTTPClientCommonTestCase.test_304_with_content_lengthc                 c   s(   � | j  |  d¡¡V }|  |jd¡ d S ©Nrª   rÑ   ©rw  rÌ   rF  rÍ   r<   rÓ   r*   r*   r+   Útest_future_interface#  s   €z.HTTPClientCommonTestCase.test_future_interfacec                 c   s„   � |   t¡�}| j |  d¡¡V  W d   ƒ n1 sw   Y  |jd us'J ‚|jjd us/J ‚|  |jjd¡ |  |jjjd¡ d S ©Nú	/notfoundé”  )	rð   r   rw  rÌ   rF  Ú	exceptionrÏ   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‡  Frì   rˆ  )rw  rÌ   rF  rÍ   rÎ   rÓ   r*   r*   r+   Útest_future_http_error_no_raise1  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ƒ  )
rF  rw  rÌ   rÍ   r;   r   r.  Ú
isinstancer
   r<   )r)   r   rÏ   Ú	response2r*   r*   r+   Ú test_reuse_request_from_response8  s   €
z9HTTPClientCommonTestCase.test_reuse_request_from_responsec                 c   s¢   � |   d¡}t|dd�}| j |¡V }|  |jd¡ |  ttf¡�}t|dd�}| j |¡V  W d   ƒ n1 s9w   Y  |  	dt
|jƒv pMdt
|jƒv ¡ d S )Nrª   z	127.0.0.1©Únetwork_interfacerÊ   znot-interface-or-ipz#Failed binding local connection end)rF  r
   rw  rÌ   rÍ   rÎ   rð   r(  r   r.  rŸ   r‰  )r)   r   r;   rÏ   rŠ  r*   r*   r+   Útest_bind_source_ipE  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²   rB  ©rö   r6  rÈ   r“   rÉ   rA  re   Tr;  s   OTHER)rÌ   rÍ   r<   r   )r)   rf   rÏ   r*   r*   r+   Útest_all_methodsT  s   ÿz)HTTPClientCommonTestCase.test_all_methodsc              	   C   sú   dD ]7}|   t¡�}| jd|dd� W d   ƒ n1 sw   Y  |  dt|jƒ¡ | jd|dd�}|  |jd¡ qdD ]>}|   t¡�}| jd|d	dd
� W d   ƒ n1 sWw   Y  |  dt|jƒ¡ |dkrz| jd|d	ddd� |  |jd¡ q<d S )Nr”  r²   T)rf   rí   zmust not be Noner;  rÊ   r“  rù   )rf   r<   rí   zmust be Noner>  )rf   r<   r<  rí   )rð   r(  rÌ   rC  rŸ   r‰  rÍ   rÎ   )r)   rf   rŠ  r  r*   r*   r+   Útest_body_sanity_checksb  s6   ÿÿÿÿû€ïz0HTTPClientCommonTestCase.test_body_sanity_checksc                 C   s*   | j dddd�}| ¡  |  |jd¡ d S )Nz/redirect?status=307&url=/putr6  s   hellorÉ   r8  )rÌ   r  rÍ   r<   rÓ   r*   r*   r+   Útest_put_307’  s
   ÿz%HTTPClientCommonTestCase.test_put_307c                 C   s,   |   d¡}| ¡  |  |jd tdƒ¡ d S )Nz/set_header?k=foo&v=%E9r…   rJ  )rÌ   r  rÍ   rQ   r   rÓ   r*   r*   r+   Útest_non_ascii_header™  s   
z.HTTPClientCommonTestCase.test_non_ascii_headerc                 C   sÄ   t   ¡ }|  d¡}| ¡  |  |j¡ |jd usJ ‚|  |jd¡ |  |jd¡ |  |j¡ |jd us5J ‚|  t|j| ƒd¡ |j	 
¡ D ]\}}|  d|  koTdk n  d|› d|› �¡ qEd S )Nrª   r   g      ð?z
time_info[z] out of bounds: )ÚtimerÌ   r  ÚassertIsNotNonerÒ   ÚassertGreaterEqualré   Ú
start_timeÚabsÚ	time_infoÚitemsr.  )r)   rœ  rÏ   rm   rn   r*   r*   r+   Útest_response_timesŸ  s   
.ÿz,HTTPClientCommonTestCase.test_response_timesc                 C   sZ   | j ddd�}|  |jd¡ | j ddd�}|  |jd¡ | j dddd�}|  |jd¡ d S )Nrª   r   )Úconnect_timeoutrÊ   )Úrequest_timeout)r¡  r¢  r2  rÓ   r*   r*   r+   Útest_zero_timeout³  s   z*HTTPClientCommonTestCase.test_zero_timeoutc                 c   s†   � | j  |  d¡¡}|  | ¡ ¡ ttdƒ�#}tdƒD ]}t 	d¡V  |j
r( n	qW d   ƒ d S W d   ƒ d S 1 s<w   Y  d S )Nz/404z$Exception after Future was cancelledéd   rJ   )rw  rÌ   rF  r.  Úcancelr   r   r€   r	   rM   Úlogged_stack)r)   ÚfutÚelrz   r*   r*   r+   Útest_error_after_cancel½  s   €ÿþû"øz0HTTPClientCommonTestCase.test_error_after_cancelc              
   C   sæ   dD ]n\}}| j |dd��& |  t¡� | jdd|id� W d   ƒ n1 s'w   Y  W d   ƒ n1 s6w   Y  | j |dd��& |  t¡� | jd|did� W d   ƒ n1 s\w   Y  W d   ƒ n1 skw   Y  qd S )N))zfoobar:Úcr)zfoo
bar:Úlf)z	foo
bar:Úcrlfr|  )r"   Úpositionrª   rM  rq  Úkey)ÚsubTestrð   r(  rÌ   )r)   Úheaderr"   r*   r*   r+   Útest_header_crlfË  s   ÿ€ÿÿ€ÿ€øz)HTTPClientCommonTestCase.test_header_crlfc              	   C   s®  dt ddid�dfdt ƒ dfdt d	dd
�dfdt ddid�dfg}|D ]±\}}}| j|dd��E |  d|  d¡ ¡}|rE| dd| d¡}| jdi t |d�|B ¤Ž}| ¡  t|jƒ}|  	d|¡ |  
d|¡ |  
d|¡ W d   ƒ n1 sww   Y  | j|dd��I |  d|  d¡ ¡}|r™| dd| d¡}| jdi t |d�|B ¤Ž}| ¡  t|jƒ}|  	d|¡ |dkrºn|  d|v pÃd|v ¡ W d   ƒ n1 sÏw   Y  q#d S )Nzmanual auth headerrP   Úsecretrq  rv   zcredentials in URLz	me:secretzauth parametersÚmer  zmanual cookie headerÚCookieÚ	different)r"   Úoriginz/redirect?url=%s&status=302r¶   rE  z
http://%s@rT   )rÄ   r[   Úsamer*   )rˆ   r¯  rF  rÇ   r  rÌ   r  r   r<   rC  ÚassertNotInr.  )r)   Ú
test_casesr"   ÚkwargsÚ	url_credsr   rÏ   Úechoed_headersr*   r*   r+   Útest_strip_headers_on_redirectÛ  sL   
üÿ
îÿ
ÿ€é€ìz7HTTPClientCommonTestCase.test_strip_headers_on_redirect)8r.   r/   r0   rò   rº   r¼   rŸ   rÇ   rÐ   rÔ   rÚ   rÛ   ÚskipIfNoTracemallocrë   rñ   rõ   r÷   rû   r  r  r  r%  r)  r1  r3  r:  r=  rD  rI  rS  rV  ra  rc  rn  rr  r   ry  r~  r  r€  r‚  r…  r‹  rŒ  r�  r’  r•  r–  r—  r˜  r   r£  r©  r±  r½  Ú__classcell__r*   r*   rÂ   r+   r©     sz    		


	%	
	$

 
		




0

r©   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚRequestProxyTestc                 C   s&   t tddd�tƒ ƒ}|  |jd¡ d S )Núhttp://example.com/rM  )rN  )r   r
   rˆ   rÍ   rN  ©r)   Úproxyr*   r*   r+   Útest_request_set  ó   ÿz!RequestProxyTest.test_request_setc                 C   s&   t tdƒtdd�ƒ}|  |jd¡ d S )NrÁ  rM  r�  )r   r
   rˆ   rÍ   r‘  rÂ  r*   r*   r+   Útest_default_set  rÅ  z!RequestProxyTest.test_default_setc                 C   s*   t tddd�tdd�ƒ}|  |jd¡ d S )NrÁ  rM  )Ú
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 S ©NrÁ  )r   r
   rˆ   ÚassertIsNoner  rÂ  r*   r*   r+   Útest_neither_set'  s   z!RequestProxyTest.test_neither_setc                 C   sF   t tdƒtƒ ƒ}|  t¡� |j W d   ƒ d S 1 sw   Y  d S rÊ  )r   r
   rˆ   rð   ÚAttributeErrorrM  rÂ  r*   r*   r+   Útest_bad_attribute+  s   "ÿz#RequestProxyTest.test_bad_attributec                 C   s   t tdƒd ƒ}|  |j¡ d S rÊ  )r   r
   rË  r  rÂ  r*   r*   r+   Útest_defaults_none0  s   z#RequestProxyTest.test_defaults_noneN)	r.   r/   r0   rÄ  rÆ  rÉ  rÌ  rÎ  rÏ  r*   r*   r*   r+   rÀ    s    rÀ  c                   @   r    )ÚHTTPResponseTestCasec                 C   s<   t tdƒdtƒ d�}t|ƒ}|  | d¡¡ |  d|¡ d S )Núhttp://example.comrÊ   )r^  zHTTPResponse(zcode=200)r   r
   r   rŸ   r.  rf  rC  )r)   rÏ   Úsr*   r*   r+   Útest_str6  s   ÿzHTTPResponseTestCase.test_strN)r.   r/   r0   rÓ  r*   r*   r*   r+   rÐ  5  r1   rÐ  c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚSyncHTTPClientTestc                    sb   t dd�ˆ_t ¡ ‰ tj‡ ‡fdd„ƒ‰‡‡fdd„}tj|d�ˆ_ˆj ¡  ˆ  	¡  t
ƒ ˆ_d S )NF)Úmake_currentc                     s<   t ƒ \} ˆ_tdtfgƒ}t|ƒˆ_ˆj | ¡ ˆ  ¡  d S )Nú/)r   rÁ   r   r!   r   Úserverr¾   Úset)rÀ   Úapp)Úeventr)   r*   r+   Úinit_serverD  s
   
z-SyncHTTPClientTest.setUp.<locals>.init_serverc                      s   ˆj  ˆ ¡ ˆj  ¡  d S rY   )Úserver_ioloopÚrun_syncrå   r*   )rÛ  r)   r*   r+   rå   L  r¦   z'SyncHTTPClientTest.setUp.<locals>.start)Útarget)r   rÜ  Ú	threadingÚEventr	   rN   ÚThreadÚserver_threadrå   Úwaitr   rw  )r)   rå   r*   )rÚ  rÛ  r)   r+   r¼   @  s   
zSyncHTTPClientTest.setUpc                    s>   ‡ fdd„}ˆ j  |¡ ˆ j ¡  ˆ j ¡  ˆ j jdd� d S )Nc                     s,   ˆ j  ¡  tj‡ fdd„ƒ} ˆ j | ¡ d S )Nc                  3   s0   � ˆ j  ¡ V  tdƒD ]} d V  qˆ j ¡  d S )Né   )r×  Úclose_all_connectionsr€   rÜ  rè   )rz   r5   r*   r+   Ú	slow_stop_  s
   €zCSyncHTTPClientTest.tearDown.<locals>.stop_server.<locals>.slow_stop)r×  rè   r	   rN   rÜ  Úadd_callback)ræ  r5   r*   r+   Ústop_serverW  s   
z0SyncHTTPClientTest.tearDown.<locals>.stop_serverT)Úall_fds)rÜ  rç  râ  r   rw  r  )r)   rè  r*   r5   r+   ÚtearDownV  s
   

zSyncHTTPClientTest.tearDownc                 C   s   d| j |f S )Nzhttp://127.0.0.1:%d%s)rÁ   rÆ   r*   r*   r+   rF  o  s   zSyncHTTPClientTest.get_urlc                 C   s$   | j  |  d¡¡}|  d|j¡ d S )NrÖ  rÑ   r„  rÓ   r*   r*   r+   Útest_sync_clientr  s   z#SyncHTTPClientTest.test_sync_clientc                 C   sP   |   t¡�}| j |  d¡¡ W d   ƒ n1 sw   Y  |  |jjd¡ d S r†  )rð   r   rw  rÌ   rF  rÍ   r‰  rÎ   )r)   Ú	assertionr*   r*   r+   Útest_sync_client_errorv  s   ÿz)SyncHTTPClientTest.test_sync_client_errorN)r.   r/   r0   r¼   rê  rF  rë  rí  r*   r*   r*   r+   rÔ  ?  s    rÔ  c                   @   r    )ÚSyncHTTPClientSubprocessTestc                 C   sT   t jtjddgt jt jddd�}|jrtdƒ tt|jƒƒ |jr(|  	d¡ d S d S )Nz-czMfrom tornado.httpclient import HTTPClient; f = lambda: None; c = HTTPClient()Té   )ÚstdoutÚstderrÚcheckÚtimeoutzSTDOUT:z%subprocess produced unexpected output)
Ú
subprocessÚrunÚsysÚ
executableÚPIPEÚSTDOUTrð  Úprintr   Úfail)r)   Úprocr*   r*   r+   Útest_destructor_log  s    ý÷ÿz0SyncHTTPClientSubprocessTest.test_destructor_logN)r.   r/   r0   rý  r*   r*   r*   r+   rî  ~  r1   rî  c                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚHTTPRequestTestCasec                 C   s&   t dddid�}|  |jddi¡ d S )NrÑ  rM  rÈ  rq  )r
   rÍ   rQ   ©r)   r;   r*   r*   r+   Útest_headers�  s   z HTTPRequestTestCase.test_headersc                 C   s(   t dƒ}ddi|_|  |jddi¡ d S )NrÑ  rÈ  Úbaz©r
   rQ   rÍ   rÿ  r*   r*   r+   Útest_headers_setter¡  s   
z'HTTPRequestTestCase.test_headers_setterc                 C   s    t dƒ}d |_|  |ji ¡ d S )NrÑ  r  rÿ  r*   r*   r+   Útest_null_headers_setter¦  s   z,HTTPRequestTestCase.test_null_headers_setterc                 C   s"   t ddd�}|  |jtdƒ¡ d S )NrÑ  rM  )r<   )r
   rÍ   r<   r   rÿ  r*   r*   r+   Ú	test_body«  ó   zHTTPRequestTestCase.test_bodyc                 C   s$   t dƒ}d|_|  |jtdƒ¡ d S )NrÑ  rM  )r
   r<   rÍ   r   rÿ  r*   r*   r+   Útest_body_setter¯  s   z$HTTPRequestTestCase.test_body_setterc                 C   s6   t j  t jj¡}td|d�}|  |jdt|ƒi¡ d S ©NrÑ  )Úif_modified_sincezIf-Modified-Since)ÚdatetimeÚnowÚtimezoneÚutcr
   rÍ   rQ   r   ©r)   Ú	http_dater;   r*   r*   r+   Útest_if_modified_since´  s
   ÿz*HTTPRequestTestCase.test_if_modified_sincec                 C   sV   t ƒ � tj ¡ }W d   ƒ n1 sw   Y  td|d�}|  |jdt|ƒi¡ d S r  )r   r
  Úutcnowr
   rÍ   rQ   r   r  r*   r*   r+   Ú'test_if_modified_since_naive_deprecated»  s   ÿÿz;HTTPRequestTestCase.test_if_modified_since_naive_deprecatedN)
r.   r/   r0   r   r  r  r  r  r  r  r*   r*   r*   r+   rþ  œ  s    rþ  c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚHTTPErrorTestCasec                 C   s2   t dƒ}t |¡}|  ||¡ |  |j|j¡ d S )Né“  )r   ÚcopyÚassertIsNotrÍ   rÎ   )r)   ÚeÚe2r*   r*   r+   Ú	test_copyÅ  s   
zHTTPErrorTestCase.test_copyc                 C   s,   t dƒ}|  t|ƒd¡ |  t|ƒd¡ d S )Nr  úHTTP 403: Forbidden)r   rÍ   rŸ   Úrepr)r)   r  r*   r*   r+   Útest_plain_errorË  s   z"HTTPErrorTestCase.test_plain_errorc                 C   sj   t tdƒdƒ}|  t¡�}| ¡  W d   ƒ n1 sw   Y  |j}|  t|ƒd¡ |  t|ƒd¡ d S )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/   r0   r  r  r  r*   r*   r*   r+   r  Ä  s    r  i   c                   @   r    )ÚMaxBodySizeSmallHandlerc                 C   s   |   dd¡ |  dt ¡ d S )Nr$   r¨   ó   a©r'   r:   ÚMAX_BODY_SIZEr5   r*   r*   r+   r,   ß  s   zMaxBodySizeSmallHandler.getNr-   r*   r*   r*   r+   r  Þ  r1   r  c                   @   r    )ÚMaxBodySizeLargeHandlerc                 C   s"   |   dd¡ |  dtd  ¡ d S )Nr$   r¨   r   rT   r!  r5   r*   r*   r+   r,   å  r  zMaxBodySizeLargeHandler.getNr-   r*   r*   r*   r+   r#  ä  r1   r#  c                   @   r�   )ÚBodyBearingRedirectHandlera-  A redirect carrying a body of its own, larger than the limit.

    Implementations differ here: libcurl discards the body of a redirect it
    follows, while simple_httpclient applies ``max_body_size`` to every
    response including the redirect, and so refuses this one before
    following it.
    c                 C   s    |   dtd  ¡ |  d¡ d S )NÚxrî   ú/small)r:   r"  rA   r5   r*   r*   r+   rC   ó  s   z"BodyBearingRedirectHandler.prepareN)r.   r/   r0   r¢   rC   r*   r*   r*   r+   r$  ê  s    r$  c                   @   s\   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚHTTPClientMaxBodySizeTestCasea4  Tests for the limit on a response body held in memory.

    Without a ``streaming_callback`` the whole body is buffered, so a
    response larger than the client is prepared to hold cannot be retrieved
    at all -- it can only be refused. Subclasses supply a client configured
    with `MAX_BODY_SIZE`.
    Nc                 C   s(   t tdtƒtdtƒtdtƒtdtƒgƒS )Nr&  ú/largeú/bombz/body_bearing_redirect)r   r   r  r#  rž   r$  r5   r*   r*   r+   rº     s   üÿz%HTTPClientMaxBodySizeTestCase.get_appc              	   K   s¦   t  ¡ �=}| jd ur| tt| jj|d�tjd�¡ |  	t
¡�}| j|fddi|¤Ž W d   ƒ n1 s5w   Y  W d   ƒ n1 sDw   Y  |  |jjd¡ d S )N©Úreason)Úlevelrí   TiW  )Ú
contextlibÚ	ExitStackÚrefusal_log_formatÚenter_contextr   r   ÚformatÚloggingÚINFOrð   r   rÌ   rÍ   r‰  rÎ   )r)   rÄ   r+  Úfetch_kwargsÚstackr  r*   r*   r+   Úassert_refused  s   

ýÿÿ€÷z,HTTPClientMaxBodySizeTestCase.assert_refusedc                    s0   d‰ ‡ fdd„}| j |||d� |  ˆ t¡ d S )Nr   c                    rÜ   rY   rÝ   rÞ   rà   r*   r+   rÖ   "  râ   zRHTTPClientMaxBodySizeTestCase.assert_streaming_refused.<locals>.streaming_callback)r+  rÖ   )r6  ÚassertLessEqualr"  )r)   rÄ   r+  rÖ   r*   rà   r+   Úassert_streaming_refused  s   z6HTTPClientMaxBodySizeTestCase.assert_streaming_refusedc                 C   s(   |   d¡}| ¡  |  |jdt ¡ d S )Nr&  r   )rÌ   r  rÍ   r<   r"  rÓ   r*   r*   r+   Útest_small_body*  s   
z-HTTPClientMaxBodySizeTestCase.test_small_bodyc                 C   ó   | j ddd� d S ©Nr(  úContent-Length too longr*  ©r6  r5   r*   r*   r+   Útest_large_body0  rc   z-HTTPClientMaxBodySizeTestCase.test_large_bodyc                 C   r:  ©Nr)  zdecompressed body too larger*  r=  r5   r*   r*   r+   Útest_decompressed_body_size4  s   z9HTTPClientMaxBodySizeTestCase.test_decompressed_body_sizec                 C   r:  r;  ©r8  r5   r*   r*   r+   Ú"test_streaming_callback_over_limit9  s   z@HTTPClientMaxBodySizeTestCase.test_streaming_callback_over_limitc                 C   r:  r?  rA  r5   r*   r*   r+   Ú/test_streaming_callback_decompressed_over_limitA  rc   zMHTTPClientMaxBodySizeTestCase.test_streaming_callback_decompressed_over_limitc                 C   s6   | j ddd� |  d¡}| ¡  |  |jdt ¡ d S )Nr(  r<  r*  r&  r   )r6  rÌ   r  rÍ   r<   r"  rÓ   r*   r*   r+   Útest_reuse_after_refusalE  s   
z6HTTPClientMaxBodySizeTestCase.test_reuse_after_refusal)r.   r/   r0   r¢   r/  rº   r6  r8  r9  r>  r@  rB  rC  rD  r*   r*   r*   r+   r'  ø  s    
r'  )irG  rO  r-  r   r  Ú	functoolsru   r2  rß  r
  Úior   rô  rö  r™  Útypingr"  Úunittestr”   Útornado.escaper   r   r   r   r   Útornador	   Útornado.httpclientr
   r   r   r   r   Útornado.httpserverr   Útornado.ioloopr   Útornado.iostreamr   Útornado.logr   r   r   Útornado.testingr   r   r   r   Útornado.test.utilr   r   Útornado.webr   r   r   Útornado.httputilr   r   rä   ÚImportErrorÚskipIfr¾  r!   r2   r8   r>   rE   rH   rO   rR   rX   rZ   r\   ra   rd   rl   rr   r„   r‡   r    rï   Ú	lru_cacherB   rU  r›   ÚBROTLI_BOMB_SIZEÚ	b64decoder¥   rœ   rž   r£   r§   r©   ÚTestCaserÀ  rÐ  rÔ  rî  rþ  r  r"  r  r#  r$  r'  r*   r*   r*   r+   Ú<module>   s®    þÿ
	


ÿ
	
      !
?(