a
    õ£BeeJ  ã                   @   sf  d dl mZmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZmZ d dlm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 eeeee f eeee
 f f dœdd	„Z G d
d„ dej!ƒZ"G dd„ dej!ƒZ#G dd„ dej!ƒZ$G dd„ dej!ƒZ%G dd„ dej!ƒZ&G dd„ dej!ƒZ'G dd„ dej!ƒZ(G dd„ dej!ƒZ)dS )é    )
Ú
url_concatÚparse_multipart_form_dataÚHTTPHeadersÚformat_timestampÚHTTPServerRequestÚparse_request_start_lineÚparse_cookieÚ	qs_to_qslÚHTTPInputErrorÚHTTPFile)Úutf8Ú
native_str)Úgen_log)Ú	ExpectLogN)ÚTupleÚDictÚList)Úreturnc                   C   s   i i fS )zÇReturn two empty dicts suitable for use with parse_multipart_form_data.

    mypy insists on type annotations for dict literals, so this lets us avoid
    the verbose types throughout this test.
    © r   r   r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/test/httputil_test.pyÚform_data_args   s    r   c                   @   sl   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d„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTestUrlConcatc                 C   s   t dddgƒ}|  |d¡ d S )Núhttps://localhost/path©Úyr   ©Úzr   úhttps://localhost/path?y=y&z=z©r   ÚassertEqual©ÚselfÚurlr   r   r   Útest_url_concat_no_query_params&   s    z-TestUrlConcat.test_url_concat_no_query_paramsc                 C   s   t dddgƒ}|  |d¡ d S )Nr   )r   z/yr   z!https://localhost/path?y=%2Fy&z=zr   r    r   r   r   Útest_url_concat_encode_args*   s    z)TestUrlConcat.test_url_concat_encode_argsc                 C   s   t dddgƒ}|  |d¡ d S )Nzhttps://localhost/path?r   r   r   r   r    r   r   r   Útest_url_concat_trailing_q.   s    z(TestUrlConcat.test_url_concat_trailing_qc                 C   s   t dddgƒ}|  |d¡ d S )Nzhttps://localhost/path?xr   r   ú!https://localhost/path?x=&y=y&z=zr   r    r   r   r   Ú&test_url_concat_q_with_no_trailing_amp2   s    z4TestUrlConcat.test_url_concat_q_with_no_trailing_ampc                 C   s   t dddgƒ}|  |d¡ d S )Nzhttps://localhost/path?x&r   r   r&   r   r    r   r   r   Útest_url_concat_trailing_amp6   s    z*TestUrlConcat.test_url_concat_trailing_ampc                 C   s   t dddgƒ}|  |d¡ d S )Nzhttps://localhost/path?a=1&b=2r   r   z&https://localhost/path?a=1&b=2&y=y&z=zr   r    r   r   r   Útest_url_concat_mult_params:   s    z)TestUrlConcat.test_url_concat_mult_paramsc                 C   s   t dg ƒ}|  |d¡ d S ©Nzhttps://localhost/path?r=1&t=2r   r    r   r   r   Útest_url_concat_no_params>   s    
