B
    u9aP) ã               @   s:  d 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mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z  G dd„ dej!ƒZ"G dd„ dej!ƒZ#G dd	„ d	ƒZ$d
d„ Z%dd„ Z&dd„ Z'G dd„ dej!ƒZ(G dd„ dej!ƒZ)G dd„ dej!ƒZ*ddd„Z+e,dkr6e+dd dS )zTests for http/cookiejar.py.é    N)Ú	time2isozÚ	http2timeÚiso2timeÚtime2netscapeÚparse_ns_headersÚjoin_header_wordsÚsplit_header_wordsÚCookieÚ	CookieJarÚDefaultCookiePolicyÚLWPCookieJarÚMozillaCookieJarÚ	LoadErrorÚlwp_cookie_strÚDEFAULT_HTTP_PORTÚescape_pathÚreachÚis_HDNÚdomain_matchÚuser_domain_matchÚrequest_pathÚrequest_portÚrequest_hostc               @   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 )ÚDateTimeTestsc             C   s–   d}d}|   t|ƒd¡ |   t|| ƒd¡ |   t|d|  ƒd¡ |   t|d|  ƒd¡ tƒ }td	ƒ}x&||fD ]}|  |d
d||f ¡ qtW d S )Nix+À<i€Q z2002-04-19 14:36:40Zz2002-04-20 14:36:40Zé   z2002-04-21 14:36:40Zé   z2002-04-22 14:36:40Zi ¡ z!^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$zbad time2isoz format: %s %s)ÚassertEqualr   ÚassertRegex)ÚselfÚbaseÚdayÚazÚbzÚtext© r$   ú)/usr/lib/python3.7/test_http_cookiejar.pyÚtest_time2isoz   s    zDateTimeTests.test_time2isozc             C   s–   d}d}|   t|ƒd¡ |   t|| ƒd¡ |   t|d|  ƒd¡ |   t|d|  ƒd¡ tƒ }td	ƒ}x&||fD ]}|  |d
d||f ¡ qtW d S )Nix+À<i€Q zFri, 19-Apr-2002 14:36:40 GMTzSat, 20-Apr-2002 14:36:40 GMTr   zSun, 21-Apr-2002 14:36:40 GMTr   zMon, 22-Apr-2002 14:36:40 GMTi ¡ z;[a-zA-Z]{3}, \d{2}-[a-zA-Z]{3}-\d{4} \d{2}:\d{2}:\d{2} GMT$zbad time2netscape format: %s %s)r   r   r   )r   r   r    r!   r"   r#   r$   r$   r%   Útest_time2netscape"   s     z DateTimeTests.test_time2netscapec             C   s<   dd„ }|   |dƒd¡ |   |dƒd¡ |   |dƒd¡ d S )	Nc             S   s   t  t| ƒ¡d d… S )Né   )ÚtimeÚgmtimer   )r#   r$   r$   r%   Ú
parse_date9   s    z0DateTimeTests.test_http2time.<locals>.parse_datez01 Jan 2001)iÑ  é   r,   r   r   g        z	03-Feb-20)iä  r   r   r   r   g        z	03-Feb-98)iÎ  r   r   r   r   g        )r   )r   r+   r$   r$   r%   Útest_http2time8   s    zDateTimeTests.test_http2timec             C   s    ddddddddd	d
ddddg}d}t |ƒ}d}|  ||d|||f ¡ xR|D ]J}|  t|ƒ||¡ |  t| ¡ ƒ|| ¡ ¡ |  t| ¡ ƒ|| ¡ ¡ qNW d S )NzThu, 03 Feb 1994 00:00:00 GMTz Thursday, 03-Feb-94 00:00:00 GMTz"Thursday, 03-Feb-1994 00:00:00 GMTz03 Feb 1994 00:00:00 GMTz03-Feb-94 00:00:00 GMTz03-Feb-1994 00:00:00 GMTz03-Feb-1994 00:00 GMTz03-Feb-1994 00:00z02-Feb-1994 24:00z	03-Feb-94z03-Feb-1994z03 Feb 1994z  03   Feb   1994  0:00  z  03-Feb-1994  i€>P-z1994-02-03 00:00:00Zz%s  =>  '%s' (%s))r   r   r   ÚlowerÚupper)r   ÚtestsÚtest_tÚresultÚexpectedÚsr$   r$   r%   Útest_http2time_formatsD   s.    
z$DateTimeTests.test_http2time_formatsc             C   s.   x(dD ] }|   t|ƒd|t|ƒf ¡ qW d S )N)Ú ÚGarbagezMandag 16. September 1996z
01-00-1980z
01-13-1980z
00-01-1980z
32-01-1980z01-01-1980 25:00:00z01-01-1980 00:61:00z01-01-1980 00:00:62z08-Oct-3697739z08-01-3697739z09 Feb 19942632 22:23:32 GMTz Wed, 09 Feb 1994834 22:23:32 GMTz,http2time(%s) is not None
http2time(test) %s)ÚassertIsNoner   )r   Útestr$   r$   r%   Útest_http2time_garbageh   s    

z$DateTimeTests.test_http2time_garbagec             C   s<   dd„ }|   |dƒd¡ |   |dƒd¡ |   |dƒd¡ d S )Nc             S   s   t  t| ƒ¡d d… S )Nr(   )r)   r*   r   )r#   r$   r$   r%   r+   ~   s    z/DateTimeTests.test_iso2time.<locals>.parse_dateZ19940203T141529Z)iÊ  r   r   é   é   é   z1994-02-03 07:15:29 -0700z1994-02-03 19:45:29 +0530)r   )r   r+   r$   r$   r%   Útest_iso2time}   s    


zDateTimeTests.test_iso2timec          
   C   st   ddddddddd	d
g
}d}xR|D ]J}|   t|ƒ||¡ |   t| ¡ ƒ|| ¡ ¡ |   t| ¡ ƒ|| ¡ ¡ q"W d S )Nz1994-02-03 00:00:00 -0000z1994-02-03 00:00:00 +0000z1994-02-03 00:00:00z
1994-02-03z1994-02-03T00:00:00Z19940203z1994-02-02 24:00:00Z19940203T000000Zz  1994-02-03 z  1994-02-03T00:00:00  i€>P-)r   r   r.   r/   )r   r0   r1   r4   r$   r$   r%   Útest_iso2time_formats   s    
z#DateTimeTests.test_iso2time_formatsc             C   s&   x dD ]}|   t|ƒd| ¡ qW d S )N)r6   r7   z Thursday, 03-Feb-94 00:00:00 GMTz
1980-00-01z
1980-13-01z
1980-01-00z
1980-01-32z1980-01-01 25:00:00z1980-01-01 00:61:00z01-01-1980 00:00:62z01-01-1980T00:00:62Z19800101T250000Zziso2time(%r))r8   r   )r   r9   r$   r$   r%   Útest_iso2time_garbage¤   s    

