blob: a35ec95a69865c817fdd44fd5a5657d5187dc8f4 [file] [log] [blame]
Georg Brandl24420152008-05-26 16:32:26 +00001"""Tests for http/cookiejar.py."""
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00002
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00003import os
4import re
5import test.support
6import time
7import unittest
8import urllib.request
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00009
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +000010from http.cookiejar import (time2isoz, http2time, time2netscape,
11 parse_ns_headers, join_header_words, split_header_words, Cookie,
12 CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar,
13 LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path,
14 reach, is_HDN, domain_match, user_domain_match, request_path,
15 request_port, request_host)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000016
Georg Brandl24420152008-05-26 16:32:26 +000017
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +000018class DateTimeTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000019
20 def test_time2isoz(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000021 base = 1019227000
22 day = 24*3600
Ezio Melottib3aedd42010-11-20 19:04:17 +000023 self.assertEqual(time2isoz(base), "2002-04-19 14:36:40Z")
24 self.assertEqual(time2isoz(base+day), "2002-04-20 14:36:40Z")
25 self.assertEqual(time2isoz(base+2*day), "2002-04-21 14:36:40Z")
26 self.assertEqual(time2isoz(base+3*day), "2002-04-22 14:36:40Z")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000027
28 az = time2isoz()
29 bz = time2isoz(500000)
30 for text in (az, bz):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000031 self.assertTrue(re.search(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$", text),
Ezio Melottib3aedd42010-11-20 19:04:17 +000032 "bad time2isoz format: %s %s" % (az, bz))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000033
34 def test_http2time(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000035 def parse_date(text):
36 return time.gmtime(http2time(text))[:6]
37
Ezio Melottib3aedd42010-11-20 19:04:17 +000038 self.assertEqual(parse_date("01 Jan 2001"), (2001, 1, 1, 0, 0, 0.0))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000039
40 # this test will break around year 2070
Ezio Melottib3aedd42010-11-20 19:04:17 +000041 self.assertEqual(parse_date("03-Feb-20"), (2020, 2, 3, 0, 0, 0.0))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000042
43 # this test will break around year 2048
Ezio Melottib3aedd42010-11-20 19:04:17 +000044 self.assertEqual(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000045
46 def test_http2time_formats(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000047 # test http2time for supported dates. Test cases with 2 digit year
48 # will probably break in year 2044.
49 tests = [
50 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format
51 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format
52 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format
53
54 '03 Feb 1994 00:00:00 GMT', # HTTP format (no weekday)
55 '03-Feb-94 00:00:00 GMT', # old rfc850 (no weekday)
56 '03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday)
57 '03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds)
58 '03-Feb-1994 00:00', # broken rfc850 (no weekday, no seconds, no tz)
59
60 '03-Feb-94', # old rfc850 HTTP format (no weekday, no time)
61 '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)
62 '03 Feb 1994', # proposed new HTTP format (no weekday, no time)
63
64 # A few tests with extra space at various places
65 ' 03 Feb 1994 0:00 ',
66 ' 03-Feb-1994 ',
67 ]
68
69 test_t = 760233600 # assume broken POSIX counting of seconds
70 result = time2isoz(test_t)
71 expected = "1994-02-03 00:00:00Z"
Ezio Melottib3aedd42010-11-20 19:04:17 +000072 self.assertEqual(result, expected,
73 "%s => '%s' (%s)" % (test_t, result, expected))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000074
75 for s in tests:
76 t = http2time(s)
77 t2 = http2time(s.lower())
78 t3 = http2time(s.upper())
79
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000080 self.assertTrue(t == t2 == t3 == test_t,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000081 "'%s' => %s, %s, %s (%s)" % (s, t, t2, t3, test_t))
82
83 def test_http2time_garbage(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000084 for test in [
85 '',
86 'Garbage',
87 'Mandag 16. September 1996',
88 '01-00-1980',
89 '01-13-1980',
90 '00-01-1980',
91 '32-01-1980',
92 '01-01-1980 25:00:00',
93 '01-01-1980 00:61:00',
94 '01-01-1980 00:00:62',
95 ]:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000096 self.assertTrue(http2time(test) is None,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000097 "http2time(%s) is not None\n"
98 "http2time(test) %s" % (test, http2time(test))
99 )
100
101
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000102class HeaderTests(unittest.TestCase):
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000103
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000104 def test_parse_ns_headers(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000105 # quotes should be stripped
Guido van Rossume2a383d2007-01-15 16:59:06 +0000106 expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]]
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000107 for hdr in [
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000108 'foo=bar; expires=01 Jan 2040 22:23:32 GMT',
109 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"',
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000110 ]:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000111 self.assertEqual(parse_ns_headers([hdr]), expected)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000112
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000113 def test_parse_ns_headers_version(self):
114
115 # quotes should be stripped
116 expected = [[('foo', 'bar'), ('version', '1')]]
117 for hdr in [
118 'foo=bar; version="1"',
119 'foo=bar; Version="1"',
120 ]:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000121 self.assertEqual(parse_ns_headers([hdr]), expected)
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000122
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000123 def test_parse_ns_headers_special_names(self):
124 # names such as 'expires' are not special in first name=value pair
125 # of Set-Cookie: header
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000126 # Cookie with name 'expires'
127 hdr = 'expires=01 Jan 2040 22:23:32 GMT'
128 expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000129 self.assertEqual(parse_ns_headers([hdr]), expected)
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000130
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000131 def test_join_header_words(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000132 joined = join_header_words([[("foo", None), ("bar", "baz")]])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000133 self.assertEqual(joined, "foo; bar=baz")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000134
Ezio Melottib3aedd42010-11-20 19:04:17 +0000135 self.assertEqual(join_header_words([[]]), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000136
137 def test_split_header_words(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000138 tests = [
139 ("foo", [[("foo", None)]]),
140 ("foo=bar", [[("foo", "bar")]]),
141 (" foo ", [[("foo", None)]]),
142 (" foo= ", [[("foo", "")]]),
143 (" foo=", [[("foo", "")]]),
144 (" foo= ; ", [[("foo", "")]]),
145 (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
146 ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
147 # doesn't really matter if this next fails, but it works ATM
148 ("foo= bar=baz", [[("foo", "bar=baz")]]),
149 ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
150 ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
151 ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
152 (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',
153 [[("foo", None), ("bar", "baz")],
154 [("spam", "")], [("foo", ',;"')], [("bar", "")]]),
155 ]
156
157 for arg, expect in tests:
158 try:
159 result = split_header_words([arg])
160 except:
Guido van Rossum34d19282007-08-09 01:03:29 +0000161 import traceback, io
162 f = io.StringIO()
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000163 traceback.print_exc(None, f)
164 result = "(error -- traceback follows)\n\n%s" % f.getvalue()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000165 self.assertEqual(result, expect, """
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000166When parsing: '%s'
167Expected: '%s'
168Got: '%s'
169""" % (arg, expect, result))
170
171 def test_roundtrip(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000172 tests = [
173 ("foo", "foo"),
174 ("foo=bar", "foo=bar"),
175 (" foo ", "foo"),
176 ("foo=", 'foo=""'),
177 ("foo=bar bar=baz", "foo=bar; bar=baz"),
178 ("foo=bar;bar=baz", "foo=bar; bar=baz"),
179 ('foo bar baz', "foo; bar; baz"),
180 (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
181 ('foo,,,bar', 'foo, bar'),
182 ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
183
184 ('text/html; charset=iso-8859-1',
185 'text/html; charset="iso-8859-1"'),
186
187 ('foo="bar"; port="80,81"; discard, bar=baz',
188 'foo=bar; port="80,81"; discard, bar=baz'),
189
190 (r'Basic realm="\"foo\\\\bar\""',
191 r'Basic; realm="\"foo\\\\bar\""')
192 ]
193
194 for arg, expect in tests:
195 input = split_header_words([arg])
196 res = join_header_words(input)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000197 self.assertEqual(res, expect, """
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000198When parsing: '%s'
199Expected: '%s'
200Got: '%s'
201Input was: '%s'
202""" % (arg, expect, res, input))
203
204
205class FakeResponse:
206 def __init__(self, headers=[], url=None):
207 """
208 headers: list of RFC822-style 'Key: value' strings
209 """
Barry Warsaw820c1202008-06-12 04:06:45 +0000210 import email
211 self._headers = email.message_from_string("\n".join(headers))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000212 self._url = url
213 def info(self): return self._headers
214
215def interact_2965(cookiejar, url, *set_cookie_hdrs):
216 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2")
217
218def interact_netscape(cookiejar, url, *set_cookie_hdrs):
219 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")
220
221def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
222 """Perform a single request / response cycle, returning Cookie: header."""
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000223 req = urllib.request.Request(url)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000224 cookiejar.add_cookie_header(req)
225 cookie_hdr = req.get_header("Cookie", "")
226 headers = []
227 for hdr in set_cookie_hdrs:
228 headers.append("%s: %s" % (hdr_name, hdr))
229 res = FakeResponse(headers, url)
230 cookiejar.extract_cookies(res, req)
231 return cookie_hdr
232
233
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000234class FileCookieJarTests(unittest.TestCase):
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000235 def test_lwp_valueless_cookie(self):
236 # cookies with no value should be saved and loaded consistently
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000237 filename = test.support.TESTFN
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000238 c = LWPCookieJar()
239 interact_netscape(c, "http://www.acme.com/", 'boo')
240 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
241 try:
242 c.save(filename, ignore_discard=True)
243 c = LWPCookieJar()
244 c.load(filename, ignore_discard=True)
245 finally:
246 try: os.unlink(filename)
247 except OSError: pass
248 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
249
Neal Norwitz3e7de592005-12-23 21:24:35 +0000250 def test_bad_magic(self):
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200251 # OSErrors (eg. file doesn't exist) are allowed to propagate
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000252 filename = test.support.TESTFN
Neal Norwitz3e7de592005-12-23 21:24:35 +0000253 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
254 c = cookiejar_class()
255 try:
256 c.load(filename="for this test to work, a file with this "
257 "filename should not exist")
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200258 except OSError as exc:
259 # an OSError subclass (likely FileNotFoundError), but not
260 # LoadError
261 self.assertIsNot(exc.__class__, LoadError)
Neal Norwitz3e7de592005-12-23 21:24:35 +0000262 else:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200263 self.fail("expected OSError for invalid filename")
Neal Norwitz3e7de592005-12-23 21:24:35 +0000264 # Invalid contents of cookies file (eg. bad magic string)
265 # causes a LoadError.
266 try:
Brett Cannon7f462fc2010-10-29 23:27:39 +0000267 with open(filename, "w") as f:
268 f.write("oops\n")
269 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
270 c = cookiejar_class()
271 self.assertRaises(LoadError, c.load, filename)
Neal Norwitz3e7de592005-12-23 21:24:35 +0000272 finally:
273 try: os.unlink(filename)
274 except OSError: pass
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000275
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000276class CookieTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000277 # XXX
278 # Get rid of string comparisons where not actually testing str / repr.
279 # .clear() etc.
280 # IP addresses like 50 (single number, no dot) and domain-matching
281 # functions (and is_HDN)? See draft RFC 2965 errata.
282 # Strictness switches
283 # is_third_party()
284 # unverifiability / third-party blocking
285 # Netscape cookies work the same as RFC 2965 with regard to port.
286 # Set-Cookie with negative max age.
287 # If turn RFC 2965 handling off, Set-Cookie2 cookies should not clobber
288 # Set-Cookie cookies.
289 # Cookie2 should be sent if *any* cookies are not V1 (ie. V0 OR V2 etc.).
290 # Cookies (V1 and V0) with no expiry date should be set to be discarded.
291 # RFC 2965 Quoting:
292 # Should accept unquoted cookie-attribute values? check errata draft.
293 # Which are required on the way in and out?
294 # Should always return quoted cookie-attribute values?
295 # Proper testing of when RFC 2965 clobbers Netscape (waiting for errata).
296 # Path-match on return (same for V0 and V1).
297 # RFC 2965 acceptance and returning rules
298 # Set-Cookie2 without version attribute is rejected.
299
300 # Netscape peculiarities list from Ronald Tschalar.
301 # The first two still need tests, the rest are covered.
302## - Quoting: only quotes around the expires value are recognized as such
303## (and yes, some folks quote the expires value); quotes around any other
304## value are treated as part of the value.
305## - White space: white space around names and values is ignored
306## - Default path: if no path parameter is given, the path defaults to the
307## path in the request-uri up to, but not including, the last '/'. Note
308## that this is entirely different from what the spec says.
309## - Commas and other delimiters: Netscape just parses until the next ';'.
310## This means it will allow commas etc inside values (and yes, both
311## commas and equals are commonly appear in the cookie value). This also
312## means that if you fold multiple Set-Cookie header fields into one,
313## comma-separated list, it'll be a headache to parse (at least my head
314## starts hurting everytime I think of that code).
315## - Expires: You'll get all sorts of date formats in the expires,
316## including emtpy expires attributes ("expires="). Be as flexible as you
317## can, and certainly don't expect the weekday to be there; if you can't
318## parse it, just ignore it and pretend it's a session cookie.
319## - Domain-matching: Netscape uses the 2-dot rule for _all_ domains, not
320## just the 7 special TLD's listed in their spec. And folks rely on
321## that...
322
323 def test_domain_return_ok(self):
324 # test optimization: .domain_return_ok() should filter out most
325 # domains in the CookieJar before we try to access them (because that
326 # may require disk access -- in particular, with MSIECookieJar)
327 # This is only a rough check for performance reasons, so it's not too
328 # critical as long as it's sufficiently liberal.
Georg Brandl24420152008-05-26 16:32:26 +0000329 pol = DefaultCookiePolicy()
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000330 for url, domain, ok in [
331 ("http://foo.bar.com/", "blah.com", False),
332 ("http://foo.bar.com/", "rhubarb.blah.com", False),
333 ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),
334 ("http://foo.bar.com/", ".foo.bar.com", True),
335 ("http://foo.bar.com/", "foo.bar.com", True),
336 ("http://foo.bar.com/", ".bar.com", True),
337 ("http://foo.bar.com/", "com", True),
338 ("http://foo.com/", "rhubarb.foo.com", False),
339 ("http://foo.com/", ".foo.com", True),
340 ("http://foo.com/", "foo.com", True),
341 ("http://foo.com/", "com", True),
342 ("http://foo/", "rhubarb.foo", False),
343 ("http://foo/", ".foo", True),
344 ("http://foo/", "foo", True),
345 ("http://foo/", "foo.local", True),
346 ("http://foo/", ".local", True),
347 ]:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000348 request = urllib.request.Request(url)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000349 r = pol.domain_return_ok(domain, request)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000350 if ok: self.assertTrue(r)
351 else: self.assertTrue(not r)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000352
353 def test_missing_value(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000354 # missing = sign in Cookie: header is regarded by Mozilla as a missing
Georg Brandl24420152008-05-26 16:32:26 +0000355 # name, and by http.cookiejar as a missing value
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000356 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000357 c = MozillaCookieJar(filename)
358 interact_netscape(c, "http://www.acme.com/", 'eggs')
359 interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
360 cookie = c._cookies["www.acme.com"]["/"]["eggs"]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000361 self.assertTrue(cookie.value is None)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000362 self.assertEqual(cookie.name, "eggs")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000363 cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000364 self.assertTrue(cookie.value is None)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000365 self.assertEqual(cookie.name, '"spam"')
366 self.assertEqual(lwp_cookie_str(cookie), (
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000367 r'"spam"; path="/foo/"; domain="www.acme.com"; '
368 'path_spec; discard; version=0'))
369 old_str = repr(c)
370 c.save(ignore_expires=True, ignore_discard=True)
371 try:
372 c = MozillaCookieJar(filename)
373 c.revert(ignore_expires=True, ignore_discard=True)
374 finally:
375 os.unlink(c.filename)
376 # cookies unchanged apart from lost info re. whether path was specified
Ezio Melottib3aedd42010-11-20 19:04:17 +0000377 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000378 repr(c),
379 re.sub("path_specified=%s" % True, "path_specified=%s" % False,
380 old_str)
381 )
Ezio Melottib3aedd42010-11-20 19:04:17 +0000382 self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
383 '"spam"; eggs')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000384
Neal Norwitz71dad722005-12-23 21:43:48 +0000385 def test_rfc2109_handling(self):
386 # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
387 # dependent on policy settings
Neal Norwitz71dad722005-12-23 21:43:48 +0000388 for rfc2109_as_netscape, rfc2965, version in [
389 # default according to rfc2965 if not explicitly specified
390 (None, False, 0),
391 (None, True, 1),
392 # explicit rfc2109_as_netscape
393 (False, False, None), # version None here means no cookie stored
394 (False, True, 1),
395 (True, False, 0),
396 (True, True, 0),
397 ]:
398 policy = DefaultCookiePolicy(
399 rfc2109_as_netscape=rfc2109_as_netscape,
400 rfc2965=rfc2965)
401 c = CookieJar(policy)
402 interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")
403 try:
404 cookie = c._cookies["www.example.com"]["/"]["ni"]
405 except KeyError:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000406 self.assertTrue(version is None) # didn't expect a stored cookie
Neal Norwitz71dad722005-12-23 21:43:48 +0000407 else:
408 self.assertEqual(cookie.version, version)
409 # 2965 cookies are unaffected
410 interact_2965(c, "http://www.example.com/",
411 "foo=bar; Version=1")
412 if rfc2965:
413 cookie2965 = c._cookies["www.example.com"]["/"]["foo"]
414 self.assertEqual(cookie2965.version, 1)
415
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000416 def test_ns_parser(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000417 c = CookieJar()
418 interact_netscape(c, "http://www.acme.com/",
419 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')
420 interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080')
421 interact_netscape(c, "http://www.acme.com:80/", 'nini=ni')
422 interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=')
423 interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; '
424 'expires="Foo Bar 25 33:22:11 3022"')
425
426 cookie = c._cookies[".acme.com"]["/"]["spam"]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000427 self.assertEqual(cookie.domain, ".acme.com")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000428 self.assertTrue(cookie.domain_specified)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000429 self.assertEqual(cookie.port, DEFAULT_HTTP_PORT)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000430 self.assertTrue(not cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000431 # case is preserved
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000432 self.assertTrue(cookie.has_nonstandard_attr("blArgh") and
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000433 not cookie.has_nonstandard_attr("blargh"))
434
435 cookie = c._cookies["www.acme.com"]["/"]["ni"]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000436 self.assertEqual(cookie.domain, "www.acme.com")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000437 self.assertTrue(not cookie.domain_specified)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000438 self.assertEqual(cookie.port, "80,8080")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000439 self.assertTrue(cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000440
441 cookie = c._cookies["www.acme.com"]["/"]["nini"]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000442 self.assertTrue(cookie.port is None)
443 self.assertTrue(not cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000444
445 # invalid expires should not cause cookie to be dropped
446 foo = c._cookies["www.acme.com"]["/"]["foo"]
447 spam = c._cookies["www.acme.com"]["/"]["foo"]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000448 self.assertTrue(foo.expires is None)
449 self.assertTrue(spam.expires is None)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000450
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000451 def test_ns_parser_special_names(self):
452 # names such as 'expires' are not special in first name=value pair
453 # of Set-Cookie: header
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000454 c = CookieJar()
455 interact_netscape(c, "http://www.acme.com/", 'expires=eggs')
456 interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')
457
458 cookies = c._cookies["www.acme.com"]["/"]
Benjamin Peterson577473f2010-01-19 00:09:57 +0000459 self.assertIn('expires', cookies)
460 self.assertIn('version', cookies)
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000461
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000462 def test_expires(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000463 # if expires is in future, keep cookie...
464 c = CookieJar()
465 future = time2netscape(time.time()+3600)
466 interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
467 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000468 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000469 now = time2netscape(time.time()-1)
470 # ... and if in past or present, discard it
471 interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %
472 now)
473 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000474 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000475 self.assertIn('spam="bar"', h)
476 self.assertNotIn("foo", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000477
478 # max-age takes precedence over expires, and zero max-age is request to
479 # delete both new cookie and any old matching cookie
480 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %
481 future)
482 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %
483 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000484 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000485 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '
486 'expires=%s; max-age=0' % future)
487 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '
488 'max-age=0; expires=%s' % future)
489 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000490 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000491
492 # test expiry at end of session for cookies with no expires attribute
493 interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000494 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000495 c.clear_session_cookies()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000496 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000497 self.assertIn('spam="bar"', h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000498
499 # XXX RFC 2965 expiry rules (some apply to V0 too)
500
501 def test_default_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000502 # RFC 2965
503 pol = DefaultCookiePolicy(rfc2965=True)
504
505 c = CookieJar(pol)
506 interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000507 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000508
509 c = CookieJar(pol)
510 interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000511 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000512
513 c = CookieJar(pol)
514 interact_2965(c, "http://www.acme.com/blah/rhubarb",
515 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000516 self.assertIn("/blah/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000517
518 c = CookieJar(pol)
519 interact_2965(c, "http://www.acme.com/blah/rhubarb/",
520 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000521 self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000522
523 # Netscape
524
525 c = CookieJar()
526 interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000527 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000528
529 c = CookieJar()
530 interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000531 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000532
533 c = CookieJar()
534 interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000535 self.assertIn("/blah", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000536
537 c = CookieJar()
538 interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000539 self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000540
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000541 def test_default_path_with_query(self):
542 cj = CookieJar()
543 uri = "http://example.com/?spam/eggs"
544 value = 'eggs="bar"'
545 interact_netscape(cj, uri, value)
546 # Default path does not include query, so is "/", not "/?spam".
547 self.assertIn("/", cj._cookies["example.com"])
548 # Cookie is sent back to the same URI.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000549 self.assertEqual(interact_netscape(cj, uri), value)
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000550
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000551 def test_escape_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000552 cases = [
553 # quoted safe
554 ("/foo%2f/bar", "/foo%2F/bar"),
555 ("/foo%2F/bar", "/foo%2F/bar"),
556 # quoted %
557 ("/foo%%/bar", "/foo%%/bar"),
558 # quoted unsafe
559 ("/fo%19o/bar", "/fo%19o/bar"),
560 ("/fo%7do/bar", "/fo%7Do/bar"),
561 # unquoted safe
562 ("/foo/bar&", "/foo/bar&"),
563 ("/foo//bar", "/foo//bar"),
564 ("\176/foo/bar", "\176/foo/bar"),
565 # unquoted unsafe
566 ("/foo\031/bar", "/foo%19/bar"),
567 ("/\175foo/bar", "/%7Dfoo/bar"),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000568 # unicode, latin-1 range
569 ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000570 # unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000571 ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000572 ]
573 for arg, result in cases:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000574 self.assertEqual(escape_path(arg), result)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000575
576 def test_request_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000577 # with parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000578 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000579 "http://www.example.com/rheum/rhaponticum;"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000580 "foo=bar;sing=song?apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000581 self.assertEqual(request_path(req),
582 "/rheum/rhaponticum;foo=bar;sing=song")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000583 # without parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000584 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000585 "http://www.example.com/rheum/rhaponticum?"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000586 "apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000587 self.assertEqual(request_path(req), "/rheum/rhaponticum")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000588 # missing final slash
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000589 req = urllib.request.Request("http://www.example.com")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000590 self.assertEqual(request_path(req), "/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000591
592 def test_request_port(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000593 req = urllib.request.Request("http://www.acme.com:1234/",
594 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000595 self.assertEqual(request_port(req), "1234")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000596 req = urllib.request.Request("http://www.acme.com/",
597 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000598 self.assertEqual(request_port(req), DEFAULT_HTTP_PORT)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000599
600 def test_request_host(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000601 # this request is illegal (RFC2616, 14.2.3)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000602 req = urllib.request.Request("http://1.1.1.1/",
603 headers={"Host": "www.acme.com:80"})
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000604 # libwww-perl wants this response, but that seems wrong (RFC 2616,
605 # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000606 #self.assertEqual(request_host(req), "www.acme.com")
607 self.assertEqual(request_host(req), "1.1.1.1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000608 req = urllib.request.Request("http://www.acme.com/",
609 headers={"Host": "irrelevant.com"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000610 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000611 # port shouldn't be in request-host
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000612 req = urllib.request.Request("http://www.acme.com:2345/resource.html",
613 headers={"Host": "www.acme.com:5432"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000614 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000615
616 def test_is_HDN(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000617 self.assertTrue(is_HDN("foo.bar.com"))
618 self.assertTrue(is_HDN("1foo2.3bar4.5com"))
619 self.assertTrue(not is_HDN("192.168.1.1"))
620 self.assertTrue(not is_HDN(""))
621 self.assertTrue(not is_HDN("."))
622 self.assertTrue(not is_HDN(".foo.bar.com"))
623 self.assertTrue(not is_HDN("..foo"))
624 self.assertTrue(not is_HDN("foo."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000625
626 def test_reach(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000627 self.assertEqual(reach("www.acme.com"), ".acme.com")
628 self.assertEqual(reach("acme.com"), "acme.com")
629 self.assertEqual(reach("acme.local"), ".local")
630 self.assertEqual(reach(".local"), ".local")
631 self.assertEqual(reach(".com"), ".com")
632 self.assertEqual(reach("."), ".")
633 self.assertEqual(reach(""), "")
634 self.assertEqual(reach("192.168.0.1"), "192.168.0.1")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000635
636 def test_domain_match(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000637 self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))
638 self.assertTrue(not domain_match("192.168.1.1", ".168.1.1"))
639 self.assertTrue(domain_match("x.y.com", "x.Y.com"))
640 self.assertTrue(domain_match("x.y.com", ".Y.com"))
641 self.assertTrue(not domain_match("x.y.com", "Y.com"))
642 self.assertTrue(domain_match("a.b.c.com", ".c.com"))
643 self.assertTrue(not domain_match(".c.com", "a.b.c.com"))
644 self.assertTrue(domain_match("example.local", ".local"))
645 self.assertTrue(not domain_match("blah.blah", ""))
646 self.assertTrue(not domain_match("", ".rhubarb.rhubarb"))
647 self.assertTrue(domain_match("", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000648
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000649 self.assertTrue(user_domain_match("acme.com", "acme.com"))
650 self.assertTrue(not user_domain_match("acme.com", ".acme.com"))
651 self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))
652 self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
653 self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))
654 self.assertTrue(user_domain_match("x.y.com", ".Y.com"))
655 self.assertTrue(not user_domain_match("x.y.com", "Y.com"))
656 self.assertTrue(user_domain_match("y.com", "Y.com"))
657 self.assertTrue(not user_domain_match(".y.com", "Y.com"))
658 self.assertTrue(user_domain_match(".y.com", ".Y.com"))
659 self.assertTrue(user_domain_match("x.y.com", ".com"))
660 self.assertTrue(not user_domain_match("x.y.com", "com"))
661 self.assertTrue(not user_domain_match("x.y.com", "m"))
662 self.assertTrue(not user_domain_match("x.y.com", ".m"))
663 self.assertTrue(not user_domain_match("x.y.com", ""))
664 self.assertTrue(not user_domain_match("x.y.com", "."))
665 self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000666 # not both HDNs, so must string-compare equal to match
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000667 self.assertTrue(not user_domain_match("192.168.1.1", ".168.1.1"))
668 self.assertTrue(not user_domain_match("192.168.1.1", "."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000669 # empty string is a special case
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000670 self.assertTrue(not user_domain_match("192.168.1.1", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000671
672 def test_wrong_domain(self):
673 # Cookies whose effective request-host name does not domain-match the
674 # domain are rejected.
675
676 # XXX far from complete
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000677 c = CookieJar()
678 interact_2965(c, "http://www.nasty.com/",
679 'foo=bar; domain=friendly.org; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000680 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000681
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 def test_strict_domain(self):
683 # Cookies whose domain is a country-code tld like .co.uk should
684 # not be set if CookiePolicy.strict_domain is true.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000685 cp = DefaultCookiePolicy(strict_domain=True)
686 cj = CookieJar(policy=cp)
687 interact_netscape(cj, "http://example.co.uk/", 'no=problemo')
688 interact_netscape(cj, "http://example.co.uk/",
689 'okey=dokey; Domain=.example.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000690 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691 for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:
692 interact_netscape(cj, "http://example.%s/" % pseudo_tld,
693 'spam=eggs; Domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000694 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000696 def test_two_component_domain_ns(self):
697 # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain
698 # should all get accepted, as should .acme.com, acme.com and no domain
699 # for 2-component domains like acme.com.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000700 c = CookieJar()
701
702 # two-component V0 domain is OK
703 interact_netscape(c, "http://foo.net/", 'ns=bar')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000704 self.assertEqual(len(c), 1)
705 self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar")
706 self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000707 # *will* be returned to any other domain (unlike RFC 2965)...
Ezio Melottib3aedd42010-11-20 19:04:17 +0000708 self.assertEqual(interact_netscape(c, "http://www.foo.net/"),
709 "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000710 # ...unless requested otherwise
711 pol = DefaultCookiePolicy(
712 strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
713 c.set_policy(pol)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000714 self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000715
716 # unlike RFC 2965, even explicit two-component domain is OK,
717 # because .foo.net matches foo.net
718 interact_netscape(c, "http://foo.net/foo/",
719 'spam1=eggs; domain=foo.net')
720 # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
721 interact_netscape(c, "http://foo.net/foo/bar/",
722 'spam2=eggs; domain=.foo.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000723 self.assertEqual(len(c), 3)
724 self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value,
725 "eggs")
726 self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,
727 "eggs")
728 self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"),
729 "spam2=eggs; spam1=eggs; ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000730
731 # top-level domain is too general
732 interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000733 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000734
735## # Netscape protocol doesn't allow non-special top level domains (such
736## # as co.uk) in the domain attribute unless there are at least three
737## # dots in it.
738 # Oh yes it does! Real implementations don't check this, and real
739 # cookies (of course) rely on that behaviour.
740 interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000741## self.assertEqual(len(c), 2)
742 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000743
744 def test_two_component_domain_rfc2965(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000745 pol = DefaultCookiePolicy(rfc2965=True)
746 c = CookieJar(pol)
747
748 # two-component V1 domain is OK
749 interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000750 self.assertEqual(len(c), 1)
751 self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar")
752 self.assertEqual(interact_2965(c, "http://foo.net/"),
753 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000754 # won't be returned to any other domain (because domain was implied)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000755 self.assertEqual(interact_2965(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000756
757 # unless domain is given explicitly, because then it must be
758 # rewritten to start with a dot: foo.net --> .foo.net, which does
759 # not domain-match foo.net
760 interact_2965(c, "http://foo.net/foo",
761 'spam=eggs; domain=foo.net; path=/foo; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000762 self.assertEqual(len(c), 1)
763 self.assertEqual(interact_2965(c, "http://foo.net/foo"),
764 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000765
766 # explicit foo.net from three-component domain www.foo.net *does* get
767 # set, because .foo.net domain-matches .foo.net
768 interact_2965(c, "http://www.foo.net/foo/",
769 'spam=eggs; domain=foo.net; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000770 self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value,
771 "eggs")
772 self.assertEqual(len(c), 2)
773 self.assertEqual(interact_2965(c, "http://foo.net/foo/"),
774 "$Version=1; foo=bar")
775 self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"),
776 '$Version=1; spam=eggs; $Domain="foo.net"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000777
778 # top-level domain is too general
779 interact_2965(c, "http://foo.net/",
780 'ni="ni"; domain=".net"; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000781 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000782
783 # RFC 2965 doesn't require blocking this
784 interact_2965(c, "http://foo.co.uk/",
785 'nasty=trick; domain=.co.uk; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000786 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000787
788 def test_domain_allow(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000789 c = CookieJar(policy=DefaultCookiePolicy(
790 blocked_domains=["acme.com"],
791 allowed_domains=["www.acme.com"]))
792
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000793 req = urllib.request.Request("http://acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000794 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
795 res = FakeResponse(headers, "http://acme.com/")
796 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000797 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000798
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000799 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000800 res = FakeResponse(headers, "http://www.acme.com/")
801 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000802 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000803
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000804 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000805 res = FakeResponse(headers, "http://www.coyote.com/")
806 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000807 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000808
809 # set a cookie with non-allowed domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000810 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000811 res = FakeResponse(headers, "http://www.coyote.com/")
812 cookies = c.make_cookies(res, req)
813 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000814 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000815 # ... and check is doesn't get returned
816 c.add_cookie_header(req)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000817 self.assertTrue(not req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000818
819 def test_domain_block(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000820 pol = DefaultCookiePolicy(
821 rfc2965=True, blocked_domains=[".acme.com"])
822 c = CookieJar(policy=pol)
823 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
824
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000825 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000826 res = FakeResponse(headers, "http://www.acme.com/")
827 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000828 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000829
830 p = pol.set_blocked_domains(["acme.com"])
831 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000832 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000833
834 c.clear()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000835 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000836 res = FakeResponse(headers, "http://www.roadrunner.net/")
837 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000838 self.assertEqual(len(c), 1)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000839 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000840 c.add_cookie_header(req)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000841 self.assertTrue((req.has_header("Cookie") and
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000842 req.has_header("Cookie2")))
843
844 c.clear()
845 pol.set_blocked_domains([".acme.com"])
846 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000847 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000848
849 # set a cookie with blocked domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000850 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000851 res = FakeResponse(headers, "http://www.acme.com/")
852 cookies = c.make_cookies(res, req)
853 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000854 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000855 # ... and check is doesn't get returned
856 c.add_cookie_header(req)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000857 self.assertTrue(not req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000858
859 def test_secure(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000860 for ns in True, False:
861 for whitespace in " ", "":
862 c = CookieJar()
863 if ns:
864 pol = DefaultCookiePolicy(rfc2965=False)
865 int = interact_netscape
866 vs = ""
867 else:
868 pol = DefaultCookiePolicy(rfc2965=True)
869 int = interact_2965
870 vs = "; Version=1"
871 c.set_policy(pol)
872 url = "http://www.acme.com/"
873 int(c, url, "foo1=bar%s%s" % (vs, whitespace))
874 int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000875 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000876 not c._cookies["www.acme.com"]["/"]["foo1"].secure,
877 "non-secure cookie registered secure")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000878 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000879 c._cookies["www.acme.com"]["/"]["foo2"].secure,
880 "secure cookie registered non-secure")
881
882 def test_quote_cookie_value(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000883 c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))
884 interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')
885 h = interact_2965(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000886 self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000887
888 def test_missing_final_slash(self):
889 # Missing slash from request URL's abs_path should be assumed present.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000890 url = "http://www.acme.com"
891 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
892 interact_2965(c, url, "foo=bar; Version=1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000893 req = urllib.request.Request(url)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000894 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000895 c.add_cookie_header(req)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000896 self.assertTrue(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000897
898 def test_domain_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000899 pol = DefaultCookiePolicy(rfc2965=True)
900
901 c = CookieJar(pol)
902 url = "http://foo.bar.com/"
903 interact_2965(c, url, "spam=eggs; Version=1")
904 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000905 self.assertNotIn("Domain", h,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000906 "absent domain returned with domain present")
907
908 c = CookieJar(pol)
909 url = "http://foo.bar.com/"
910 interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
911 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000912 self.assertIn('$Domain=".bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000913
914 c = CookieJar(pol)
915 url = "http://foo.bar.com/"
916 # note missing initial dot in Domain
917 interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
918 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000919 self.assertIn('$Domain="bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000920
921 def test_path_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000922 pol = DefaultCookiePolicy(rfc2965=True)
923
924 c = CookieJar(pol)
925 url = "http://foo.bar.com/"
926 interact_2965(c, url, "spam=eggs; Version=1")
927 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000928 self.assertNotIn("Path", h, "absent path returned with path present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000929
930 c = CookieJar(pol)
931 url = "http://foo.bar.com/"
932 interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
933 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000934 self.assertIn('$Path="/"', h, "path not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000935
936 def test_port_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000937 pol = DefaultCookiePolicy(rfc2965=True)
938
939 c = CookieJar(pol)
940 url = "http://foo.bar.com/"
941 interact_2965(c, url, "spam=eggs; Version=1")
942 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000943 self.assertNotIn("Port", h, "absent port returned with port present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000944
945 c = CookieJar(pol)
946 url = "http://foo.bar.com/"
947 interact_2965(c, url, "spam=eggs; Version=1; Port")
948 h = interact_2965(c, url)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000949 self.assertTrue(re.search("\$Port([^=]|$)", h),
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000950 "port with no value not returned with no value")
951
952 c = CookieJar(pol)
953 url = "http://foo.bar.com/"
954 interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
955 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000956 self.assertIn('$Port="80"', h,
957 "port with single value not returned with single value")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000958
959 c = CookieJar(pol)
960 url = "http://foo.bar.com/"
961 interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
962 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000963 self.assertIn('$Port="80,8080"', h,
964 "port with multiple values not returned with multiple "
965 "values")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000966
967 def test_no_return_comment(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000968 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
969 url = "http://foo.bar.com/"
970 interact_2965(c, url, 'spam=eggs; Version=1; '
971 'Comment="does anybody read these?"; '
972 'CommentURL="http://foo.bar.net/comment.html"')
973 h = interact_2965(c, url)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000974 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000975 "Comment" not in h,
976 "Comment or CommentURL cookie-attributes returned to server")
977
978 def test_Cookie_iterator(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000979 cs = CookieJar(DefaultCookiePolicy(rfc2965=True))
980 # add some random cookies
981 interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '
982 'Comment="does anybody read these?"; '
983 'CommentURL="http://foo.bar.net/comment.html"')
984 interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")
985 interact_2965(cs, "http://www.acme.com/blah/",
986 "foo=bar; secure; Version=1")
987 interact_2965(cs, "http://www.acme.com/blah/",
988 "foo=bar; path=/; Version=1")
989 interact_2965(cs, "http://www.sol.no",
990 r'bang=wallop; version=1; domain=".sol.no"; '
991 r'port="90,100, 80,8080"; '
992 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
993
994 versions = [1, 1, 1, 0, 1]
995 names = ["bang", "foo", "foo", "spam", "foo"]
996 domains = [".sol.no", "blah.spam.org", "www.acme.com",
997 "www.acme.com", "www.acme.com"]
998 paths = ["/", "/", "/", "/blah", "/blah/"]
999
1000 for i in range(4):
1001 i = 0
1002 for c in cs:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001003 self.assertTrue(isinstance(c, Cookie))
Ezio Melottib3aedd42010-11-20 19:04:17 +00001004 self.assertEqual(c.version, versions[i])
1005 self.assertEqual(c.name, names[i])
1006 self.assertEqual(c.domain, domains[i])
1007 self.assertEqual(c.path, paths[i])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001008 i = i + 1
1009
1010 def test_parse_ns_headers(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001011 # missing domain value (invalid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001012 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001013 parse_ns_headers(["foo=bar; path=/; domain"]),
1014 [[("foo", "bar"),
1015 ("path", "/"), ("domain", None), ("version", "0")]]
1016 )
1017 # invalid expires value
Ezio Melottib3aedd42010-11-20 19:04:17 +00001018 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001019 parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),
1020 [[("foo", "bar"), ("expires", None), ("version", "0")]]
1021 )
1022 # missing cookie value (valid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001023 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001024 parse_ns_headers(["foo"]),
1025 [[("foo", None), ("version", "0")]]
1026 )
1027 # shouldn't add version if header is empty
Ezio Melottib3aedd42010-11-20 19:04:17 +00001028 self.assertEqual(parse_ns_headers([""]), [])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001029
1030 def test_bad_cookie_header(self):
1031
1032 def cookiejar_from_cookie_headers(headers):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001033 c = CookieJar()
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001034 req = urllib.request.Request("http://www.example.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001035 r = FakeResponse(headers, "http://www.example.com/")
1036 c.extract_cookies(r, req)
1037 return c
1038
1039 # none of these bad headers should cause an exception to be raised
1040 for headers in [
1041 ["Set-Cookie: "], # actually, nothing wrong with this
1042 ["Set-Cookie2: "], # ditto
1043 # missing domain value
1044 ["Set-Cookie2: a=foo; path=/; Version=1; domain"],
1045 # bad max-age
1046 ["Set-Cookie: b=foo; max-age=oops"],
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +00001047 # bad version
1048 ["Set-Cookie: b=foo; version=spam"],
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001049 ]:
1050 c = cookiejar_from_cookie_headers(headers)
1051 # these bad cookies shouldn't be set
Ezio Melottib3aedd42010-11-20 19:04:17 +00001052 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001053
1054 # cookie with invalid expires is treated as session cookie
1055 headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
1056 c = cookiejar_from_cookie_headers(headers)
1057 cookie = c._cookies["www.example.com"]["/"]["c"]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001058 self.assertTrue(cookie.expires is None)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001059
1060
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001061class LWPCookieTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001062 # Tests taken from libwww-perl, with a few modifications and additions.
1063
1064 def test_netscape_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001065 #-------------------------------------------------------------------
1066 # First we check that it works for the original example at
1067 # http://www.netscape.com/newsref/std/cookie_spec.html
1068
1069 # Client requests a document, and receives in the response:
1070 #
1071 # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT
1072 #
1073 # When client requests a URL in path "/" on this server, it sends:
1074 #
1075 # Cookie: CUSTOMER=WILE_E_COYOTE
1076 #
1077 # Client requests a document, and receives in the response:
1078 #
1079 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1080 #
1081 # When client requests a URL in path "/" on this server, it sends:
1082 #
1083 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1084 #
1085 # Client receives:
1086 #
1087 # Set-Cookie: SHIPPING=FEDEX; path=/fo
1088 #
1089 # When client requests a URL in path "/" on this server, it sends:
1090 #
1091 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1092 #
1093 # When client requests a URL in path "/foo" on this server, it sends:
1094 #
1095 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX
1096 #
1097 # The last Cookie is buggy, because both specifications say that the
1098 # most specific cookie must be sent first. SHIPPING=FEDEX is the
1099 # most specific and should thus be first.
1100
1101 year_plus_one = time.localtime()[0] + 1
1102
1103 headers = []
1104
1105 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1106
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001107 #req = urllib.request.Request("http://1.1.1.1/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001108 # headers={"Host": "www.acme.com:80"})
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001109 req = urllib.request.Request("http://www.acme.com:80/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001110 headers={"Host": "www.acme.com:80"})
1111
1112 headers.append(
1113 "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "
1114 "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)
1115 res = FakeResponse(headers, "http://www.acme.com/")
1116 c.extract_cookies(res, req)
1117
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001118 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001119 c.add_cookie_header(req)
1120
1121 self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")
1122 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
1123
1124 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1125 res = FakeResponse(headers, "http://www.acme.com/")
1126 c.extract_cookies(res, req)
1127
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001128 req = urllib.request.Request("http://www.acme.com/foo/bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001129 c.add_cookie_header(req)
1130
1131 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001132 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1133 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001134
1135 headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
1136 res = FakeResponse(headers, "http://www.acme.com")
1137 c.extract_cookies(res, req)
1138
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001139 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001140 c.add_cookie_header(req)
1141
1142 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001143 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1144 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1145 self.assertNotIn("SHIPPING=FEDEX", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001146
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001147 req = urllib.request.Request("http://www.acme.com/foo/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001148 c.add_cookie_header(req)
1149
1150 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001151 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1152 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1153 self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001154
1155 def test_netscape_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001156 # Second Example transaction sequence:
1157 #
1158 # Assume all mappings from above have been cleared.
1159 #
1160 # Client receives:
1161 #
1162 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1163 #
1164 # When client requests a URL in path "/" on this server, it sends:
1165 #
1166 # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001
1167 #
1168 # Client receives:
1169 #
1170 # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
1171 #
1172 # When client requests a URL in path "/ammo" on this server, it sends:
1173 #
1174 # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001
1175 #
1176 # NOTE: There are two name/value pairs named "PART_NUMBER" due to
1177 # the inheritance of the "/" mapping in addition to the "/ammo" mapping.
1178
1179 c = CookieJar()
1180 headers = []
1181
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001182 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001183 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1184 res = FakeResponse(headers, "http://www.acme.com/")
1185
1186 c.extract_cookies(res, req)
1187
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001188 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001189 c.add_cookie_header(req)
1190
Ezio Melottib3aedd42010-11-20 19:04:17 +00001191 self.assertEqual(req.get_header("Cookie"),
1192 "PART_NUMBER=ROCKET_LAUNCHER_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001193
1194 headers.append(
1195 "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")
1196 res = FakeResponse(headers, "http://www.acme.com/")
1197 c.extract_cookies(res, req)
1198
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001199 req = urllib.request.Request("http://www.acme.com/ammo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001200 c.add_cookie_header(req)
1201
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001202 self.assertTrue(re.search(r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001203 "PART_NUMBER=ROCKET_LAUNCHER_0001",
1204 req.get_header("Cookie")))
1205
1206 def test_ietf_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001207 #-------------------------------------------------------------------
1208 # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt
1209 #
1210 # 5. EXAMPLES
1211
1212 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1213
1214 #
1215 # 5.1 Example 1
1216 #
1217 # Most detail of request and response headers has been omitted. Assume
1218 # the user agent has no stored cookies.
1219 #
1220 # 1. User Agent -> Server
1221 #
1222 # POST /acme/login HTTP/1.1
1223 # [form data]
1224 #
1225 # User identifies self via a form.
1226 #
1227 # 2. Server -> User Agent
1228 #
1229 # HTTP/1.1 200 OK
1230 # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
1231 #
1232 # Cookie reflects user's identity.
1233
1234 cookie = interact_2965(
1235 c, 'http://www.acme.com/acme/login',
1236 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001237 self.assertTrue(not cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001238
1239 #
1240 # 3. User Agent -> Server
1241 #
1242 # POST /acme/pickitem HTTP/1.1
1243 # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
1244 # [form data]
1245 #
1246 # User selects an item for ``shopping basket.''
1247 #
1248 # 4. Server -> User Agent
1249 #
1250 # HTTP/1.1 200 OK
1251 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1252 # Path="/acme"
1253 #
1254 # Shopping basket contains an item.
1255
1256 cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
1257 'Part_Number="Rocket_Launcher_0001"; '
1258 'Version="1"; Path="/acme"');
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001259 self.assertTrue(re.search(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001260 r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$',
1261 cookie))
1262
1263 #
1264 # 5. User Agent -> Server
1265 #
1266 # POST /acme/shipping HTTP/1.1
1267 # Cookie: $Version="1";
1268 # Customer="WILE_E_COYOTE"; $Path="/acme";
1269 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1270 # [form data]
1271 #
1272 # User selects shipping method from form.
1273 #
1274 # 6. Server -> User Agent
1275 #
1276 # HTTP/1.1 200 OK
1277 # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"
1278 #
1279 # New cookie reflects shipping method.
1280
1281 cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
1282 'Shipping="FedEx"; Version="1"; Path="/acme"')
1283
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001284 self.assertTrue(re.search(r'^\$Version="?1"?;', cookie))
1285 self.assertTrue(re.search(r'Part_Number="?Rocket_Launcher_0001"?;'
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001286 '\s*\$Path="\/acme"', cookie))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001287 self.assertTrue(re.search(r'Customer="?WILE_E_COYOTE"?;\s*\$Path="\/acme"',
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001288 cookie))
1289
1290 #
1291 # 7. User Agent -> Server
1292 #
1293 # POST /acme/process HTTP/1.1
1294 # Cookie: $Version="1";
1295 # Customer="WILE_E_COYOTE"; $Path="/acme";
1296 # Part_Number="Rocket_Launcher_0001"; $Path="/acme";
1297 # Shipping="FedEx"; $Path="/acme"
1298 # [form data]
1299 #
1300 # User chooses to process order.
1301 #
1302 # 8. Server -> User Agent
1303 #
1304 # HTTP/1.1 200 OK
1305 #
1306 # Transaction is complete.
1307
1308 cookie = interact_2965(c, "http://www.acme.com/acme/process")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001309 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001310 re.search(r'Shipping="?FedEx"?;\s*\$Path="\/acme"', cookie) and
1311 "WILE_E_COYOTE" in cookie)
1312
1313 #
1314 # The user agent makes a series of requests on the origin server, after
1315 # each of which it receives a new cookie. All the cookies have the same
1316 # Path attribute and (default) domain. Because the request URLs all have
1317 # /acme as a prefix, and that matches the Path attribute, each request
1318 # contains all the cookies received so far.
1319
1320 def test_ietf_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001321 # 5.2 Example 2
1322 #
1323 # This example illustrates the effect of the Path attribute. All detail
1324 # of request and response headers has been omitted. Assume the user agent
1325 # has no stored cookies.
1326
1327 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1328
1329 # Imagine the user agent has received, in response to earlier requests,
1330 # the response headers
1331 #
1332 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1333 # Path="/acme"
1334 #
1335 # and
1336 #
1337 # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
1338 # Path="/acme/ammo"
1339
1340 interact_2965(
1341 c, "http://www.acme.com/acme/ammo/specific",
1342 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
1343 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
1344
1345 # A subsequent request by the user agent to the (same) server for URLs of
1346 # the form /acme/ammo/... would include the following request header:
1347 #
1348 # Cookie: $Version="1";
1349 # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
1350 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1351 #
1352 # Note that the NAME=VALUE pair for the cookie with the more specific Path
1353 # attribute, /acme/ammo, comes before the one with the less specific Path
1354 # attribute, /acme. Further note that the same cookie name appears more
1355 # than once.
1356
1357 cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001358 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001359 re.search(r"Riding_Rocket_0023.*Rocket_Launcher_0001", cookie))
1360
1361 # A subsequent request by the user agent to the (same) server for a URL of
1362 # the form /acme/parts/ would include the following request header:
1363 #
1364 # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1365 #
1366 # Here, the second cookie's Path attribute /acme/ammo is not a prefix of
1367 # the request URL, /acme/parts/, so the cookie does not get forwarded to
1368 # the server.
1369
1370 cookie = interact_2965(c, "http://www.acme.com/acme/parts/")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001371 self.assertIn("Rocket_Launcher_0001", cookie)
1372 self.assertNotIn("Riding_Rocket_0023", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001373
1374 def test_rejection(self):
1375 # Test rejection of Set-Cookie2 responses based on domain, path, port.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001376 pol = DefaultCookiePolicy(rfc2965=True)
1377
1378 c = LWPCookieJar(policy=pol)
1379
1380 max_age = "max-age=3600"
1381
1382 # illegal domain (no embedded dots)
1383 cookie = interact_2965(c, "http://www.acme.com",
1384 'foo=bar; domain=".com"; version=1')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001385 self.assertTrue(not c)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001386
1387 # legal domain
1388 cookie = interact_2965(c, "http://www.acme.com",
1389 'ping=pong; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001390 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001391
1392 # illegal domain (host prefix "www.a" contains a dot)
1393 cookie = interact_2965(c, "http://www.a.acme.com",
1394 'whiz=bang; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001395 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001396
1397 # legal domain
1398 cookie = interact_2965(c, "http://www.a.acme.com",
1399 'wow=flutter; domain=".a.acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001400 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001401
1402 # can't partially match an IP-address
1403 cookie = interact_2965(c, "http://125.125.125.125",
1404 'zzzz=ping; domain="125.125.125"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001405 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001406
1407 # illegal path (must be prefix of request path)
1408 cookie = interact_2965(c, "http://www.sol.no",
1409 'blah=rhubarb; domain=".sol.no"; path="/foo"; '
1410 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001411 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001412
1413 # legal path
1414 cookie = interact_2965(c, "http://www.sol.no/foo/bar",
1415 'bing=bong; domain=".sol.no"; path="/foo"; '
1416 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001417 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001418
1419 # illegal port (request-port not in list)
1420 cookie = interact_2965(c, "http://www.sol.no",
1421 'whiz=ffft; domain=".sol.no"; port="90,100"; '
1422 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001423 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001424
1425 # legal port
1426 cookie = interact_2965(
1427 c, "http://www.sol.no",
1428 r'bang=wallop; version=1; domain=".sol.no"; '
1429 r'port="90,100, 80,8080"; '
1430 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001431 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001432
1433 # port attribute without any value (current port)
1434 cookie = interact_2965(c, "http://www.sol.no",
1435 'foo9=bar; version=1; domain=".sol.no"; port; '
1436 'max-age=100;')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001437 self.assertEqual(len(c), 5)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001438
1439 # encoded path
1440 # LWP has this test, but unescaping allowed path characters seems
1441 # like a bad idea, so I think this should fail:
1442## cookie = interact_2965(c, "http://www.sol.no/foo/",
1443## r'foo8=bar; version=1; path="/%66oo"')
1444 # but this is OK, because '<' is not an allowed HTTP URL path
1445 # character:
1446 cookie = interact_2965(c, "http://www.sol.no/<oo/",
1447 r'foo8=bar; version=1; path="/%3coo"')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001448 self.assertEqual(len(c), 6)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001449
1450 # save and restore
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001451 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001452
1453 try:
1454 c.save(filename, ignore_discard=True)
1455 old = repr(c)
1456
1457 c = LWPCookieJar(policy=pol)
1458 c.load(filename, ignore_discard=True)
1459 finally:
1460 try: os.unlink(filename)
1461 except OSError: pass
1462
Ezio Melottib3aedd42010-11-20 19:04:17 +00001463 self.assertEqual(old, repr(c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001464
1465 def test_url_encoding(self):
1466 # Try some URL encodings of the PATHs.
1467 # (the behaviour here has changed from libwww-perl)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001468 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
Guido van Rossum52dbbb92008-08-18 21:44:30 +00001469 interact_2965(c, "http://www.acme.com/foo%2f%25/"
1470 "%3c%3c%0Anew%C3%A5/%C3%A5",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001471 "foo = bar; version = 1")
1472
1473 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001474 c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001475 'bar=baz; path="/foo/"; version=1');
1476 version_re = re.compile(r'^\$version=\"?1\"?', re.I)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001477 self.assertIn("foo=bar", cookie)
1478 self.assertTrue(version_re.search(cookie))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001479
1480 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001481 c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001482 self.assertTrue(not cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001483
1484 # unicode URL doesn't raise exception
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001485 cookie = interact_2965(c, "http://www.acme.com/\xfc")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001486
1487 def test_mozilla(self):
1488 # Save / load Mozilla/Netscape cookie file format.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001489 year_plus_one = time.localtime()[0] + 1
1490
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001491 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001492
1493 c = MozillaCookieJar(filename,
1494 policy=DefaultCookiePolicy(rfc2965=True))
1495 interact_2965(c, "http://www.acme.com/",
1496 "foo1=bar; max-age=100; Version=1")
1497 interact_2965(c, "http://www.acme.com/",
1498 'foo2=bar; port="80"; max-age=100; Discard; Version=1')
1499 interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")
1500
1501 expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
1502 interact_netscape(c, "http://www.foo.com/",
1503 "fooa=bar; %s" % expires)
1504 interact_netscape(c, "http://www.foo.com/",
1505 "foob=bar; Domain=.foo.com; %s" % expires)
1506 interact_netscape(c, "http://www.foo.com/",
1507 "fooc=bar; Domain=www.foo.com; %s" % expires)
1508
1509 def save_and_restore(cj, ignore_discard):
1510 try:
1511 cj.save(ignore_discard=ignore_discard)
1512 new_c = MozillaCookieJar(filename,
1513 DefaultCookiePolicy(rfc2965=True))
1514 new_c.load(ignore_discard=ignore_discard)
1515 finally:
1516 try: os.unlink(filename)
1517 except OSError: pass
1518 return new_c
1519
1520 new_c = save_and_restore(c, True)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001521 self.assertEqual(len(new_c), 6) # none discarded
Benjamin Peterson577473f2010-01-19 00:09:57 +00001522 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001523
1524 new_c = save_and_restore(c, False)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001525 self.assertEqual(len(new_c), 4) # 2 of them discarded on save
Benjamin Peterson577473f2010-01-19 00:09:57 +00001526 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001527
1528 def test_netscape_misc(self):
1529 # Some additional Netscape cookies tests.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001530 c = CookieJar()
1531 headers = []
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001532 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001533
1534 # Netscape allows a host part that contains dots
1535 headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
1536 res = FakeResponse(headers, "http://www.acme.com/foo")
1537 c.extract_cookies(res, req)
1538
1539 # and that the domain is the same as the host without adding a leading
1540 # dot to the domain. Should not quote even if strange chars are used
1541 # in the cookie value.
1542 headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
1543 res = FakeResponse(headers, "http://www.acme.com/foo")
1544 c.extract_cookies(res, req)
1545
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001546 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001547 c.add_cookie_header(req)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001548 self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie"))
1549 self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001550
1551 def test_intranet_domains_2965(self):
1552 # Test handling of local intranet hostnames without a dot.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001553 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1554 interact_2965(c, "http://example/",
1555 "foo1=bar; PORT; Discard; Version=1;")
1556 cookie = interact_2965(c, "http://example/",
1557 'foo2=bar; domain=".local"; Version=1')
Benjamin Peterson577473f2010-01-19 00:09:57 +00001558 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001559
1560 interact_2965(c, "http://example/", 'foo3=bar; Version=1')
1561 cookie = interact_2965(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001562 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001563 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001564
1565 def test_intranet_domains_ns(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001566 c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
1567 interact_netscape(c, "http://example/", "foo1=bar")
1568 cookie = interact_netscape(c, "http://example/",
1569 'foo2=bar; domain=.local')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001570 self.assertEqual(len(c), 2)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001571 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001572
1573 cookie = interact_netscape(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001574 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001575 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001576
1577 def test_empty_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001578 # Test for empty path
1579 # Broken web-server ORION/1.3.38 returns to the client response like
1580 #
1581 # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
1582 #
1583 # ie. with Path set to nothing.
1584 # In this case, extract_cookies() must set cookie to / (root)
1585 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1586 headers = []
1587
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001588 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001589 headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
1590 res = FakeResponse(headers, "http://www.ants.com/")
1591 c.extract_cookies(res, req)
1592
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001593 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001594 c.add_cookie_header(req)
1595
Ezio Melottib3aedd42010-11-20 19:04:17 +00001596 self.assertEqual(req.get_header("Cookie"),
1597 "JSESSIONID=ABCDERANDOM123")
1598 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001599
1600 # missing path in the request URI
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001601 req = urllib.request.Request("http://www.ants.com:8080")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001602 c.add_cookie_header(req)
1603
Ezio Melottib3aedd42010-11-20 19:04:17 +00001604 self.assertEqual(req.get_header("Cookie"),
1605 "JSESSIONID=ABCDERANDOM123")
1606 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001607
1608 def test_session_cookies(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001609 year_plus_one = time.localtime()[0] + 1
1610
1611 # Check session cookies are deleted properly by
1612 # CookieJar.clear_session_cookies method
1613
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001614 req = urllib.request.Request('http://www.perlmeister.com/scripts')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001615 headers = []
1616 headers.append("Set-Cookie: s1=session;Path=/scripts")
1617 headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"
1618 "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %
1619 year_plus_one)
1620 headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "
1621 "02-Feb-%d 23:24:20 GMT" % year_plus_one)
1622 headers.append("Set-Cookie: s2=session;Path=/scripts;"
1623 "Domain=.perlmeister.com")
1624 headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
1625 res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
1626
1627 c = CookieJar()
1628 c.extract_cookies(res, req)
1629 # How many session/permanent cookies do we have?
1630 counter = {"session_after": 0,
1631 "perm_after": 0,
1632 "session_before": 0,
1633 "perm_before": 0}
1634 for cookie in c:
1635 key = "%s_before" % cookie.value
1636 counter[key] = counter[key] + 1
1637 c.clear_session_cookies()
1638 # How many now?
1639 for cookie in c:
1640 key = "%s_after" % cookie.value
1641 counter[key] = counter[key] + 1
1642
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001643 self.assertTrue(not (
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001644 # a permanent cookie got lost accidently
1645 counter["perm_after"] != counter["perm_before"] or
1646 # a session cookie hasn't been cleared
1647 counter["session_after"] != 0 or
1648 # we didn't have session cookies in the first place
1649 counter["session_before"] == 0))
1650
1651
1652def test_main(verbose=None):
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001653 test.support.run_unittest(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001654 DateTimeTests,
1655 HeaderTests,
1656 CookieTests,
Martin v. Löwisc5574e82005-03-03 10:57:37 +00001657 FileCookieJarTests,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001658 LWPCookieTests,
1659 )
1660
1661if __name__ == "__main__":
1662 test_main(verbose=True)