o
    wio                     @   sj  d dl mZ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 deeeee  f eeee
 f f f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ParseMultipartConfig)utf8
native_str)gen_log)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   W/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/tornado/test/httputil_test.pyform_data_args   s   r   c                   @   l   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      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'      z-TestUrlConcat.test_url_concat_no_query_paramsc                 C   r   )Nr   )r   z/yr   z!https://localhost/path?y=%2Fy&z=zr!   r#   r   r   r   test_url_concat_encode_args+   r'   z)TestUrlConcat.test_url_concat_encode_argsc                 C   r   )Nzhttps://localhost/path?r   r   r    r!   r#   r   r   r   test_url_concat_trailing_q/   r'   z(TestUrlConcat.test_url_concat_trailing_qc                 C   r   )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   r'   z4TestUrlConcat.test_url_concat_q_with_no_trailing_ampc                 C   r   )Nzhttps://localhost/path?x&r   r   r*   r!   r#   r   r   r   test_url_concat_trailing_amp7   r'   z*TestUrlConcat.test_url_concat_trailing_ampc                 C   r   )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;   r'   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?      
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   r0   z)TestUrlConcat.test_url_concat_none_paramsc                 C      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      z'TestUrlConcat.test_url_concat_with_fragc                 C   r   )Nr   )r   y1)r   y2z https://localhost/path?y=y1&y=y2r!   r#   r   r   r   !test_url_concat_multi_same_paramsK   r'   z/TestUrlConcat.test_url_concat_multi_same_paramsc                 C   r2   )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   r4   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/   r1   r3   r7   r8   r:   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)r?   3)b2)urllibparseparse_qslistr	   assertIn)r$   qsstringqsqslr   r   r   test_parsingY   s   zQsParseTest.test_parsingN)r;   r<   r=   rL   r   r   r   r   r>   X   s    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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   T   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argsrS   filer   r   r   test_file_uploadc      
z&MultipartFormDataTest.test_file_uploadc                 C   rN   )NsP   --1234
Content-Disposition: form-data; name=files; filename=ab.txt

Foo
--1234--rP   rQ   rR   rS   r   rT   rU   rV   rW   rX   rZ   r   r   r   test_unquoted_namesr   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a b.txtza	b.txtztrying filename %rP--1234
Content-Disposition: form-data; name="files"; filename="%s"

Foo
--1234--\\\"\"

rR   rS   r   rT   rV   rW   )loggingdebugrY   r   r   r   r"   )r$   	filenamesrT   str_datar[   r\   rS   r]   r   r   r   test_special_filenames   s$   

z,MultipartFormDataTest.test_special_filenamesc              	   C   s   g d}|D ]@}d| dd dd }t| dd}t \}}| t}td	||| W d    n1 s8w   Y  | d
t|j qd S )N)zab.txtza b.txtzab.txtra   rb   rc   rd   re   rf   rg   rR   zInvalid header value)	rY   r   r   assertRaisesr
   r   rH   str	exception)r$   rj   rT   rk   r[   r\   rS   cmr   r   r   test_invalid_chars   s"   

z(MultipartFormDataTest.test_invalid_charsc                 C   rN   )Nsr   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"; filename*=UTF-8''%C3%A1b.txt

Foo
--1234--rP   rQ   rR   rS   r   rT   u   áb.txtrV   rW   rX   rZ   r   r   r   test_non_ascii_filename_rfc5987   r_   z5MultipartFormDataTest.test_non_ascii_filename_rfc5987c                 C   sZ   d ddd}t \}}td||| |d d }| |d d	 | |d
 d d S )NuX   --1234
Content-Disposition: form-data; name="files"; filename="测试.txt"

Foo
--1234--zutf-8rP   rQ   rR   rS   r   rT   u
   测试.txtrV   rW   )encoderY   r   r   r"   rZ   r   r   r   test_non_ascii_filename_raw   s   

z1MultipartFormDataTest.test_non_ascii_filename_rawc                 C   rN   )NrO   rP   rQ   s   "1234"rS   r   rT   rU   rV   rW   rX   rZ   r   r   r   )test_boundary_starts_and_ends_with_quotes   r_   z?MultipartFormDataTest.test_boundary_starts_and_ends_with_quotesc                 C   b   d dd}t \}}| jtdd td||| W d    n1 s$w   Y  | |i  d S )Ns   --1234