z#DateTimeTests.test_iso2time_garbageN)Ú__name__Ú
__module__Ú__qualname__r&   r'   r-   r5   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d„ Zd	d
„ Zdd„ ZdS )ÚHeaderTestsc             C   s0   dddgg}xdD ]}|   t|gƒ|¡ qW d S )N)ÚfooÚbar)Úexpiresl   d9W )ÚversionÚ0)z)foo=bar; expires=01 Jan 2040 22:23:32 GMTz+foo=bar; expires="01 Jan 2040 22:23:32 GMT")r   r   )r   r3   Úhdrr$   r$   r%   Útest_parse_ns_headers¹   s    
z!HeaderTests.test_parse_ns_headersc             C   s.   ddgg}xdD ]}|   t|gƒ|¡ qW d S )N)rE   rF   )rH   Ú1)zfoo=bar; version="1"zfoo=bar; Version="1")r   r   )r   r3   rJ   r$   r$   r%   Útest_parse_ns_headers_versionÂ   s    

z)HeaderTests.test_parse_ns_headers_versionc             C   s$   d}ddgg}|   t|gƒ|¡ d S )Nz expires=01 Jan 2040 22:23:32 GMT)rG   z01 Jan 2040 22:23:32 GMT)rH   rI   )r   r   )r   rJ   r3   r$   r$   r%   Ú#test_parse_ns_headers_special_namesÌ   s    
z/HeaderTests.test_parse_ns_headers_special_namesc             C   s0   t ddggƒ}|  |d¡ |  t g gƒd¡ d S )N)rE   N)rF   Úbazzfoo; bar=bazr6   )r   r   )r   Zjoinedr$   r$   r%   Útest_join_header_wordsÔ   s    z"HeaderTests.test_join_header_wordsc             C   s   ddggfddggfddggfddggfddggfd	dggfd
ddggfdddggfddggfdddggfddddggfddgdgdggfdddgdgdgdggfg}xt|D ]l\}}yt |gƒ}W n<   dd l}dd l}| ¡ }| d |¡ d| ¡  }Y nX |  ||d|||f ¡ q¬W d S )NrE   )rE   Nzfoo=bar)rE   rF   z	   foo   z
   foo=   )rE   r6   z   foo=z   foo=   ; z   foo=   ; bar= baz )rF   rO   zfoo=bar bar=bazzfoo= bar=baz)rE   zbar=bazzfoo=bar;bar=bazzfoo bar baz)rF   N)rO   Nza, b, c)ÚaN)ÚbN)ÚcNz(foo; bar=baz, spam=, foo="\,\;\"", bar= )Úspamr6   )rE   z,;")rF   r6   r   z (error -- traceback follows)

%sz:
When parsing: '%s'
Expected:     '%s'
Got:          '%s'
)r   Ú	tracebackÚioÚStringIOÚ	print_excÚgetvaluer   )r   r0   ÚargÚexpectr2   rU   rV   Úfr$   r$   r%   Útest_split_header_wordsÚ   s2    






z#HeaderTests.test_split_header_wordsc             C   s`   ddddddddd	d
dddg}x<|D ]4\}}t |gƒ}t|ƒ}|  ||d||||f ¡ q$W d S )N)rE   rE   )zfoo=barzfoo=bar)z	   foo   rE   )zfoo=zfoo="")zfoo=bar bar=bazzfoo=bar; bar=baz)zfoo=bar;bar=bazzfoo=bar; bar=baz)zfoo bar bazzfoo; bar; baz)zfoo="\"" bar="\\"zfoo="\""; bar="\\")z	foo,,,barzfoo, bar)zfoo=bar,bar=bazzfoo=bar, bar=baz)ztext/html; charset=iso-8859-1ztext/html; charset="iso-8859-1")z)foo="bar"; port="80,81"; discard, bar=bazz'foo=bar; port="80,81"; discard, bar=baz)zBasic realm="\"foo\\\\bar\""zBasic; realm="\"foo\\\\bar\""zM
When parsing: '%s'
Expected:     '%s'
Got:          '%s'
Input was:    '%s'
)r   r   r   )r   r0   rZ   r[   ÚinputÚresr$   r$   r%   Útest_roundtripü   s$    
zHeaderTests.test_roundtripN)	rA   rB   rC   rK   rM   rN   rP   r]   r`   r$   r$   r$   r%   rD   ·   s   	
"rD   c               @   s"   e Zd Zg dfdd„Zdd„ ZdS )ÚFakeResponseNc             C   s$   ddl }| d |¡¡| _|| _dS )zD
        headers: list of RFC822-style 'Key: value' strings
        r   NÚ
)ÚemailZmessage_from_stringÚjoinÚ_headersZ_url)r   ÚheadersÚurlrc   r$   r$   r%   Ú__init__  s    zFakeResponse.__init__c             C   s   | j S )N)re   )r   r$   r$   r%   Úinfo&  ó    zFakeResponse.info)rA   rB   rC   rh   ri   r$   r$   r$   r%   ra     s   ra   c             G   s   t | ||dƒS )NzSet-Cookie2)Ú	_interact)Ú	cookiejarrg   Úset_cookie_hdrsr$   r$   r%   Úinteract_2965(  s    rn   c             G   s   t | ||dƒS )Nz
Set-Cookie)rk   )rl   rg   rm   r$   r$   r%   Úinteract_netscape+  s    ro   c       	      C   s`   t j |¡}|  |¡ | dd¡}g }x|D ]}| d||f ¡ q,W t||ƒ}|  ||¡ |S )zDPerform a single request / response cycle, returning Cookie: header.r	   r6   z%s: %s)ÚurllibÚrequestÚRequestÚadd_cookie_headerÚ
get_headerÚappendra   Úextract_cookies)	rl   rg   rm   Zhdr_nameÚreqZ
cookie_hdrrf   rJ   r_   r$   r$   r%   rk   .  s    


rk   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚFileCookieJarTestsc             C   s¤   t jj}tƒ }t|ddƒ |  |jd d d jd ¡ z&|j|dd tƒ }|j	|dd W d yt
 |¡ W n tk