z'TestUrlConcat.test_url_concat_no_paramsc                 C   s   t dd ƒ}|  |d¡ d S r*   r   r    r   r   r   Útest_url_concat_none_paramsB   s    
z)TestUrlConcat.test_url_concat_none_paramsc                 C   s   t ddgƒ}|  |d¡ d S )Nzhttps://localhost/path#tabr   zhttps://localhost/path?y=y#tabr   r    r   r   r   Útest_url_concat_with_fragF   s    z'TestUrlConcat.test_url_concat_with_fragc                 C   s   t dddgƒ}|  |d¡ d S )Nr   )r   Úy1)r   Úy2z https://localhost/path?y=y1&y=y2r   r    r   r   r   Ú!test_url_concat_multi_same_paramsJ   s    z/TestUrlConcat.test_url_concat_multi_same_paramsc                 C   s   t ddgƒ}|  |d¡ d S )Nzhttps://localhost/path?r=1&r=2r   z"https://localhost/path?r=1&r=2&y=yr   r    r   r   r   Ú'test_url_concat_multi_same_query_paramsN   s    z5TestUrlConcat.test_url_concat_multi_same_query_paramsc                 C   s    t dtdd�ƒ}|  |d¡ d S )Nr   r   )r   zhttps://localhost/path?y=y)r   Údictr   r    r   r   r   Útest_url_concat_dict_paramsR   s    z)TestUrlConcat.test_url_concat_dict_paramsN)Ú__name__Ú
__module__Ú__qualname__r#   r$   r%   r'   r(   r)   r+   r,   r-   r0   r1   r3   r   r   r   r   r   %   s   r   c                   @   s   e Zd Zdd„ ZdS )ÚQsParseTestc                 C   sD   d}t j |¡}tt|ƒƒ}|  d|¡ |  d|¡ |  d|¡ d S )Nza=1&b=2&a=3)ÚaÚ1)r8   Ú3)ÚbÚ2)ÚurllibÚparseÚparse_qsÚlistr	   ÚassertIn)r!   ZqsstringÚqsZqslr   r   r   Útest_parsingX   s    zQsParseTest.test_parsingN)r4   r5   r6   rC   r   r   r   r   r7   W   s   r7   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d„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚMultipartFormDataTestc                 C   sT   d  dd¡}tƒ \}}td|||ƒ |d d }|  |d d¡ |  |d	 d
¡ d S )NóT   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo
--1234--ó   
ó   
ó   1234Úfilesr   Úfilenameúab.txtÚbodyó   Foo©Úreplacer   r   r   ©r!   ÚdataÚargsrI   Úfiler   r   r   Útest_file_uploadb   s    ú
z&MultipartFormDataTest.test_file_uploadc                 C   sT   d  dd¡}tƒ \}}td|||ƒ |d d }|  |d d¡ |  |d	 d
¡ d S )NsP   --1234
Content-Disposition: form-data; name=files; filename=ab.txt

Foo
--1234--rF   rG   rH   rI   r   rJ   rK   rL   rM   rN   rP   r   r   r   Útest_unquoted_namesq   s    ú
z)MultipartFormDataTest.test_unquoted_namesc                 C   sŽ   g d¢}|D ]|}t  d|¡ d| dd¡ dd¡ }t| dd	¡ƒ}tƒ \}}td
|||ƒ |d d }|  |d |¡ |  |d d¡ qd S )N)za;b.txtza"b.txtza";b.txtza;"b.txtz	a";";.txtza\"b.txtza\b.txtztrying filename %rzP--1234
Content-Disposition: form-data; name="files"; filename="%s"

Foo
--1234--ú\z\\ú"z\"Ú
ú
rH   rI   r   rJ   rL   rM   )ÚloggingÚdebugrO   r   r   r   r   )r!   Ú	filenamesrJ   Zstr_datarQ   rR   rI   rS   r   r   r   Útest_special_filenames�   s     	ÿýû

z,MultipartFormDataTest.test_special_filenamesc                 C   sT   d  dd¡}tƒ \}}td|||ƒ |d d }|  |d d¡ |  |d	 d
¡ d S )Nsr   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"; filename*=UTF-8''%C3%A1b.txt

Foo
--1234--rF   rG   rH   rI   r   rJ   u   Ã¡b.txtrL   rM   rN   rP   r   r   r   Útest_non_ascii_filenamež   s    ú
z-MultipartFormDataTest.test_non_ascii_filenamec                 C   sT   d  dd¡}tƒ \}}td|||ƒ |d d }|  |d d¡ |  |d	 d
¡ d S )NrE   rF   rG   s   "1234"rI   r   rJ   rK   rL   rM   rN   rP   r   r   r   Ú)test_boundary_starts_and_ends_with_quotes­   s    ú
z?MultipartFormDataTest.test_boundary_starts_and_ends_with_quotesc                 C   s^   d  dd¡}tƒ \}}ttdƒ� td|||ƒ W d   ƒ n1 sD0    Y  |  |i ¡ d S )Ns   --1234

