U
    lufWM                     @   sr  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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)	ExpectLog)ignore_deprecationN)TupleDictList)returnc                   C   s   i i fS )zReturn 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   >/tmp/pip-unpacked-wheel-bmg6zs32/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_amp3   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_amp7   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_paramsC   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_fragG   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_paramsK   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_paramsO   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_paramsS   s    z)TestUrlConcat.test_url_concat_dict_paramsN)__name__
__module____qualname__r$   r%   r&   r(   r)   r*   r,   r-   r.   r1   r2   r4   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)r9   3)b2)urllibparseparse_qslistr	   assertIn)r"   ZqsstringqsZqslr   r   r   test_parsingY   s    zQsParseTest.test_parsingN)r5   r6   r7   rD   r   r   r   r   r8   X   s   r8   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 )NT   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo
--1234--   
   
   1234filesr   filenameab.txtbody   Fooreplacer   r   r    r"   dataargsrJ   filer   r   r   test_file_uploadc   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--rG   rH   rI   rJ   r   rK   rL   rM   rN   rO   rQ   r   r   r   test_unquoted_namesr   s     
z)MultipartFormDataTest.test_unquoted_namesc                 C   s   dddddddg}|D ]|}t d| d	|d
ddd }t|dd}t \}}td||| |d d }| |d | | |d d qd S )Nz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\"

rI   rJ   r   rK   rM   rN   )loggingdebugrP   r   r   r   r    )r"   	filenamesrK   Zstr_datarR   rS   rJ   rT   r   r   r   test_special_filenames   s2    	  

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--rG   rH   rI   rJ   r   rK   u   áb.txtrM   rN   rO   rQ   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 )NrF   rG   rH   s   "1234"rJ   r   rK   rL   rM   rN   rO   rQ   r   r   r   )test_boundary_starts_and_ends_with_quotes   s     
z?MultipartFormDataTest.test_boundary_starts_and_ends_with_quotesc              	   C   sJ   d dd}t \}}ttd td||| W 5 Q R X | |i  d S )Ns   --1234

Foo
--1234--rG   rH   z#multipart/form-data missing headersrI   rP   r   r   r   r   r    r"   rR   rS   rJ   r   r   r   test_missing_headers   s     
z*MultipartFormDataTest.test_missing_headersc              	   C   sJ   d dd}t \}}ttd td||| W 5 Q R X | |i  d S )NsR   --1234
Content-Disposition: invalid; name="files"; filename="ab.txt"

Foo
--1234--rG   rH   Invalid multipart/form-datarI   ra   rb   r   r   r    test_invalid_content_disposition   s     
z6MultipartFormDataTest.test_invalid_content_dispositionc              	   C   sJ   d dd}t \}}ttd td||| W 5 Q R X | |i  d S )NsS   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo--1234--rG   rH   rd   rI   ra   rb   r   r   r   .test_line_does_not_end_with_correct_line_break   s     
zDMultipartFormDataTest.test_line_does_not_end_with_correct_line_breakc              	   C   sJ   d dd}t \}}ttd td||| W 5 Q R X | |i  d S )NsF   --1234
Content-Disposition: form-data; filename="ab.txt"