r€   Y nX X |  |jd d d jd ¡ d S )Nzhttp://www.acme.com/Zboozwww.acme.comú/T)Úignore_discard)r9   ÚsupportÚTESTFNr   ro   r   Ú_cookiesÚvalueÚsaveÚloadÚosÚunlinkÚOSError)r   ÚfilenamerS   r$   r$   r%   Útest_lwp_valueless_cookie<  s      z,FileCookieJarTests.test_lwp_valueless_cookiec             C   sä   t jj}xbttfD ]V}|ƒ }y|jdd W n0 tk
r\ } z|  |jt	¡ W d d }~X Y qX |  
d¡ qW zLt|dƒ8}| d¡ x&ttfD ]}|ƒ }|  t	|j|¡ qŽW W d Q R X W d yt |¡ W n tk
rÜ   Y nX X d S )NzAfor this test to work, a file with this filename should not exist)r„   z%expected OSError for invalid filenameÚwzoops
)r9   r{   r|   r   r   r€   rƒ   ZassertIsNotÚ	__class__r   ZfailÚopenÚwriteZassertRaisesr   r‚   )r   r„   Zcookiejar_classrS   Úexcr\   r$   r$   r%   Útest_bad_magicK  s$     
"  z!FileCookieJarTests.test_bad_magicN)rA   rB   rC   r…   r‹   r$   r$   r$   r%   rx   ;  s   rx   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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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAS )BÚCookieTestsc             C   sP   t ƒ }xDdD ]<\}}}tj |¡}| ||¡}|r>|  |¡ q|  |¡ qW d S )N))zhttp://foo.bar.com/zblah.comF)zhttp://foo.bar.com/zrhubarb.blah.comF)zhttp://foo.bar.com/zrhubarb.foo.bar.comF)zhttp://foo.bar.com/z.foo.bar.comT)zhttp://foo.bar.com/zfoo.bar.comT)zhttp://foo.bar.com/z.bar.comT)zhttp://foo.bar.com/zbar.comT)zhttp://foo.bar.com/ÚcomT)zhttp://foo.com/zrhubarb.foo.comF)zhttp://foo.com/z.foo.comT)zhttp://foo.com/zfoo.comT)zhttp://foo.com/r   T)zhttp://foo/zrhubarb.fooF)zhttp://foo/z.fooT)zhttp://foo/rE   T)zhttp://foo/z	foo.localT)zhttp://foo/z.localT)zhttp://barfoo.comz.foo.comF)zhttp://barfoo.comzfoo.comF)r   rp   rq   rr   Zdomain_return_okÚ
assertTrueÚassertFalse)r   Úpolrg   ÚdomainÚokrq   Úrr$   r$   r%   Útest_domain_return_ok”  s     z!CookieTests.test_domain_return_okc          	   C   s  t jj}t|ƒ}t|ddƒ t|ddƒ |jd d d }|  |j¡ |  |j	d¡ |jd d d }|  |j¡ |  |j	d¡ |  t
|ƒd¡ t|ƒ}|jd	d	d
 zt|ƒ}|jd	d	d
 W d t |j¡ X |  t|ƒt dd	 dd |¡¡ |  t|dƒd¡ d S )Nzhttp://www.acme.com/Úeggsz"spam"; path=/foo/zwww.acme.comry   z/foo/z"spam"zJ"spam"; path="/foo/"; domain="www.acme.com"; path_spec; discard; version=0T)Zignore_expiresrz   zpath_specified=%sFzhttp://www.acme.com/foo/z"spam"; eggs)r9   r{   r|   r   ro   r}   r8   r~   r   Únamer   Úreprr   Zrevertr   r‚   r„   ÚreÚsub)r   r„   rS   ÚcookieZold_strr$   r$   r%   Útest_missing_valueµ  s0    
zCookieTests.test_missing_valuec          	   C   s¬   x¦dD ]ž\}}}t ||d}t|ƒ}t|ddƒ y|jd d d }W n tk
rd   |  |¡ Y qX |  |j|¡ t|ddƒ |r|jd d d	 }|  |jd
¡ qW d S )N))NFr   )NTr,   )FFN)FTr,   )TFr   )TTr   )Úrfc2109_as_netscapeÚrfc2965zhttp://www.example.com/zni=ni; Version=1zwww.example.comry   Únizfoo=bar; Version=1rE   r,   )	r   r
   ro   r}   ÚKeyErrorr8   r   rH   rn   )r   rœ   r   rH   ÚpolicyrS   rš   Z
cookie2965r$   r$   r%   Útest_rfc2109_handlingÕ  s     
z!CookieTests.test_rfc2109_handlingc             C   sÆ  t ƒ }t|ddƒ t|ddƒ t|ddƒ t|ddƒ t|ddƒ t|ddƒ t|dd	ƒ t|dd
ƒ |jd d d }|  |jd¡ |  |j¡ |  |jt¡ |  	|j
¡ |  | d¡¡ |  	| d¡¡ |jd d d }|  |jd¡ |  	|j¡ |  |jd¡ |  |j
¡ |jd d d }|  |j¡ |  	|j
¡ |jd d d }|jd d d }|  |j¡ |  |j¡ |jd d d }|  |j¡ |  |jd¡ |jd d d
 }|  |j¡ d S )Nzhttp://www.acme.com/z0spam=eggs; DoMain=.acme.com; port; blArgh="feep"zni=ni; port=80,8080zhttp://www.acme.com:80/znini=nizfoo=bar; expires=z-spam=eggs; expires="Foo Bar 25 33:22:11 3022"z	fortytwo=z=unladenswallowZholyhandgrenadez	.acme.comry   rT   ZblArghZblarghzwww.acme.comrž   z80,8080ZninirE   Zfortytwor6   )r
   ro   r}   r   r‘   rŽ   Zdomain_specifiedZportr   r   Zport_specifiedZhas_nonstandard_attrr8   rG   ZassertIsNotNoner~   )r   rS   rš   rE   rT   r$   r$   r%   Útest_ns_parserô  sD    zCookieTests.test_ns_parserc             C   sH   t ƒ }t|ddƒ t|ddƒ |jd d }|  d|¡ |  d|¡ d S )Nzhttp://www.acme.com/zexpires=eggszversion=eggs; spam=eggszwww.acme.comry   rG   rH   )r
   ro   r}   ÚassertIn)r   rS   Úcookiesr$   r$   r%   Útest_ns_parser_special_names$  s    z(CookieTests.test_ns_parser_special_namesc             C   sj  t ƒ }tt ¡ d ƒ}t|dd| ƒ |  t|ƒd¡ tt ¡ d ƒ}t|dd| ƒ t|dƒ}|  t|ƒd¡ |  d|¡ |  d|¡ t|dd| ƒ t|dd	| ƒ |  t|ƒd
¡ t|dd| ƒ t|dd| ƒ t|dƒ}|  t|ƒd¡ t|ddƒ |  t|ƒd¡ | ¡  |  t|ƒd¡ |  d|¡ t	dddd dddddddddd d i ƒ}|  |j
d¡ d S )Ni  zhttp://www.acme.com/zspam="bar"; expires=%sr,   zfoo="eggs"; expires=%sz
spam="bar"rE   zeggs="bar"; expires=%szbar="bar"; expires=%sr   z!eggs="bar"; expires=%s; max-age=0z bar="bar"; max-age=0; expires=%szhttp://www.rhubarb.net/zwhum="fizz"r   r   r–   r~   Fzwww.python.orgTry   z1444312383.018307i?uV)r
   r   r)   ro   r   Úlenr£   ÚassertNotInÚclear_session_cookiesr	   rG   )r   rS   ÚfutureZnowÚhrš   r$   r$   r%   Útest_expires/  sF    

