B
    u9aH  ã               @   sr   d dl Z d dlmZmZ d dlZd dlmZ d dlZG dd„ dejƒZ	G dd„ dejƒZ
dd	„ Zed
krneƒ  dS )é    N)Úrun_unittestÚrun_doctest)Úcookiesc               @   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 )ÚCookieTestsc          
   C   sæ   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  d¡dœg}x~|D ]v}t ¡ }| |d ¡ |  t|ƒ|d  ¡ |  |jdd!|d" ¡ x.t|d#  ¡ ƒD ]\}}|  || j	|¡ qÀW qhW d S )$Nzchips=ahoy; vienna=fingerZahoyZfinger)ZchipsZ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;"Z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ÚcasesZcaseÚCÚkÚv© r    ú'/usr/lib/python3.7/test_http_cookies.pyÚ
test_basic   s8    
zCookieTests.test_basicc             C   sˆ   t  ¡ }| d¡ |  |d jd¡ |  |d d d¡ |  |d d d¡ |  | dg¡d¡ |  | ¡ d	¡ |  | dg¡d
¡ d S )Nz/Customer="WILE_E_COYOTE"; Version=1; Path=/acmeÚCustomerÚWILE_E_COYOTEÚversionÚ1Úpathz/acmez0Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acmezÆ
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
        // end hiding -->
        </script>
        z»
        <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   r    r    r!   Ú	test_load=   s    

zCookieTests.test_loadc             C   s(   t  ¡ }d|d< |  | dg¡d¡ d S )Nzsome,funky;stuffÚvalz(Set-Cookie: val="some\054funky\073stuff")r   r   r   r	   )r   r   r    r    r!   Útest_extended_encodeV   s    z CookieTests.test_extended_encodec             C   s    t  d¡}d|d d< |  | ¡  d¡¡ t  ¡ }| d¡ |  |d d d¡ t  ¡ }| d¡ |  |d d d	¡ t  d¡}d
|d d< |  | ¡ d¡ d S )NzCustomer="WILE_E_COYOTE"r   r#   ÚexpiresZ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é
   zmax-agez0Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10)r   r   Ú
assertTruer	   Úendswithr   r   )r   r   r    r    r!   Útest_special_attrs^   s    




zCookieTests.test_special_attrsc             C   s6   t  d¡}d|d d< d|d d< |  | ¡ d¡ d S )NzCustomer="WILE_E_COYOTE"Tr#   ÚsecureÚhttponlyz6Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure)r   r   r   r	   )r   r   r    r    r!   Útest_set_secure_httponly_attrsu   s
    

z*CookieTests.test_set_secure_httponly_attrsc             C   s:   t  ¡ }| d¡ |  |d d ¡ |  |d d ¡ d S )Nzeggs=scrambled; Path=/baconÚeggsr2   r1   )r   r   r   ÚassertFalse)r   r   r    r    r!   Ú)test_secure_httponly_false_if_not_present|   s    
z5CookieTests.test_secure_httponly_false_if_not_presentc             C   s:   t  ¡ }| d¡ |  |d d ¡ |  |d d ¡ d S )Nz-eggs=scrambled; httponly; secure; Path=/baconr4   r2   r1   )r   r   r   r.   )r   r   r    r    r!   Ú$test_secure_httponly_true_if_present‚   s    
z0CookieTests.test_secure_httponly_true_if_presentc             C   sb   t  ¡ }| d¡ |  |d d ¡ |  |d d ¡ |  |d d d¡ |  |d d d¡ d S )Nz5eggs=scrambled; httponly=foo; secure=bar; Path=/baconr4   r2   r1   ÚfooÚbar)r   r   r   r.   r   )r   r   r    r    r!   Ú'test_secure_httponly_true_if_have_value‰   s    
z3CookieTests.test_secure_httponly_true_if_have_valuec             C   s&   t  ¡ }| d¡ |  | ¡ d¡ d S )Nz=eggs  =  scrambled  ;  secure  ;  path  =  bar   ; foo=foo   zASet-Cookie: eggs=scrambled; Path=bar; Secure
Set-Cookie: foo=foo)r   r   r   r   r	   )r   r   r    r    r!   Útest_extra_spaces•   s    

zCookieTests.test_extra_spacesc             C   sˆ   t  ¡ }| d¡ |  |d jd¡ |  |d d d¡ |  |d d d¡ |  | dg¡d¡ |  | ¡ d	¡ |  | dg¡d
¡ d S )Nz3Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"r#   r$   r%   r&   r'   z/acmez0Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acmezÆ
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
        // end hiding -->
        </script>
        z»
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
        // end hiding -->
        </script>
        )r   r   r   r   r   r	   r(   )r   r   r    r    r!   Útest_quoted_meta›   s    

zCookieTests.test_quoted_metac             C   sD   t  ¡ }x6dD ].}| |¡ |  t|ƒi ¡ |  | ¡ d¡ qW d S )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   Úsr    r    r!   Útest_invalid_cookies´   s
    

