
    dafJ                         d dl Z d dlZd dlZd dlmZ d dlZ G d dej                        Z G d dej                        Zd Z	e
dk(  r ej                          yy)	    N)cookiesc                   f    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zy)CookieTestsc           
         ddddddddd	d
iddddd	diddddddidddddddddddj                  d      dg}|D ]  }t        j                         }|j                  |d          | j	                  t        |      |d           | j	                  |j                  d!      |d"          t        |d#   j                               D ]$  \  }}| j	                  ||   j                  |       &  y )$Nzchips=ahoy; vienna=fingerahoyfinger)chipsviennaz,<SimpleCookie: chips='ahoy' vienna='finger'>z0Set-Cookie: chips=ahoy
Set-Cookie: vienna=finger)datadictreproutputz)keebler="E=mc2; L=\"Loves\"; fudge=\012;"keeblerzE=mc2; L="Loves"; fudge=
;z5<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\n;'>z5Set-Cookie: keebler="E=mc2; L=\"Loves\"; fudge=\012;"zkeebler=E=mc2zE=mc2z<SimpleCookie: keebler='E=mc2'>zSet-Cookie: keebler=E=mc2zkey:term=value:termzkey:termz
value:termz%<SimpleCookie: key:term='value:term'>zSet-Cookie: key:term=value:termza=b; c=[; d=r; f=hb[rh)acdfz'<SimpleCookie: a='b' c='[' d='r' f='h'>
)zSet-Cookie: a=bzSet-Cookie: c=[zSet-Cookie: d=rzSet-Cookie: f=hr   r   )sepr   r   )
joinr   SimpleCookieloadassertEqualr   r   sorteditemsvalue)selfcasescaseCkvs         1/root/Python-3.12.4/Lib/test/test_http_cookies.py
test_basiczCookieTests.test_basic   s9   0$x8CJL
 D"ABQQS %'*624 , <0<8: - cs<A)) % 	
7&
P  	0D$$&AFF4< T!Wd6l3QXX$X/h@tF|1134 01  1Q/0	0    c                    t        j                         }|j                  d       | j                  |d   j                  d       | j                  |d   d   d       | j                  |d   d   d       | j                  |j                  dg      d       | j                  |j                         d	       | j                  |j                  dg      d
       y )Nz/Customer="WILE_E_COYOTE"; Version=1; Path=/acmeCustomerWILE_E_COYOTEversion1path/acme0Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
        // end hiding -->
        </script>
        
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
        // end hiding -->
        </script>
        r   r   r   r   r    r   	js_outputr!   r$   s     r'   	test_loadzCookieTests.test_load=   s      "	@A:,,o>:y137:v.86(+>	@ ) 	 	fX. 1 	r)   c                 z    t        j                         }d|d<   | j                  |j                  dg      d       y )Nzsome,funky;stuffvalz(Set-Cookie: val="some\054funky\073stuff"r   r   r   r   r6   s     r'   test_extended_encodez CookieTests.test_extended_encodeV   s9       "%%5'*8	:r)   c                    t        j                  d      }d|d   d<   | j                  |j                         j	                  d             t        j                         }|j                  d       | j                  |d   d   d       t        j                         }|j                  d       | j                  |d   d   d	       t        j                  d      }d
|d   d<   | j                  |j                         d       y )NCustomer="WILE_E_COYOTE"r   r+   expiresGMTz3Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMTzWed, 01 Jan 2010 00:00:00 GMTz1Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMTzWed, 01 Jan 98 00:00:00 GMT
   max-agez0Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10)r   r   