Foo
--1234--rF   rG   z#multipart/form-data missing headersrH   ©rO   r   r   r   r   r   ©r!   rQ   rR   rI   r   r   r   Útest_missing_headers¼   s    û
,z*MultipartFormDataTest.test_missing_headersc                 C   s^   d  dd¡}tƒ \}}ttdƒ� td|||ƒ W d   ƒ n1 sD0    Y  |  |i ¡ d S )NsR   --1234
Content-Disposition: invalid; name="files"; filename="ab.txt"

Foo
--1234--rF   rG   úInvalid multipart/form-datarH   r`   ra   r   r   r   Ú test_invalid_content_dispositionÉ   s    ú
,z6MultipartFormDataTest.test_invalid_content_dispositionc                 C   s^   d  dd¡}tƒ \}}ttdƒ� td|||ƒ W d   ƒ n1 sD0    Y  |  |i ¡ d S )NsS   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo--1234--rF   rG   rc   rH   r`   ra   r   r   r   Ú.test_line_does_not_end_with_correct_line_break×   s    û
,zDMultipartFormDataTest.test_line_does_not_end_with_correct_line_breakc                 C   s^   d  dd¡}tƒ \}}ttdƒ� td|||ƒ W d   ƒ n1 sD0    Y  |  |i ¡ d S )NsF   --1234
Content-Disposition: form-data; filename="ab.txt"