z CookieTests.test_invalid_cookiesc          
   C   s†   d}d| }t  ¡ }| |¡ |  | ¡ |¡ xRttjd ƒD ]@}| j|d* t 	tj
||d¡}|  | ¡ |¡ W d Q R X q>W d S )Nz/Customer="WILE_E_COYOTE"; Path=/acme; Version=1zSet-Cookie: %sé   )Úproto)Zprotocol)r   r   r   r   r	   ÚrangeÚpickleÚHIGHEST_PROTOCOLÚsubTestÚloadsÚdumps)r   ÚrawdataZexpected_outputr   rA   ZC1r    r    r!   Útest_pickle¿   s    
zCookieTests.test_picklec          	   C   s2   d}t  ¡ }|  t j¡ | |¡ W d Q R X d S )Nz
a=b; c,d=e)r   r   ÚassertRaisesÚCookieErrorr   )r   rH   r   r    r    r!   Útest_illegal_charsÌ   s    zCookieTests.test_illegal_charsc             C   sH   t  ¡ }d|d< |  t|d ƒd¡ d|d d< |  t|d ƒd¡ d S )Nõ   Â©r8   zSet-Cookie: foo="\251"u
   comment Â©Úcommentz.Set-Cookie: foo="\251"; Comment="comment \251")r   r   r   Ústr)r   r   r    r    r!   Útest_comment_quotingÒ   s    
z CookieTests.test_comment_quotingN)Ú__name__Ú
__module__Ú__qualname__r"   r)   r+   r0   r3   r6   r7   r:   r;   r<   r?   rI   rL   rP   r    r    r    r!   r   
   s   1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d„ Z
dd„ Zdd„ Zdd„ ZdS )ÚMorselTestsc             C   sl   t  ¡ }|  |j¡ |  |j¡ |  |j¡ |  | ¡ t jj ¡ ¡ x"| 	¡ D ]\}}|  |d|¡ qNW d S )Nr=   )
r   ÚMorselZassertIsNoneÚkeyr   Úcoded_valuer   ÚkeysÚ	_reservedr   )r   ÚmorselrV   r*   r    r    r!   Útest_defaultsà   s    zMorselTests.test_defaultsc             C   s†   t  ¡ }x*|jD ] }|  | |¡¡ d| ||< qW x"|jD ]}|  || d| ¡ q<W x(d ¡ D ]}|  t j|j	|d| ¡ qbW d S )Nz%s_valuezthe holy hand grenade)
r   rU   rY   r.   ZisReservedKeyr   ÚsplitrJ   rK   Ú__setitem__)r   ÚMÚir    r    r!   Útest_reserved_keysé   s    zMorselTests.test_reserved_keysc          	   C   s  t  ¡ }x,|jD ]"}|  t j|j|d| d| ¡ qW xœd ¡ D ]}d|d< | |d| d| ¡ |  |j|¡ |  |j	d| ¡ |  |j
d| ¡ |  | ¡ d|d| f ¡ d|d| f }|  | ¡ |¡ q@W x*d	D ]"}|  t j|j|d| d| ¡ qÚW d S )
Nz%s_valuez(thou cast _the- !holy! ^hand| +*grenade~z/foor'   z%s_valz%s_coded_valzSet-Cookie: %s=%s; Path=/fooz¥
        <script type="text/javascript">
        <!-- begin hiding
        document.cookie = "%s=%s; Path=/foo";
        // end hiding -->
        </script>
        )zfoo barzfoo@bar)r   rU   rY   rJ   rK   Úsetr\   r   rV   r   rW   r	   r(   )r   r^   r_   Zexpected_js_outputr    r    r!   Útest_setterø   s$    