assertTruer   endswithr   r   r6   s     r'   test_special_attrszCookieTests.test_special_attrs^   s      !;<#$*i 
++E23   "	DE:y18	:  "	BC:y16	8   !;<#%*i K	Mr)   c                     t        j                  d      }d|d   d<   d|d   d<   | j                  |j                         d       y )Nr=   Tr+   securehttponlyz6Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Securer:   r6   s     r'   test_set_secure_httponly_attrsz*CookieTests.test_set_secure_httponly_attrsu   sI      !;<"&*h$(*j!D	Fr)   c                 h   g d}|D ]  }| j                  |      5  t        j                  d      }||d   d<   | j                  |j	                         d|z         t        j                         }|j                  d|z         | j                  |d   d   |       d d d         y # 1 sw Y   xY w)N)StrictLaxstrictlax)r9   r=   r+   samesitez1Set-Cookie: Customer="WILE_E_COYOTE"; SameSite=%sz%Customer="WILL_E_COYOTE"; SameSite=%s)subTestr   r   r   r   r   )r!   samesite_valuesr9   r$   s       r'   test_samesite_attrszCookieTests.test_samesite_attrs|   s    <" 		AC#& A(()CD,/*j)  G#MO ((*>DE  :z!:C@A A		AA As   BB((B1	c                     t        j                         }|j                  d       | j                  |d   d          | j                  |d   d          y )Nzeggs=scrambled; Path=/baconeggsrG   rF   )r   r   r   assertFalser6   s     r'   )test_secure_httponly_false_if_not_presentz5CookieTests.test_secure_httponly_false_if_not_present   sK      "	,-6:./68,-r)   c                     t        j                         }|j                  d       | j                  |d   d          | j                  |d   d          y )Nz-eggs=scrambled; httponly; secure; Path=/baconrS   rG   rF   )r   r   r   rB   r6   s     r'   $test_secure_httponly_true_if_presentz0CookieTests.test_secure_httponly_true_if_present   sG      "	>?&	*-.&	(+,r)   c                 
   t        j                         }|j                  d       | j                  |d   d          | j                  |d   d          | j	                  |d   d   d       | j	                  |d   d   d       y )Nz5eggs=scrambled; httponly=foo; secure=bar; Path=/baconrS   rG   rF   foobar)r   r   r   rB   r   r6   s     r'   'test_secure_httponly_true_if_have_valuez3CookieTests.test_secure_httponly_true_if_have_value   s}       "	FG&	*-.&	(+, 	6:.668,e4r)   c                     t        j                         }|j                  d       | j                  |j	                         d       y )Nz=eggs  =  scrambled  ;  secure  ;  path  =  bar   ; foo=foo   zASet-Cookie: eggs=scrambled; Path=bar; Secure
Set-Cookie: foo=foo)r   r   r   r   r   r6   s     r'   test_extra_spaceszCookieTests.test_extra_spaces   s7      "	NOQ	Sr)   c                    t        j                         }|j                  d       | j                  |d   j                  d       | j                  |d   d   d       | j                  |d   d   d       | j                  |j                  dg      d       | j                  |j                         d	       | j                  |j                  dg      d
       y )Nz3Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r6   s     r'   test_quoted_metazCookieTests.test_quoted_meta   s      "	DE:,,o>:y137:v.86(+K	M ) 	 	fX. 1 	r)   c                     t        j                         }dD ]N  }|j                  |       | j                  t	        |      i        | j                  |j                         d       P y )N)
z]foo=xz[foo=xz
blah]foo=xz
blah[foo=xzSet-Cookie: foo=barzSet-Cookie: foozfoo=bar; bazzbaz; foo=barzsecure;foo=barzVersion=1;foo=bar )r   r   r   r   r   r   )r!   r$   ss      r'   test_invalid_cookiesz CookieTests.test_invalid_cookies   sX      "9 	-A FF1IT!Wb)QXXZ,	-r)   c                    d}d|z  }t        j                         }|j                  |       | j                  |j	                         |       t        t        j                  dz         D ]g  }| j                  |      5  t        j                  t        j                  ||            }| j                  |j	                         |       d d d        i y # 1 sw Y   txY w)Nz/Customer="WILE_E_COYOTE"; Path=/acme; Version=1zSet-Cookie: %s   proto)protocol)r   r   r   r   r   rangepickleHIGHEST_PROTOCOLrO   loadsdumps)r!   rawdataexpected_outputr$   rg   C1s         r'   test_picklezCookieTests.test_pickle   s    C*W4  "	w_5622Q67 	?EE* ?\\&,,q5"AB  o>? ?	?? ?s   >ACC	c                     d}t        j                         }| j                  t         j                        5  |j	                  |       d d d        y # 1 sw Y   y xY w)Nz
a=b; c,d=e)r   r   assertRaisesCookieErrorr   )r!   rn   r$   s      r'   test_illegal_charszCookieTests.test_illegal_chars   sI      "w223 	FF7O	 	 	s   AAc                     t        j                         }d|d<   | j                  t        |d         d       d|d   d<   | j                  t        |d         d       y )N   ©rY   zSet-Cookie: foo="\251"u
   comment ©commentz.Set-Cookie: foo="\251"; Comment="comment \251")r   r   r   str)r!   r   s     r'   test_comment_quotingz CookieTests.test_comment_quoting   s\      "'%QuX(AB:%%M>	
r)   N)__name__
__module____qualname__r(   r7   r;   rD   rH   rQ   rU   rW   r[   r]   r_   rc   rq   ru   rz    r)   r'   r   r   
   sR    /0b2:M.FA.-
5S2	-?
r)   r   c                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)MorselTestszTests for the Morsel object.c                    t        j                         }| j                  |j                         | j                  |j                         | j                  |j
                         | j                  |j                         t         j                  j                  j                                |j                         D ]  \  }}| j                  |d|        y Nra   )