Foo
--1234--rP   rQ   z#multipart/form-data missing headersmsgrR   rY   r   rm   r
   r   r"   r$   r[   r\   rS   r   r   r   test_missing_headers   s   
z*MultipartFormDataTest.test_missing_headersc                 C   rv   )NsR   --1234
Content-Disposition: invalid; name="files"; filename="ab.txt"

Foo
--1234--rP   rQ   Invalid multipart/form-datarw   rR   ry   rz   r   r   r    test_invalid_content_disposition   s   
z6MultipartFormDataTest.test_invalid_content_dispositionc                 C   rv   )NsS   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo--1234--rP   rQ   r|   rw   rR   ry   rz   r   r   r   .test_line_does_not_end_with_correct_line_break  s   
zDMultipartFormDataTest.test_line_does_not_end_with_correct_line_breakc                 C   rv   )NsF   --1234
Content-Disposition: form-data; filename="ab.txt"

Foo
--1234--rP   rQ   z&multipart/form-data value missing namerw   rR   ry   rz   r   r   r   6test_content_disposition_header_without_name_parameter  s   
zLMultipartFormDataTest.test_content_disposition_header_without_name_parameterc                 C   rN   )NsU   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

Foo
--1234--
rP   rQ   rR   rS   r   rT   rU   rV   rW   rX   rZ   r   r   r   test_data_after_final_boundary!  s   
	z4MultipartFormDataTest.test_data_after_final_boundaryc                 C   B   dd }|d}|d}|| dkr|  d|d| d S d S )Nc                 S   s>   t  }dd|   d d }i }i }td||| t  | S )Ns+   --1234
Content-Disposition: form-data; x="   ;s   "; s1   name="files"; filename="a.txt"

Foo
--1234--
rR   )timeperf_counterr   )nstartmessager\   rS   r   r   r   f8  s   zJMultipartFormDataTest.test_disposition_param_linear_performance.<locals>.fi  '     z,Disposition param parsing is not linear: d1= vs d2=failr$   r   d1d2r   r   r   )test_disposition_param_linear_performance4  s   z?MultipartFormDataTest.test_disposition_param_linear_performancec           
      C   sV  d}d dd}t }t \}}t|||||d | |d d d d	 tdd
}| t}t|||||d W d    n1 sCw   Y  | dt|j	 tdd}| t}t|||||d W d    n1 sow   Y  | dt|j	 tdd}	| t}t|||||	d W d    n1 sw   Y  | dt|j	 d S )NrR   sP   --1234
Content-Disposition: form-data; name="files"; filename="ab.txt"

--1234--rP   rQ   )configrS   r   rT   rU   )	max_partsztoo many parts
   )max_part_header_sizezheader too largeF)enabledz'multipart/form-data parsing is disabled)
rY   r   r   r   r"   rm   r
   rH   rn   ro   )
r$   boundaryrV   r   r\   rS   config_no_partsrp   config_small_headersconfig_disabledr   r   r   test_multipart_configL  s>   