Foo
--1234--rG   rH   z&multipart/form-data value missing namerI   ra   rb   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--
rG   rH   rI   rJ   r   rK   rL   rM   rN   rO   rQ   r   r   r   test_data_after_final_boundary   s     	
z4MultipartFormDataTest.test_data_after_final_boundaryN)r5   r6   r7   rU   rV   r^   r_   r`   rc   re   rf   rg   rh   r   r   r   r   rE   b   s   rE   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| dddg d S )Nz>Foo: bar
 baz
Asdf: qwer
	zxcv
Foo: even
     more
     lines
rY   rZ   Zasdf	qwer zxcvFoozbar baz,even more linesfoobar bazeven more lines)ZAsdfrj   )rk   rm   )rk   rn   )rP   r   r?   r    get_listsortedrA   get_all)r"   rR   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"   rR   r   r   r   test_malformed_continuation"  s    z+HTTPHeadersTest.test_malformed_continuationc              
   C   s   dddddddg}|D ]}dD ]}z~z| |}W n tk
rL   Y W qY nX d	| d
 }tt|d}ddt|d d fg}| |t|  W q t	k
r   t
d||  Y qX qqd S )N   u    u    )r   latin1s   Cookie: foo=s   barr{   Cookiezfoo=barzfailed while trying %r in %s)encodeUnicodeEncodeErrorr   r?   r   decoder    rA   rq   	Exceptionr   warning)r"   newlinesnewlineencodingencodedrR   rr   expectedr   r   r   test_unicode_newlines)  s2    	z%HTTPHeadersTest.test_unicode_newlinesc                 C   s(   t d}| t| dddg 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    rp   rq   r"   rr   r   r   r   test_optional_crQ  s
    

z HTTPHeadersTest.test_optional_crc           	      C   s   dddg}t  }|D ]\}}||| q| }t|}t|}||||fD ]}| tt| | qR|||fD ](}| || | |	d|	d qzd S )N)Ar:   )r   r=   )Bcr   )
r   addcopydeepcopyr    rA   rp   rq   ZassertIsNotro   )	r"   Z	all_pairsh1kvh2h3h4rr   r   r   r   	test_copy[  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    rp   rq   items)r"   rr   ZpickledZ	unpickledr   r   r   test_pickle_roundtripl  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}   rl   ZbazZquuxxyzzy)rk   r}   )ZQuuxr   )r   r    
setdefaultrp   rq   r   r   r   r   test_setdefaultv  s    zHTTPHeadersTest.test_setdefaultc                 C   sH   t  }|dd |dd |dd t t|}| || d S )Nrk   r:   r=   r;   )r   r   r?   strr    )r"   rr   Zheaders2r   r   r   test_string  s    zHTTPHeadersTest.test_stringN)r5   r6   r7   rs   ru   r   r   r   r   r   r   r   r   r   r   ri     s   (

ri   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 )FormatTimestampTestg*; R]AAz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tjjjd d d S )N)tzinfo)r   datetimefromtimestampr   timezoneutcrP   r   r   r   r   test_utc_naive_datetime  s     z+FormatTimestampTest.test_utc_naive_datetimec              	   C   s*   t   | tj| j W 5 Q R X d S r   )r   r   r   utcfromtimestampr   r   r   r   r   "test_utc_naive_datetime_deprecated  s    z6FormatTimestampTest.test_utc_naive_datetime_deprecatedc                 C   s   |  tj| jtjj d S r   )r   r   r   r   r   r   r   r   r   r   test_utc_aware_datetime  s    z+FormatTimestampTest.test_utc_aware_datetimec              
   C   s(   |  tj| jttjdd d S )N)hours)r   r   r   r   r   	timedeltar   r   r   r   test_other_aware_datetime  s     z-FormatTimestampTest.test_other_aware_datetimeN)r5   r6   r7   r   r   r   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assertIsInstancerM   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|k d S )Nr   ZCanaryz	Coal Mine)r   rr   )r   r   
assertTruerepr)r"   requestr   r   r   "test_repr_does_not_contain_headers  s
     z8HTTPServerRequestTest.test_repr_does_not_contain_headersN)r5   r6   r7   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"   Z
start_lineZparsed_start_liner   r   r   test_parse_request_start_line  s
    z7ParseRequestStartLineTest.test_parse_request_start_lineN)r5   r6   r7   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\012rX   )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)r9   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)r9   Domainza=b; h=i; a=cr   i)r9   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<   rX   r   )r9   r   r   za b c=d e = f; gh=izd e = fr   )za b cZ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)rB   r   keysr    r   r   r   r   r   test_invalid_cookies  s*    
 
 z$ParseCookieTest.test_invalid_cookiesN)r5   r6   r7   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   Ztornado.test.utilr   r   r   r[   r   r   urllib.parser>   Zunittesttypingr   r   r   r   r   r   ZTestCaser   r8   rE   ri   r   r   r   r   r   r   r   r   <module>   s.   0.	2
 & 1