zMorselTests.test_setterc          	   C   s`   t  ¡ }|  t¡ d|_W d Q R X |  t¡ d|_W d Q R X |  t¡ d|_W d Q R X d S )Nr=   )r   rU   rJ   ÚAttributeErrorrV   r   rW   )r   rZ   r    r    r!   Útest_set_properties  s    zMorselTests.test_set_propertiesc       	   
   C   sÆ  d}dddddœ}t  ¡ }| |¡ |j|Ž  t  ¡ }| |¡ |j|Ž  |  ||k¡ |  ||k¡ d}xZ|D ]R}|  |¡> t  ¡ }| |¡ |j|Ž  |  ||k¡ |  ||k¡ W d Q R X qpW t  ¡ }| |¡ |j|Ž  d|d	< |  ||k¡ |  ||k¡ |  t  ¡ d
k¡ |  t  ¡ d
k¡ |  t  ¡ dk¡ |  t  ¡ dk¡ tt  ¡  ¡ ƒ}|  t  ¡ |k¡ |  t  ¡ |k¡ t  ¡ }|j|Ž  | |¡ |  |t	|ƒk¡ |  |t	|ƒk¡ d S )N)rV   r   z"value"ú/r8   zexample.comé   )r'   rN   Údomainr%   ))rV   r   Úmismatch)rV   rh   z"value")rh   r   z"value"r9   rN   r@   r=   )
r   rU   Úupdatera   r.   r5   rE   Úlistr   r   )	r   Z	base_caseÚattribsÚmorsel_aÚmorsel_br   Zcase_br   rZ   r    r    r!   Útest_eq   sN    










zMorselTests.test_eqc             C   sˆ   t  ¡ }| ddd¡ | dddœ¡ | ¡ }|  |t j¡ |  ||¡ |  ||¡ t |¡}|  |t j¡ |  ||¡ |  ||¡ d S )Nr8   r9   Úbazrf   )r%   rN   )r   rU   ra   ri   ÚcopyÚassertIsInstanceZassertIsNotr   )r   rl   rm   r    r    r!   Ú	test_copyT  s    

zMorselTests.test_copyc          	   C   s€   t  ¡ }d|d< |  |d d¡ d|d< |  |d d¡ d|d< |  |d d¡ |  t j¡ d	|d
< W d Q R X |  d
|¡ d S )Nr   r,   rf   ÚVersionr%   zexample.comÚDOMAINrg   r   Úinvalid)r   rU   r   rJ   rK   ÚassertNotIn)r   rZ   r    r    r!   Útest_setiteme  s    zMorselTests.test_setitemc          	   C   s¸   t  ¡ }| dddœ¡ |  | dd¡d¡ |  |d d¡ |  | dd¡d¡ |  |d	 d¡ |  | d
d¡d¡ |  |d d¡ |  t j¡ | dd¡ W d Q R X |  d|¡ d S )Nzexample.comrf   )rg   r%   r,   r   r=   rs   r@   r%   rt   rg   ru   )r   rU   ri   r   Ú
setdefaultrJ   rK   rv   )r   rZ   r    r    r!   Útest_setdefaultr  s    
zMorselTests.test_setdefaultc          	   C   s<  ddddœ}t  ¡ }| |¡ |  |d d¡ |  |d d¡ |  |d d¡ t  ¡ }| t| ¡ ƒ¡ |  |d d¡ |  |d d¡ |  |d d¡ t  ¡ }| dd	„ | ¡ D ƒ¡ |  |d d¡ |  |d d¡ |  |d d¡ |  t j¡ | d
di¡ W d Q R X |  d
|¡ |  t	|j¡ |  t	|jd¡ d S )Nr@   rf   zexample.com)r,   rs   rt   r,   r%   rg   c             s   s   | ]\}}||fV  qd S )Nr    )Ú.0r   r   r    r    r!   ú	<genexpr>”  s    z*MorselTests.test_update.<locals>.<genexpr>ru   r   r   )
r   rU   ri   r   rj   r   rJ   rK   rv   Ú	TypeError)r   rk   rZ   r    r    r!   Útest_update„  s*    
zMorselTests.test_updatec          
   C   sš   t  ¡ }| ddd¡ | dddœ¡ xnttjd ƒD ]\}| j|dF t t 	||¡¡}|  
|t j¡ |  ||¡ |  t|ƒt|ƒ¡ W d Q R X q6W d S )Nr8   r9   ro   rf   )r%   rN   r@   )rA   )r   rU   ra   ri   rB   rC   rD   rE   rF   rG   rq   r   rO   )r   rl   rA   rm   r    r    r!   rI   Ÿ  s    
zMorselTests.test_picklec          	   C   s   t  ¡ }|  t|ƒd¡ |  t|ƒd¡ | ddd¡ |  t|ƒd¡ |  t|ƒd¡ | dd	d
ddddœ¡ |  t|ƒd¡ |  t|ƒd¡ d|d< d|d< |  t|ƒd¡ |  t|ƒd¡ t  ¡ }| ddd¡ d|d< |  t|ƒd¡ |  t|ƒd¡ d S )Nz<Morsel: None=None>zSet-Cookie: None=NonerV   r*   Z	coded_valz<Morsel: key=coded_val>zSet-Cookie: key=coded_valre   r8   zexample.comr   r@   )r'   rN   rg   zmax-ager1   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=1Tr1   r2   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   rU   r   r   rO   ra   ri   ZassertRegex)r   rZ   r    r    r!   Ú	test_repr­  s<    






zMorselTests.test_reprN)rQ   rR   rS   r[   r`   rb   rd   rn   rr   rw   ry   r}   rI   r~   r    r    r    r!   rT   Ý   s   		4rT   c               C   s   t ttƒ ttƒ d S )N)r   r   rT   r   r   r    r    r    r!   Ú	test_mainÕ  s    
r   Ú__main__)rp   Ztest.supportr   r   ZunittestZhttpr   rC   ZTestCaser   rT   r   rQ   r    r    r    r!   Ú<module>   s    T y