z+MultipartFormDataTest.test_multipart_configN)r;   r<   r=   r^   r`   rl   rq   rr   rt   ru   r{   r}   r~   r   r   r   r   r   r   r   r   rM   b   s    rM   c                   @   r   )HTTPHeadersTestc                 C   s   d dd}t|}| |d d | |ddg | |d d | |dd	d
g | tt| g d | d|v  | d|v  d S )Nz>Foo: bar
 baz
Asdf: qwer
	zxcv
Foo: even
     more
     lines
rf   rg   asdf	qwer zxcvFoozbar baz,even more linesfoobar bazeven more lines))Asdfr   )r   r   )r   r   r   )	rY   r   rE   r"   get_listsortedrG   get_all
assertTruer$   r[   headersr   r   r   test_multi_linep  s    
zHTTPHeadersTest.test_multi_linec                 C   sJ   d}t |}| |d d d}| tt j| d}| tt j| d S )NzFoo: bar
	asdfr   zbar asdfz	 Foo: barzFoo: bar
asdf)r   rE   r"   rm   r
   r   r   r   r   test_continuation  s   
z!HTTPHeadersTest.test_continuationc                 C   sr   t dD ]2}dt| d}|dks|dkr.|dkr.t|}| |d dt| d	 q| ttj| qd S )
N   zFoo: barzbaz
	          r   barbaz)rangechrr   rE   r"   rm   r
   )r$   cr[   r   r   r   r   test_forbidden_ascii_characters  s   
z/HTTPHeadersTest.test_forbidden_ascii_charactersc              
   C   s   g d}|D ]R}dD ]M}z<z| |}W n
 ty   Y W q
w d| d }tt|d}ddt|d d fg}| |t|  W q
 t	yW   t
d	||  w qd S )
N)   u    u    )r   latin1s   Cookie: foo=s   barr   Cookiezfoo=r   zfailed while trying %r in %s)rs   UnicodeEncodeErrorr   rE   r   decoder"   rG   r   	Exceptionr   warning)r$   newlinesnewlineencodingencodedr[   r   expectedr   r   r   test_unicode_newlines  s,   z%HTTPHeadersTest.test_unicode_newlinesc                 C   sV   g d}|D ]"\}}t d| }|rdg}ndd| fg}| |t|  qd S )N)) T)	T)    F)r   FzTransfer-Encoding: %schunked)Transfer-Encodingchunkedr   z	%schunked)r   rE   r"   rG   r   )r$   
whitespacer   strippedr   r   r   r   r   test_unicode_whitespace  s   z'HTTPHeadersTest.test_unicode_whitespacec                 C   sZ   |  t td W d    n1 sw   Y  td}| t| g d d S )Nz&CRLF: crlf
LF: lf
CR: crMore: more
zCRLF: crlf
LF: lf
More: more
))Crlfcrlf)Lflf)Moremore)rm   r
   r   rE   r"   r   r   r$   r   r   r   r   test_optional_cr  s   

z HTTPHeadersTest.test_optional_crc           	      C   s   g d}t  }|D ]
\}}||| q	| }t|}t|}||||fD ]}| tt| | q(|||fD ]}| || | |	d|	d q<d S )N))Ar@   )r   rC   )Br   r   )
r   addcopydeepcopyr"   rG   r   r   assertIsNotr   )	r$   	all_pairsh1kvh2h3h4r   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"   r   r   items)r$   r   pickled	unpickledr   r   r   test_pickle_roundtrip  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   r   r   quuxxyzzy)r   r   )Quuxr   )r   r"   
setdefaultr   r   r   r   r   r   test_setdefault  s   zHTTPHeadersTest.test_setdefaultc                 C   sH   t  }|dd |dd |dd t t|}| || d S )Nr   r@   rC   rA   )r   r   rE   rn   r"   )r$   r   headers2r   r   r   test_string  s   zHTTPHeadersTest.test_stringc              	   C   sR   g d}|D ] }t  }| t ||d W d    n1 s!w   Y  qd S )N) zfoo barzfoo	barzfoo
barzfoo barzfoo z foo   ér   )r   rm   r
   r   )r$   invalid_namesnamer   r   r   r   test_invalid_header_names!  s   
z)HTTPHeadersTest.test_invalid_header_namesc                 C   r   )Nc                 S   s4   t  }t }t| D ]}|dd qt  | S )NzX-Foor   )r   r   r   r   r   )r   r   r   ir   r   r   r   2  s
   z2HTTPHeadersTest.test_linear_performance.<locals>.fr   順 r   z.HTTPHeaders.add() does not scale linearly: d1=r   r   r   r   r   r   test_linear_performance1  s   z'HTTPHeadersTest.test_linear_performanceN)r;   r<   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   o  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 )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   checkF     zFormatTimestampTest.checkc                 C   s   |  | j d S r   )r   	TIMESTAMPr$   r   r   r   test_unix_time_floatI  s   z(FormatTimestampTest.test_unix_time_floatc                 C   s   |  t| j d S r   )r   intr   r   r   r   r   test_unix_time_intL  s   z&FormatTimestampTest.test_unix_time_intc                 C   s   |  t| j d S r   )r   r   gmtimer   r   r   r   r   test_struct_timeO  r   z$FormatTimestampTest.test_struct_timec                 C   s.   t t| j}| dt| | | d S )Nr   )tupler   r  r   r"   lenr   )r$   tupr   r   r   test_time_tupleR  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utcrY   r   r   r   r   test_utc_naive_datetimeW  s   
z+FormatTimestampTest.test_utc_naive_datetimec                 C   s@   t   | tj| j W d    d S 1 sw   Y  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_datetimeb  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_datetimeg  s
   z-FormatTimestampTest.test_other_aware_datetimeN)r;   r<   r=   r   r   r   r   r  r  r  r  r  r  r  r   r   r   r   r   A  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_constructors  s   z.HTTPServerRequestTest.test_default_constructorc                 C   s   t dd}| |jt d S r  )r   assertIsInstancerV   bytesr$   requestr   r   r   test_body_is_a_byte_stringy  s   
z0HTTPServerRequestTest.test_body_is_a_byte_stringc                 C   s*   t dtddgid}| dt| d S )Nr  Canaryz	Coal Mine)r  r   )r   r   assertNotInreprr  r   r   r   "test_repr_does_not_contain_headers}  s   z8HTTPServerRequestTest.test_repr_does_not_contain_headersN)r;   r<   r=   r  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 )Nr   )	joinMETHODPATHVERSIONr   r"   methodpathversion)r$   
start_lineparsed_start_liner   r   r   test_parse_request_start_line  s
   z7ParseRequestStartLineTest.test_parse_request_start_lineN)r;   r<   r=   r&  r'  r(  r.  r   r   r   r   r#    s
    r#  c                   @   s4   e Zd Zdd Zd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=fingerahoyfinger)chipsviennaz)keebler="E=mc2; L=\"Loves\"; fudge=\012;"z"E=mc2z	\"Loves\"z\012rd   )keeblerLfudger   zkeebler=E=mc2r4  zE=mc2zkey:term=value:termzkey:termz
value:termza=b; c=[; d=r; f=hrB   [rh)r?   r   dr   N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.comrB   zexample.com)r?   Domainza=b; h=i; a=cr   r   )r?   r9  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.
        django_languagez$abc=def; unnamed; django_language=enza=b; "; c=drB   rd   r:  )r?   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é Bessettesaintu   André Bessettez  =  b  ;  ;  =  ;   c  =  ;  r   )r   r   N)rH   r   keysr"   r   r   r   r   r   test_invalid_cookies  s&   

z$ParseCookieTest.test_invalid_cookiesc              	   C   s\   g d}|D ]%\}}|  | t|}| |d | W d    n1 s&w   Y  qd S )N))za="b=\""b=")za="b=\\"b=\)za="b=\="zb==)za="b=\n"zb=n)z
a="b=\042"rD  )z
a="b=\134"rE  )z
a="b=\377"u   b=ÿ)z
a="b=\400"zb=400)z	a="b=\42"zb=42)za="b=\\042"zb=\042)za="b=\\134"zb=\134)z
a="b=\\\""b=\")za="b=\\\042"rF  )za="b=\134\""rF  )za="b=\134\042"rF  r?   )subTestr   r"   )r$   casesr   decodedr   r   r   r   test_unquote  s   zParseCookieTest.test_unquotec              	   C   s   d}dD ]T}|  |E t }d||  d }t|d }t }| |d d d | |dd  d	 | t||d
  | || dd W d    n1 sSw   Y  qd S )Nr   )rc   z\134za="b=rd   r?      rE  z\\\      zTest took too long)rG  r   r   r"   r  
assertLess)r$   r   r   r   r[   r   endr   r   r   test_unquote_large  s   z"ParseCookieTest.test_unquote_largeN)r;   r<   r=   r<  r>  rC  rJ  rQ  r   r   r   r   r/    s    	"r/  )+tornado.httputilr   r   r   r   r   r   r   r	   r
   r   r   tornado.escaper   r   tornado.logr   tornado.test.utilr   r   r	  rh   r   r   urllib.parserD   unittesttypingr   r   r   rn   r  r   TestCaser   r>   rM   r   r   r  r#  r/  r   r   r   r   <module>   s0   4 .	2
   S1