zCookieTests.test_expiresc             C   s6  t dd}t|ƒ}t|ddƒ |  d|jd ¡ t|ƒ}t|ddƒ |  d|jd ¡ t|ƒ}t|d	dƒ |  d
|jd ¡ t|ƒ}t|ddƒ |  d|jd ¡ tƒ }t|ddƒ |  d|jd ¡ tƒ }t|ddƒ |  d|jd ¡ tƒ }t|d	dƒ |  d|jd ¡ tƒ }t|ddƒ |  d|jd ¡ d S )NT)r   zhttp://www.acme.com/zspam="bar"; Version="1"ry   zwww.acme.comzhttp://www.acme.com/blahzeggs="bar"; Version="1"z http://www.acme.com/blah/rhubarbz/blah/z!http://www.acme.com/blah/rhubarb/z/blah/rhubarb/z
spam="bar"z
eggs="bar"z/blahz/blah/rhubarb)r   r
   rn   r£   r}   ro   )r   r   rS   r$   r$   r%   Útest_default_path_  s6    
zCookieTests.test_default_pathc             C   sB   t ƒ }d}d}t|||ƒ |  d|jd ¡ |  t||ƒ|¡ d S )Nzhttp://example.com/?spam/eggsz
eggs="bar"ry   zexample.com)r
   ro   r£   r}   r   )r   ÚcjZurir~   r$   r$   r%   Útest_default_path_with_query‡  s    z(CookieTests.test_default_path_with_queryc             C   sB   ddddddddd	d
ddg}x |D ]\}}|   t|ƒ|¡ q"W d S )N)z/foo%2f/barz/foo%2F/bar)z/foo%2F/barz/foo%2F/bar)z
/foo%%/barz
/foo%%/bar)z/fo%19o/barz/fo%19o/bar)z/fo%7do/barz/fo%7Do/bar)z	/foo/bar&z	/foo/bar&)z	/foo//barz	/foo//bar)z	~/foo/barz	~/foo/bar)z	/foo/barz/foo%19/bar)z	/}foo/barz/%7Dfoo/bar)u
   /foo/barÃ¼z/foo/bar%C3%BC)u   /foo/barê¯z/foo/bar%EA%AF%8D)r   r   )r   ZcasesrZ   r2   r$   r$   r%   Útest_escape_path‘  s    zCookieTests.test_escape_pathc             C   sX   t j d¡}|  t|ƒd¡ t j d¡}|  t|ƒd¡ t j d¡}|  t|ƒd¡ d S )NzThttp://www.example.com/rheum/rhaponticum;foo=bar;sing=song?apples=pears&spam=eggs#niz$/rheum/rhaponticum;foo=bar;sing=songzBhttp://www.example.com/rheum/rhaponticum?apples=pears&spam=eggs#niz/rheum/rhaponticumzhttp://www.example.comry   )rp   rq   rr   r   r   )r   rw   r$   r$   r%   Útest_request_pathª  s    
zCookieTests.test_request_pathc             C   sÄ   t ƒ }t dd}t|ƒ}d}t||dƒ |jd d d }x„dD ]|\}}|› |› }tj |¡}	t||ƒ}
|r–|  d	|
d
|› ¡ |  | 	||	¡¡ q@|  
d	|
d|› ¡ |  | 	||	¡¡ q@W d S )NT)Zstrict_ns_set_pathzhttp://bar.comzspam=eggs; Path=/foozbar.comz/foorT   ))z/fooT)z/foo/T)z/foo/barT)ry   F)z/foobad/fooFz	spam=eggszcookie not set for zcookie set for )r   r
   ro   r}   rp   rq   rr   r£   rŽ   Zset_ok_pathr§   r   )r   r   Zstrict_ns_path_polrS   Zbase_urlrš   Úpathr’   rg   rw   rª   r$   r$   r%   Útest_path_prefix_matchº  s    

z"CookieTests.test_path_prefix_matchc             C   sL   t jjdddid}|  t|ƒd¡ t jjdddid}|  t|ƒt¡ d S )Nzhttp://www.acme.com:1234/ÚHostzwww.acme.com:4321)rf   Z1234zhttp://www.acme.com/)rp   rq   rr   r   r   r   )r   rw   r$   r$   r%   Útest_request_portÒ  s    zCookieTests.test_request_portc             C   sp   t jjdddid}|  t|ƒd¡ t jjdddid}|  t|ƒd¡ t jjd	dd
id}|  t|ƒd¡ d S )Nzhttp://1.1.1.1/r³   zwww.acme.com:80)rf   z1.1.1.1zhttp://www.acme.com/zirrelevant.comzwww.acme.comz&http://www.acme.com:2345/resource.htmlzwww.acme.com:5432)rp   rq   rr   r   r   )r   rw   r$   r$   r%   Útest_request_hostÚ  s    zCookieTests.test_request_hostc             C   st   |   tdƒ¡ |   tdƒ¡ |  tdƒ¡ |  tdƒ¡ |  tdƒ¡ |  tdƒ¡ |  tdƒ¡ |  tdƒ¡ d S )	Nzfoo.bar.comz1foo2.3bar4.5comz192.168.1.1r6   Ú.z.foo.bar.comz..foozfoo.)rŽ   r   r   )r   r$   r$   r%   Útest_is_HDNê  s    zCookieTests.test_is_HDNc             C   s„   |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ |   tdƒd¡ |   td	ƒd	¡ d S )
Nzwww.acme.comz	.acme.comzacme.comz
acme.localz.localz.comr¶   r6   z192.168.0.1)r   r   )r   r$   r$   r%   Ú
test_reachô  s    zCookieTests.test_reachc             C   sô  |   tddƒ¡ |  tddƒ¡ |   tddƒ¡ |   tddƒ¡ |  tddƒ¡ |   tddƒ¡ |  tddƒ¡ |   td	d
ƒ¡ |  tddƒ¡ |  tddƒ¡ |   tddƒ¡ |   tddƒ¡ |  tddƒ¡ |   tddƒ¡ |   tddƒ¡ |   tddƒ¡ |   tddƒ¡ |  tddƒ¡ |   tddƒ¡ |  tddƒ¡ |   tddƒ¡ |   tddƒ¡ |  tddƒ¡ |  tddƒ¡ |  tddƒ¡ |  tddƒ¡ |  tddƒ¡ |   tddƒ¡ |  tddƒ¡ |  tddƒ¡ |  tddƒ¡ d S )Nz192.168.1.1z.168.1.1zx.y.comzx.Y.comz.Y.comzY.comz	a.b.c.comz.c.comzexample.localz.localz	blah.blahr6   z.rhubarb.rhubarbzacme.comz	.acme.comzrhubarb.acme.comzwww.rhubarb.acme.comzy.comz.y.comz.comr   Úmz.mr¶   )rŽ   r   r   r   )r   r$   r$   r%   Útest_domain_matchþ  s>    zCookieTests.test_domain_matchc             C   s&   t ƒ }t|ddƒ |  t|ƒd¡ d S )Nzhttp://www.nasty.com/z)foo=bar; domain=friendly.org; Version="1"r   )r
   rn   r   r¦   )r   rS   r$   r$   r%   Útest_wrong_domain"  s    zCookieTests.test_wrong_domainc             C   sn   t dd}t|d}t|ddƒ t|ddƒ |  t|ƒd¡ x,dD ]$}t|d	| d