r   MorselassertIsNonekeyr    coded_valuer   keys	_reservedr   )r!   morselr   r9   s       r'   test_defaultszMorselTests.test_defaults   s    !&**%&,,'&,,-(@(@(E(E(GH 	+HCS"c*	+r)   c                 x   t        j                         }|j                  D ]*  }| j                  |j	                  |             d|z  ||<   , |j                  D ]  }| j                  ||   d|z          dj                         D ]1  }| j                  t         j                  |j                  |d|z         3 y )N%s_valuezthe holy hand grenade)
r   r   r   rB   isReservedKeyr   splitrs   rt   __setitem__)r!   Mis      r'   test_reserved_keyszMorselTests.test_reserved_keys   s    NN 	"AOOAOOA./>AaD	"  	3AQqT:>2	3 )..0 	@Ag11mmQ
Q@	@r)   c           	         t        j                         }|j                  D ]5  }| j                  t         j                  |j
                  |d|z  d|z         7 dj                         D ]  }d|d<   |j                  |d|z  d|z         | j                  |j                  |       | j                  |j                  d|z         | j                  |j                  d|z         | j                  |j                         d|dd|z  d	       d
|dd|z  d}| j                  |j                         |        dD ]5  }| j                  t         j                  |j
                  |d|z  d|z         7 y )Nr   z(thou cast _the- !holy! ^hand| +*grenade~z/foor/   z%s_valz%s_coded_valzSet-Cookie: =z; Path=/fooz^
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "zB; Path=/foo";
        // end hiding -->
        </script>
        )zfoo barzfoo@bar)r   r   r   rs   rt   setr   r   r   r    r   r   r5   )r!   r   r   expected_js_outputs       r'   test_setterzMorselTests.test_setter  se   NN 	HAg11eeQ
Q
QH	H <AAC 	@A AfIEE!X\>A#56QUUA&QWWhl3Q]]NQ,>?
23^a5GHJ .1$"& Q[[],>?%	@& ( 	HAg11eeQ
Q
QH	Hr)   c                 R   t        j                         }| j                  t              5  d|_        d d d        | j                  t              5  d|_        d d d        | j                  t              5  d|_        d d d        y # 1 sw Y   TxY w# 1 sw Y   ;xY w# 1 sw Y   y xY wr   )r   r   rs   AttributeErrorr   r    r   r!   r   s     r'   test_set_propertieszMorselTests.test_set_properties$  s    !~. 	FJ	~. 	FL	~. 	$!#F	$ 	$		 		 		$ 	$s#   BB4BBBB&c                 
   d}ddddd}t        j                         }|j                  |        |j                  |  t        j                         }|j                  |        |j                  |  | j	                  ||k(         | j                  ||k7         d}|D ]x  }| j                  |      5  t        j                         }|j                  |        |j                  |  | j                  ||k(         | j	                  ||k7         d d d        z t        j                         }|j                  |        |j                  |  d|d	<   | j                  ||k(         | j	                  ||k7         | j                  t        j                         d
k(         | j	                  t        j                         d
k7         | j                  t        j                         dk(         | j	                  t        j                         dk7         t        t        j                         j                               }| j                  t        j                         |k(         | j	                  t        j                         |k7         t        j                         } |j                  |  |j                  |       | j	                  |t        |      k(         | j                  |t        |      k7         y # 1 sw Y   dxY w)N)r   r    "value"/rY   example.com   )r/   rx   domainr-   ))r   r    mismatch)r   r   r   )r   r    r   rZ   rx   re   ra   )
r   r   updater   rB   rT   rO   listr   r   )	r!   	base_caseattribsmorsel_amorsel_br"   case_br   r   s	            r'   test_eqzMorselTests.test_eq-  sp   /	#	
 >># i >># i H,-X-.

  	6Ff% 6">>+(f%  X!56H 456 6	6 >># i #X-.H,- 	)Q./(A-.)R/0(B./W^^%++-.)U23(E12 !

Ig$v,./4</096 6s   2AK88L	c                    t        j                         }|j                  ddd       |j                  ddd       |j	                         }| j                  |t         j                         | j                  ||       | j                  ||       t	        j                  |      }| j                  |t         j                         | j                  ||       | j                  ||       y )NrY   rZ   bazr   r-   rx   )r   r   r   r   copyassertIsInstanceassertIsNotr   )r!   r   r   s      r'   	test_copyzMorselTests.test_copya  s    >>#UE5)
 	 ==?h78,8,99X&h78,8,r)   c                 ^   t        j                         }d|d<   | j                  |d   d       d|d<   | j                  |d   d       d|d<   | j                  |d   d       | j                  t         j                        5  d	|d