Foo
--1234--rF   rG   z&multipart/form-data value missing namerH   r`   ra   r   r   r   Ú6test_content_disposition_header_without_name_parameterä   s    ú
,zLMultipartFormDataTest.test_content_disposition_header_without_name_parameterc                 C   sT   d  dd¡}tƒ \}}td|||ƒ |d d }|  |d d¡ |  |d	 d
¡ d S )NsU   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo
--1234--
rF   rG   rH   rI   r   rJ   rK   rL   rM   rN   rP   r   r   r   Útest_data_after_final_boundaryò   s    ù	
z4MultipartFormDataTest.test_data_after_final_boundaryN)r4   r5   r6   rT   rU   r]   r^   r_   rb   rd   re   rf   rg   r   r   r   r   rD   a   s   rD   c                   @   sL   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d„ Z
dS )ÚHTTPHeadersTestc                 C   s€   d  dd¡}t |¡}|  |d d¡ |  | d¡dg¡ |  |d d¡ |  | d¡d	d
g¡ |  tt| ¡ ƒƒg d¢¡ d S )Nz>Foo: bar
 baz
Asdf: qwer
	zxcv
Foo: even
     more
     lines
rX   rY   Zasdfú	qwer zxcvÚFoozbar baz,even more linesÚfooúbar bazúeven more lines))ZAsdfri   )rj   rl   )rj   rm   )rO   r   r>   r   Úget_listÚsortedr@   Úget_all)r!   rQ   Úheadersr   r   r   Útest_multi_line  s    	÷
þzHTTPHeadersTest.test_multi_linec                 C   s   d}|   ttj|¡ d S )Nz	 Foo: bar)ÚassertRaisesr
   r   r>   )r!   rQ   r   r   r   Útest_malformed_continuation!  s    z+HTTPHeadersTest.test_malformed_continuationc              
   C   sÀ   g d¢}|D ]®}dD ]¤}z|z|  |¡}W n ty@   Y W qY n0 d| d }t t| d¡ƒ¡}ddt| d¡ƒ d fg}|  |t| ¡ ƒ¡ W q t	y¶   t
 d	||¡ ‚ Y q0 qqd S )
N)úúúúõ   Â…u   â€¨u   â€©)r   Úlatin1s   Cookie: foo=s   barrz   ÚCookiezfoo=Úbarzfailed while trying %r in %s)ÚencodeÚUnicodeEncodeErrorr   r>   r   Údecoder   r@   rp   Ú	Exceptionr   Úwarning)r!   ÚnewlinesÚnewlineÚencodingÚencodedrQ   rq   Úexpectedr   r   r   Útest_unicode_newlines(  s$    	þÿz%HTTPHeadersTest.test_unicode_newlinesc                 C   s&   t  d¡}|  t| ¡ ƒg d¢¡ d S )Nz&CRLF: crlf
LF: lf
CR: crMore: more
))ZCrzcrMore: more)ZCrlfZcrlf)ZLfÚlf)r   r>   r   ro   rp   ©r!   rq   r   r   r   Útest_optional_crP  s
    

þz HTTPHeadersTest.test_optional_crc           	      C   s¦   g d¢}t ƒ }|D ]\}}| ||¡ q| ¡ }t |¡}t |¡}||||fD ]}|  tt| ¡ ƒƒ|¡ qP|||fD ](}|  ||¡ |  | 	d¡| 	d¡¡ qxd S )N))ÚAr9   )r‹   r<   )ÚBÚcr‹   )
r   ÚaddÚcopyÚdeepcopyr   r@   ro   rp   ZassertIsNotrn   )	r!   Z	all_pairsZh1ÚkÚvZh2Zh3Zh4rq   r   r   r   Ú	test_copyZ  s    

zHTTPHeadersTest.test_copyc                 C   sz   t ƒ }| dd¡ | dd¡ | dd¡ t |¡}t |¡}|  t| ¡ ƒt| ¡ ƒ¡ |  t| ¡ ƒt| ¡ ƒ¡ d S )Nz
Set-Cookieza=bzc=dzContent-Typez	text/html)	r   rŽ   ÚpickleÚdumpsÚloadsr   ro   rp   Úitems)r!   rq   ZpickledZ	unpickledr   r   r   Útest_pickle_roundtripk  s    

z%HTTPHeadersTest.test_pickle_roundtripc                 C   sr   t ƒ }d|d< |  | dd¡d¡ |  |d d¡ |  | dd¡d¡ |  |d d¡ |  t| ¡ ƒddg¡ d S )Nr|   rk   ZbazZquuxÚxyzzy)rj   r|   )ZQuuxr™   )r   r   Ú
setdefaultro   rp   r‰   r   r   r   Útest_setdefaultu  s    zHTTPHeadersTest.test_setdefaultc                 C   sH   t ƒ }| dd¡ | dd¡ | dd¡ t  t|ƒ¡}|  ||¡ d S )Nrj   r9   r<   r:   )r   rŽ   r>   Ústrr   )r!   rq   Zheaders2r   r   r   Útest_string€  s    zHTTPHeadersTest.test_stringN)r4   r5   r6   rr   rt   r‡   rŠ   r“   r˜   r›   r�   r   r   r   r   rh     s   (

rh   c                   @   sD   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S )ÚFormatTimestampTestg*; R]AÔAzSun, 27 Jan 2013 18:43:20 GMTc                 C   s   |   t|ƒ| j¡ d S ©N)r   r   ÚEXPECTED)r!   Úvaluer   r   r   ÚcheckŽ  s    zFormatTimestampTest.checkc                 C   s   |   | j¡ d S rŸ   )r¢   Ú	TIMESTAMP©r!   r   r   r   Útest_unix_time_float‘  s    z(FormatTimestampTest.test_unix_time_floatc                 C   s   |   t| jƒ¡ d S rŸ   )r¢   Úintr£   r¤   r   r   r   Útest_unix_time_int”  s    z&FormatTimestampTest.test_unix_time_intc                 C   s   |   t | j¡¡ d S rŸ   )r¢   ÚtimeÚgmtimer£   r¤   r   r   r   Útest_struct_time—  s    z$FormatTimestampTest.test_struct_timec                 C   s.   t t | j¡ƒ}|  dt|ƒ¡ |  |¡ d S )Né	   )Útupler¨   r©   r£   r   Úlenr¢   )r!   Útupr   r   r   Útest_time_tupleš  s    z#FormatTimestampTest.test_time_tuplec                 C   s   |   tj | j¡¡ d S rŸ   )r¢   ÚdatetimeÚutcfromtimestampr£   r¤   r   r   r   Útest_datetimeŸ  s    z!FormatTimestampTest.test_datetimeN)r4   r5   r6   r£   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 )ÚHTTPServerRequestTestc                 C   s   t dd� d S ©Nú/)Úuri)r   r¤   r   r   r   Útest_default_constructor¦  s    z.HTTPServerRequestTest.test_default_constructorc                 C   s   t dd�}|  |jt¡ d S r´   )r   ZassertIsInstancerL   Úbytes)r!   Zrequetsr   r   r   Útest_body_is_a_byte_string¬  s    
z0HTTPServerRequestTest.test_body_is_a_byte_stringc                 C   s,   t dtddgiƒd�}|  dt|ƒv¡ d S )Nrµ   ZCanaryz	Coal Mine)r¶   rq   )r   r   Ú
assertTrueÚrepr)r!   Úrequestr   r   r   Ú"test_repr_does_not_contain_headers°  s    ÿz8HTTPServerRequestTest.test_repr_does_not_contain_headersN)r4   r5   r6   r·   r¹   r½   r   r   r   r   r³   ¥  s   r³   c                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚParseRequestStartLineTestÚGETz/foozHTTP/1.1c                 C   sR   d  | j| j| jg¡}t|ƒ}|  |j| j¡ |  |j| j¡ |  |j| j¡ d S )Nú )	ÚjoinÚMETHODÚPATHÚVERSIONr   r   ÚmethodÚpathÚversion)r!   Ú
start_lineZparsed_start_liner   r   r   Útest_parse_request_start_line¼  s
    z7ParseRequestStartLineTest.test_parse_request_start_lineN)r4   r5   r6   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 )ÚParseCookieTestc                 C   sv   |   tdƒdddœ¡ |   tdƒdddd	d
œ¡ |   tdƒddi¡ |   tdƒddi¡ |   tdƒdddddœ¡ dS )zO
        Test cases copied from Python's Lib/test/test_http_cookies.py
        zchips=ahoy; vienna=fingerZahoyZfinger)ZchipsZviennaz)keebler="E=mc2; L=\"Loves\"; fudge=\012;"z"E=mc2z	\"Loves\"z\012rW   )ÚkeeblerÚLZfudgeÚ zkeebler=E=mc2rË   zE=mc2zkey:term=value:termzkey:termz
value:termza=b; c=[; d=r; f=hr;   ú[ÚrÚh)r8   r�   ÚdÚfN©r   r   r¤   r   r   r   Útest_python_cookiesÇ  s    þþÿÿz#ParseCookieTest.test_python_cookiesc                 C   s0   |   tdƒdddœ¡ |   tdƒdddœ¡ d S )	Nza=b; Domain=example.comr;   zexample.com)r8   ÚDomainza=b; h=i; a=cr�   Úi)r8   rÐ   rÓ   r¤   r   r   r   Útest_cookie_edgecasesà  s    ÿz%ParseCookieTest.test_cookie_edgecasesc                 C   sˆ   |   dtdƒ ¡ ¡ |  tdƒddddœ¡ |  tdƒd	d
dœ¡ |  tdƒddi¡ |  tdƒdtdƒi¡ |  tdƒdddœ¡ dS )zs
        Cookie strings that go against RFC6265 but browsers will send if set
        via document.cookie.
        Zdjango_languagez$abc=def; unnamed; django_language=enza=b; "; c=dr;   rW   rÑ   )r8   rÍ   r�   za b c=d e = f; gh=izd e = frÖ   )za b cÚghza   b,c<>@:/[]?{}=d  "  =e,f gza   b,c<>@:/[]?{}zd  "  =e,f gu   saint=AndrÃ© BessetteZsaintu   AndrÃ© Bessettez  =  b  ;  ;  =  ;   c  =  ;  rÍ   )rÍ   r�   N)rA   r   Úkeysr   r   r¤   r   r   r   Útest_invalid_cookiesé  s&    
þÿþ
þÿz$ParseCookieTest.test_invalid_cookiesN)r4   r5   r6   rÔ   r×   rÚ   r   r   r   r   rÊ   Ä  s   	rÊ   )*Ztornado.httputilr   r   r   r   r   r   r   r	   r
   r   Ztornado.escaper   r   Ztornado.logr   Ztornado.testingr   r�   r°   rZ   r”   r¨   Úurllib.parser=   ZunittestÚtypingr   r   r   rœ   r¸   r   ZTestCaser   r7   rD   rh   rž   r³   r¾   rÊ   r   r   r   r   Ú<module>   s,   0.	2
 & 