ƒ |  t|ƒd¡ qBW d S )NT)Zstrict_domain)r    zhttp://example.co.uk/zno=problemoz!okey=dokey; Domain=.example.co.ukr   )z.co.ukz.org.zaz.tx.usz.name.uszhttp://example.%s/zspam=eggs; Domain=.co.uk)r   r
   ro   r   r¦   )r   Zcpr­   Z
pseudo_tldr$   r$   r%   Útest_strict_domain,  s    



zCookieTests.test_strict_domainc             C   s8  t ƒ }t|ddƒ |  t|ƒd¡ |  |jd d d jd¡ |  t|dƒd¡ |  t|dƒd¡ ttjd	}| |¡ |  t|dƒd
¡ t|ddƒ t|ddƒ |  t|ƒd¡ |  |jd d d jd¡ |  |jd d d jd¡ |  t|dƒd¡ t|ddƒ |  t|ƒd¡ t|ddƒ |  t|ƒd¡ d S )Nzhttp://foo.net/zns=barr,   zfoo.netry   ÚnsrF   zhttp://www.foo.net/)Zstrict_ns_domainr6   zhttp://foo.net/foo/zspam1=eggs; domain=foo.netzhttp://foo.net/foo/bar/zspam2=eggs; domain=.foo.netr   z.foo.netz/fooZspam1r•   z/foo/barZspam2zspam2=eggs; spam1=eggs; ns=barznini="ni"; domain=.netzhttp://foo.co.ukznasty=trick; domain=.co.uké   )	r
   ro   r   r¦   r}   r~   r   ZDomainStrictNonDomainÚ
set_policy)r   rS   r   r$   r$   r%   Útest_two_component_domain_ns:  s4    

z(CookieTests.test_two_component_domain_nsc             C   s4  t dd}t|ƒ}t|ddƒ |  t|ƒd¡ |  |jd d d jd	¡ |  t|dƒd
¡ |  t|dƒd¡ t|ddƒ |  t|ƒd¡ |  t|dƒd
¡ t|ddƒ |  |jd d d jd¡ |  t|ƒd¡ |  t|dƒd
¡ |  t|dƒd¡ t|ddƒ |  t|ƒd¡ t|ddƒ |  t|ƒd¡ d S )NT)r   zhttp://foo.net/zfoo=bar; Version="1"r,   zfoo.netry   rE   rF   z$Version=1; foo=barzhttp://www.foo.net/r6   zhttp://foo.net/fooz1spam=eggs; domain=foo.net; path=/foo; Version="1"zhttp://www.foo.net/foo/z&spam=eggs; domain=foo.net; Version="1"z.foo.netz/foo/rT   r•   r   zhttp://foo.net/foo/z($Version=1; spam=eggs; $Domain="foo.net"z#ni="ni"; domain=".net"; Version="1"zhttp://foo.co.uk/z'nasty=trick; domain=.co.uk; Version="1"r   )r   r
   rn   r   r¦   r}   r~   )r   r   rS   r$   r$   r%   Ú!test_two_component_domain_rfc2965j  s8    
z-CookieTests.test_two_component_domain_rfc2965c             C   s  t tdgdgdd}tj d¡}dg}t|dƒ}| ||¡ |  t|ƒd¡ tj d¡}t|dƒ}| ||¡ |  t|ƒd	¡ tj d
¡}t|d
ƒ}| ||¡ |  t|ƒd	¡ tj d
¡}t|d
ƒ}| 	||¡}| 
|d ¡ |  t|ƒd¡ | |¡ |  | d¡¡ d S )Nzacme.comzwww.acme.com)Úblocked_domainsZallowed_domains)r    zhttp://acme.com/z*Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/r   zhttp://www.acme.com/r,   zhttp://www.coyote.com/r   r	   )r
   r   rp   rq   rr   ra   rv   r   r¦   Úmake_cookiesÚ
set_cookiers   r   Ú
has_header)r   rS   rw   rf   r_   r¤   r$   r$   r%   Útest_domain_allow–  s.    




zCookieTests.test_domain_allowc             C   s€  t ddgd}t|d}dg}tj d¡}t|dƒ}| ||¡ |  t|ƒd¡ | 	dg¡}| ||¡ |  t|ƒd	¡ | 
¡  tj d
¡}t|d
ƒ}| ||¡ |  t|ƒd	¡ tj d
¡}| |¡ |  | d¡¡ |  | d¡¡ | 
¡  | 	dg¡ | ||¡ |  t|ƒd	¡ tj d¡}t|dƒ}| ||¡}| |d ¡ |  t|ƒd¡ | |¡ |  | d¡¡ | 
¡  | 	g ¡ tj d¡}t|dƒ}| ||¡}| ||¡ |  t|ƒd	¡ tj d¡}| |¡ |  | d¡¡ tj d¡}| |¡ |  | |d |¡¡ |  | d¡¡ | 	dg¡}tj d¡}| |¡ |  | d¡¡ tj d¡}| |¡ |  | d¡¡ d S )NTz	.acme.com)r   rÂ   )r    z*Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/zhttp://www.acme.com/r   zacme.comr,   zhttp://www.roadrunner.net/r	   ÚCookie2r   zhttp://acme.com/zhttp://badacme.com/)r   r
   rp   rq   rr   ra   rv   r   r¦   Zset_blocked_domainsÚclearrs   rŽ   rÅ   rÃ   rÄ   r   Z	return_ok)r   r   rS   rf   rw   r_   Úpr¤   r$   r$   r%   Útest_domain_blockµ  sh    











zCookieTests.test_domain_blockc             C   s¾   x¸dD ]°}xªdD ]¢}t ƒ }|r2tdd}t}d}ntdd}t}d}| |¡ d}|||d	||f ƒ |||d
||f ƒ |  |jd d d jd¡ |  |jd d d jd¡ qW qW d S )N)TF)ú r6   F)r   r6   Tz; Version=1zhttp://www.acme.com/zfoo1=bar%s%szfoo2=bar%s; secure%szwww.acme.comry   Zfoo1z#non-secure cookie registered secureZfoo2z#secure cookie registered non-secure)	r
   r   ro   rn   r¿   r   r}   ZsecurerŽ   )r   r½   Z
