blob: 22bf41cf1d91c6cc7b8f7f16202d91933c752de6 [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
Stéphane Wirtel4b219ce2019-03-01 21:40:54 +01009import pathlib
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000010
Ezio Melotti1d237e52013-08-10 18:20:09 +030011from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +000012 parse_ns_headers, join_header_words, split_header_words, Cookie,
13 CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar,
14 LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path,
15 reach, is_HDN, domain_match, user_domain_match, request_path,
16 request_port, request_host)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000017
Georg Brandl24420152008-05-26 16:32:26 +000018
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +000019class DateTimeTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000020
21 def test_time2isoz(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000022 base = 1019227000
23 day = 24*3600
Ezio Melottib3aedd42010-11-20 19:04:17 +000024 self.assertEqual(time2isoz(base), "2002-04-19 14:36:40Z")
25 self.assertEqual(time2isoz(base+day), "2002-04-20 14:36:40Z")
26 self.assertEqual(time2isoz(base+2*day), "2002-04-21 14:36:40Z")
27 self.assertEqual(time2isoz(base+3*day), "2002-04-22 14:36:40Z")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000028
29 az = time2isoz()
30 bz = time2isoz(500000)
31 for text in (az, bz):
Serhiy Storchaka9d282f62013-11-17 13:45:02 +020032 self.assertRegex(text, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$",
33 "bad time2isoz format: %s %s" % (az, bz))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000034
Senthil Kumarand5b47fb2016-07-10 06:45:38 -070035 def test_time2netscape(self):
36 base = 1019227000
37 day = 24*3600
38 self.assertEqual(time2netscape(base), "Fri, 19-Apr-2002 14:36:40 GMT")
39 self.assertEqual(time2netscape(base+day),
40 "Sat, 20-Apr-2002 14:36:40 GMT")
41
42 self.assertEqual(time2netscape(base+2*day),
43 "Sun, 21-Apr-2002 14:36:40 GMT")
44
45 self.assertEqual(time2netscape(base+3*day),
46 "Mon, 22-Apr-2002 14:36:40 GMT")
47
48 az = time2netscape()
49 bz = time2netscape(500000)
50 for text in (az, bz):
51 # Format "%s, %02d-%s-%04d %02d:%02d:%02d GMT"
52 self.assertRegex(
53 text,
54 r"[a-zA-Z]{3}, \d{2}-[a-zA-Z]{3}-\d{4} \d{2}:\d{2}:\d{2} GMT$",
55 "bad time2netscape format: %s %s" % (az, bz))
56
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000057 def test_http2time(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000058 def parse_date(text):
59 return time.gmtime(http2time(text))[:6]
60
Ezio Melottib3aedd42010-11-20 19:04:17 +000061 self.assertEqual(parse_date("01 Jan 2001"), (2001, 1, 1, 0, 0, 0.0))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000062
63 # this test will break around year 2070
Ezio Melottib3aedd42010-11-20 19:04:17 +000064 self.assertEqual(parse_date("03-Feb-20"), (2020, 2, 3, 0, 0, 0.0))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000065
66 # this test will break around year 2048
Ezio Melottib3aedd42010-11-20 19:04:17 +000067 self.assertEqual(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000068
69 def test_http2time_formats(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000070 # test http2time for supported dates. Test cases with 2 digit year
71 # will probably break in year 2044.
72 tests = [
73 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format
74 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format
75 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format
76
77 '03 Feb 1994 00:00:00 GMT', # HTTP format (no weekday)
78 '03-Feb-94 00:00:00 GMT', # old rfc850 (no weekday)
79 '03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday)
80 '03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds)
81 '03-Feb-1994 00:00', # broken rfc850 (no weekday, no seconds, no tz)
Ezio Melotti7ac17f82013-08-10 18:07:25 +030082 '02-Feb-1994 24:00', # broken rfc850 (no weekday, no seconds,
83 # no tz) using hour 24 with yesterday date
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000084
85 '03-Feb-94', # old rfc850 HTTP format (no weekday, no time)
86 '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)
87 '03 Feb 1994', # proposed new HTTP format (no weekday, no time)
88
89 # A few tests with extra space at various places
90 ' 03 Feb 1994 0:00 ',
91 ' 03-Feb-1994 ',
92 ]
93
94 test_t = 760233600 # assume broken POSIX counting of seconds
95 result = time2isoz(test_t)
96 expected = "1994-02-03 00:00:00Z"
Ezio Melottib3aedd42010-11-20 19:04:17 +000097 self.assertEqual(result, expected,
98 "%s => '%s' (%s)" % (test_t, result, expected))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +000099
100 for s in tests:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200101 self.assertEqual(http2time(s), test_t, s)
102 self.assertEqual(http2time(s.lower()), test_t, s.lower())
103 self.assertEqual(http2time(s.upper()), test_t, s.upper())
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000104
105 def test_http2time_garbage(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000106 for test in [
107 '',
108 'Garbage',
109 'Mandag 16. September 1996',
110 '01-00-1980',
111 '01-13-1980',
112 '00-01-1980',
113 '32-01-1980',
114 '01-01-1980 25:00:00',
115 '01-01-1980 00:61:00',
116 '01-01-1980 00:00:62',
Berker Peksag20be53e2016-03-14 05:48:02 +0200117 '08-Oct-3697739',
118 '08-01-3697739',
119 '09 Feb 19942632 22:23:32 GMT',
120 'Wed, 09 Feb 1994834 22:23:32 GMT',
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000121 ]:
Ezio Melotti1d237e52013-08-10 18:20:09 +0300122 self.assertIsNone(http2time(test),
123 "http2time(%s) is not None\n"
124 "http2time(test) %s" % (test, http2time(test)))
125
126 def test_iso2time(self):
127 def parse_date(text):
128 return time.gmtime(iso2time(text))[:6]
129
130 # ISO 8601 compact format
131 self.assertEqual(parse_date("19940203T141529Z"),
132 (1994, 2, 3, 14, 15, 29))
133
134 # ISO 8601 with time behind UTC
135 self.assertEqual(parse_date("1994-02-03 07:15:29 -0700"),
136 (1994, 2, 3, 14, 15, 29))
137
138 # ISO 8601 with time ahead of UTC
139 self.assertEqual(parse_date("1994-02-03 19:45:29 +0530"),
140 (1994, 2, 3, 14, 15, 29))
141
142 def test_iso2time_formats(self):
143 # test iso2time for supported dates.
144 tests = [
145 '1994-02-03 00:00:00 -0000', # ISO 8601 format
146 '1994-02-03 00:00:00 +0000', # ISO 8601 format
147 '1994-02-03 00:00:00', # zone is optional
148 '1994-02-03', # only date
149 '1994-02-03T00:00:00', # Use T as separator
150 '19940203', # only date
151 '1994-02-02 24:00:00', # using hour-24 yesterday date
152 '19940203T000000Z', # ISO 8601 compact format
153
154 # A few tests with extra space at various places
155 ' 1994-02-03 ',
156 ' 1994-02-03T00:00:00 ',
157 ]
158
159 test_t = 760233600 # assume broken POSIX counting of seconds
160 for s in tests:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200161 self.assertEqual(iso2time(s), test_t, s)
162 self.assertEqual(iso2time(s.lower()), test_t, s.lower())
163 self.assertEqual(iso2time(s.upper()), test_t, s.upper())
Ezio Melotti1d237e52013-08-10 18:20:09 +0300164
165 def test_iso2time_garbage(self):
166 for test in [
167 '',
168 'Garbage',
169 'Thursday, 03-Feb-94 00:00:00 GMT',
170 '1980-00-01',
171 '1980-13-01',
172 '1980-01-00',
173 '1980-01-32',
174 '1980-01-01 25:00:00',
175 '1980-01-01 00:61:00',
176 '01-01-1980 00:00:62',
177 '01-01-1980T00:00:62',
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +0200178 '19800101T250000Z',
Ezio Melotti1d237e52013-08-10 18:20:09 +0300179 ]:
180 self.assertIsNone(iso2time(test),
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +0200181 "iso2time(%r)" % test)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000182
183
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000184class HeaderTests(unittest.TestCase):
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000185
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000186 def test_parse_ns_headers(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000187 # quotes should be stripped
Guido van Rossume2a383d2007-01-15 16:59:06 +0000188 expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]]
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000189 for hdr in [
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000190 'foo=bar; expires=01 Jan 2040 22:23:32 GMT',
191 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"',
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000192 ]:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000193 self.assertEqual(parse_ns_headers([hdr]), expected)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000194
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000195 def test_parse_ns_headers_version(self):
196
197 # quotes should be stripped
198 expected = [[('foo', 'bar'), ('version', '1')]]
199 for hdr in [
200 'foo=bar; version="1"',
201 'foo=bar; Version="1"',
202 ]:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000203 self.assertEqual(parse_ns_headers([hdr]), expected)
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000204
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000205 def test_parse_ns_headers_special_names(self):
206 # names such as 'expires' are not special in first name=value pair
207 # of Set-Cookie: header
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000208 # Cookie with name 'expires'
209 hdr = 'expires=01 Jan 2040 22:23:32 GMT'
210 expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000211 self.assertEqual(parse_ns_headers([hdr]), expected)
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000212
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000213 def test_join_header_words(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000214 joined = join_header_words([[("foo", None), ("bar", "baz")]])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000215 self.assertEqual(joined, "foo; bar=baz")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000216
Ezio Melottib3aedd42010-11-20 19:04:17 +0000217 self.assertEqual(join_header_words([[]]), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000218
219 def test_split_header_words(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000220 tests = [
221 ("foo", [[("foo", None)]]),
222 ("foo=bar", [[("foo", "bar")]]),
223 (" foo ", [[("foo", None)]]),
224 (" foo= ", [[("foo", "")]]),
225 (" foo=", [[("foo", "")]]),
226 (" foo= ; ", [[("foo", "")]]),
227 (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
228 ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
229 # doesn't really matter if this next fails, but it works ATM
230 ("foo= bar=baz", [[("foo", "bar=baz")]]),
231 ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
232 ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
233 ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
234 (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',
235 [[("foo", None), ("bar", "baz")],
236 [("spam", "")], [("foo", ',;"')], [("bar", "")]]),
237 ]
238
239 for arg, expect in tests:
240 try:
241 result = split_header_words([arg])
242 except:
Guido van Rossum34d19282007-08-09 01:03:29 +0000243 import traceback, io
244 f = io.StringIO()
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000245 traceback.print_exc(None, f)
246 result = "(error -- traceback follows)\n\n%s" % f.getvalue()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000247 self.assertEqual(result, expect, """
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000248When parsing: '%s'
249Expected: '%s'
250Got: '%s'
251""" % (arg, expect, result))
252
253 def test_roundtrip(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000254 tests = [
255 ("foo", "foo"),
256 ("foo=bar", "foo=bar"),
257 (" foo ", "foo"),
258 ("foo=", 'foo=""'),
259 ("foo=bar bar=baz", "foo=bar; bar=baz"),
260 ("foo=bar;bar=baz", "foo=bar; bar=baz"),
261 ('foo bar baz', "foo; bar; baz"),
262 (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
263 ('foo,,,bar', 'foo, bar'),
264 ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
265
266 ('text/html; charset=iso-8859-1',
267 'text/html; charset="iso-8859-1"'),
268
269 ('foo="bar"; port="80,81"; discard, bar=baz',
270 'foo=bar; port="80,81"; discard, bar=baz'),
271
272 (r'Basic realm="\"foo\\\\bar\""',
273 r'Basic; realm="\"foo\\\\bar\""')
274 ]
275
276 for arg, expect in tests:
277 input = split_header_words([arg])
278 res = join_header_words(input)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000279 self.assertEqual(res, expect, """
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000280When parsing: '%s'
281Expected: '%s'
282Got: '%s'
283Input was: '%s'
284""" % (arg, expect, res, input))
285
286
287class FakeResponse:
288 def __init__(self, headers=[], url=None):
289 """
290 headers: list of RFC822-style 'Key: value' strings
291 """
Barry Warsaw820c1202008-06-12 04:06:45 +0000292 import email
293 self._headers = email.message_from_string("\n".join(headers))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000294 self._url = url
295 def info(self): return self._headers
296
297def interact_2965(cookiejar, url, *set_cookie_hdrs):
298 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2")
299
300def interact_netscape(cookiejar, url, *set_cookie_hdrs):
301 return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")
302
303def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
304 """Perform a single request / response cycle, returning Cookie: header."""
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000305 req = urllib.request.Request(url)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000306 cookiejar.add_cookie_header(req)
307 cookie_hdr = req.get_header("Cookie", "")
308 headers = []
309 for hdr in set_cookie_hdrs:
310 headers.append("%s: %s" % (hdr_name, hdr))
311 res = FakeResponse(headers, url)
312 cookiejar.extract_cookies(res, req)
313 return cookie_hdr
314
315
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000316class FileCookieJarTests(unittest.TestCase):
Stéphane Wirtel4b219ce2019-03-01 21:40:54 +0100317 def test_constructor_with_str(self):
318 filename = test.support.TESTFN
319 c = LWPCookieJar(filename)
320 self.assertEqual(c.filename, filename)
321
322 def test_constructor_with_path_like(self):
323 filename = pathlib.Path(test.support.TESTFN)
324 c = LWPCookieJar(filename)
325 self.assertEqual(c.filename, os.fspath(filename))
326
327 def test_constructor_with_none(self):
328 c = LWPCookieJar(None)
329 self.assertIsNone(c.filename)
330
331 def test_constructor_with_other_types(self):
332 class A:
333 pass
334
335 for type_ in (int, float, A):
336 with self.subTest(filename=type_):
337 with self.assertRaises(TypeError):
338 instance = type_()
339 c = LWPCookieJar(filename=instance)
340
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000341 def test_lwp_valueless_cookie(self):
342 # cookies with no value should be saved and loaded consistently
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000343 filename = test.support.TESTFN
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000344 c = LWPCookieJar()
345 interact_netscape(c, "http://www.acme.com/", 'boo')
346 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
347 try:
348 c.save(filename, ignore_discard=True)
349 c = LWPCookieJar()
350 c.load(filename, ignore_discard=True)
351 finally:
352 try: os.unlink(filename)
353 except OSError: pass
354 self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
355
Neal Norwitz3e7de592005-12-23 21:24:35 +0000356 def test_bad_magic(self):
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200357 # OSErrors (eg. file doesn't exist) are allowed to propagate
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000358 filename = test.support.TESTFN
Neal Norwitz3e7de592005-12-23 21:24:35 +0000359 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
360 c = cookiejar_class()
361 try:
362 c.load(filename="for this test to work, a file with this "
363 "filename should not exist")
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200364 except OSError as exc:
365 # an OSError subclass (likely FileNotFoundError), but not
366 # LoadError
367 self.assertIsNot(exc.__class__, LoadError)
Neal Norwitz3e7de592005-12-23 21:24:35 +0000368 else:
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200369 self.fail("expected OSError for invalid filename")
Neal Norwitz3e7de592005-12-23 21:24:35 +0000370 # Invalid contents of cookies file (eg. bad magic string)
371 # causes a LoadError.
372 try:
Brett Cannon7f462fc2010-10-29 23:27:39 +0000373 with open(filename, "w") as f:
374 f.write("oops\n")
375 for cookiejar_class in LWPCookieJar, MozillaCookieJar:
376 c = cookiejar_class()
377 self.assertRaises(LoadError, c.load, filename)
Neal Norwitz3e7de592005-12-23 21:24:35 +0000378 finally:
379 try: os.unlink(filename)
380 except OSError: pass
Martin v. Löwisc5574e82005-03-03 10:57:37 +0000381
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000382class CookieTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000383 # XXX
384 # Get rid of string comparisons where not actually testing str / repr.
385 # .clear() etc.
386 # IP addresses like 50 (single number, no dot) and domain-matching
387 # functions (and is_HDN)? See draft RFC 2965 errata.
388 # Strictness switches
389 # is_third_party()
390 # unverifiability / third-party blocking
391 # Netscape cookies work the same as RFC 2965 with regard to port.
392 # Set-Cookie with negative max age.
393 # If turn RFC 2965 handling off, Set-Cookie2 cookies should not clobber
394 # Set-Cookie cookies.
395 # Cookie2 should be sent if *any* cookies are not V1 (ie. V0 OR V2 etc.).
396 # Cookies (V1 and V0) with no expiry date should be set to be discarded.
397 # RFC 2965 Quoting:
398 # Should accept unquoted cookie-attribute values? check errata draft.
399 # Which are required on the way in and out?
400 # Should always return quoted cookie-attribute values?
401 # Proper testing of when RFC 2965 clobbers Netscape (waiting for errata).
402 # Path-match on return (same for V0 and V1).
403 # RFC 2965 acceptance and returning rules
404 # Set-Cookie2 without version attribute is rejected.
405
406 # Netscape peculiarities list from Ronald Tschalar.
407 # The first two still need tests, the rest are covered.
408## - Quoting: only quotes around the expires value are recognized as such
409## (and yes, some folks quote the expires value); quotes around any other
410## value are treated as part of the value.
411## - White space: white space around names and values is ignored
412## - Default path: if no path parameter is given, the path defaults to the
413## path in the request-uri up to, but not including, the last '/'. Note
414## that this is entirely different from what the spec says.
415## - Commas and other delimiters: Netscape just parses until the next ';'.
416## This means it will allow commas etc inside values (and yes, both
417## commas and equals are commonly appear in the cookie value). This also
418## means that if you fold multiple Set-Cookie header fields into one,
419## comma-separated list, it'll be a headache to parse (at least my head
Ezio Melotti85a86292013-08-17 16:57:41 +0300420## starts hurting every time I think of that code).
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000421## - Expires: You'll get all sorts of date formats in the expires,
Martin Pantereb995702016-07-28 01:11:04 +0000422## including empty expires attributes ("expires="). Be as flexible as you
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000423## can, and certainly don't expect the weekday to be there; if you can't
424## parse it, just ignore it and pretend it's a session cookie.
425## - Domain-matching: Netscape uses the 2-dot rule for _all_ domains, not
426## just the 7 special TLD's listed in their spec. And folks rely on
427## that...
428
429 def test_domain_return_ok(self):
430 # test optimization: .domain_return_ok() should filter out most
431 # domains in the CookieJar before we try to access them (because that
432 # may require disk access -- in particular, with MSIECookieJar)
433 # This is only a rough check for performance reasons, so it's not too
434 # critical as long as it's sufficiently liberal.
Georg Brandl24420152008-05-26 16:32:26 +0000435 pol = DefaultCookiePolicy()
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000436 for url, domain, ok in [
437 ("http://foo.bar.com/", "blah.com", False),
438 ("http://foo.bar.com/", "rhubarb.blah.com", False),
439 ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),
440 ("http://foo.bar.com/", ".foo.bar.com", True),
441 ("http://foo.bar.com/", "foo.bar.com", True),
442 ("http://foo.bar.com/", ".bar.com", True),
Xtreakca7fe502019-03-10 07:39:48 +0530443 ("http://foo.bar.com/", "bar.com", True),
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000444 ("http://foo.bar.com/", "com", True),
445 ("http://foo.com/", "rhubarb.foo.com", False),
446 ("http://foo.com/", ".foo.com", True),
447 ("http://foo.com/", "foo.com", True),
448 ("http://foo.com/", "com", True),
449 ("http://foo/", "rhubarb.foo", False),
450 ("http://foo/", ".foo", True),
451 ("http://foo/", "foo", True),
452 ("http://foo/", "foo.local", True),
453 ("http://foo/", ".local", True),
Xtreakca7fe502019-03-10 07:39:48 +0530454 ("http://barfoo.com", ".foo.com", False),
455 ("http://barfoo.com", "foo.com", False),
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000456 ]:
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000457 request = urllib.request.Request(url)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000458 r = pol.domain_return_ok(domain, request)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000459 if ok: self.assertTrue(r)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200460 else: self.assertFalse(r)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000461
462 def test_missing_value(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000463 # missing = sign in Cookie: header is regarded by Mozilla as a missing
Georg Brandl24420152008-05-26 16:32:26 +0000464 # name, and by http.cookiejar as a missing value
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000465 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000466 c = MozillaCookieJar(filename)
467 interact_netscape(c, "http://www.acme.com/", 'eggs')
468 interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
469 cookie = c._cookies["www.acme.com"]["/"]["eggs"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200470 self.assertIsNone(cookie.value)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000471 self.assertEqual(cookie.name, "eggs")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000472 cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200473 self.assertIsNone(cookie.value)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000474 self.assertEqual(cookie.name, '"spam"')
475 self.assertEqual(lwp_cookie_str(cookie), (
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000476 r'"spam"; path="/foo/"; domain="www.acme.com"; '
477 'path_spec; discard; version=0'))
478 old_str = repr(c)
479 c.save(ignore_expires=True, ignore_discard=True)
480 try:
481 c = MozillaCookieJar(filename)
482 c.revert(ignore_expires=True, ignore_discard=True)
483 finally:
484 os.unlink(c.filename)
485 # cookies unchanged apart from lost info re. whether path was specified
Ezio Melottib3aedd42010-11-20 19:04:17 +0000486 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000487 repr(c),
488 re.sub("path_specified=%s" % True, "path_specified=%s" % False,
489 old_str)
490 )
Ezio Melottib3aedd42010-11-20 19:04:17 +0000491 self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
492 '"spam"; eggs')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000493
Neal Norwitz71dad722005-12-23 21:43:48 +0000494 def test_rfc2109_handling(self):
495 # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
496 # dependent on policy settings
Neal Norwitz71dad722005-12-23 21:43:48 +0000497 for rfc2109_as_netscape, rfc2965, version in [
498 # default according to rfc2965 if not explicitly specified
499 (None, False, 0),
500 (None, True, 1),
501 # explicit rfc2109_as_netscape
502 (False, False, None), # version None here means no cookie stored
503 (False, True, 1),
504 (True, False, 0),
505 (True, True, 0),
506 ]:
507 policy = DefaultCookiePolicy(
508 rfc2109_as_netscape=rfc2109_as_netscape,
509 rfc2965=rfc2965)
510 c = CookieJar(policy)
511 interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")
512 try:
513 cookie = c._cookies["www.example.com"]["/"]["ni"]
514 except KeyError:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200515 self.assertIsNone(version) # didn't expect a stored cookie
Neal Norwitz71dad722005-12-23 21:43:48 +0000516 else:
517 self.assertEqual(cookie.version, version)
518 # 2965 cookies are unaffected
519 interact_2965(c, "http://www.example.com/",
520 "foo=bar; Version=1")
521 if rfc2965:
522 cookie2965 = c._cookies["www.example.com"]["/"]["foo"]
523 self.assertEqual(cookie2965.version, 1)
524
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000525 def test_ns_parser(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000526 c = CookieJar()
527 interact_netscape(c, "http://www.acme.com/",
528 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')
529 interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080')
530 interact_netscape(c, "http://www.acme.com:80/", 'nini=ni')
531 interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=')
532 interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; '
533 'expires="Foo Bar 25 33:22:11 3022"')
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +0200534 interact_netscape(c, 'http://www.acme.com/', 'fortytwo=')
535 interact_netscape(c, 'http://www.acme.com/', '=unladenswallow')
536 interact_netscape(c, 'http://www.acme.com/', 'holyhandgrenade')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000537
538 cookie = c._cookies[".acme.com"]["/"]["spam"]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000539 self.assertEqual(cookie.domain, ".acme.com")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000540 self.assertTrue(cookie.domain_specified)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000541 self.assertEqual(cookie.port, DEFAULT_HTTP_PORT)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200542 self.assertFalse(cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000543 # case is preserved
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200544 self.assertTrue(cookie.has_nonstandard_attr("blArgh"))
545 self.assertFalse(cookie.has_nonstandard_attr("blargh"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000546
547 cookie = c._cookies["www.acme.com"]["/"]["ni"]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000548 self.assertEqual(cookie.domain, "www.acme.com")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200549 self.assertFalse(cookie.domain_specified)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000550 self.assertEqual(cookie.port, "80,8080")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000551 self.assertTrue(cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000552
553 cookie = c._cookies["www.acme.com"]["/"]["nini"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200554 self.assertIsNone(cookie.port)
555 self.assertFalse(cookie.port_specified)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000556
557 # invalid expires should not cause cookie to be dropped
558 foo = c._cookies["www.acme.com"]["/"]["foo"]
559 spam = c._cookies["www.acme.com"]["/"]["foo"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200560 self.assertIsNone(foo.expires)
561 self.assertIsNone(spam.expires)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000562
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +0200563 cookie = c._cookies['www.acme.com']['/']['fortytwo']
564 self.assertIsNotNone(cookie.value)
565 self.assertEqual(cookie.value, '')
566
567 # there should be a distinction between a present but empty value
568 # (above) and a value that's entirely missing (below)
569
570 cookie = c._cookies['www.acme.com']['/']['holyhandgrenade']
571 self.assertIsNone(cookie.value)
572
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000573 def test_ns_parser_special_names(self):
574 # names such as 'expires' are not special in first name=value pair
575 # of Set-Cookie: header
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000576 c = CookieJar()
577 interact_netscape(c, "http://www.acme.com/", 'expires=eggs')
578 interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')
579
580 cookies = c._cookies["www.acme.com"]["/"]
Benjamin Peterson577473f2010-01-19 00:09:57 +0000581 self.assertIn('expires', cookies)
582 self.assertIn('version', cookies)
Martin v. Löwis4ea3ead2005-03-03 10:48:12 +0000583
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000584 def test_expires(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000585 # if expires is in future, keep cookie...
586 c = CookieJar()
587 future = time2netscape(time.time()+3600)
588 interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
589 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000590 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000591 now = time2netscape(time.time()-1)
592 # ... and if in past or present, discard it
593 interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %
594 now)
595 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000596 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000597 self.assertIn('spam="bar"', h)
598 self.assertNotIn("foo", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000599
600 # max-age takes precedence over expires, and zero max-age is request to
601 # delete both new cookie and any old matching cookie
602 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %
603 future)
604 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %
605 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000606 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000607 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '
608 'expires=%s; max-age=0' % future)
609 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '
610 'max-age=0; expires=%s' % future)
611 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000612 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000613
614 # test expiry at end of session for cookies with no expires attribute
615 interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000616 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000617 c.clear_session_cookies()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000618 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000619 self.assertIn('spam="bar"', h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000620
Robert Collinsa0e5d982015-08-04 10:06:29 +1200621 # test if fractional expiry is accepted
622 cookie = Cookie(0, "name", "value",
623 None, False, "www.python.org",
624 True, False, "/",
625 False, False, "1444312383.018307",
626 False, None, None,
627 {})
628 self.assertEqual(cookie.expires, 1444312383)
629
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000630 # XXX RFC 2965 expiry rules (some apply to V0 too)
631
632 def test_default_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000633 # RFC 2965
634 pol = DefaultCookiePolicy(rfc2965=True)
635
636 c = CookieJar(pol)
637 interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000638 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000639
640 c = CookieJar(pol)
641 interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000642 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000643
644 c = CookieJar(pol)
645 interact_2965(c, "http://www.acme.com/blah/rhubarb",
646 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000647 self.assertIn("/blah/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000648
649 c = CookieJar(pol)
650 interact_2965(c, "http://www.acme.com/blah/rhubarb/",
651 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000652 self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000653
654 # Netscape
655
656 c = CookieJar()
657 interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000658 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000659
660 c = CookieJar()
661 interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000662 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000663
664 c = CookieJar()
665 interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000666 self.assertIn("/blah", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000667
668 c = CookieJar()
669 interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000670 self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000671
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000672 def test_default_path_with_query(self):
673 cj = CookieJar()
674 uri = "http://example.com/?spam/eggs"
675 value = 'eggs="bar"'
676 interact_netscape(cj, uri, value)
677 # Default path does not include query, so is "/", not "/?spam".
678 self.assertIn("/", cj._cookies["example.com"])
679 # Cookie is sent back to the same URI.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000680 self.assertEqual(interact_netscape(cj, uri), value)
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000681
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000682 def test_escape_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000683 cases = [
684 # quoted safe
685 ("/foo%2f/bar", "/foo%2F/bar"),
686 ("/foo%2F/bar", "/foo%2F/bar"),
687 # quoted %
688 ("/foo%%/bar", "/foo%%/bar"),
689 # quoted unsafe
690 ("/fo%19o/bar", "/fo%19o/bar"),
691 ("/fo%7do/bar", "/fo%7Do/bar"),
692 # unquoted safe
693 ("/foo/bar&", "/foo/bar&"),
694 ("/foo//bar", "/foo//bar"),
695 ("\176/foo/bar", "\176/foo/bar"),
696 # unquoted unsafe
697 ("/foo\031/bar", "/foo%19/bar"),
698 ("/\175foo/bar", "/%7Dfoo/bar"),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000699 # unicode, latin-1 range
700 ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000701 # unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000702 ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000703 ]
704 for arg, result in cases:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000705 self.assertEqual(escape_path(arg), result)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000706
707 def test_request_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000708 # with parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000709 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000710 "http://www.example.com/rheum/rhaponticum;"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000711 "foo=bar;sing=song?apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000712 self.assertEqual(request_path(req),
713 "/rheum/rhaponticum;foo=bar;sing=song")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000714 # without parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000715 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000716 "http://www.example.com/rheum/rhaponticum?"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000717 "apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000718 self.assertEqual(request_path(req), "/rheum/rhaponticum")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000719 # missing final slash
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000720 req = urllib.request.Request("http://www.example.com")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000721 self.assertEqual(request_path(req), "/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000722
Xtreak0e1f1f02019-03-10 22:42:28 +0530723 def test_path_prefix_match(self):
724 pol = DefaultCookiePolicy()
725 strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
726
727 c = CookieJar(pol)
728 base_url = "http://bar.com"
729 interact_netscape(c, base_url, 'spam=eggs; Path=/foo')
730 cookie = c._cookies['bar.com']['/foo']['spam']
731
732 for path, ok in [('/foo', True),
733 ('/foo/', True),
734 ('/foo/bar', True),
735 ('/', False),
736 ('/foobad/foo', False)]:
737 url = f'{base_url}{path}'
738 req = urllib.request.Request(url)
739 h = interact_netscape(c, url)
740 if ok:
741 self.assertIn('spam=eggs', h, f"cookie not set for {path}")
742 self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req))
743 else:
744 self.assertNotIn('spam=eggs', h, f"cookie set for {path}")
745 self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
746
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000747 def test_request_port(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000748 req = urllib.request.Request("http://www.acme.com:1234/",
749 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000750 self.assertEqual(request_port(req), "1234")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000751 req = urllib.request.Request("http://www.acme.com/",
752 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000753 self.assertEqual(request_port(req), DEFAULT_HTTP_PORT)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000754
755 def test_request_host(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000756 # this request is illegal (RFC2616, 14.2.3)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000757 req = urllib.request.Request("http://1.1.1.1/",
758 headers={"Host": "www.acme.com:80"})
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000759 # libwww-perl wants this response, but that seems wrong (RFC 2616,
760 # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000761 #self.assertEqual(request_host(req), "www.acme.com")
762 self.assertEqual(request_host(req), "1.1.1.1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000763 req = urllib.request.Request("http://www.acme.com/",
764 headers={"Host": "irrelevant.com"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000765 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000766 # port shouldn't be in request-host
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000767 req = urllib.request.Request("http://www.acme.com:2345/resource.html",
768 headers={"Host": "www.acme.com:5432"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000769 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000770
771 def test_is_HDN(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000772 self.assertTrue(is_HDN("foo.bar.com"))
773 self.assertTrue(is_HDN("1foo2.3bar4.5com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200774 self.assertFalse(is_HDN("192.168.1.1"))
775 self.assertFalse(is_HDN(""))
776 self.assertFalse(is_HDN("."))
777 self.assertFalse(is_HDN(".foo.bar.com"))
778 self.assertFalse(is_HDN("..foo"))
779 self.assertFalse(is_HDN("foo."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000780
781 def test_reach(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000782 self.assertEqual(reach("www.acme.com"), ".acme.com")
783 self.assertEqual(reach("acme.com"), "acme.com")
784 self.assertEqual(reach("acme.local"), ".local")
785 self.assertEqual(reach(".local"), ".local")
786 self.assertEqual(reach(".com"), ".com")
787 self.assertEqual(reach("."), ".")
788 self.assertEqual(reach(""), "")
789 self.assertEqual(reach("192.168.0.1"), "192.168.0.1")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000790
791 def test_domain_match(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000792 self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200793 self.assertFalse(domain_match("192.168.1.1", ".168.1.1"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000794 self.assertTrue(domain_match("x.y.com", "x.Y.com"))
795 self.assertTrue(domain_match("x.y.com", ".Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200796 self.assertFalse(domain_match("x.y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000797 self.assertTrue(domain_match("a.b.c.com", ".c.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200798 self.assertFalse(domain_match(".c.com", "a.b.c.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000799 self.assertTrue(domain_match("example.local", ".local"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200800 self.assertFalse(domain_match("blah.blah", ""))
801 self.assertFalse(domain_match("", ".rhubarb.rhubarb"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000802 self.assertTrue(domain_match("", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000803
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000804 self.assertTrue(user_domain_match("acme.com", "acme.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200805 self.assertFalse(user_domain_match("acme.com", ".acme.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000806 self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))
807 self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
808 self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))
809 self.assertTrue(user_domain_match("x.y.com", ".Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200810 self.assertFalse(user_domain_match("x.y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000811 self.assertTrue(user_domain_match("y.com", "Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200812 self.assertFalse(user_domain_match(".y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000813 self.assertTrue(user_domain_match(".y.com", ".Y.com"))
814 self.assertTrue(user_domain_match("x.y.com", ".com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200815 self.assertFalse(user_domain_match("x.y.com", "com"))
816 self.assertFalse(user_domain_match("x.y.com", "m"))
817 self.assertFalse(user_domain_match("x.y.com", ".m"))
818 self.assertFalse(user_domain_match("x.y.com", ""))
819 self.assertFalse(user_domain_match("x.y.com", "."))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000820 self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000821 # not both HDNs, so must string-compare equal to match
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200822 self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1"))
823 self.assertFalse(user_domain_match("192.168.1.1", "."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000824 # empty string is a special case
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200825 self.assertFalse(user_domain_match("192.168.1.1", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000826
827 def test_wrong_domain(self):
828 # Cookies whose effective request-host name does not domain-match the
829 # domain are rejected.
830
831 # XXX far from complete
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000832 c = CookieJar()
833 interact_2965(c, "http://www.nasty.com/",
834 'foo=bar; domain=friendly.org; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000835 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000836
Thomas Wouters477c8d52006-05-27 19:21:47 +0000837 def test_strict_domain(self):
838 # Cookies whose domain is a country-code tld like .co.uk should
839 # not be set if CookiePolicy.strict_domain is true.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000840 cp = DefaultCookiePolicy(strict_domain=True)
841 cj = CookieJar(policy=cp)
842 interact_netscape(cj, "http://example.co.uk/", 'no=problemo')
843 interact_netscape(cj, "http://example.co.uk/",
844 'okey=dokey; Domain=.example.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000845 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846 for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:
847 interact_netscape(cj, "http://example.%s/" % pseudo_tld,
848 'spam=eggs; Domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000849 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000850
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000851 def test_two_component_domain_ns(self):
852 # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain
853 # should all get accepted, as should .acme.com, acme.com and no domain
854 # for 2-component domains like acme.com.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000855 c = CookieJar()
856
857 # two-component V0 domain is OK
858 interact_netscape(c, "http://foo.net/", 'ns=bar')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000859 self.assertEqual(len(c), 1)
860 self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar")
861 self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000862 # *will* be returned to any other domain (unlike RFC 2965)...
Ezio Melottib3aedd42010-11-20 19:04:17 +0000863 self.assertEqual(interact_netscape(c, "http://www.foo.net/"),
864 "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000865 # ...unless requested otherwise
866 pol = DefaultCookiePolicy(
867 strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
868 c.set_policy(pol)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000869 self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000870
871 # unlike RFC 2965, even explicit two-component domain is OK,
872 # because .foo.net matches foo.net
873 interact_netscape(c, "http://foo.net/foo/",
874 'spam1=eggs; domain=foo.net')
875 # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
876 interact_netscape(c, "http://foo.net/foo/bar/",
877 'spam2=eggs; domain=.foo.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000878 self.assertEqual(len(c), 3)
879 self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value,
880 "eggs")
881 self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,
882 "eggs")
883 self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"),
884 "spam2=eggs; spam1=eggs; ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000885
886 # top-level domain is too general
887 interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000888 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000889
890## # Netscape protocol doesn't allow non-special top level domains (such
891## # as co.uk) in the domain attribute unless there are at least three
892## # dots in it.
893 # Oh yes it does! Real implementations don't check this, and real
894 # cookies (of course) rely on that behaviour.
895 interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000896## self.assertEqual(len(c), 2)
897 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000898
899 def test_two_component_domain_rfc2965(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000900 pol = DefaultCookiePolicy(rfc2965=True)
901 c = CookieJar(pol)
902
903 # two-component V1 domain is OK
904 interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000905 self.assertEqual(len(c), 1)
906 self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar")
907 self.assertEqual(interact_2965(c, "http://foo.net/"),
908 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000909 # won't be returned to any other domain (because domain was implied)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000910 self.assertEqual(interact_2965(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000911
912 # unless domain is given explicitly, because then it must be
913 # rewritten to start with a dot: foo.net --> .foo.net, which does
914 # not domain-match foo.net
915 interact_2965(c, "http://foo.net/foo",
916 'spam=eggs; domain=foo.net; path=/foo; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000917 self.assertEqual(len(c), 1)
918 self.assertEqual(interact_2965(c, "http://foo.net/foo"),
919 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000920
921 # explicit foo.net from three-component domain www.foo.net *does* get
922 # set, because .foo.net domain-matches .foo.net
923 interact_2965(c, "http://www.foo.net/foo/",
924 'spam=eggs; domain=foo.net; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000925 self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value,
926 "eggs")
927 self.assertEqual(len(c), 2)
928 self.assertEqual(interact_2965(c, "http://foo.net/foo/"),
929 "$Version=1; foo=bar")
930 self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"),
931 '$Version=1; spam=eggs; $Domain="foo.net"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000932
933 # top-level domain is too general
934 interact_2965(c, "http://foo.net/",
935 'ni="ni"; domain=".net"; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000936 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000937
938 # RFC 2965 doesn't require blocking this
939 interact_2965(c, "http://foo.co.uk/",
940 'nasty=trick; domain=.co.uk; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000941 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000942
943 def test_domain_allow(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000944 c = CookieJar(policy=DefaultCookiePolicy(
945 blocked_domains=["acme.com"],
946 allowed_domains=["www.acme.com"]))
947
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000948 req = urllib.request.Request("http://acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000949 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
950 res = FakeResponse(headers, "http://acme.com/")
951 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000952 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000953
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000954 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000955 res = FakeResponse(headers, "http://www.acme.com/")
956 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000957 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000958
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000959 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000960 res = FakeResponse(headers, "http://www.coyote.com/")
961 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000962 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000963
964 # set a cookie with non-allowed domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000965 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000966 res = FakeResponse(headers, "http://www.coyote.com/")
967 cookies = c.make_cookies(res, req)
968 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000969 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000970 # ... and check is doesn't get returned
971 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200972 self.assertFalse(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000973
974 def test_domain_block(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000975 pol = DefaultCookiePolicy(
976 rfc2965=True, blocked_domains=[".acme.com"])
977 c = CookieJar(policy=pol)
978 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
979
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000980 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000981 res = FakeResponse(headers, "http://www.acme.com/")
982 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000983 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000984
985 p = pol.set_blocked_domains(["acme.com"])
986 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000987 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000988
989 c.clear()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000990 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000991 res = FakeResponse(headers, "http://www.roadrunner.net/")
992 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000993 self.assertEqual(len(c), 1)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000994 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000995 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200996 self.assertTrue(req.has_header("Cookie"))
997 self.assertTrue(req.has_header("Cookie2"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000998
999 c.clear()
1000 pol.set_blocked_domains([".acme.com"])
1001 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001002 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001003
1004 # set a cookie with blocked domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001005 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001006 res = FakeResponse(headers, "http://www.acme.com/")
1007 cookies = c.make_cookies(res, req)
1008 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001009 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001010 # ... and check is doesn't get returned
1011 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001012 self.assertFalse(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001013
Xtreakca7fe502019-03-10 07:39:48 +05301014 c.clear()
1015
1016 pol.set_blocked_domains([])
1017 req = urllib.request.Request("http://acme.com/")
1018 res = FakeResponse(headers, "http://acme.com/")
1019 cookies = c.make_cookies(res, req)
1020 c.extract_cookies(res, req)
1021 self.assertEqual(len(c), 1)
1022
1023 req = urllib.request.Request("http://acme.com/")
1024 c.add_cookie_header(req)
1025 self.assertTrue(req.has_header("Cookie"))
1026
1027 req = urllib.request.Request("http://badacme.com/")
1028 c.add_cookie_header(req)
1029 self.assertFalse(pol.return_ok(cookies[0], req))
1030 self.assertFalse(req.has_header("Cookie"))
1031
1032 p = pol.set_blocked_domains(["acme.com"])
1033 req = urllib.request.Request("http://acme.com/")
1034 c.add_cookie_header(req)
1035 self.assertFalse(req.has_header("Cookie"))
1036
1037 req = urllib.request.Request("http://badacme.com/")
1038 c.add_cookie_header(req)
1039 self.assertFalse(req.has_header("Cookie"))
1040
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001041 def test_secure(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001042 for ns in True, False:
1043 for whitespace in " ", "":
1044 c = CookieJar()
1045 if ns:
1046 pol = DefaultCookiePolicy(rfc2965=False)
1047 int = interact_netscape
1048 vs = ""
1049 else:
1050 pol = DefaultCookiePolicy(rfc2965=True)
1051 int = interact_2965
1052 vs = "; Version=1"
1053 c.set_policy(pol)
1054 url = "http://www.acme.com/"
1055 int(c, url, "foo1=bar%s%s" % (vs, whitespace))
1056 int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001057 self.assertFalse(
1058 c._cookies["www.acme.com"]["/"]["foo1"].secure,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001059 "non-secure cookie registered secure")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001060 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001061 c._cookies["www.acme.com"]["/"]["foo2"].secure,
1062 "secure cookie registered non-secure")
1063
Paul Bailey4c339972018-10-08 13:49:29 -05001064 def test_secure_block(self):
1065 pol = DefaultCookiePolicy()
1066 c = CookieJar(policy=pol)
1067
1068 headers = ["Set-Cookie: session=narf; secure; path=/"]
1069 req = urllib.request.Request("https://www.acme.com/")
1070 res = FakeResponse(headers, "https://www.acme.com/")
1071 c.extract_cookies(res, req)
1072 self.assertEqual(len(c), 1)
1073
1074 req = urllib.request.Request("https://www.acme.com/")
1075 c.add_cookie_header(req)
1076 self.assertTrue(req.has_header("Cookie"))
1077
1078 req = urllib.request.Request("http://www.acme.com/")
1079 c.add_cookie_header(req)
1080 self.assertFalse(req.has_header("Cookie"))
1081
1082 # secure websocket protocol
1083 req = urllib.request.Request("wss://www.acme.com/")
1084 c.add_cookie_header(req)
1085 self.assertTrue(req.has_header("Cookie"))
1086
1087 # non-secure websocket protocol
1088 req = urllib.request.Request("ws://www.acme.com/")
1089 c.add_cookie_header(req)
1090 self.assertFalse(req.has_header("Cookie"))
1091
1092 def test_custom_secure_protocols(self):
1093 pol = DefaultCookiePolicy(secure_protocols=["foos"])
1094 c = CookieJar(policy=pol)
1095
1096 headers = ["Set-Cookie: session=narf; secure; path=/"]
1097 req = urllib.request.Request("https://www.acme.com/")
1098 res = FakeResponse(headers, "https://www.acme.com/")
1099 c.extract_cookies(res, req)
1100 self.assertEqual(len(c), 1)
1101
1102 # test https removed from secure protocol list
1103 req = urllib.request.Request("https://www.acme.com/")
1104 c.add_cookie_header(req)
1105 self.assertFalse(req.has_header("Cookie"))
1106
1107 req = urllib.request.Request("http://www.acme.com/")
1108 c.add_cookie_header(req)
1109 self.assertFalse(req.has_header("Cookie"))
1110
1111 req = urllib.request.Request("foos://www.acme.com/")
1112 c.add_cookie_header(req)
1113 self.assertTrue(req.has_header("Cookie"))
1114
1115 req = urllib.request.Request("foo://www.acme.com/")
1116 c.add_cookie_header(req)
1117 self.assertFalse(req.has_header("Cookie"))
1118
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001119 def test_quote_cookie_value(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001120 c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))
1121 interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')
1122 h = interact_2965(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +00001123 self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001124
1125 def test_missing_final_slash(self):
1126 # Missing slash from request URL's abs_path should be assumed present.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001127 url = "http://www.acme.com"
1128 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1129 interact_2965(c, url, "foo=bar; Version=1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001130 req = urllib.request.Request(url)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001131 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001132 c.add_cookie_header(req)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001133 self.assertTrue(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001134
1135 def test_domain_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001136 pol = DefaultCookiePolicy(rfc2965=True)
1137
1138 c = CookieJar(pol)
1139 url = "http://foo.bar.com/"
1140 interact_2965(c, url, "spam=eggs; Version=1")
1141 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001142 self.assertNotIn("Domain", h,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001143 "absent domain returned with domain present")
1144
1145 c = CookieJar(pol)
1146 url = "http://foo.bar.com/"
1147 interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
1148 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001149 self.assertIn('$Domain=".bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001150
1151 c = CookieJar(pol)
1152 url = "http://foo.bar.com/"
1153 # note missing initial dot in Domain
1154 interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
1155 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001156 self.assertIn('$Domain="bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001157
1158 def test_path_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001159 pol = DefaultCookiePolicy(rfc2965=True)
1160
1161 c = CookieJar(pol)
1162 url = "http://foo.bar.com/"
1163 interact_2965(c, url, "spam=eggs; Version=1")
1164 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001165 self.assertNotIn("Path", h, "absent path returned with path present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001166
1167 c = CookieJar(pol)
1168 url = "http://foo.bar.com/"
1169 interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
1170 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001171 self.assertIn('$Path="/"', h, "path not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001172
1173 def test_port_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001174 pol = DefaultCookiePolicy(rfc2965=True)
1175
1176 c = CookieJar(pol)
1177 url = "http://foo.bar.com/"
1178 interact_2965(c, url, "spam=eggs; Version=1")
1179 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001180 self.assertNotIn("Port", h, "absent port returned with port present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001181
1182 c = CookieJar(pol)
1183 url = "http://foo.bar.com/"
1184 interact_2965(c, url, "spam=eggs; Version=1; Port")
1185 h = interact_2965(c, url)
R David Murray44b548d2016-09-08 13:59:53 -04001186 self.assertRegex(h, r"\$Port([^=]|$)",
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001187 "port with no value not returned with no value")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001188
1189 c = CookieJar(pol)
1190 url = "http://foo.bar.com/"
1191 interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
1192 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001193 self.assertIn('$Port="80"', h,
1194 "port with single value not returned with single value")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001195
1196 c = CookieJar(pol)
1197 url = "http://foo.bar.com/"
1198 interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
1199 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001200 self.assertIn('$Port="80,8080"', h,
1201 "port with multiple values not returned with multiple "
1202 "values")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001203
1204 def test_no_return_comment(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001205 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1206 url = "http://foo.bar.com/"
1207 interact_2965(c, url, 'spam=eggs; Version=1; '
1208 'Comment="does anybody read these?"; '
1209 'CommentURL="http://foo.bar.net/comment.html"')
1210 h = interact_2965(c, url)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001211 self.assertNotIn("Comment", h,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001212 "Comment or CommentURL cookie-attributes returned to server")
1213
1214 def test_Cookie_iterator(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001215 cs = CookieJar(DefaultCookiePolicy(rfc2965=True))
1216 # add some random cookies
1217 interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '
1218 'Comment="does anybody read these?"; '
1219 'CommentURL="http://foo.bar.net/comment.html"')
1220 interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")
1221 interact_2965(cs, "http://www.acme.com/blah/",
1222 "foo=bar; secure; Version=1")
1223 interact_2965(cs, "http://www.acme.com/blah/",
1224 "foo=bar; path=/; Version=1")
1225 interact_2965(cs, "http://www.sol.no",
1226 r'bang=wallop; version=1; domain=".sol.no"; '
1227 r'port="90,100, 80,8080"; '
1228 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
1229
1230 versions = [1, 1, 1, 0, 1]
1231 names = ["bang", "foo", "foo", "spam", "foo"]
1232 domains = [".sol.no", "blah.spam.org", "www.acme.com",
1233 "www.acme.com", "www.acme.com"]
1234 paths = ["/", "/", "/", "/blah", "/blah/"]
1235
1236 for i in range(4):
1237 i = 0
1238 for c in cs:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001239 self.assertIsInstance(c, Cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001240 self.assertEqual(c.version, versions[i])
1241 self.assertEqual(c.name, names[i])
1242 self.assertEqual(c.domain, domains[i])
1243 self.assertEqual(c.path, paths[i])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001244 i = i + 1
1245
1246 def test_parse_ns_headers(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001247 # missing domain value (invalid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001248 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001249 parse_ns_headers(["foo=bar; path=/; domain"]),
1250 [[("foo", "bar"),
1251 ("path", "/"), ("domain", None), ("version", "0")]]
1252 )
1253 # invalid expires value
Ezio Melottib3aedd42010-11-20 19:04:17 +00001254 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001255 parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),
1256 [[("foo", "bar"), ("expires", None), ("version", "0")]]
1257 )
1258 # missing cookie value (valid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001259 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001260 parse_ns_headers(["foo"]),
1261 [[("foo", None), ("version", "0")]]
1262 )
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001263 # missing cookie values for parsed attributes
1264 self.assertEqual(
1265 parse_ns_headers(['foo=bar; expires']),
1266 [[('foo', 'bar'), ('expires', None), ('version', '0')]])
1267 self.assertEqual(
1268 parse_ns_headers(['foo=bar; version']),
1269 [[('foo', 'bar'), ('version', None)]])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001270 # shouldn't add version if header is empty
Ezio Melottib3aedd42010-11-20 19:04:17 +00001271 self.assertEqual(parse_ns_headers([""]), [])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001272
1273 def test_bad_cookie_header(self):
1274
1275 def cookiejar_from_cookie_headers(headers):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001276 c = CookieJar()
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001277 req = urllib.request.Request("http://www.example.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001278 r = FakeResponse(headers, "http://www.example.com/")
1279 c.extract_cookies(r, req)
1280 return c
1281
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001282 future = time2netscape(time.time()+3600)
1283
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001284 # none of these bad headers should cause an exception to be raised
1285 for headers in [
1286 ["Set-Cookie: "], # actually, nothing wrong with this
1287 ["Set-Cookie2: "], # ditto
1288 # missing domain value
1289 ["Set-Cookie2: a=foo; path=/; Version=1; domain"],
1290 # bad max-age
1291 ["Set-Cookie: b=foo; max-age=oops"],
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +00001292 # bad version
1293 ["Set-Cookie: b=foo; version=spam"],
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001294 ["Set-Cookie:; Expires=%s" % future],
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001295 ]:
1296 c = cookiejar_from_cookie_headers(headers)
1297 # these bad cookies shouldn't be set
Ezio Melottib3aedd42010-11-20 19:04:17 +00001298 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001299
1300 # cookie with invalid expires is treated as session cookie
1301 headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
1302 c = cookiejar_from_cookie_headers(headers)
1303 cookie = c._cookies["www.example.com"]["/"]["c"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001304 self.assertIsNone(cookie.expires)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001305
1306
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001307class LWPCookieTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001308 # Tests taken from libwww-perl, with a few modifications and additions.
1309
1310 def test_netscape_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001311 #-------------------------------------------------------------------
1312 # First we check that it works for the original example at
1313 # http://www.netscape.com/newsref/std/cookie_spec.html
1314
1315 # Client requests a document, and receives in the response:
1316 #
1317 # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT
1318 #
1319 # When client requests a URL in path "/" on this server, it sends:
1320 #
1321 # Cookie: CUSTOMER=WILE_E_COYOTE
1322 #
1323 # Client requests a document, and receives in the response:
1324 #
1325 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1326 #
1327 # When client requests a URL in path "/" on this server, it sends:
1328 #
1329 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1330 #
1331 # Client receives:
1332 #
1333 # Set-Cookie: SHIPPING=FEDEX; path=/fo
1334 #
1335 # When client requests a URL in path "/" on this server, it sends:
1336 #
1337 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1338 #
1339 # When client requests a URL in path "/foo" on this server, it sends:
1340 #
1341 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX
1342 #
1343 # The last Cookie is buggy, because both specifications say that the
1344 # most specific cookie must be sent first. SHIPPING=FEDEX is the
1345 # most specific and should thus be first.
1346
1347 year_plus_one = time.localtime()[0] + 1
1348
1349 headers = []
1350
1351 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1352
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001353 #req = urllib.request.Request("http://1.1.1.1/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001354 # headers={"Host": "www.acme.com:80"})
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001355 req = urllib.request.Request("http://www.acme.com:80/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001356 headers={"Host": "www.acme.com:80"})
1357
1358 headers.append(
1359 "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "
1360 "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)
1361 res = FakeResponse(headers, "http://www.acme.com/")
1362 c.extract_cookies(res, req)
1363
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001364 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001365 c.add_cookie_header(req)
1366
1367 self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")
1368 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
1369
1370 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1371 res = FakeResponse(headers, "http://www.acme.com/")
1372 c.extract_cookies(res, req)
1373
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001374 req = urllib.request.Request("http://www.acme.com/foo/bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001375 c.add_cookie_header(req)
1376
1377 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001378 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1379 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001380
1381 headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
1382 res = FakeResponse(headers, "http://www.acme.com")
1383 c.extract_cookies(res, req)
1384
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001385 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001386 c.add_cookie_header(req)
1387
1388 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001389 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1390 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1391 self.assertNotIn("SHIPPING=FEDEX", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001392
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001393 req = urllib.request.Request("http://www.acme.com/foo/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001394 c.add_cookie_header(req)
1395
1396 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001397 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1398 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1399 self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001400
1401 def test_netscape_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001402 # Second Example transaction sequence:
1403 #
1404 # Assume all mappings from above have been cleared.
1405 #
1406 # Client receives:
1407 #
1408 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1409 #
1410 # When client requests a URL in path "/" on this server, it sends:
1411 #
1412 # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001
1413 #
1414 # Client receives:
1415 #
1416 # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
1417 #
1418 # When client requests a URL in path "/ammo" on this server, it sends:
1419 #
1420 # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001
1421 #
1422 # NOTE: There are two name/value pairs named "PART_NUMBER" due to
1423 # the inheritance of the "/" mapping in addition to the "/ammo" mapping.
1424
1425 c = CookieJar()
1426 headers = []
1427
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001428 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001429 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1430 res = FakeResponse(headers, "http://www.acme.com/")
1431
1432 c.extract_cookies(res, req)
1433
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001434 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001435 c.add_cookie_header(req)
1436
Ezio Melottib3aedd42010-11-20 19:04:17 +00001437 self.assertEqual(req.get_header("Cookie"),
1438 "PART_NUMBER=ROCKET_LAUNCHER_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001439
1440 headers.append(
1441 "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")
1442 res = FakeResponse(headers, "http://www.acme.com/")
1443 c.extract_cookies(res, req)
1444
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001445 req = urllib.request.Request("http://www.acme.com/ammo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001446 c.add_cookie_header(req)
1447
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001448 self.assertRegex(req.get_header("Cookie"),
1449 r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
1450 "PART_NUMBER=ROCKET_LAUNCHER_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001451
1452 def test_ietf_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001453 #-------------------------------------------------------------------
1454 # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt
1455 #
1456 # 5. EXAMPLES
1457
1458 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1459
1460 #
1461 # 5.1 Example 1
1462 #
1463 # Most detail of request and response headers has been omitted. Assume
1464 # the user agent has no stored cookies.
1465 #
1466 # 1. User Agent -> Server
1467 #
1468 # POST /acme/login HTTP/1.1
1469 # [form data]
1470 #
1471 # User identifies self via a form.
1472 #
1473 # 2. Server -> User Agent
1474 #
1475 # HTTP/1.1 200 OK
1476 # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
1477 #
1478 # Cookie reflects user's identity.
1479
1480 cookie = interact_2965(
1481 c, 'http://www.acme.com/acme/login',
1482 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001483 self.assertFalse(cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001484
1485 #
1486 # 3. User Agent -> Server
1487 #
1488 # POST /acme/pickitem HTTP/1.1
1489 # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
1490 # [form data]
1491 #
1492 # User selects an item for ``shopping basket.''
1493 #
1494 # 4. Server -> User Agent
1495 #
1496 # HTTP/1.1 200 OK
1497 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1498 # Path="/acme"
1499 #
1500 # Shopping basket contains an item.
1501
1502 cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
1503 'Part_Number="Rocket_Launcher_0001"; '
1504 'Version="1"; Path="/acme"');
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001505 self.assertRegex(cookie,
1506 r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001507
1508 #
1509 # 5. User Agent -> Server
1510 #
1511 # POST /acme/shipping HTTP/1.1
1512 # Cookie: $Version="1";
1513 # Customer="WILE_E_COYOTE"; $Path="/acme";
1514 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1515 # [form data]
1516 #
1517 # User selects shipping method from form.
1518 #
1519 # 6. Server -> User Agent
1520 #
1521 # HTTP/1.1 200 OK
1522 # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"
1523 #
1524 # New cookie reflects shipping method.
1525
1526 cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
1527 'Shipping="FedEx"; Version="1"; Path="/acme"')
1528
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001529 self.assertRegex(cookie, r'^\$Version="?1"?;')
1530 self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;'
R David Murray44b548d2016-09-08 13:59:53 -04001531 r'\s*\$Path="\/acme"')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001532 self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;'
R David Murray44b548d2016-09-08 13:59:53 -04001533 r'\s*\$Path="\/acme"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001534
1535 #
1536 # 7. User Agent -> Server
1537 #
1538 # POST /acme/process HTTP/1.1
1539 # Cookie: $Version="1";
1540 # Customer="WILE_E_COYOTE"; $Path="/acme";
1541 # Part_Number="Rocket_Launcher_0001"; $Path="/acme";
1542 # Shipping="FedEx"; $Path="/acme"
1543 # [form data]
1544 #
1545 # User chooses to process order.
1546 #
1547 # 8. Server -> User Agent
1548 #
1549 # HTTP/1.1 200 OK
1550 #
1551 # Transaction is complete.
1552
1553 cookie = interact_2965(c, "http://www.acme.com/acme/process")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001554 self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"')
1555 self.assertIn("WILE_E_COYOTE", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001556
1557 #
1558 # The user agent makes a series of requests on the origin server, after
1559 # each of which it receives a new cookie. All the cookies have the same
1560 # Path attribute and (default) domain. Because the request URLs all have
1561 # /acme as a prefix, and that matches the Path attribute, each request
1562 # contains all the cookies received so far.
1563
1564 def test_ietf_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001565 # 5.2 Example 2
1566 #
1567 # This example illustrates the effect of the Path attribute. All detail
1568 # of request and response headers has been omitted. Assume the user agent
1569 # has no stored cookies.
1570
1571 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1572
1573 # Imagine the user agent has received, in response to earlier requests,
1574 # the response headers
1575 #
1576 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1577 # Path="/acme"
1578 #
1579 # and
1580 #
1581 # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
1582 # Path="/acme/ammo"
1583
1584 interact_2965(
1585 c, "http://www.acme.com/acme/ammo/specific",
1586 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
1587 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
1588
1589 # A subsequent request by the user agent to the (same) server for URLs of
1590 # the form /acme/ammo/... would include the following request header:
1591 #
1592 # Cookie: $Version="1";
1593 # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
1594 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1595 #
1596 # Note that the NAME=VALUE pair for the cookie with the more specific Path
1597 # attribute, /acme/ammo, comes before the one with the less specific Path
1598 # attribute, /acme. Further note that the same cookie name appears more
1599 # than once.
1600
1601 cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001602 self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001603
1604 # A subsequent request by the user agent to the (same) server for a URL of
1605 # the form /acme/parts/ would include the following request header:
1606 #
1607 # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1608 #
1609 # Here, the second cookie's Path attribute /acme/ammo is not a prefix of
1610 # the request URL, /acme/parts/, so the cookie does not get forwarded to
1611 # the server.
1612
1613 cookie = interact_2965(c, "http://www.acme.com/acme/parts/")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001614 self.assertIn("Rocket_Launcher_0001", cookie)
1615 self.assertNotIn("Riding_Rocket_0023", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001616
1617 def test_rejection(self):
1618 # Test rejection of Set-Cookie2 responses based on domain, path, port.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001619 pol = DefaultCookiePolicy(rfc2965=True)
1620
1621 c = LWPCookieJar(policy=pol)
1622
1623 max_age = "max-age=3600"
1624
1625 # illegal domain (no embedded dots)
1626 cookie = interact_2965(c, "http://www.acme.com",
1627 'foo=bar; domain=".com"; version=1')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001628 self.assertFalse(c)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001629
1630 # legal domain
1631 cookie = interact_2965(c, "http://www.acme.com",
1632 'ping=pong; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001633 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001634
1635 # illegal domain (host prefix "www.a" contains a dot)
1636 cookie = interact_2965(c, "http://www.a.acme.com",
1637 'whiz=bang; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001638 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001639
1640 # legal domain
1641 cookie = interact_2965(c, "http://www.a.acme.com",
1642 'wow=flutter; domain=".a.acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001643 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001644
1645 # can't partially match an IP-address
1646 cookie = interact_2965(c, "http://125.125.125.125",
1647 'zzzz=ping; domain="125.125.125"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001648 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001649
1650 # illegal path (must be prefix of request path)
1651 cookie = interact_2965(c, "http://www.sol.no",
1652 'blah=rhubarb; domain=".sol.no"; path="/foo"; '
1653 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001654 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001655
1656 # legal path
1657 cookie = interact_2965(c, "http://www.sol.no/foo/bar",
1658 'bing=bong; domain=".sol.no"; path="/foo"; '
1659 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001660 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001661
1662 # illegal port (request-port not in list)
1663 cookie = interact_2965(c, "http://www.sol.no",
1664 'whiz=ffft; domain=".sol.no"; port="90,100"; '
1665 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001666 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001667
1668 # legal port
1669 cookie = interact_2965(
1670 c, "http://www.sol.no",
1671 r'bang=wallop; version=1; domain=".sol.no"; '
1672 r'port="90,100, 80,8080"; '
1673 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001674 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001675
1676 # port attribute without any value (current port)
1677 cookie = interact_2965(c, "http://www.sol.no",
1678 'foo9=bar; version=1; domain=".sol.no"; port; '
1679 'max-age=100;')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001680 self.assertEqual(len(c), 5)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001681
1682 # encoded path
1683 # LWP has this test, but unescaping allowed path characters seems
1684 # like a bad idea, so I think this should fail:
1685## cookie = interact_2965(c, "http://www.sol.no/foo/",
1686## r'foo8=bar; version=1; path="/%66oo"')
1687 # but this is OK, because '<' is not an allowed HTTP URL path
1688 # character:
1689 cookie = interact_2965(c, "http://www.sol.no/<oo/",
1690 r'foo8=bar; version=1; path="/%3coo"')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001691 self.assertEqual(len(c), 6)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001692
1693 # save and restore
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001694 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001695
1696 try:
1697 c.save(filename, ignore_discard=True)
1698 old = repr(c)
1699
1700 c = LWPCookieJar(policy=pol)
1701 c.load(filename, ignore_discard=True)
1702 finally:
1703 try: os.unlink(filename)
1704 except OSError: pass
1705
Ezio Melottib3aedd42010-11-20 19:04:17 +00001706 self.assertEqual(old, repr(c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001707
1708 def test_url_encoding(self):
1709 # Try some URL encodings of the PATHs.
1710 # (the behaviour here has changed from libwww-perl)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001711 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
Guido van Rossum52dbbb92008-08-18 21:44:30 +00001712 interact_2965(c, "http://www.acme.com/foo%2f%25/"
1713 "%3c%3c%0Anew%C3%A5/%C3%A5",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001714 "foo = bar; version = 1")
1715
1716 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001717 c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001718 'bar=baz; path="/foo/"; version=1');
1719 version_re = re.compile(r'^\$version=\"?1\"?', re.I)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001720 self.assertIn("foo=bar", cookie)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001721 self.assertRegex(cookie, version_re)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001722
1723 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001724 c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001725 self.assertFalse(cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001726
1727 # unicode URL doesn't raise exception
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001728 cookie = interact_2965(c, "http://www.acme.com/\xfc")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001729
1730 def test_mozilla(self):
1731 # Save / load Mozilla/Netscape cookie file format.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001732 year_plus_one = time.localtime()[0] + 1
1733
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001734 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001735
1736 c = MozillaCookieJar(filename,
1737 policy=DefaultCookiePolicy(rfc2965=True))
1738 interact_2965(c, "http://www.acme.com/",
1739 "foo1=bar; max-age=100; Version=1")
1740 interact_2965(c, "http://www.acme.com/",
1741 'foo2=bar; port="80"; max-age=100; Discard; Version=1')
1742 interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")
1743
1744 expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
1745 interact_netscape(c, "http://www.foo.com/",
1746 "fooa=bar; %s" % expires)
1747 interact_netscape(c, "http://www.foo.com/",
1748 "foob=bar; Domain=.foo.com; %s" % expires)
1749 interact_netscape(c, "http://www.foo.com/",
1750 "fooc=bar; Domain=www.foo.com; %s" % expires)
1751
1752 def save_and_restore(cj, ignore_discard):
1753 try:
1754 cj.save(ignore_discard=ignore_discard)
1755 new_c = MozillaCookieJar(filename,
1756 DefaultCookiePolicy(rfc2965=True))
1757 new_c.load(ignore_discard=ignore_discard)
1758 finally:
1759 try: os.unlink(filename)
1760 except OSError: pass
1761 return new_c
1762
1763 new_c = save_and_restore(c, True)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001764 self.assertEqual(len(new_c), 6) # none discarded
Benjamin Peterson577473f2010-01-19 00:09:57 +00001765 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001766
1767 new_c = save_and_restore(c, False)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001768 self.assertEqual(len(new_c), 4) # 2 of them discarded on save
Benjamin Peterson577473f2010-01-19 00:09:57 +00001769 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001770
1771 def test_netscape_misc(self):
1772 # Some additional Netscape cookies tests.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001773 c = CookieJar()
1774 headers = []
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001775 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001776
1777 # Netscape allows a host part that contains dots
1778 headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
1779 res = FakeResponse(headers, "http://www.acme.com/foo")
1780 c.extract_cookies(res, req)
1781
1782 # and that the domain is the same as the host without adding a leading
1783 # dot to the domain. Should not quote even if strange chars are used
1784 # in the cookie value.
1785 headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
1786 res = FakeResponse(headers, "http://www.acme.com/foo")
1787 c.extract_cookies(res, req)
1788
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001789 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001790 c.add_cookie_header(req)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001791 self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie"))
1792 self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001793
1794 def test_intranet_domains_2965(self):
1795 # Test handling of local intranet hostnames without a dot.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001796 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1797 interact_2965(c, "http://example/",
1798 "foo1=bar; PORT; Discard; Version=1;")
1799 cookie = interact_2965(c, "http://example/",
1800 'foo2=bar; domain=".local"; Version=1')
Benjamin Peterson577473f2010-01-19 00:09:57 +00001801 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001802
1803 interact_2965(c, "http://example/", 'foo3=bar; Version=1')
1804 cookie = interact_2965(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001805 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001806 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001807
1808 def test_intranet_domains_ns(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001809 c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
1810 interact_netscape(c, "http://example/", "foo1=bar")
1811 cookie = interact_netscape(c, "http://example/",
1812 'foo2=bar; domain=.local')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001813 self.assertEqual(len(c), 2)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001814 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001815
1816 cookie = interact_netscape(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001817 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001818 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001819
1820 def test_empty_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001821 # Test for empty path
1822 # Broken web-server ORION/1.3.38 returns to the client response like
1823 #
1824 # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
1825 #
1826 # ie. with Path set to nothing.
1827 # In this case, extract_cookies() must set cookie to / (root)
1828 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1829 headers = []
1830
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001831 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001832 headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
1833 res = FakeResponse(headers, "http://www.ants.com/")
1834 c.extract_cookies(res, req)
1835
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001836 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001837 c.add_cookie_header(req)
1838
Ezio Melottib3aedd42010-11-20 19:04:17 +00001839 self.assertEqual(req.get_header("Cookie"),
1840 "JSESSIONID=ABCDERANDOM123")
1841 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001842
1843 # missing path in the request URI
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001844 req = urllib.request.Request("http://www.ants.com:8080")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001845 c.add_cookie_header(req)
1846
Ezio Melottib3aedd42010-11-20 19:04:17 +00001847 self.assertEqual(req.get_header("Cookie"),
1848 "JSESSIONID=ABCDERANDOM123")
1849 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001850
1851 def test_session_cookies(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001852 year_plus_one = time.localtime()[0] + 1
1853
1854 # Check session cookies are deleted properly by
1855 # CookieJar.clear_session_cookies method
1856
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001857 req = urllib.request.Request('http://www.perlmeister.com/scripts')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001858 headers = []
1859 headers.append("Set-Cookie: s1=session;Path=/scripts")
1860 headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"
1861 "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %
1862 year_plus_one)
1863 headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "
1864 "02-Feb-%d 23:24:20 GMT" % year_plus_one)
1865 headers.append("Set-Cookie: s2=session;Path=/scripts;"
1866 "Domain=.perlmeister.com")
1867 headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
1868 res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
1869
1870 c = CookieJar()
1871 c.extract_cookies(res, req)
1872 # How many session/permanent cookies do we have?
1873 counter = {"session_after": 0,
1874 "perm_after": 0,
1875 "session_before": 0,
1876 "perm_before": 0}
1877 for cookie in c:
1878 key = "%s_before" % cookie.value
1879 counter[key] = counter[key] + 1
1880 c.clear_session_cookies()
1881 # How many now?
1882 for cookie in c:
1883 key = "%s_after" % cookie.value
1884 counter[key] = counter[key] + 1
1885
Martin Panter46f50722016-05-26 05:35:26 +00001886 # a permanent cookie got lost accidentally
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001887 self.assertEqual(counter["perm_after"], counter["perm_before"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001888 # a session cookie hasn't been cleared
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001889 self.assertEqual(counter["session_after"], 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001890 # we didn't have session cookies in the first place
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001891 self.assertNotEqual(counter["session_before"], 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001892
1893
1894def test_main(verbose=None):
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001895 test.support.run_unittest(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001896 DateTimeTests,
1897 HeaderTests,
1898 CookieTests,
Martin v. Löwisc5574e82005-03-03 10:57:37 +00001899 FileCookieJarTests,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001900 LWPCookieTests,
1901 )
1902
1903if __name__ == "__main__":
1904 test_main(verbose=True)