blob: e9f0356050783238ac9bd304ccbcb288e54d9059 [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
Ezio Melotti1d237e52013-08-10 18:20:09 +030010from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +000011 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):
Serhiy Storchaka9d282f62013-11-17 13:45:02 +020031 self.assertRegex(text, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$",
32 "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)
Ezio Melotti7ac17f82013-08-10 18:07:25 +030059 '02-Feb-1994 24:00', # broken rfc850 (no weekday, no seconds,
60 # no tz) using hour 24 with yesterday date
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000061
62 '03-Feb-94', # old rfc850 HTTP format (no weekday, no time)
63 '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)
64 '03 Feb 1994', # proposed new HTTP format (no weekday, no time)
65
66 # A few tests with extra space at various places
67 ' 03 Feb 1994 0:00 ',
68 ' 03-Feb-1994 ',
69 ]
70
71 test_t = 760233600 # assume broken POSIX counting of seconds
72 result = time2isoz(test_t)
73 expected = "1994-02-03 00:00:00Z"
Ezio Melottib3aedd42010-11-20 19:04:17 +000074 self.assertEqual(result, expected,
75 "%s => '%s' (%s)" % (test_t, result, expected))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000076
77 for s in tests:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +020078 self.assertEqual(http2time(s), test_t, s)
79 self.assertEqual(http2time(s.lower()), test_t, s.lower())
80 self.assertEqual(http2time(s.upper()), test_t, s.upper())
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000081
82 def test_http2time_garbage(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000083 for test in [
84 '',
85 'Garbage',
86 'Mandag 16. September 1996',
87 '01-00-1980',
88 '01-13-1980',
89 '00-01-1980',
90 '32-01-1980',
91 '01-01-1980 25:00:00',
92 '01-01-1980 00:61:00',
93 '01-01-1980 00:00:62',
94 ]:
Ezio Melotti1d237e52013-08-10 18:20:09 +030095 self.assertIsNone(http2time(test),
96 "http2time(%s) is not None\n"
97 "http2time(test) %s" % (test, http2time(test)))
98
99 def test_iso2time(self):
100 def parse_date(text):
101 return time.gmtime(iso2time(text))[:6]
102
103 # ISO 8601 compact format
104 self.assertEqual(parse_date("19940203T141529Z"),
105 (1994, 2, 3, 14, 15, 29))
106
107 # ISO 8601 with time behind UTC
108 self.assertEqual(parse_date("1994-02-03 07:15:29 -0700"),
109 (1994, 2, 3, 14, 15, 29))
110
111 # ISO 8601 with time ahead of UTC
112 self.assertEqual(parse_date("1994-02-03 19:45:29 +0530"),
113 (1994, 2, 3, 14, 15, 29))
114
115 def test_iso2time_formats(self):
116 # test iso2time for supported dates.
117 tests = [
118 '1994-02-03 00:00:00 -0000', # ISO 8601 format
119 '1994-02-03 00:00:00 +0000', # ISO 8601 format
120 '1994-02-03 00:00:00', # zone is optional
121 '1994-02-03', # only date
122 '1994-02-03T00:00:00', # Use T as separator
123 '19940203', # only date
124 '1994-02-02 24:00:00', # using hour-24 yesterday date
125 '19940203T000000Z', # ISO 8601 compact format
126
127 # A few tests with extra space at various places
128 ' 1994-02-03 ',
129 ' 1994-02-03T00:00:00 ',
130 ]
131
132 test_t = 760233600 # assume broken POSIX counting of seconds
133 for s in tests:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200134 self.assertEqual(iso2time(s), test_t, s)
135 self.assertEqual(iso2time(s.lower()), test_t, s.lower())
136 self.assertEqual(iso2time(s.upper()), test_t, s.upper())
Ezio Melotti1d237e52013-08-10 18:20:09 +0300137
138 def test_iso2time_garbage(self):
139 for test in [
140 '',
141 'Garbage',
142 'Thursday, 03-Feb-94 00:00:00 GMT',
143 '1980-00-01',
144 '1980-13-01',
145 '1980-01-00',
146 '1980-01-32',
147 '1980-01-01 25:00:00',
148 '1980-01-01 00:61:00',
149 '01-01-1980 00:00:62',
150 '01-01-1980T00:00:62',
151 '19800101T250000Z'
152 '1980-01-01 00:00:00 -2500',
153 ]:
154 self.assertIsNone(iso2time(test),
155 "iso2time(%s) is not None\n"
156 "iso2time(test) %s" % (test, iso2time(test)))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000157
158
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000159class HeaderTests(unittest.TestCase):
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000160
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000161 def test_parse_ns_headers(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000162 # quotes should be stripped
Guido van Rossume2a383d2007-01-15 16:59:06 +0000163 expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]]
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000164 for hdr in [
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000165 'foo=bar; expires=01 Jan 2040 22:23:32 GMT',
166 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"',
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000167 ]:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000168 self.assertEqual(parse_ns_headers([hdr]), expected)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000169
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000170 def test_parse_ns_headers_version(self):
171
172 # quotes should be stripped
173 expected = [[('foo', 'bar'), ('version', '1')]]
174 for hdr in [
175 'foo=bar; version="1"',
176 'foo=bar; Version="1"',
177 ]:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000178 self.assertEqual(parse_ns_headers([hdr]), expected)
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000179
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000180 def test_parse_ns_headers_special_names(self):
181 # names such as 'expires' are not special in first name=value pair
182 # of Set-Cookie: header
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000183 # Cookie with name 'expires'
184 hdr = 'expires=01 Jan 2040 22:23:32 GMT'
185 expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000186 self.assertEqual(parse_ns_headers([hdr]), expected)
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000187
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000188 def test_join_header_words(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000189 joined = join_header_words([[("foo", None), ("bar", "baz")]])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000190 self.assertEqual(joined, "foo; bar=baz")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000191
Ezio Melottib3aedd42010-11-20 19:04:17 +0000192 self.assertEqual(join_header_words([[]]), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000193
194 def test_split_header_words(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000195 tests = [
196 ("foo", [[("foo", None)]]),
197 ("foo=bar", [[("foo", "bar")]]),
198 (" foo ", [[("foo", None)]]),
199 (" foo= ", [[("foo", "")]]),
200 (" foo=", [[("foo", "")]]),
201 (" foo= ; ", [[("foo", "")]]),
202 (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
203 ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
204 # doesn't really matter if this next fails, but it works ATM
205 ("foo= bar=baz", [[("foo", "bar=baz")]]),
206 ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
207 ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
208 ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
209 (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',
210 [[("foo", None), ("bar", "baz")],
211 [("spam", "")], [("foo", ',;"')], [("bar", "")]]),
212 ]
213
214 for arg, expect in tests:
215 try:
216 result = split_header_words([arg])
217 except:
Guido van Rossum34d19282007-08-09 01:03:29 +0000218 import traceback, io
219 f = io.StringIO()
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000220 traceback.print_exc(None, f)
221 result = "(error -- traceback follows)\n\n%s" % f.getvalue()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000222 self.assertEqual(result, expect, """
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000223When parsing: '%s'
224Expected: '%s'
225Got: '%s'
226""" % (arg, expect, result))
227
228 def test_roundtrip(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000229 tests = [
230 ("foo", "foo"),
231 ("foo=bar", "foo=bar"),
232 (" foo ", "foo"),
233 ("foo=", 'foo=""'),
234 ("foo=bar bar=baz", "foo=bar; bar=baz"),
235 ("foo=bar;bar=baz", "foo=bar; bar=baz"),
236 ('foo bar baz', "foo; bar; baz"),
237 (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
238 ('foo,,,bar', 'foo, bar'),
239 ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
240
241 ('text/html; charset=iso-8859-1',
242 'text/html; charset="iso-8859-1"'),
243
244 ('foo="bar"; port="80,81"; discard, bar=baz',
245 'foo=bar; port="80,81"; discard, bar=baz'),
246
247 (r'Basic realm="\"foo\\\\bar\""',
248 r'Basic; realm="\"foo\\\\bar\""')
249 ]
250
251 for arg, expect in tests:
252 input = split_header_words([arg])
253 res = join_header_words(input)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000254 self.assertEqual(res, expect, """
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000255When parsing: '%s'
256Expected: '%s'
257Got: '%s'
258Input was: '%s'
259""" % (arg, expect, res, input))
260
261
262class FakeResponse:
263 def __init__(self, headers=[], url=None):
264 """
265 headers: list of RFC822-style 'Key: value' strings
266 """
Barry Warsaw820c1202008-06-12 04:06:45 +0000267 import email
268 self._headers = email.message_from_string("\n".join(headers))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000269 self._url = url
270 def info(self): return self._headers
271
272def interact_2965(cookiejar, url, *set_cookie_hdrs):
273 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2")
274
275def interact_netscape(cookiejar, url, *set_cookie_hdrs):
276 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")
277
278def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
279 """Perform a single request / response cycle, returning Cookie: header."""
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000280 req = urllib.request.Request(url)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000281 cookiejar.add_cookie_header(req)
282 cookie_hdr = req.get_header("Cookie", "")
283 headers = []
284 for hdr in set_cookie_hdrs:
285 headers.append("%s: %s" % (hdr_name, hdr))
286 res = FakeResponse(headers, url)
287 cookiejar.extract_cookies(res, req)
288 return cookie_hdr
289
290
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000291class FileCookieJarTests(unittest.TestCase):
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000292 def test_lwp_valueless_cookie(self):
293 # cookies with no value should be saved and loaded consistently
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000294 filename = test.support.TESTFN
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000295 c = LWPCookieJar()
296 interact_netscape(c, "http://www.acme.com/", 'boo')
297 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
298 try:
299 c.save(filename, ignore_discard=True)
300 c = LWPCookieJar()
301 c.load(filename, ignore_discard=True)
302 finally:
303 try: os.unlink(filename)
304 except OSError: pass
305 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
306
Neal Norwitz3e7de592005-12-23 21:24:35 +0000307 def test_bad_magic(self):
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200308 # OSErrors (eg. file doesn't exist) are allowed to propagate
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000309 filename = test.support.TESTFN
Neal Norwitz3e7de592005-12-23 21:24:35 +0000310 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
311 c = cookiejar_class()
312 try:
313 c.load(filename="for this test to work, a file with this "
314 "filename should not exist")
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200315 except OSError as exc:
316 # an OSError subclass (likely FileNotFoundError), but not
317 # LoadError
318 self.assertIsNot(exc.__class__, LoadError)
Neal Norwitz3e7de592005-12-23 21:24:35 +0000319 else:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200320 self.fail("expected OSError for invalid filename")
Neal Norwitz3e7de592005-12-23 21:24:35 +0000321 # Invalid contents of cookies file (eg. bad magic string)
322 # causes a LoadError.
323 try:
Brett Cannon7f462fc2010-10-29 23:27:39 +0000324 with open(filename, "w") as f:
325 f.write("oops\n")
326 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
327 c = cookiejar_class()
328 self.assertRaises(LoadError, c.load, filename)
Neal Norwitz3e7de592005-12-23 21:24:35 +0000329 finally:
330 try: os.unlink(filename)
331 except OSError: pass
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000332
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000333class CookieTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000334 # XXX
335 # Get rid of string comparisons where not actually testing str / repr.
336 # .clear() etc.
337 # IP addresses like 50 (single number, no dot) and domain-matching
338 # functions (and is_HDN)? See draft RFC 2965 errata.
339 # Strictness switches
340 # is_third_party()
341 # unverifiability / third-party blocking
342 # Netscape cookies work the same as RFC 2965 with regard to port.
343 # Set-Cookie with negative max age.
344 # If turn RFC 2965 handling off, Set-Cookie2 cookies should not clobber
345 # Set-Cookie cookies.
346 # Cookie2 should be sent if *any* cookies are not V1 (ie. V0 OR V2 etc.).
347 # Cookies (V1 and V0) with no expiry date should be set to be discarded.
348 # RFC 2965 Quoting:
349 # Should accept unquoted cookie-attribute values? check errata draft.
350 # Which are required on the way in and out?
351 # Should always return quoted cookie-attribute values?
352 # Proper testing of when RFC 2965 clobbers Netscape (waiting for errata).
353 # Path-match on return (same for V0 and V1).
354 # RFC 2965 acceptance and returning rules
355 # Set-Cookie2 without version attribute is rejected.
356
357 # Netscape peculiarities list from Ronald Tschalar.
358 # The first two still need tests, the rest are covered.
359## - Quoting: only quotes around the expires value are recognized as such
360## (and yes, some folks quote the expires value); quotes around any other
361## value are treated as part of the value.
362## - White space: white space around names and values is ignored
363## - Default path: if no path parameter is given, the path defaults to the
364## path in the request-uri up to, but not including, the last '/'. Note
365## that this is entirely different from what the spec says.
366## - Commas and other delimiters: Netscape just parses until the next ';'.
367## This means it will allow commas etc inside values (and yes, both
368## commas and equals are commonly appear in the cookie value). This also
369## means that if you fold multiple Set-Cookie header fields into one,
370## comma-separated list, it'll be a headache to parse (at least my head
Ezio Melotti85a86292013-08-17 16:57:41 +0300371## starts hurting every time I think of that code).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000372## - Expires: You'll get all sorts of date formats in the expires,
373## including emtpy expires attributes ("expires="). Be as flexible as you
374## can, and certainly don't expect the weekday to be there; if you can't
375## parse it, just ignore it and pretend it's a session cookie.
376## - Domain-matching: Netscape uses the 2-dot rule for _all_ domains, not
377## just the 7 special TLD's listed in their spec. And folks rely on
378## that...
379
380 def test_domain_return_ok(self):
381 # test optimization: .domain_return_ok() should filter out most
382 # domains in the CookieJar before we try to access them (because that
383 # may require disk access -- in particular, with MSIECookieJar)
384 # This is only a rough check for performance reasons, so it's not too
385 # critical as long as it's sufficiently liberal.
Georg Brandl24420152008-05-26 16:32:26 +0000386 pol = DefaultCookiePolicy()
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000387 for url, domain, ok in [
388 ("http://foo.bar.com/", "blah.com", False),
389 ("http://foo.bar.com/", "rhubarb.blah.com", False),
390 ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),
391 ("http://foo.bar.com/", ".foo.bar.com", True),
392 ("http://foo.bar.com/", "foo.bar.com", True),
393 ("http://foo.bar.com/", ".bar.com", True),
394 ("http://foo.bar.com/", "com", True),
395 ("http://foo.com/", "rhubarb.foo.com", False),
396 ("http://foo.com/", ".foo.com", True),
397 ("http://foo.com/", "foo.com", True),
398 ("http://foo.com/", "com", True),
399 ("http://foo/", "rhubarb.foo", False),
400 ("http://foo/", ".foo", True),
401 ("http://foo/", "foo", True),
402 ("http://foo/", "foo.local", True),
403 ("http://foo/", ".local", True),
404 ]:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000405 request = urllib.request.Request(url)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000406 r = pol.domain_return_ok(domain, request)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000407 if ok: self.assertTrue(r)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200408 else: self.assertFalse(r)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000409
410 def test_missing_value(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000411 # missing = sign in Cookie: header is regarded by Mozilla as a missing
Georg Brandl24420152008-05-26 16:32:26 +0000412 # name, and by http.cookiejar as a missing value
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000413 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000414 c = MozillaCookieJar(filename)
415 interact_netscape(c, "http://www.acme.com/", 'eggs')
416 interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
417 cookie = c._cookies["www.acme.com"]["/"]["eggs"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200418 self.assertIsNone(cookie.value)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000419 self.assertEqual(cookie.name, "eggs")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000420 cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200421 self.assertIsNone(cookie.value)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000422 self.assertEqual(cookie.name, '"spam"')
423 self.assertEqual(lwp_cookie_str(cookie), (
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000424 r'"spam"; path="/foo/"; domain="www.acme.com"; '
425 'path_spec; discard; version=0'))
426 old_str = repr(c)
427 c.save(ignore_expires=True, ignore_discard=True)
428 try:
429 c = MozillaCookieJar(filename)
430 c.revert(ignore_expires=True, ignore_discard=True)
431 finally:
432 os.unlink(c.filename)
433 # cookies unchanged apart from lost info re. whether path was specified
Ezio Melottib3aedd42010-11-20 19:04:17 +0000434 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000435 repr(c),
436 re.sub("path_specified=%s" % True, "path_specified=%s" % False,
437 old_str)
438 )
Ezio Melottib3aedd42010-11-20 19:04:17 +0000439 self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
440 '"spam"; eggs')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000441
Neal Norwitz71dad722005-12-23 21:43:48 +0000442 def test_rfc2109_handling(self):
443 # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
444 # dependent on policy settings
Neal Norwitz71dad722005-12-23 21:43:48 +0000445 for rfc2109_as_netscape, rfc2965, version in [
446 # default according to rfc2965 if not explicitly specified
447 (None, False, 0),
448 (None, True, 1),
449 # explicit rfc2109_as_netscape
450 (False, False, None), # version None here means no cookie stored
451 (False, True, 1),
452 (True, False, 0),
453 (True, True, 0),
454 ]:
455 policy = DefaultCookiePolicy(
456 rfc2109_as_netscape=rfc2109_as_netscape,
457 rfc2965=rfc2965)
458 c = CookieJar(policy)
459 interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")
460 try:
461 cookie = c._cookies["www.example.com"]["/"]["ni"]
462 except KeyError:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200463 self.assertIsNone(version) # didn't expect a stored cookie
Neal Norwitz71dad722005-12-23 21:43:48 +0000464 else:
465 self.assertEqual(cookie.version, version)
466 # 2965 cookies are unaffected
467 interact_2965(c, "http://www.example.com/",
468 "foo=bar; Version=1")
469 if rfc2965:
470 cookie2965 = c._cookies["www.example.com"]["/"]["foo"]
471 self.assertEqual(cookie2965.version, 1)
472
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000473 def test_ns_parser(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000474 c = CookieJar()
475 interact_netscape(c, "http://www.acme.com/",
476 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')
477 interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080')
478 interact_netscape(c, "http://www.acme.com:80/", 'nini=ni')
479 interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=')
480 interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; '
481 'expires="Foo Bar 25 33:22:11 3022"')
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +0200482 interact_netscape(c, 'http://www.acme.com/', 'fortytwo=')
483 interact_netscape(c, 'http://www.acme.com/', '=unladenswallow')
484 interact_netscape(c, 'http://www.acme.com/', 'holyhandgrenade')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000485
486 cookie = c._cookies[".acme.com"]["/"]["spam"]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000487 self.assertEqual(cookie.domain, ".acme.com")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000488 self.assertTrue(cookie.domain_specified)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000489 self.assertEqual(cookie.port, DEFAULT_HTTP_PORT)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200490 self.assertFalse(cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000491 # case is preserved
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200492 self.assertTrue(cookie.has_nonstandard_attr("blArgh"))
493 self.assertFalse(cookie.has_nonstandard_attr("blargh"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000494
495 cookie = c._cookies["www.acme.com"]["/"]["ni"]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000496 self.assertEqual(cookie.domain, "www.acme.com")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200497 self.assertFalse(cookie.domain_specified)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000498 self.assertEqual(cookie.port, "80,8080")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000499 self.assertTrue(cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000500
501 cookie = c._cookies["www.acme.com"]["/"]["nini"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200502 self.assertIsNone(cookie.port)
503 self.assertFalse(cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000504
505 # invalid expires should not cause cookie to be dropped
506 foo = c._cookies["www.acme.com"]["/"]["foo"]
507 spam = c._cookies["www.acme.com"]["/"]["foo"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200508 self.assertIsNone(foo.expires)
509 self.assertIsNone(spam.expires)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000510
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +0200511 cookie = c._cookies['www.acme.com']['/']['fortytwo']
512 self.assertIsNotNone(cookie.value)
513 self.assertEqual(cookie.value, '')
514
515 # there should be a distinction between a present but empty value
516 # (above) and a value that's entirely missing (below)
517
518 cookie = c._cookies['www.acme.com']['/']['holyhandgrenade']
519 self.assertIsNone(cookie.value)
520
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000521 def test_ns_parser_special_names(self):
522 # names such as 'expires' are not special in first name=value pair
523 # of Set-Cookie: header
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000524 c = CookieJar()
525 interact_netscape(c, "http://www.acme.com/", 'expires=eggs')
526 interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')
527
528 cookies = c._cookies["www.acme.com"]["/"]
Benjamin Peterson577473f2010-01-19 00:09:57 +0000529 self.assertIn('expires', cookies)
530 self.assertIn('version', cookies)
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000531
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000532 def test_expires(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000533 # if expires is in future, keep cookie...
534 c = CookieJar()
535 future = time2netscape(time.time()+3600)
536 interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
537 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000538 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000539 now = time2netscape(time.time()-1)
540 # ... and if in past or present, discard it
541 interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %
542 now)
543 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000544 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000545 self.assertIn('spam="bar"', h)
546 self.assertNotIn("foo", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000547
548 # max-age takes precedence over expires, and zero max-age is request to
549 # delete both new cookie and any old matching cookie
550 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %
551 future)
552 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %
553 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000554 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000555 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '
556 'expires=%s; max-age=0' % future)
557 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '
558 'max-age=0; expires=%s' % future)
559 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000560 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000561
562 # test expiry at end of session for cookies with no expires attribute
563 interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000564 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000565 c.clear_session_cookies()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000566 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000567 self.assertIn('spam="bar"', h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000568
569 # XXX RFC 2965 expiry rules (some apply to V0 too)
570
571 def test_default_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000572 # RFC 2965
573 pol = DefaultCookiePolicy(rfc2965=True)
574
575 c = CookieJar(pol)
576 interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000577 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000578
579 c = CookieJar(pol)
580 interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000581 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000582
583 c = CookieJar(pol)
584 interact_2965(c, "http://www.acme.com/blah/rhubarb",
585 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000586 self.assertIn("/blah/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000587
588 c = CookieJar(pol)
589 interact_2965(c, "http://www.acme.com/blah/rhubarb/",
590 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000591 self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000592
593 # Netscape
594
595 c = CookieJar()
596 interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000597 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000598
599 c = CookieJar()
600 interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000601 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000602
603 c = CookieJar()
604 interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000605 self.assertIn("/blah", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000606
607 c = CookieJar()
608 interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000609 self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000610
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000611 def test_default_path_with_query(self):
612 cj = CookieJar()
613 uri = "http://example.com/?spam/eggs"
614 value = 'eggs="bar"'
615 interact_netscape(cj, uri, value)
616 # Default path does not include query, so is "/", not "/?spam".
617 self.assertIn("/", cj._cookies["example.com"])
618 # Cookie is sent back to the same URI.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000619 self.assertEqual(interact_netscape(cj, uri), value)
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000620
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000621 def test_escape_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000622 cases = [
623 # quoted safe
624 ("/foo%2f/bar", "/foo%2F/bar"),
625 ("/foo%2F/bar", "/foo%2F/bar"),
626 # quoted %
627 ("/foo%%/bar", "/foo%%/bar"),
628 # quoted unsafe
629 ("/fo%19o/bar", "/fo%19o/bar"),
630 ("/fo%7do/bar", "/fo%7Do/bar"),
631 # unquoted safe
632 ("/foo/bar&", "/foo/bar&"),
633 ("/foo//bar", "/foo//bar"),
634 ("\176/foo/bar", "\176/foo/bar"),
635 # unquoted unsafe
636 ("/foo\031/bar", "/foo%19/bar"),
637 ("/\175foo/bar", "/%7Dfoo/bar"),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000638 # unicode, latin-1 range
639 ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000640 # unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000641 ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000642 ]
643 for arg, result in cases:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000644 self.assertEqual(escape_path(arg), result)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000645
646 def test_request_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000647 # with parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000648 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000649 "http://www.example.com/rheum/rhaponticum;"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000650 "foo=bar;sing=song?apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000651 self.assertEqual(request_path(req),
652 "/rheum/rhaponticum;foo=bar;sing=song")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000653 # without parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000654 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000655 "http://www.example.com/rheum/rhaponticum?"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000656 "apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000657 self.assertEqual(request_path(req), "/rheum/rhaponticum")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000658 # missing final slash
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000659 req = urllib.request.Request("http://www.example.com")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000660 self.assertEqual(request_path(req), "/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000661
662 def test_request_port(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000663 req = urllib.request.Request("http://www.acme.com:1234/",
664 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000665 self.assertEqual(request_port(req), "1234")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000666 req = urllib.request.Request("http://www.acme.com/",
667 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000668 self.assertEqual(request_port(req), DEFAULT_HTTP_PORT)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000669
670 def test_request_host(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000671 # this request is illegal (RFC2616, 14.2.3)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000672 req = urllib.request.Request("http://1.1.1.1/",
673 headers={"Host": "www.acme.com:80"})
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000674 # libwww-perl wants this response, but that seems wrong (RFC 2616,
675 # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000676 #self.assertEqual(request_host(req), "www.acme.com")
677 self.assertEqual(request_host(req), "1.1.1.1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000678 req = urllib.request.Request("http://www.acme.com/",
679 headers={"Host": "irrelevant.com"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000680 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000681 # port shouldn't be in request-host
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000682 req = urllib.request.Request("http://www.acme.com:2345/resource.html",
683 headers={"Host": "www.acme.com:5432"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000684 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000685
686 def test_is_HDN(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000687 self.assertTrue(is_HDN("foo.bar.com"))
688 self.assertTrue(is_HDN("1foo2.3bar4.5com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200689 self.assertFalse(is_HDN("192.168.1.1"))
690 self.assertFalse(is_HDN(""))
691 self.assertFalse(is_HDN("."))
692 self.assertFalse(is_HDN(".foo.bar.com"))
693 self.assertFalse(is_HDN("..foo"))
694 self.assertFalse(is_HDN("foo."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000695
696 def test_reach(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000697 self.assertEqual(reach("www.acme.com"), ".acme.com")
698 self.assertEqual(reach("acme.com"), "acme.com")
699 self.assertEqual(reach("acme.local"), ".local")
700 self.assertEqual(reach(".local"), ".local")
701 self.assertEqual(reach(".com"), ".com")
702 self.assertEqual(reach("."), ".")
703 self.assertEqual(reach(""), "")
704 self.assertEqual(reach("192.168.0.1"), "192.168.0.1")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000705
706 def test_domain_match(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000707 self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200708 self.assertFalse(domain_match("192.168.1.1", ".168.1.1"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000709 self.assertTrue(domain_match("x.y.com", "x.Y.com"))
710 self.assertTrue(domain_match("x.y.com", ".Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200711 self.assertFalse(domain_match("x.y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000712 self.assertTrue(domain_match("a.b.c.com", ".c.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200713 self.assertFalse(domain_match(".c.com", "a.b.c.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000714 self.assertTrue(domain_match("example.local", ".local"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200715 self.assertFalse(domain_match("blah.blah", ""))
716 self.assertFalse(domain_match("", ".rhubarb.rhubarb"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000717 self.assertTrue(domain_match("", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000718
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000719 self.assertTrue(user_domain_match("acme.com", "acme.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200720 self.assertFalse(user_domain_match("acme.com", ".acme.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000721 self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))
722 self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
723 self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))
724 self.assertTrue(user_domain_match("x.y.com", ".Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200725 self.assertFalse(user_domain_match("x.y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000726 self.assertTrue(user_domain_match("y.com", "Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200727 self.assertFalse(user_domain_match(".y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000728 self.assertTrue(user_domain_match(".y.com", ".Y.com"))
729 self.assertTrue(user_domain_match("x.y.com", ".com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200730 self.assertFalse(user_domain_match("x.y.com", "com"))
731 self.assertFalse(user_domain_match("x.y.com", "m"))
732 self.assertFalse(user_domain_match("x.y.com", ".m"))
733 self.assertFalse(user_domain_match("x.y.com", ""))
734 self.assertFalse(user_domain_match("x.y.com", "."))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000735 self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000736 # not both HDNs, so must string-compare equal to match
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200737 self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1"))
738 self.assertFalse(user_domain_match("192.168.1.1", "."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000739 # empty string is a special case
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200740 self.assertFalse(user_domain_match("192.168.1.1", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000741
742 def test_wrong_domain(self):
743 # Cookies whose effective request-host name does not domain-match the
744 # domain are rejected.
745
746 # XXX far from complete
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000747 c = CookieJar()
748 interact_2965(c, "http://www.nasty.com/",
749 'foo=bar; domain=friendly.org; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000750 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000751
Thomas Wouters477c8d52006-05-27 19:21:47 +0000752 def test_strict_domain(self):
753 # Cookies whose domain is a country-code tld like .co.uk should
754 # not be set if CookiePolicy.strict_domain is true.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755 cp = DefaultCookiePolicy(strict_domain=True)
756 cj = CookieJar(policy=cp)
757 interact_netscape(cj, "http://example.co.uk/", 'no=problemo')
758 interact_netscape(cj, "http://example.co.uk/",
759 'okey=dokey; Domain=.example.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000760 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000761 for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:
762 interact_netscape(cj, "http://example.%s/" % pseudo_tld,
763 'spam=eggs; Domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000764 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000765
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000766 def test_two_component_domain_ns(self):
767 # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain
768 # should all get accepted, as should .acme.com, acme.com and no domain
769 # for 2-component domains like acme.com.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000770 c = CookieJar()
771
772 # two-component V0 domain is OK
773 interact_netscape(c, "http://foo.net/", 'ns=bar')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000774 self.assertEqual(len(c), 1)
775 self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar")
776 self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000777 # *will* be returned to any other domain (unlike RFC 2965)...
Ezio Melottib3aedd42010-11-20 19:04:17 +0000778 self.assertEqual(interact_netscape(c, "http://www.foo.net/"),
779 "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000780 # ...unless requested otherwise
781 pol = DefaultCookiePolicy(
782 strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
783 c.set_policy(pol)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000784 self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000785
786 # unlike RFC 2965, even explicit two-component domain is OK,
787 # because .foo.net matches foo.net
788 interact_netscape(c, "http://foo.net/foo/",
789 'spam1=eggs; domain=foo.net')
790 # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
791 interact_netscape(c, "http://foo.net/foo/bar/",
792 'spam2=eggs; domain=.foo.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000793 self.assertEqual(len(c), 3)
794 self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value,
795 "eggs")
796 self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,
797 "eggs")
798 self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"),
799 "spam2=eggs; spam1=eggs; ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000800
801 # top-level domain is too general
802 interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000803 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000804
805## # Netscape protocol doesn't allow non-special top level domains (such
806## # as co.uk) in the domain attribute unless there are at least three
807## # dots in it.
808 # Oh yes it does! Real implementations don't check this, and real
809 # cookies (of course) rely on that behaviour.
810 interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000811## self.assertEqual(len(c), 2)
812 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000813
814 def test_two_component_domain_rfc2965(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000815 pol = DefaultCookiePolicy(rfc2965=True)
816 c = CookieJar(pol)
817
818 # two-component V1 domain is OK
819 interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000820 self.assertEqual(len(c), 1)
821 self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar")
822 self.assertEqual(interact_2965(c, "http://foo.net/"),
823 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000824 # won't be returned to any other domain (because domain was implied)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000825 self.assertEqual(interact_2965(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000826
827 # unless domain is given explicitly, because then it must be
828 # rewritten to start with a dot: foo.net --> .foo.net, which does
829 # not domain-match foo.net
830 interact_2965(c, "http://foo.net/foo",
831 'spam=eggs; domain=foo.net; path=/foo; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000832 self.assertEqual(len(c), 1)
833 self.assertEqual(interact_2965(c, "http://foo.net/foo"),
834 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000835
836 # explicit foo.net from three-component domain www.foo.net *does* get
837 # set, because .foo.net domain-matches .foo.net
838 interact_2965(c, "http://www.foo.net/foo/",
839 'spam=eggs; domain=foo.net; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000840 self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value,
841 "eggs")
842 self.assertEqual(len(c), 2)
843 self.assertEqual(interact_2965(c, "http://foo.net/foo/"),
844 "$Version=1; foo=bar")
845 self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"),
846 '$Version=1; spam=eggs; $Domain="foo.net"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000847
848 # top-level domain is too general
849 interact_2965(c, "http://foo.net/",
850 'ni="ni"; domain=".net"; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000851 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000852
853 # RFC 2965 doesn't require blocking this
854 interact_2965(c, "http://foo.co.uk/",
855 'nasty=trick; domain=.co.uk; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000856 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000857
858 def test_domain_allow(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000859 c = CookieJar(policy=DefaultCookiePolicy(
860 blocked_domains=["acme.com"],
861 allowed_domains=["www.acme.com"]))
862
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000863 req = urllib.request.Request("http://acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000864 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
865 res = FakeResponse(headers, "http://acme.com/")
866 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000867 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000868
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000869 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000870 res = FakeResponse(headers, "http://www.acme.com/")
871 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000872 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000873
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000874 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000875 res = FakeResponse(headers, "http://www.coyote.com/")
876 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000877 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000878
879 # set a cookie with non-allowed domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000880 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000881 res = FakeResponse(headers, "http://www.coyote.com/")
882 cookies = c.make_cookies(res, req)
883 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000884 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000885 # ... and check is doesn't get returned
886 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200887 self.assertFalse(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000888
889 def test_domain_block(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000890 pol = DefaultCookiePolicy(
891 rfc2965=True, blocked_domains=[".acme.com"])
892 c = CookieJar(policy=pol)
893 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
894
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000895 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000896 res = FakeResponse(headers, "http://www.acme.com/")
897 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000898 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000899
900 p = pol.set_blocked_domains(["acme.com"])
901 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000902 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000903
904 c.clear()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000905 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000906 res = FakeResponse(headers, "http://www.roadrunner.net/")
907 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000908 self.assertEqual(len(c), 1)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000909 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000910 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200911 self.assertTrue(req.has_header("Cookie"))
912 self.assertTrue(req.has_header("Cookie2"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000913
914 c.clear()
915 pol.set_blocked_domains([".acme.com"])
916 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000917 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000918
919 # set a cookie with blocked domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000920 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000921 res = FakeResponse(headers, "http://www.acme.com/")
922 cookies = c.make_cookies(res, req)
923 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000924 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000925 # ... and check is doesn't get returned
926 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200927 self.assertFalse(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000928
929 def test_secure(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000930 for ns in True, False:
931 for whitespace in " ", "":
932 c = CookieJar()
933 if ns:
934 pol = DefaultCookiePolicy(rfc2965=False)
935 int = interact_netscape
936 vs = ""
937 else:
938 pol = DefaultCookiePolicy(rfc2965=True)
939 int = interact_2965
940 vs = "; Version=1"
941 c.set_policy(pol)
942 url = "http://www.acme.com/"
943 int(c, url, "foo1=bar%s%s" % (vs, whitespace))
944 int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200945 self.assertFalse(
946 c._cookies["www.acme.com"]["/"]["foo1"].secure,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000947 "non-secure cookie registered secure")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000948 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000949 c._cookies["www.acme.com"]["/"]["foo2"].secure,
950 "secure cookie registered non-secure")
951
952 def test_quote_cookie_value(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000953 c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))
954 interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')
955 h = interact_2965(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000956 self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000957
958 def test_missing_final_slash(self):
959 # Missing slash from request URL's abs_path should be assumed present.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000960 url = "http://www.acme.com"
961 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
962 interact_2965(c, url, "foo=bar; Version=1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000963 req = urllib.request.Request(url)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000964 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000965 c.add_cookie_header(req)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000966 self.assertTrue(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000967
968 def test_domain_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000969 pol = DefaultCookiePolicy(rfc2965=True)
970
971 c = CookieJar(pol)
972 url = "http://foo.bar.com/"
973 interact_2965(c, url, "spam=eggs; Version=1")
974 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000975 self.assertNotIn("Domain", h,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000976 "absent domain returned with domain present")
977
978 c = CookieJar(pol)
979 url = "http://foo.bar.com/"
980 interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
981 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000982 self.assertIn('$Domain=".bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000983
984 c = CookieJar(pol)
985 url = "http://foo.bar.com/"
986 # note missing initial dot in Domain
987 interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
988 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000989 self.assertIn('$Domain="bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000990
991 def test_path_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000992 pol = DefaultCookiePolicy(rfc2965=True)
993
994 c = CookieJar(pol)
995 url = "http://foo.bar.com/"
996 interact_2965(c, url, "spam=eggs; Version=1")
997 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000998 self.assertNotIn("Path", h, "absent path returned with path present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000999
1000 c = CookieJar(pol)
1001 url = "http://foo.bar.com/"
1002 interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
1003 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001004 self.assertIn('$Path="/"', h, "path not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001005
1006 def test_port_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001007 pol = DefaultCookiePolicy(rfc2965=True)
1008
1009 c = CookieJar(pol)
1010 url = "http://foo.bar.com/"
1011 interact_2965(c, url, "spam=eggs; Version=1")
1012 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001013 self.assertNotIn("Port", h, "absent port returned with port present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001014
1015 c = CookieJar(pol)
1016 url = "http://foo.bar.com/"
1017 interact_2965(c, url, "spam=eggs; Version=1; Port")
1018 h = interact_2965(c, url)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001019 self.assertRegex(h, "\$Port([^=]|$)",
1020 "port with no value not returned with no value")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001021
1022 c = CookieJar(pol)
1023 url = "http://foo.bar.com/"
1024 interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
1025 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001026 self.assertIn('$Port="80"', h,
1027 "port with single value not returned with single value")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001028
1029 c = CookieJar(pol)
1030 url = "http://foo.bar.com/"
1031 interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
1032 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001033 self.assertIn('$Port="80,8080"', h,
1034 "port with multiple values not returned with multiple "
1035 "values")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001036
1037 def test_no_return_comment(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001038 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1039 url = "http://foo.bar.com/"
1040 interact_2965(c, url, 'spam=eggs; Version=1; '
1041 'Comment="does anybody read these?"; '
1042 'CommentURL="http://foo.bar.net/comment.html"')
1043 h = interact_2965(c, url)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001044 self.assertNotIn("Comment", h,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001045 "Comment or CommentURL cookie-attributes returned to server")
1046
1047 def test_Cookie_iterator(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001048 cs = CookieJar(DefaultCookiePolicy(rfc2965=True))
1049 # add some random cookies
1050 interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '
1051 'Comment="does anybody read these?"; '
1052 'CommentURL="http://foo.bar.net/comment.html"')
1053 interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")
1054 interact_2965(cs, "http://www.acme.com/blah/",
1055 "foo=bar; secure; Version=1")
1056 interact_2965(cs, "http://www.acme.com/blah/",
1057 "foo=bar; path=/; Version=1")
1058 interact_2965(cs, "http://www.sol.no",
1059 r'bang=wallop; version=1; domain=".sol.no"; '
1060 r'port="90,100, 80,8080"; '
1061 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
1062
1063 versions = [1, 1, 1, 0, 1]
1064 names = ["bang", "foo", "foo", "spam", "foo"]
1065 domains = [".sol.no", "blah.spam.org", "www.acme.com",
1066 "www.acme.com", "www.acme.com"]
1067 paths = ["/", "/", "/", "/blah", "/blah/"]
1068
1069 for i in range(4):
1070 i = 0
1071 for c in cs:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001072 self.assertIsInstance(c, Cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001073 self.assertEqual(c.version, versions[i])
1074 self.assertEqual(c.name, names[i])
1075 self.assertEqual(c.domain, domains[i])
1076 self.assertEqual(c.path, paths[i])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001077 i = i + 1
1078
1079 def test_parse_ns_headers(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001080 # missing domain value (invalid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001081 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001082 parse_ns_headers(["foo=bar; path=/; domain"]),
1083 [[("foo", "bar"),
1084 ("path", "/"), ("domain", None), ("version", "0")]]
1085 )
1086 # invalid expires value
Ezio Melottib3aedd42010-11-20 19:04:17 +00001087 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001088 parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),
1089 [[("foo", "bar"), ("expires", None), ("version", "0")]]
1090 )
1091 # missing cookie value (valid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001092 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001093 parse_ns_headers(["foo"]),
1094 [[("foo", None), ("version", "0")]]
1095 )
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001096 # missing cookie values for parsed attributes
1097 self.assertEqual(
1098 parse_ns_headers(['foo=bar; expires']),
1099 [[('foo', 'bar'), ('expires', None), ('version', '0')]])
1100 self.assertEqual(
1101 parse_ns_headers(['foo=bar; version']),
1102 [[('foo', 'bar'), ('version', None)]])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001103 # shouldn't add version if header is empty
Ezio Melottib3aedd42010-11-20 19:04:17 +00001104 self.assertEqual(parse_ns_headers([""]), [])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001105
1106 def test_bad_cookie_header(self):
1107
1108 def cookiejar_from_cookie_headers(headers):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001109 c = CookieJar()
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001110 req = urllib.request.Request("http://www.example.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001111 r = FakeResponse(headers, "http://www.example.com/")
1112 c.extract_cookies(r, req)
1113 return c
1114
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001115 future = time2netscape(time.time()+3600)
1116
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001117 # none of these bad headers should cause an exception to be raised
1118 for headers in [
1119 ["Set-Cookie: "], # actually, nothing wrong with this
1120 ["Set-Cookie2: "], # ditto
1121 # missing domain value
1122 ["Set-Cookie2: a=foo; path=/; Version=1; domain"],
1123 # bad max-age
1124 ["Set-Cookie: b=foo; max-age=oops"],
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +00001125 # bad version
1126 ["Set-Cookie: b=foo; version=spam"],
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001127 ["Set-Cookie:; Expires=%s" % future],
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001128 ]:
1129 c = cookiejar_from_cookie_headers(headers)
1130 # these bad cookies shouldn't be set
Ezio Melottib3aedd42010-11-20 19:04:17 +00001131 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001132
1133 # cookie with invalid expires is treated as session cookie
1134 headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
1135 c = cookiejar_from_cookie_headers(headers)
1136 cookie = c._cookies["www.example.com"]["/"]["c"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001137 self.assertIsNone(cookie.expires)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001138
1139
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001140class LWPCookieTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001141 # Tests taken from libwww-perl, with a few modifications and additions.
1142
1143 def test_netscape_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001144 #-------------------------------------------------------------------
1145 # First we check that it works for the original example at
1146 # http://www.netscape.com/newsref/std/cookie_spec.html
1147
1148 # Client requests a document, and receives in the response:
1149 #
1150 # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT
1151 #
1152 # When client requests a URL in path "/" on this server, it sends:
1153 #
1154 # Cookie: CUSTOMER=WILE_E_COYOTE
1155 #
1156 # Client requests a document, and receives in the response:
1157 #
1158 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1159 #
1160 # When client requests a URL in path "/" on this server, it sends:
1161 #
1162 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1163 #
1164 # Client receives:
1165 #
1166 # Set-Cookie: SHIPPING=FEDEX; path=/fo
1167 #
1168 # When client requests a URL in path "/" on this server, it sends:
1169 #
1170 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1171 #
1172 # When client requests a URL in path "/foo" on this server, it sends:
1173 #
1174 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX
1175 #
1176 # The last Cookie is buggy, because both specifications say that the
1177 # most specific cookie must be sent first. SHIPPING=FEDEX is the
1178 # most specific and should thus be first.
1179
1180 year_plus_one = time.localtime()[0] + 1
1181
1182 headers = []
1183
1184 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1185
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001186 #req = urllib.request.Request("http://1.1.1.1/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001187 # headers={"Host": "www.acme.com:80"})
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001188 req = urllib.request.Request("http://www.acme.com:80/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001189 headers={"Host": "www.acme.com:80"})
1190
1191 headers.append(
1192 "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "
1193 "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)
1194 res = FakeResponse(headers, "http://www.acme.com/")
1195 c.extract_cookies(res, req)
1196
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001197 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001198 c.add_cookie_header(req)
1199
1200 self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")
1201 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
1202
1203 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1204 res = FakeResponse(headers, "http://www.acme.com/")
1205 c.extract_cookies(res, req)
1206
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001207 req = urllib.request.Request("http://www.acme.com/foo/bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001208 c.add_cookie_header(req)
1209
1210 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001211 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1212 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001213
1214 headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
1215 res = FakeResponse(headers, "http://www.acme.com")
1216 c.extract_cookies(res, req)
1217
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001218 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001219 c.add_cookie_header(req)
1220
1221 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001222 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1223 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1224 self.assertNotIn("SHIPPING=FEDEX", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001225
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001226 req = urllib.request.Request("http://www.acme.com/foo/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001227 c.add_cookie_header(req)
1228
1229 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001230 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1231 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1232 self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001233
1234 def test_netscape_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001235 # Second Example transaction sequence:
1236 #
1237 # Assume all mappings from above have been cleared.
1238 #
1239 # Client receives:
1240 #
1241 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1242 #
1243 # When client requests a URL in path "/" on this server, it sends:
1244 #
1245 # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001
1246 #
1247 # Client receives:
1248 #
1249 # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
1250 #
1251 # When client requests a URL in path "/ammo" on this server, it sends:
1252 #
1253 # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001
1254 #
1255 # NOTE: There are two name/value pairs named "PART_NUMBER" due to
1256 # the inheritance of the "/" mapping in addition to the "/ammo" mapping.
1257
1258 c = CookieJar()
1259 headers = []
1260
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001261 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001262 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1263 res = FakeResponse(headers, "http://www.acme.com/")
1264
1265 c.extract_cookies(res, req)
1266
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001267 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001268 c.add_cookie_header(req)
1269
Ezio Melottib3aedd42010-11-20 19:04:17 +00001270 self.assertEqual(req.get_header("Cookie"),
1271 "PART_NUMBER=ROCKET_LAUNCHER_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001272
1273 headers.append(
1274 "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")
1275 res = FakeResponse(headers, "http://www.acme.com/")
1276 c.extract_cookies(res, req)
1277
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001278 req = urllib.request.Request("http://www.acme.com/ammo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001279 c.add_cookie_header(req)
1280
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001281 self.assertRegex(req.get_header("Cookie"),
1282 r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
1283 "PART_NUMBER=ROCKET_LAUNCHER_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001284
1285 def test_ietf_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001286 #-------------------------------------------------------------------
1287 # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt
1288 #
1289 # 5. EXAMPLES
1290
1291 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1292
1293 #
1294 # 5.1 Example 1
1295 #
1296 # Most detail of request and response headers has been omitted. Assume
1297 # the user agent has no stored cookies.
1298 #
1299 # 1. User Agent -> Server
1300 #
1301 # POST /acme/login HTTP/1.1
1302 # [form data]
1303 #
1304 # User identifies self via a form.
1305 #
1306 # 2. Server -> User Agent
1307 #
1308 # HTTP/1.1 200 OK
1309 # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
1310 #
1311 # Cookie reflects user's identity.
1312
1313 cookie = interact_2965(
1314 c, 'http://www.acme.com/acme/login',
1315 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001316 self.assertFalse(cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001317
1318 #
1319 # 3. User Agent -> Server
1320 #
1321 # POST /acme/pickitem HTTP/1.1
1322 # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
1323 # [form data]
1324 #
1325 # User selects an item for ``shopping basket.''
1326 #
1327 # 4. Server -> User Agent
1328 #
1329 # HTTP/1.1 200 OK
1330 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1331 # Path="/acme"
1332 #
1333 # Shopping basket contains an item.
1334
1335 cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
1336 'Part_Number="Rocket_Launcher_0001"; '
1337 'Version="1"; Path="/acme"');
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001338 self.assertRegex(cookie,
1339 r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001340
1341 #
1342 # 5. User Agent -> Server
1343 #
1344 # POST /acme/shipping HTTP/1.1
1345 # Cookie: $Version="1";
1346 # Customer="WILE_E_COYOTE"; $Path="/acme";
1347 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1348 # [form data]
1349 #
1350 # User selects shipping method from form.
1351 #
1352 # 6. Server -> User Agent
1353 #
1354 # HTTP/1.1 200 OK
1355 # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"
1356 #
1357 # New cookie reflects shipping method.
1358
1359 cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
1360 'Shipping="FedEx"; Version="1"; Path="/acme"')
1361
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001362 self.assertRegex(cookie, r'^\$Version="?1"?;')
1363 self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;'
1364 '\s*\$Path="\/acme"')
1365 self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;'
1366 '\s*\$Path="\/acme"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001367
1368 #
1369 # 7. User Agent -> Server
1370 #
1371 # POST /acme/process HTTP/1.1
1372 # Cookie: $Version="1";
1373 # Customer="WILE_E_COYOTE"; $Path="/acme";
1374 # Part_Number="Rocket_Launcher_0001"; $Path="/acme";
1375 # Shipping="FedEx"; $Path="/acme"
1376 # [form data]
1377 #
1378 # User chooses to process order.
1379 #
1380 # 8. Server -> User Agent
1381 #
1382 # HTTP/1.1 200 OK
1383 #
1384 # Transaction is complete.
1385
1386 cookie = interact_2965(c, "http://www.acme.com/acme/process")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001387 self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"')
1388 self.assertIn("WILE_E_COYOTE", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001389
1390 #
1391 # The user agent makes a series of requests on the origin server, after
1392 # each of which it receives a new cookie. All the cookies have the same
1393 # Path attribute and (default) domain. Because the request URLs all have
1394 # /acme as a prefix, and that matches the Path attribute, each request
1395 # contains all the cookies received so far.
1396
1397 def test_ietf_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001398 # 5.2 Example 2
1399 #
1400 # This example illustrates the effect of the Path attribute. All detail
1401 # of request and response headers has been omitted. Assume the user agent
1402 # has no stored cookies.
1403
1404 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1405
1406 # Imagine the user agent has received, in response to earlier requests,
1407 # the response headers
1408 #
1409 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1410 # Path="/acme"
1411 #
1412 # and
1413 #
1414 # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
1415 # Path="/acme/ammo"
1416
1417 interact_2965(
1418 c, "http://www.acme.com/acme/ammo/specific",
1419 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
1420 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
1421
1422 # A subsequent request by the user agent to the (same) server for URLs of
1423 # the form /acme/ammo/... would include the following request header:
1424 #
1425 # Cookie: $Version="1";
1426 # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
1427 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1428 #
1429 # Note that the NAME=VALUE pair for the cookie with the more specific Path
1430 # attribute, /acme/ammo, comes before the one with the less specific Path
1431 # attribute, /acme. Further note that the same cookie name appears more
1432 # than once.
1433
1434 cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001435 self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001436
1437 # A subsequent request by the user agent to the (same) server for a URL of
1438 # the form /acme/parts/ would include the following request header:
1439 #
1440 # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1441 #
1442 # Here, the second cookie's Path attribute /acme/ammo is not a prefix of
1443 # the request URL, /acme/parts/, so the cookie does not get forwarded to
1444 # the server.
1445
1446 cookie = interact_2965(c, "http://www.acme.com/acme/parts/")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001447 self.assertIn("Rocket_Launcher_0001", cookie)
1448 self.assertNotIn("Riding_Rocket_0023", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001449
1450 def test_rejection(self):
1451 # Test rejection of Set-Cookie2 responses based on domain, path, port.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001452 pol = DefaultCookiePolicy(rfc2965=True)
1453
1454 c = LWPCookieJar(policy=pol)
1455
1456 max_age = "max-age=3600"
1457
1458 # illegal domain (no embedded dots)
1459 cookie = interact_2965(c, "http://www.acme.com",
1460 'foo=bar; domain=".com"; version=1')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001461 self.assertFalse(c)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001462
1463 # legal domain
1464 cookie = interact_2965(c, "http://www.acme.com",
1465 'ping=pong; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001466 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001467
1468 # illegal domain (host prefix "www.a" contains a dot)
1469 cookie = interact_2965(c, "http://www.a.acme.com",
1470 'whiz=bang; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001471 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001472
1473 # legal domain
1474 cookie = interact_2965(c, "http://www.a.acme.com",
1475 'wow=flutter; domain=".a.acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001476 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001477
1478 # can't partially match an IP-address
1479 cookie = interact_2965(c, "http://125.125.125.125",
1480 'zzzz=ping; domain="125.125.125"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001481 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001482
1483 # illegal path (must be prefix of request path)
1484 cookie = interact_2965(c, "http://www.sol.no",
1485 'blah=rhubarb; domain=".sol.no"; path="/foo"; '
1486 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001487 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001488
1489 # legal path
1490 cookie = interact_2965(c, "http://www.sol.no/foo/bar",
1491 'bing=bong; domain=".sol.no"; path="/foo"; '
1492 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001493 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001494
1495 # illegal port (request-port not in list)
1496 cookie = interact_2965(c, "http://www.sol.no",
1497 'whiz=ffft; domain=".sol.no"; port="90,100"; '
1498 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001499 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001500
1501 # legal port
1502 cookie = interact_2965(
1503 c, "http://www.sol.no",
1504 r'bang=wallop; version=1; domain=".sol.no"; '
1505 r'port="90,100, 80,8080"; '
1506 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001507 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001508
1509 # port attribute without any value (current port)
1510 cookie = interact_2965(c, "http://www.sol.no",
1511 'foo9=bar; version=1; domain=".sol.no"; port; '
1512 'max-age=100;')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001513 self.assertEqual(len(c), 5)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001514
1515 # encoded path
1516 # LWP has this test, but unescaping allowed path characters seems
1517 # like a bad idea, so I think this should fail:
1518## cookie = interact_2965(c, "http://www.sol.no/foo/",
1519## r'foo8=bar; version=1; path="/%66oo"')
1520 # but this is OK, because '<' is not an allowed HTTP URL path
1521 # character:
1522 cookie = interact_2965(c, "http://www.sol.no/<oo/",
1523 r'foo8=bar; version=1; path="/%3coo"')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001524 self.assertEqual(len(c), 6)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001525
1526 # save and restore
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001527 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001528
1529 try:
1530 c.save(filename, ignore_discard=True)
1531 old = repr(c)
1532
1533 c = LWPCookieJar(policy=pol)
1534 c.load(filename, ignore_discard=True)
1535 finally:
1536 try: os.unlink(filename)
1537 except OSError: pass
1538
Ezio Melottib3aedd42010-11-20 19:04:17 +00001539 self.assertEqual(old, repr(c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001540
1541 def test_url_encoding(self):
1542 # Try some URL encodings of the PATHs.
1543 # (the behaviour here has changed from libwww-perl)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001544 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
Guido van Rossum52dbbb92008-08-18 21:44:30 +00001545 interact_2965(c, "http://www.acme.com/foo%2f%25/"
1546 "%3c%3c%0Anew%C3%A5/%C3%A5",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001547 "foo = bar; version = 1")
1548
1549 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001550 c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001551 'bar=baz; path="/foo/"; version=1');
1552 version_re = re.compile(r'^\$version=\"?1\"?', re.I)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001553 self.assertIn("foo=bar", cookie)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001554 self.assertRegex(cookie, version_re)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001555
1556 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001557 c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001558 self.assertFalse(cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001559
1560 # unicode URL doesn't raise exception
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001561 cookie = interact_2965(c, "http://www.acme.com/\xfc")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001562
1563 def test_mozilla(self):
1564 # Save / load Mozilla/Netscape cookie file format.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001565 year_plus_one = time.localtime()[0] + 1
1566
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001567 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001568
1569 c = MozillaCookieJar(filename,
1570 policy=DefaultCookiePolicy(rfc2965=True))
1571 interact_2965(c, "http://www.acme.com/",
1572 "foo1=bar; max-age=100; Version=1")
1573 interact_2965(c, "http://www.acme.com/",
1574 'foo2=bar; port="80"; max-age=100; Discard; Version=1')
1575 interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")
1576
1577 expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
1578 interact_netscape(c, "http://www.foo.com/",
1579 "fooa=bar; %s" % expires)
1580 interact_netscape(c, "http://www.foo.com/",
1581 "foob=bar; Domain=.foo.com; %s" % expires)
1582 interact_netscape(c, "http://www.foo.com/",
1583 "fooc=bar; Domain=www.foo.com; %s" % expires)
1584
1585 def save_and_restore(cj, ignore_discard):
1586 try:
1587 cj.save(ignore_discard=ignore_discard)
1588 new_c = MozillaCookieJar(filename,
1589 DefaultCookiePolicy(rfc2965=True))
1590 new_c.load(ignore_discard=ignore_discard)
1591 finally:
1592 try: os.unlink(filename)
1593 except OSError: pass
1594 return new_c
1595
1596 new_c = save_and_restore(c, True)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001597 self.assertEqual(len(new_c), 6) # none discarded
Benjamin Peterson577473f2010-01-19 00:09:57 +00001598 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001599
1600 new_c = save_and_restore(c, False)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001601 self.assertEqual(len(new_c), 4) # 2 of them discarded on save
Benjamin Peterson577473f2010-01-19 00:09:57 +00001602 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001603
1604 def test_netscape_misc(self):
1605 # Some additional Netscape cookies tests.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001606 c = CookieJar()
1607 headers = []
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001608 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001609
1610 # Netscape allows a host part that contains dots
1611 headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
1612 res = FakeResponse(headers, "http://www.acme.com/foo")
1613 c.extract_cookies(res, req)
1614
1615 # and that the domain is the same as the host without adding a leading
1616 # dot to the domain. Should not quote even if strange chars are used
1617 # in the cookie value.
1618 headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
1619 res = FakeResponse(headers, "http://www.acme.com/foo")
1620 c.extract_cookies(res, req)
1621
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001622 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001623 c.add_cookie_header(req)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001624 self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie"))
1625 self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001626
1627 def test_intranet_domains_2965(self):
1628 # Test handling of local intranet hostnames without a dot.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001629 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1630 interact_2965(c, "http://example/",
1631 "foo1=bar; PORT; Discard; Version=1;")
1632 cookie = interact_2965(c, "http://example/",
1633 'foo2=bar; domain=".local"; Version=1')
Benjamin Peterson577473f2010-01-19 00:09:57 +00001634 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001635
1636 interact_2965(c, "http://example/", 'foo3=bar; Version=1')
1637 cookie = interact_2965(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001638 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001639 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001640
1641 def test_intranet_domains_ns(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001642 c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
1643 interact_netscape(c, "http://example/", "foo1=bar")
1644 cookie = interact_netscape(c, "http://example/",
1645 'foo2=bar; domain=.local')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001646 self.assertEqual(len(c), 2)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001647 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001648
1649 cookie = interact_netscape(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001650 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001651 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001652
1653 def test_empty_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001654 # Test for empty path
1655 # Broken web-server ORION/1.3.38 returns to the client response like
1656 #
1657 # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
1658 #
1659 # ie. with Path set to nothing.
1660 # In this case, extract_cookies() must set cookie to / (root)
1661 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1662 headers = []
1663
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001664 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001665 headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
1666 res = FakeResponse(headers, "http://www.ants.com/")
1667 c.extract_cookies(res, req)
1668
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001669 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001670 c.add_cookie_header(req)
1671
Ezio Melottib3aedd42010-11-20 19:04:17 +00001672 self.assertEqual(req.get_header("Cookie"),
1673 "JSESSIONID=ABCDERANDOM123")
1674 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001675
1676 # missing path in the request URI
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001677 req = urllib.request.Request("http://www.ants.com:8080")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001678 c.add_cookie_header(req)
1679
Ezio Melottib3aedd42010-11-20 19:04:17 +00001680 self.assertEqual(req.get_header("Cookie"),
1681 "JSESSIONID=ABCDERANDOM123")
1682 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001683
1684 def test_session_cookies(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001685 year_plus_one = time.localtime()[0] + 1
1686
1687 # Check session cookies are deleted properly by
1688 # CookieJar.clear_session_cookies method
1689
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001690 req = urllib.request.Request('http://www.perlmeister.com/scripts')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001691 headers = []
1692 headers.append("Set-Cookie: s1=session;Path=/scripts")
1693 headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"
1694 "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %
1695 year_plus_one)
1696 headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "
1697 "02-Feb-%d 23:24:20 GMT" % year_plus_one)
1698 headers.append("Set-Cookie: s2=session;Path=/scripts;"
1699 "Domain=.perlmeister.com")
1700 headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
1701 res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
1702
1703 c = CookieJar()
1704 c.extract_cookies(res, req)
1705 # How many session/permanent cookies do we have?
1706 counter = {"session_after": 0,
1707 "perm_after": 0,
1708 "session_before": 0,
1709 "perm_before": 0}
1710 for cookie in c:
1711 key = "%s_before" % cookie.value
1712 counter[key] = counter[key] + 1
1713 c.clear_session_cookies()
1714 # How many now?
1715 for cookie in c:
1716 key = "%s_after" % cookie.value
1717 counter[key] = counter[key] + 1
1718
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001719 # a permanent cookie got lost accidently
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001720 self.assertEqual(counter["perm_after"], counter["perm_before"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001721 # a session cookie hasn't been cleared
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001722 self.assertEqual(counter["session_after"], 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001723 # we didn't have session cookies in the first place
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001724 self.assertNotEqual(counter["session_before"], 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001725
1726
1727def test_main(verbose=None):
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001728 test.support.run_unittest(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001729 DateTimeTests,
1730 HeaderTests,
1731 CookieTests,
Martin v. Löwisc5574e82005-03-03 10:57:37 +00001732 FileCookieJarTests,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001733 LWPCookieTests,
1734 )
1735
1736if __name__ == "__main__":
1737 test_main(verbose=True)