whitespacerS   r   ÚintZvsrg   r$   r$   r%   Útest_secureø  s(    




zCookieTests.test_securec             C   s6   t tddd}t|ddƒ t|dƒ}|  |d¡ d S )NT)r   )r    zhttp://www.acme.com/zfoo=\b"a"r; Version=1z$Version=1; foo=\\b\"a\"r)r
   r   rn   r   )r   rS   rª   r$   r$   r%   Útest_quote_cookie_value  s    
z#CookieTests.test_quote_cookie_valuec             C   sX   d}t tddƒ}t||dƒ tj |¡}|  t|ƒd¡ | |¡ |  	| 
d¡¡ d S )Nzhttp://www.acme.comT)r   zfoo=bar; Version=1r,   r	   )r
   r   rn   rp   rq   rr   r   r¦   rs   rŽ   rÅ   )r   rg   rS   rw   r$   r$   r%   Útest_missing_final_slash  s    
z$CookieTests.test_missing_final_slashc             C   sž   t dd}t|ƒ}d}t||dƒ t||ƒ}|  d|d¡ t|ƒ}d}t||dƒ t||ƒ}|  d|d	¡ t|ƒ}d}t||d
ƒ t||ƒ}|  d|d	¡ d S )NT)r   zhttp://foo.bar.com/zspam=eggs; Version=1ZDomainz*absent domain returned with domain presentz%spam=eggs; Version=1; Domain=.bar.comz$Domain=".bar.com"zdomain not returnedz$spam=eggs; Version=1; Domain=bar.comz$Domain="bar.com")r   r
   rn   r§   r£   )r   r   rS   rg   rª   r$   r$   r%   Útest_domain_mirror  s"    



zCookieTests.test_domain_mirrorc             C   sn   t dd}t|ƒ}d}t||dƒ t||ƒ}|  d|d¡ t|ƒ}d}t||dƒ t||ƒ}|  d|d	¡ d S )
NT)r   zhttp://foo.bar.com/zspam=eggs; Version=1ZPathz&absent path returned with path presentzspam=eggs; Version=1; Path=/z	$Path="/"zpath not returned)r   r
   rn   r§   r£   )r   r   rS   rg   rª   r$   r$   r%   Útest_path_mirror6  s    


zCookieTests.test_path_mirrorc             C   sÎ   t dd}t|ƒ}d}t||dƒ t||ƒ}|  d|d¡ t|ƒ}d}t||dƒ t||ƒ}|  |dd	¡ t|ƒ}d}t||d
ƒ t||ƒ}|  d|d¡ t|ƒ}d}t||dƒ t||ƒ}|  d|d¡ d S )NT)r   zhttp://foo.bar.com/zspam=eggs; Version=1ZPortz&absent port returned with port presentzspam=eggs; Version=1; Portz\$Port([^=]|$)z-port with no value not returned with no valuezspam=eggs; Version=1; Port="80"z
$Port="80"z5port with single value not returned with single valuez$spam=eggs; Version=1; Port="80,8080"z$Port="80,8080"z;port with multiple values not returned with multiple values)r   r
   rn   r§   r   r£   )r   r   rS   rg   rª   r$   r$   r%   Útest_port_mirrorE  s0    




zCookieTests.test_port_mirrorc             C   s:   t tddƒ}d}t||dƒ t||ƒ}|  d|d¡ d S )NT)r   zhttp://foo.bar.com/zfspam=eggs; Version=1; Comment="does anybody read these?"; CommentURL="http://foo.bar.net/comment.html"ÚCommentz:Comment or CommentURL cookie-attributes returned to server)r
   r   rn   r§   )r   rS   rg   rª   r$   r$   r%   Útest_no_return_commentd  s    