<   d d d        | j                  d
|       y # 1 sw Y   xY w)Nr   r>   r   Versionr-   r   DOMAINr   r    invalid)r   r   r   rs   rt   assertNotInr   s     r'   test_setitemzMorselTests.test_setitemr  s    !y	*A.y	*A.(x)=9w223 	( 'F9	(F+	( 	(s   B##B,c                 N   t        j                         }|j                  ddd       | j                  |j	                  dd      d       | j                  |d   d       | j                  |j	                  dd      d       | j                  |d	   d       | j                  |j	                  d
d      d       | j                  |d   d       | j                  t         j                        5  |j	                  dd       d d d        | j                  d|       y # 1 sw Y   xY w)Nr   r   )r   r-   r>   r    ra   r   re   r-   r   r   r   )r   r   r   r   
setdefaultrs   rt   r   r   s     r'   test_setdefaultzMorselTests.test_setdefault  s   !#
 	
 	**9g>C	*B/**9a8!<	*A.**8W=}M)=9w223 	2i1	2F+	2 	2s   -DD$c                    dddd}t        j                         }|j                  |       | j                  |d   d       | j                  |d   d       | j                  |d   d       t        j                         }|j                  t	        |j                                      | j                  |d   d       | j                  |d   d       | j                  |d   d       t        j                         }|j                  d |j                         D               | j                  |d   d       | j                  |d   d       | j                  |d   d       | j                  t         j                        5  |j                  d	d
i       d d d        | j                  d	|       | j                  t        |j                         | j                  t        |j                  d       y # 1 sw Y   ]xY w)Nre   r   r   )r>   r   r   r>   r-   r   c              3   *   K   | ]  \  }}||f  y wNr~   ).0r%   r&   s      r'   	<genexpr>z*MorselTests.test_update.<locals>.<genexpr>  s     9Aq!f9s   r   r    r   )
r   r   r   r   r   r   rs   rt   r   	TypeError)r!   r   r   s      r'   test_updatezMorselTests.test_update  s   AG!g	*A.	*A.)=9!d7==?+,	*A.	*A.)=9!999	*A.	*A.)=9w223 	0MM9g./	0F+)V]]3)V]]A6		0 	0s   >G..G7c                    t        j                         }|j                  ddd       |j                  ddd       t	        t
        j                  dz         D ]  }| j                  |      5  t        j                  t        j                  ||            }| j                  |t         j                         | j                  ||       | j                  t        |      t        |             d d d         y # 1 sw Y   xY w)NrY   rZ   r   r   r   re   rf   )r   r   r   r   ri   rj   rk   rO   rl   rm   r   r   ry   )r!   r   rg   r   s       r'   rq   zMorselTests.test_pickle  s    >>#UE5)
 	 622Q67 	?EE* ?!<<Xu(EF%%h?  84  XH>	? ?	?? ?s   -B C88D	c           	      
   t        j                         }| j                  t        |      d       | j                  t	        |      d       |j                  ddd       | j                  t        |      d       | j                  t	        |      d       |j                  dd	d
dddd       | j                  t        |      d       | j                  t	        |      d       d|d<   d|d<   | j                  t        |      d       | j                  t	        |      d       t        j                         }|j                  ddd       d|d<   | j                  t        |      d       | j                  t	        |      d       y )Nz<Morsel: None=None>zSet-Cookie: None=Noner   r9   	coded_valz<Morsel: key=coded_val>zSet-Cookie: key=coded_valr   rY   r   r   re   )r/   rx   r   rA   rF   r-   zV<Morsel: key=coded_val; Comment=foo; Domain=example.com; Max-Age=0; Path=/; Version=1>zXSet-Cookie: key=coded_val; Comment=foo; Domain=example.com; Max-Age=0; Path=/; Version=1TrF   rG   zh<Morsel: key=coded_val; Comment=foo; Domain=example.com; HttpOnly; Max-Age=0; Path=/; Secure; Version=1>zjSet-Cookie: key=coded_val; Comment=foo; Domain=example.com; HttpOnly; Max-Age=0; Path=/; Secure; Version=1r>   zA<Morsel: key=coded_val; expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+>zCSet-Cookie: key=coded_val; expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+)r   r   r   r   ry   r   r   assertRegexr   s     r'   	test_reprzMorselTests.test_repr  su   !f'<=V&=>

5%-f'@AV&AB#
 	 	f0	1 	V/	0  xzfB	C 	VA	B !

5%-yf=	> 	V<	=r)   N)r{   r|   r}   __doc__r   r   r   r   r   r   r   r   r   rq   r   r~   r)   r'   r   r      s?    &+@H>$21h-",,$76?&=r)   r   c                 V    |j                  t        j                  t                     |S r   )addTestdoctestDocTestSuiter   )loadertestspatterns      r'   
load_testsr     s    	MM'&&w/0Lr)   __main__)r   unittestr   httpr   rj   TestCaser   r   r   r{   mainr~   r)   r'   <module>r      s_        ]
(## ]
@v=(## v=r
 zHMMO r)   