z"CookieTests.test_no_return_commentc             C   s  t tddƒ}t|ddƒ t|ddƒ t|ddƒ t|ddƒ t|d	d
ƒ dddddg}dddddg}dddddg}dddddg}x~tdƒD ]r}d}xh|D ]`}|  |t¡ |  |j|| ¡ |  |j	|| ¡ |  |j
|| ¡ |  |j|| ¡ |d }qšW qŒW d S )NT)r   zhttp://blah.spam.org/zefoo=eggs; Version=1; Comment="does anybody read these?"; CommentURL="http://foo.bar.net/comment.html"zhttp://www.acme.com/blah/zspam=bar; securezfoo=bar; secure; Version=1zfoo=bar; path=/; Version=1zhttp://www.sol.nozsbang=wallop; version=1; domain=".sol.no"; port="90,100, 80,8080"; max-age=100; Comment = "Just kidding! (\"|\\\\) "r,   r   ZbangrE   rT   z.sol.nozblah.spam.orgzwww.acme.comry   z/blahz/blah/r¾   )r
   r   rn   ro   ÚrangeZassertIsInstancer	   r   rH   r–   r‘   r±   )r   ÚcsZversionsÚnamesZdomainsÚpathsÚirS   r$   r$   r%   Útest_Cookie_iteratorn  s.    
z CookieTests.test_Cookie_iteratorc             C   s–   |   tdgƒddddgg¡ |   tdgƒdddgg¡ |   tdgƒd	dgg¡ |   td
gƒdddgg¡ |   tdgƒddgg¡ |   tdgƒg ¡ d S )Nzfoo=bar; path=/; domain)rE   rF   )r±   ry   )r‘   N)rH   rI   z)foo=bar; expires=Foo Bar 12 33:22:11 2000)rG   NrE   )rE   Nzfoo=bar; expireszfoo=bar; version)rH   Nr6   )r   r   )r   r$   r$   r%   rK   Ž  s"    z!CookieTests.test_parse_ns_headersc             C   sŠ   dd„ }t t ¡ d ƒ}x@dgdgdgdgdgd	| ggD ]}||ƒ}|  t|ƒd
¡ q:W dg}||ƒ}|jd d d }|  |j¡ d S )Nc             S   s,   t ƒ }tj d¡}t| dƒ}| ||¡ |S )Nzhttp://www.example.com/)r
   rp   rq   rr   ra   rv   )rf   rS   rw   r“   r$   r$   r%   Úcookiejar_from_cookie_headers«  s
    
zICookieTests.test_bad_cookie_header.<locals>.cookiejar_from_cookie_headersi  zSet-Cookie: zSet-Cookie2: z-Set-Cookie2: a=foo; path=/; Version=1; domainzSet-Cookie: b=foo; max-age=oopszSet-Cookie: b=foo; version=spamzSet-Cookie:; Expires=%sr   z3Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000zwww.example.comry   rS   )r   r)   r   r¦   r}   r8   rG   )r   rÛ   r©   rf   rS   rš   r$   r$   r%   Útest_bad_cookie_header©  s    z"CookieTests.test_bad_cookie_headerN)#rA   rB   rC   r”   r›   r¡   r¢   r¥   r«   r¬   r®   r¯   r°   r²   r´   rµ   r·   r¸   rº   r»   r¼   rÀ   rÁ   rÆ   rÊ   rÍ   rÎ   rÏ   rÐ   rÑ   rÒ   rÔ   rÚ   rK   rÜ   r$   r$   r$   r%   rŒ   e  s@   /! 00(


$
0,C

 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 )ÚLWPCookieTestsc             C   sœ  t  ¡ d d }g }ttddƒ}tjjdddid}| d	| ¡ t|d
ƒ}| 	||¡ tj d
¡}| 
|¡ |  | d¡d¡ |  | d¡d¡ | d¡ t|d
ƒ}| 	||¡ tj d¡}| 
|¡ | d¡}|  d|¡ |  d|¡ | d¡ t|dƒ}| 	||¡ tj d
¡}| 
|¡ | d¡}|  d|¡ |  d|¡ |  d|¡ tj d¡}| 
|¡ | d¡}|  d|¡ |  d|¡ |  | d¡¡ d S )Nr   r,   T)r   zhttp://www.acme.com:80/r³   zwww.acme.com:80)rf   zVSet-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; expires=Wednesday, 09-Nov-%d 23:12:40 GMTzhttp://www.acme.com/r	   zCUSTOMER=WILE_E_COYOTErÇ   z$Version="1"z4Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/zhttp://www.acme.com/foo/barz PART_NUMBER=ROCKET_LAUNCHER_0001z%Set-Cookie: SHIPPING=FEDEX; path=/foozhttp://www.acme.comzSHIPPING=FEDEXzhttp://www.acme.com/foo/zSHIPPING=FEDEX;)r)   Ú	localtimer
   r   rp   rq   rr   ru   ra   rv   rs   r   rt   r£   r§   rŽ   Ú
startswith)r   Úyear_plus_onerf   rS   rw   r_   rª   r$   r$   r%   Útest_netscape_example_1Î  sJ    %











z&LWPCookieTests.test_netscape_example_1c             C   sª   t ƒ }g }tj d¡}| d¡ t|dƒ}| ||¡ tj d¡}| |¡ |  | 	d¡d¡ | d¡ t|dƒ}| ||¡ tj d¡}| |¡ |  
| 	d¡d¡ d S )Nzhttp://www.acme.com/z4Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/r	   z PART_NUMBER=ROCKET_LAUNCHER_0001z6Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammozhttp://www.acme.com/ammozBPART_NUMBER=RIDING_ROCKET_0023;\s*PART_NUMBER=ROCKET_LAUNCHER_0001)r
   rp   rq   rr   ru   ra   rv   rs   r   rt   r   )r   rS   rf   rw   r_   r$   r$   r%   Útest_netscape_example_2)  s$    




z&LWPCookieTests.test_netscape_example_2c             C   s’   t tddƒ}t|ddƒ}|  |¡ t|ddƒ}|  |d¡ t|dd	ƒ}|  |d
¡ |  |d¡ |  |d¡ t|dƒ}|  |d¡ |  d|¡ d S )NT)r   zhttp://www.acme.com/acme/loginz3Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"z!http://www.acme.com/acme/pickitemz=Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"z=^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$z!http://www.acme.com/acme/shippingz+Shipping="FedEx"; Version="1"; Path="/acme"z^\$Version="?1"?;z7Part_Number="?Rocket_Launcher_0001"?;\s*\$Path="\/acme"z-Customer="?WILE_E_COYOTE"?;\s*\$Path="\/acme"z http://www.acme.com/acme/processz%Shipping="?FedEx"?;\s*\$Path="\/acme"ZWILE_E_COYOTE)r
   r   rn   r   r   r£   )r   rS   rš   r$   r$   r%   Útest_ietf_example_1\  s"    

z"LWPCookieTests.test_ietf_example_1c             C   sX   t tddƒ}t|dddƒ t|dƒ}|  |d¡ t|dƒ}|  d	|¡ |  d
|¡ d S )NT)r   z&http://www.acme.com/acme/ammo/specificz=Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"z@Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"z!http://www.acme.com/acme/ammo/...z(Riding_Rocket_0023.*Rocket_Launcher_0001zhttp://www.acme.com/acme/parts/ZRocket_Launcher_0001ZRiding_Rocket_0023)r
   r   rn   r   r£   r§   )r   rS   rš   r$   r$   r%   Útest_ietf_example_2Ì  s    

z"LWPCookieTests.test_ietf_example_2c             C   s¾  t dd}t|d}d}t|ddƒ}|  |¡ t|ddƒ}|  t|ƒd¡ t|d	d
ƒ}|  t|ƒd¡ t|d	dƒ}|  t|ƒd¡ t|ddƒ}|  t|ƒd¡ t|ddƒ}|  t|ƒd¡ t|ddƒ}|  t|ƒd¡ t|ddƒ}|  t|ƒd¡ t|ddƒ}|  t|ƒd¡ t|ddƒ}|  t|ƒd¡ t|ddƒ}|  t|ƒd¡ tjj}z2|j	|dd t
|ƒ}t|d}|j|dd W d yt |¡ W n tk
r¦   Y nX X |  |t
|ƒ¡ d S )NT)r   )r    zmax-age=3600zhttp://www.acme.comz!foo=bar; domain=".com"; version=1z'ping=pong; domain="acme.com"; version=1r,   zhttp://www.a.acme.comz'whiz=bang; domain="acme.com"; version=1z,wow=flutter; domain=".a.acme.com"; version=1r   zhttp://125.125.125.125z*zzzz=ping; domain="125.125.125"; version=1zhttp://www.sol.noz6blah=rhubarb; domain=".sol.no"; path="/foo"; version=1zhttp://www.sol.no/foo/barz3bing=bong; domain=".sol.no"; path="/foo"; version=1r   z5whiz=ffft; domain=".sol.no"; port="90,100"; version=1zsbang=wallop; version=1; domain=".sol.no"; port="90,100, 80,8080"; max-age=100; Comment = "Just kidding! (\"|\\\\) "r¾   z9foo9=bar; version=1; domain=".sol.no"; port; max-age=100;é   zhttp://www.sol.no/<oo/z"foo8=bar; version=1; path="/%3coo"r(   )rz   )r   r   rn   r   r   r¦   r9   r{   r|   r   r—   r€   r   r‚   rƒ   )r   r   rS   Zmax_agerš   r„   Úoldr$   r$   r%   Útest_rejection  s`    


	
  zLWPCookieTests.test_rejectionc             C   sn   t tddƒ}t|ddƒ t|ddƒ}t dtj¡}|  d|¡ |  ||¡ t|d	ƒ}|  |¡ t|d
ƒ}d S )NT)r   z7http://www.acme.com/foo%2f%25/%3c%3c%0Anew%C3%A5/%C3%A5zfoo  =   bar; version    =   1u/   http://www.acme.com/foo%2f%25/<<%0anewÃ¥/Ã¦Ã¸Ã¥z bar=baz; path="/foo/"; version=1z^\$version=\"?1\"?zfoo=baru-   http://www.acme.com/foo/%25/<<%0anewÃ¥/Ã¦Ã¸Ã¥u   http://www.acme.com/Ã¼)	r
   r   rn   r˜   ÚcompileÚIr£   r   r   )r   rS   rš   Z
version_rer$   r$   r%   Útest_url_encoding\  s    
z LWPCookieTests.test_url_encodingc                sì   t  ¡ d d }tjj‰ tˆ tddd}t|ddƒ t|ddƒ t|dd	ƒ d
|f }t|dd| ƒ t|dd| ƒ t|dd| ƒ ‡ fdd„}||dƒ}|  	t
|ƒd¡ |  dt|ƒ¡ ||dƒ}|  	t
|ƒd¡ |  dt|ƒ¡ d S )Nr   r,   T)r   )r    zhttp://www.acme.com/z foo1=bar; max-age=100; Version=1z4foo2=bar; port="80"; max-age=100; Discard; Version=1zfoo3=bar; secure; Version=1zexpires=09-Nov-%d 23:12:40 GMTzhttp://www.foo.com/zfooa=bar; %szfoob=bar; Domain=.foo.com; %sz fooc=bar; Domain=www.foo.com; %sc                sX   z,| j |d tˆ tddƒ}|j|d W d yt ˆ ¡ W n tk
rP   Y nX X |S )N)rz   T)r   )r   r   r   r€   r   r‚   rƒ   )r­   rz   Únew_c)r„   r$   r%   Úsave_and_restoreˆ  s      z5LWPCookieTests.test_mozilla.<locals>.save_and_restorer(   zname='foo1', value='bar'Fr¾   )r)   rÞ   r9   r{   r|   r   r   rn   ro   r   r¦   r£   r—   )r   rà   rS   rG   rì   rë   r$   )r„   r%   Útest_mozillar  s.    





zLWPCookieTests.test_mozillac             C   s”   t ƒ }g }tj d¡}| d¡ t|dƒ}| ||¡ | d¡ t|dƒ}| ||¡ tj d¡}| |¡ |  d| 	d¡¡ |  d| 	d¡¡ d S )Nzhttp://foo.bar.acme.com/fooz4Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.comzhttp://www.acme.com/fooz4Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.comzPART_NUMBER=3,4r	   zCustomer=WILE_E_COYOTE)
r
   rp   rq   rr   ru   ra   rv   rs   r£   rt   )r   rS   rf   rw   r_   r$   r$   r%   Útest_netscape_misc›  s    




z!LWPCookieTests.test_netscape_miscc             C   sh   t tddƒ}t|ddƒ t|ddƒ}|  d|¡ t|ddƒ t|dƒ}|  d|¡ |  t|ƒd	¡ d S )
NT)r   zhttp://example/z#foo1=bar; PORT; Discard; Version=1;z$foo2=bar; domain=".local"; Version=1zfoo1=barzfoo3=bar; Version=1zfoo2=barr   )r
   r   rn   r£   r   r¦   )r   rS   rš   r$   r$   r%   Útest_intranet_domains_2965²  s    
z)LWPCookieTests.test_intranet_domains_2965c             C   sl   t tddƒ}t|ddƒ t|ddƒ}|  t|ƒd¡ |  d|¡ t|dƒ}|  d|¡ |  t|ƒd¡ d S )NF)r   zhttp://example/zfoo1=barzfoo2=bar; domain=.localr   zfoo2=bar)r
   r   ro   r   r¦   r£   )r   rS   rš   r$   r$   r%   Útest_intranet_domains_nsÀ  s    
z'LWPCookieTests.test_intranet_domains_nsc             C   s¶   t tddƒ}g }tj d¡}| d¡ t|dƒ}| ||¡ tj d¡}| |¡ |  	| 
d¡d¡ |  	| 
d¡d¡ tj d	¡}| |¡ |  	| 
d¡d¡ |  	| 
d¡d¡ d S )
NT)r   zhttp://www.ants.com/z,Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=r	   zJSESSIONID=ABCDERANDOM123rÇ   z$Version="1"zhttp://www.ants.com:8080)r
   r   rp   rq   rr   ru   ra   rv   rs   r   rt   )r   rS   rf   rw   r_   r$   r$   r%   Útest_empty_pathÌ  s     



zLWPCookieTests.test_empty_pathc       	      C   s  t  ¡ d d }tj d¡}g }| d¡ | d| ¡ | d| ¡ | d¡ | d¡ t|dƒ}tƒ }| ||¡ ddddd	œ}x&|D ]}d
|j	 }|| d ||< qŠW | 
¡  x&|D ]}d|j	 }|| d ||< qºW |  |d |d ¡ |  |d d¡ |  |d d¡ d S )Nr   r,   z"http://www.perlmeister.com/scriptsz$Set-Cookie: s1=session;Path=/scriptszWSet-Cookie: p1=perm; Domain=.perlmeister.com;Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMTz>Set-Cookie: p2=perm;Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMTz<Set-Cookie: s2=session;Path=/scripts;Domain=.perlmeister.comz2Set-Cookie2: s3=session;Version=1;Discard;Path="/")Úsession_afterÚ
perm_afterÚsession_beforeÚperm_beforez	%s_beforez%s_afterró   rõ   rò   rô   )r)   rÞ   rp   rq   rr   ru   ra   r
   rv   r~   r¨   r   ZassertNotEqual)	r   rà   rw   rf   r_   rS   Zcounterrš   Úkeyr$   r$   r%   Útest_session_cookiesë  s6    







z#LWPCookieTests.test_session_cookiesN)rA   rB   rC   rá   râ   rã   rä   rç   rê   rí   rî   rï   rð   rñ   r÷   r$   r$   r$   r%   rÝ   Ë  s   [3p5[)rÝ   c             C   s   t j ttttt¡ d S )N)r9   r{   Zrun_unittestr   rD   rŒ   rx   rÝ   )Úverboser$   r$   r%   Ú	test_main  s    rù   Ú__main__T)rø   )N)-Ú__doc__r   r˜   Ztest.supportr9   r)   ZunittestZurllib.requestrp   Zhttp.cookiejarr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZTestCaser   rD   ra   rn   ro   rk   rx   rŒ   rÝ   rù   rA   r$   r$   r$   r%   Ú<module>   s<   d &g
*      l    O
	
