blob: 853a40044965b153840caf533b661df5aa832a1d [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)
Miss Islington (bot)44cb89a2019-09-13 04:47:51 -0700588
589 with test.support.check_no_warnings(self):
590 headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"]
591 req = urllib.request.Request("http://www.coyote.com/")
592 res = FakeResponse(headers, "http://www.coyote.com/")
593 cookies = c.make_cookies(res, req)
594 self.assertEqual(len(cookies), 1)
595 self.assertEqual(time2netscape(cookies[0].expires), future)
596
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000597 interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
598 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000599 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000600 now = time2netscape(time.time()-1)
601 # ... and if in past or present, discard it
602 interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %
603 now)
604 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000605 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000606 self.assertIn('spam="bar"', h)
607 self.assertNotIn("foo", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000608
609 # max-age takes precedence over expires, and zero max-age is request to
610 # delete both new cookie and any old matching cookie
611 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %
612 future)
613 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %
614 future)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000615 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000616 interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '
617 'expires=%s; max-age=0' % future)
618 interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '
619 'max-age=0; expires=%s' % future)
620 h = interact_netscape(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000621 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000622
623 # test expiry at end of session for cookies with no expires attribute
624 interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000625 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000626 c.clear_session_cookies()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000627 self.assertEqual(len(c), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000628 self.assertIn('spam="bar"', h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000629
Robert Collinsa0e5d982015-08-04 10:06:29 +1200630 # test if fractional expiry is accepted
631 cookie = Cookie(0, "name", "value",
632 None, False, "www.python.org",
633 True, False, "/",
634 False, False, "1444312383.018307",
635 False, None, None,
636 {})
637 self.assertEqual(cookie.expires, 1444312383)
638
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000639 # XXX RFC 2965 expiry rules (some apply to V0 too)
640
641 def test_default_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000642 # RFC 2965
643 pol = DefaultCookiePolicy(rfc2965=True)
644
645 c = CookieJar(pol)
646 interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000647 self.assertIn("/", 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", 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000651 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000652
653 c = CookieJar(pol)
654 interact_2965(c, "http://www.acme.com/blah/rhubarb",
655 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000656 self.assertIn("/blah/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000657
658 c = CookieJar(pol)
659 interact_2965(c, "http://www.acme.com/blah/rhubarb/",
660 'eggs="bar"; Version="1"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000661 self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000662
663 # Netscape
664
665 c = CookieJar()
666 interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000667 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000668
669 c = CookieJar()
670 interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000671 self.assertIn("/", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000672
673 c = CookieJar()
674 interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000675 self.assertIn("/blah", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000676
677 c = CookieJar()
678 interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
Benjamin Peterson577473f2010-01-19 00:09:57 +0000679 self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000680
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000681 def test_default_path_with_query(self):
682 cj = CookieJar()
683 uri = "http://example.com/?spam/eggs"
684 value = 'eggs="bar"'
685 interact_netscape(cj, uri, value)
686 # Default path does not include query, so is "/", not "/?spam".
687 self.assertIn("/", cj._cookies["example.com"])
688 # Cookie is sent back to the same URI.
Ezio Melottib3aedd42010-11-20 19:04:17 +0000689 self.assertEqual(interact_netscape(cj, uri), value)
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000690
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000691 def test_escape_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000692 cases = [
693 # quoted safe
694 ("/foo%2f/bar", "/foo%2F/bar"),
695 ("/foo%2F/bar", "/foo%2F/bar"),
696 # quoted %
697 ("/foo%%/bar", "/foo%%/bar"),
698 # quoted unsafe
699 ("/fo%19o/bar", "/fo%19o/bar"),
700 ("/fo%7do/bar", "/fo%7Do/bar"),
701 # unquoted safe
702 ("/foo/bar&", "/foo/bar&"),
703 ("/foo//bar", "/foo//bar"),
704 ("\176/foo/bar", "\176/foo/bar"),
705 # unquoted unsafe
706 ("/foo\031/bar", "/foo%19/bar"),
707 ("/\175foo/bar", "/%7Dfoo/bar"),
Guido van Rossum52dbbb92008-08-18 21:44:30 +0000708 # unicode, latin-1 range
709 ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000710 # unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000711 ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000712 ]
713 for arg, result in cases:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000714 self.assertEqual(escape_path(arg), result)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000715
716 def test_request_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000717 # with parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000718 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000719 "http://www.example.com/rheum/rhaponticum;"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000720 "foo=bar;sing=song?apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000721 self.assertEqual(request_path(req),
722 "/rheum/rhaponticum;foo=bar;sing=song")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000723 # without parameters
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000724 req = urllib.request.Request(
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +0000725 "http://www.example.com/rheum/rhaponticum?"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000726 "apples=pears&spam=eggs#ni")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000727 self.assertEqual(request_path(req), "/rheum/rhaponticum")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000728 # missing final slash
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000729 req = urllib.request.Request("http://www.example.com")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000730 self.assertEqual(request_path(req), "/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000731
Xtreak0e1f1f02019-03-10 22:42:28 +0530732 def test_path_prefix_match(self):
733 pol = DefaultCookiePolicy()
734 strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
735
736 c = CookieJar(pol)
737 base_url = "http://bar.com"
738 interact_netscape(c, base_url, 'spam=eggs; Path=/foo')
739 cookie = c._cookies['bar.com']['/foo']['spam']
740
741 for path, ok in [('/foo', True),
742 ('/foo/', True),
743 ('/foo/bar', True),
744 ('/', False),
745 ('/foobad/foo', False)]:
746 url = f'{base_url}{path}'
747 req = urllib.request.Request(url)
748 h = interact_netscape(c, url)
749 if ok:
750 self.assertIn('spam=eggs', h, f"cookie not set for {path}")
751 self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req))
752 else:
753 self.assertNotIn('spam=eggs', h, f"cookie set for {path}")
754 self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
755
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000756 def test_request_port(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000757 req = urllib.request.Request("http://www.acme.com:1234/",
758 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000759 self.assertEqual(request_port(req), "1234")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000760 req = urllib.request.Request("http://www.acme.com/",
761 headers={"Host": "www.acme.com:4321"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000762 self.assertEqual(request_port(req), DEFAULT_HTTP_PORT)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000763
764 def test_request_host(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000765 # this request is illegal (RFC2616, 14.2.3)
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000766 req = urllib.request.Request("http://1.1.1.1/",
767 headers={"Host": "www.acme.com:80"})
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000768 # libwww-perl wants this response, but that seems wrong (RFC 2616,
769 # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000770 #self.assertEqual(request_host(req), "www.acme.com")
771 self.assertEqual(request_host(req), "1.1.1.1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000772 req = urllib.request.Request("http://www.acme.com/",
773 headers={"Host": "irrelevant.com"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000774 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000775 # port shouldn't be in request-host
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000776 req = urllib.request.Request("http://www.acme.com:2345/resource.html",
777 headers={"Host": "www.acme.com:5432"})
Ezio Melottib3aedd42010-11-20 19:04:17 +0000778 self.assertEqual(request_host(req), "www.acme.com")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000779
780 def test_is_HDN(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000781 self.assertTrue(is_HDN("foo.bar.com"))
782 self.assertTrue(is_HDN("1foo2.3bar4.5com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200783 self.assertFalse(is_HDN("192.168.1.1"))
784 self.assertFalse(is_HDN(""))
785 self.assertFalse(is_HDN("."))
786 self.assertFalse(is_HDN(".foo.bar.com"))
787 self.assertFalse(is_HDN("..foo"))
788 self.assertFalse(is_HDN("foo."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000789
790 def test_reach(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000791 self.assertEqual(reach("www.acme.com"), ".acme.com")
792 self.assertEqual(reach("acme.com"), "acme.com")
793 self.assertEqual(reach("acme.local"), ".local")
794 self.assertEqual(reach(".local"), ".local")
795 self.assertEqual(reach(".com"), ".com")
796 self.assertEqual(reach("."), ".")
797 self.assertEqual(reach(""), "")
798 self.assertEqual(reach("192.168.0.1"), "192.168.0.1")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000799
800 def test_domain_match(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000801 self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200802 self.assertFalse(domain_match("192.168.1.1", ".168.1.1"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000803 self.assertTrue(domain_match("x.y.com", "x.Y.com"))
804 self.assertTrue(domain_match("x.y.com", ".Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200805 self.assertFalse(domain_match("x.y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000806 self.assertTrue(domain_match("a.b.c.com", ".c.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200807 self.assertFalse(domain_match(".c.com", "a.b.c.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000808 self.assertTrue(domain_match("example.local", ".local"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200809 self.assertFalse(domain_match("blah.blah", ""))
810 self.assertFalse(domain_match("", ".rhubarb.rhubarb"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000811 self.assertTrue(domain_match("", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000812
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000813 self.assertTrue(user_domain_match("acme.com", "acme.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200814 self.assertFalse(user_domain_match("acme.com", ".acme.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000815 self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))
816 self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
817 self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))
818 self.assertTrue(user_domain_match("x.y.com", ".Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200819 self.assertFalse(user_domain_match("x.y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000820 self.assertTrue(user_domain_match("y.com", "Y.com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200821 self.assertFalse(user_domain_match(".y.com", "Y.com"))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000822 self.assertTrue(user_domain_match(".y.com", ".Y.com"))
823 self.assertTrue(user_domain_match("x.y.com", ".com"))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200824 self.assertFalse(user_domain_match("x.y.com", "com"))
825 self.assertFalse(user_domain_match("x.y.com", "m"))
826 self.assertFalse(user_domain_match("x.y.com", ".m"))
827 self.assertFalse(user_domain_match("x.y.com", ""))
828 self.assertFalse(user_domain_match("x.y.com", "."))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000829 self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000830 # not both HDNs, so must string-compare equal to match
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200831 self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1"))
832 self.assertFalse(user_domain_match("192.168.1.1", "."))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000833 # empty string is a special case
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200834 self.assertFalse(user_domain_match("192.168.1.1", ""))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000835
836 def test_wrong_domain(self):
837 # Cookies whose effective request-host name does not domain-match the
838 # domain are rejected.
839
840 # XXX far from complete
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000841 c = CookieJar()
842 interact_2965(c, "http://www.nasty.com/",
843 'foo=bar; domain=friendly.org; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000844 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000845
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846 def test_strict_domain(self):
847 # Cookies whose domain is a country-code tld like .co.uk should
848 # not be set if CookiePolicy.strict_domain is true.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000849 cp = DefaultCookiePolicy(strict_domain=True)
850 cj = CookieJar(policy=cp)
851 interact_netscape(cj, "http://example.co.uk/", 'no=problemo')
852 interact_netscape(cj, "http://example.co.uk/",
853 'okey=dokey; Domain=.example.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000854 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000855 for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:
856 interact_netscape(cj, "http://example.%s/" % pseudo_tld,
857 'spam=eggs; Domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000858 self.assertEqual(len(cj), 2)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000860 def test_two_component_domain_ns(self):
861 # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain
862 # should all get accepted, as should .acme.com, acme.com and no domain
863 # for 2-component domains like acme.com.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000864 c = CookieJar()
865
866 # two-component V0 domain is OK
867 interact_netscape(c, "http://foo.net/", 'ns=bar')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000868 self.assertEqual(len(c), 1)
869 self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar")
870 self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000871 # *will* be returned to any other domain (unlike RFC 2965)...
Ezio Melottib3aedd42010-11-20 19:04:17 +0000872 self.assertEqual(interact_netscape(c, "http://www.foo.net/"),
873 "ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000874 # ...unless requested otherwise
875 pol = DefaultCookiePolicy(
876 strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
877 c.set_policy(pol)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000878 self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000879
880 # unlike RFC 2965, even explicit two-component domain is OK,
881 # because .foo.net matches foo.net
882 interact_netscape(c, "http://foo.net/foo/",
883 'spam1=eggs; domain=foo.net')
884 # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
885 interact_netscape(c, "http://foo.net/foo/bar/",
886 'spam2=eggs; domain=.foo.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000887 self.assertEqual(len(c), 3)
888 self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value,
889 "eggs")
890 self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,
891 "eggs")
892 self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"),
893 "spam2=eggs; spam1=eggs; ns=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000894
895 # top-level domain is too general
896 interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000897 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000898
899## # Netscape protocol doesn't allow non-special top level domains (such
900## # as co.uk) in the domain attribute unless there are at least three
901## # dots in it.
902 # Oh yes it does! Real implementations don't check this, and real
903 # cookies (of course) rely on that behaviour.
904 interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000905## self.assertEqual(len(c), 2)
906 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000907
908 def test_two_component_domain_rfc2965(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000909 pol = DefaultCookiePolicy(rfc2965=True)
910 c = CookieJar(pol)
911
912 # two-component V1 domain is OK
913 interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000914 self.assertEqual(len(c), 1)
915 self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar")
916 self.assertEqual(interact_2965(c, "http://foo.net/"),
917 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000918 # won't be returned to any other domain (because domain was implied)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000919 self.assertEqual(interact_2965(c, "http://www.foo.net/"), "")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000920
921 # unless domain is given explicitly, because then it must be
922 # rewritten to start with a dot: foo.net --> .foo.net, which does
923 # not domain-match foo.net
924 interact_2965(c, "http://foo.net/foo",
925 'spam=eggs; domain=foo.net; path=/foo; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000926 self.assertEqual(len(c), 1)
927 self.assertEqual(interact_2965(c, "http://foo.net/foo"),
928 "$Version=1; foo=bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000929
930 # explicit foo.net from three-component domain www.foo.net *does* get
931 # set, because .foo.net domain-matches .foo.net
932 interact_2965(c, "http://www.foo.net/foo/",
933 'spam=eggs; domain=foo.net; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000934 self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value,
935 "eggs")
936 self.assertEqual(len(c), 2)
937 self.assertEqual(interact_2965(c, "http://foo.net/foo/"),
938 "$Version=1; foo=bar")
939 self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"),
940 '$Version=1; spam=eggs; $Domain="foo.net"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000941
942 # top-level domain is too general
943 interact_2965(c, "http://foo.net/",
944 'ni="ni"; domain=".net"; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000945 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000946
947 # RFC 2965 doesn't require blocking this
948 interact_2965(c, "http://foo.co.uk/",
949 'nasty=trick; domain=.co.uk; Version="1"')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000950 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000951
952 def test_domain_allow(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000953 c = CookieJar(policy=DefaultCookiePolicy(
954 blocked_domains=["acme.com"],
955 allowed_domains=["www.acme.com"]))
956
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000957 req = urllib.request.Request("http://acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000958 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
959 res = FakeResponse(headers, "http://acme.com/")
960 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000961 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000962
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000963 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000964 res = FakeResponse(headers, "http://www.acme.com/")
965 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000966 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000967
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000968 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000969 res = FakeResponse(headers, "http://www.coyote.com/")
970 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000971 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000972
973 # set a cookie with non-allowed domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000974 req = urllib.request.Request("http://www.coyote.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000975 res = FakeResponse(headers, "http://www.coyote.com/")
976 cookies = c.make_cookies(res, req)
977 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +0000978 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000979 # ... and check is doesn't get returned
980 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +0200981 self.assertFalse(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000982
983 def test_domain_block(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000984 pol = DefaultCookiePolicy(
985 rfc2965=True, blocked_domains=[".acme.com"])
986 c = CookieJar(policy=pol)
987 headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
988
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000989 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000990 res = FakeResponse(headers, "http://www.acme.com/")
991 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000992 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000993
994 p = pol.set_blocked_domains(["acme.com"])
995 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000996 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +0000997
998 c.clear()
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000999 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001000 res = FakeResponse(headers, "http://www.roadrunner.net/")
1001 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001002 self.assertEqual(len(c), 1)
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001003 req = urllib.request.Request("http://www.roadrunner.net/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001004 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001005 self.assertTrue(req.has_header("Cookie"))
1006 self.assertTrue(req.has_header("Cookie2"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001007
1008 c.clear()
1009 pol.set_blocked_domains([".acme.com"])
1010 c.extract_cookies(res, req)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001011 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001012
1013 # set a cookie with blocked domain...
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001014 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001015 res = FakeResponse(headers, "http://www.acme.com/")
1016 cookies = c.make_cookies(res, req)
1017 c.set_cookie(cookies[0])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001018 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001019 # ... and check is doesn't get returned
1020 c.add_cookie_header(req)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001021 self.assertFalse(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001022
Xtreakca7fe502019-03-10 07:39:48 +05301023 c.clear()
1024
1025 pol.set_blocked_domains([])
1026 req = urllib.request.Request("http://acme.com/")
1027 res = FakeResponse(headers, "http://acme.com/")
1028 cookies = c.make_cookies(res, req)
1029 c.extract_cookies(res, req)
1030 self.assertEqual(len(c), 1)
1031
1032 req = urllib.request.Request("http://acme.com/")
1033 c.add_cookie_header(req)
1034 self.assertTrue(req.has_header("Cookie"))
1035
1036 req = urllib.request.Request("http://badacme.com/")
1037 c.add_cookie_header(req)
1038 self.assertFalse(pol.return_ok(cookies[0], req))
1039 self.assertFalse(req.has_header("Cookie"))
1040
1041 p = pol.set_blocked_domains(["acme.com"])
1042 req = urllib.request.Request("http://acme.com/")
1043 c.add_cookie_header(req)
1044 self.assertFalse(req.has_header("Cookie"))
1045
1046 req = urllib.request.Request("http://badacme.com/")
1047 c.add_cookie_header(req)
1048 self.assertFalse(req.has_header("Cookie"))
1049
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001050 def test_secure(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001051 for ns in True, False:
1052 for whitespace in " ", "":
1053 c = CookieJar()
1054 if ns:
1055 pol = DefaultCookiePolicy(rfc2965=False)
1056 int = interact_netscape
1057 vs = ""
1058 else:
1059 pol = DefaultCookiePolicy(rfc2965=True)
1060 int = interact_2965
1061 vs = "; Version=1"
1062 c.set_policy(pol)
1063 url = "http://www.acme.com/"
1064 int(c, url, "foo1=bar%s%s" % (vs, whitespace))
1065 int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001066 self.assertFalse(
1067 c._cookies["www.acme.com"]["/"]["foo1"].secure,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001068 "non-secure cookie registered secure")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001069 self.assertTrue(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001070 c._cookies["www.acme.com"]["/"]["foo2"].secure,
1071 "secure cookie registered non-secure")
1072
Paul Bailey4c339972018-10-08 13:49:29 -05001073 def test_secure_block(self):
1074 pol = DefaultCookiePolicy()
1075 c = CookieJar(policy=pol)
1076
1077 headers = ["Set-Cookie: session=narf; secure; path=/"]
1078 req = urllib.request.Request("https://www.acme.com/")
1079 res = FakeResponse(headers, "https://www.acme.com/")
1080 c.extract_cookies(res, req)
1081 self.assertEqual(len(c), 1)
1082
1083 req = urllib.request.Request("https://www.acme.com/")
1084 c.add_cookie_header(req)
1085 self.assertTrue(req.has_header("Cookie"))
1086
1087 req = urllib.request.Request("http://www.acme.com/")
1088 c.add_cookie_header(req)
1089 self.assertFalse(req.has_header("Cookie"))
1090
1091 # secure websocket protocol
1092 req = urllib.request.Request("wss://www.acme.com/")
1093 c.add_cookie_header(req)
1094 self.assertTrue(req.has_header("Cookie"))
1095
1096 # non-secure websocket protocol
1097 req = urllib.request.Request("ws://www.acme.com/")
1098 c.add_cookie_header(req)
1099 self.assertFalse(req.has_header("Cookie"))
1100
1101 def test_custom_secure_protocols(self):
1102 pol = DefaultCookiePolicy(secure_protocols=["foos"])
1103 c = CookieJar(policy=pol)
1104
1105 headers = ["Set-Cookie: session=narf; secure; path=/"]
1106 req = urllib.request.Request("https://www.acme.com/")
1107 res = FakeResponse(headers, "https://www.acme.com/")
1108 c.extract_cookies(res, req)
1109 self.assertEqual(len(c), 1)
1110
1111 # test https removed from secure protocol list
1112 req = urllib.request.Request("https://www.acme.com/")
1113 c.add_cookie_header(req)
1114 self.assertFalse(req.has_header("Cookie"))
1115
1116 req = urllib.request.Request("http://www.acme.com/")
1117 c.add_cookie_header(req)
1118 self.assertFalse(req.has_header("Cookie"))
1119
1120 req = urllib.request.Request("foos://www.acme.com/")
1121 c.add_cookie_header(req)
1122 self.assertTrue(req.has_header("Cookie"))
1123
1124 req = urllib.request.Request("foo://www.acme.com/")
1125 c.add_cookie_header(req)
1126 self.assertFalse(req.has_header("Cookie"))
1127
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001128 def test_quote_cookie_value(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001129 c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))
1130 interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')
1131 h = interact_2965(c, "http://www.acme.com/")
Ezio Melottib3aedd42010-11-20 19:04:17 +00001132 self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001133
1134 def test_missing_final_slash(self):
1135 # Missing slash from request URL's abs_path should be assumed present.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001136 url = "http://www.acme.com"
1137 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1138 interact_2965(c, url, "foo=bar; Version=1")
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001139 req = urllib.request.Request(url)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001140 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001141 c.add_cookie_header(req)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001142 self.assertTrue(req.has_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001143
1144 def test_domain_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001145 pol = DefaultCookiePolicy(rfc2965=True)
1146
1147 c = CookieJar(pol)
1148 url = "http://foo.bar.com/"
1149 interact_2965(c, url, "spam=eggs; Version=1")
1150 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001151 self.assertNotIn("Domain", h,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001152 "absent domain returned with domain present")
1153
1154 c = CookieJar(pol)
1155 url = "http://foo.bar.com/"
1156 interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
1157 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001158 self.assertIn('$Domain=".bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001159
1160 c = CookieJar(pol)
1161 url = "http://foo.bar.com/"
1162 # note missing initial dot in Domain
1163 interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
1164 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001165 self.assertIn('$Domain="bar.com"', h, "domain not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001166
1167 def test_path_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001168 pol = DefaultCookiePolicy(rfc2965=True)
1169
1170 c = CookieJar(pol)
1171 url = "http://foo.bar.com/"
1172 interact_2965(c, url, "spam=eggs; Version=1")
1173 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001174 self.assertNotIn("Path", h, "absent path returned with path present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001175
1176 c = CookieJar(pol)
1177 url = "http://foo.bar.com/"
1178 interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
1179 h = interact_2965(c, url)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001180 self.assertIn('$Path="/"', h, "path not returned")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001181
1182 def test_port_mirror(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001183 pol = DefaultCookiePolicy(rfc2965=True)
1184
1185 c = CookieJar(pol)
1186 url = "http://foo.bar.com/"
1187 interact_2965(c, url, "spam=eggs; Version=1")
1188 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001189 self.assertNotIn("Port", h, "absent port returned with port present")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001190
1191 c = CookieJar(pol)
1192 url = "http://foo.bar.com/"
1193 interact_2965(c, url, "spam=eggs; Version=1; Port")
1194 h = interact_2965(c, url)
R David Murray44b548d2016-09-08 13:59:53 -04001195 self.assertRegex(h, r"\$Port([^=]|$)",
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001196 "port with no value not returned with no value")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001197
1198 c = CookieJar(pol)
1199 url = "http://foo.bar.com/"
1200 interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
1201 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001202 self.assertIn('$Port="80"', h,
1203 "port with single value not returned with single value")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001204
1205 c = CookieJar(pol)
1206 url = "http://foo.bar.com/"
1207 interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
1208 h = interact_2965(c, url)
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001209 self.assertIn('$Port="80,8080"', h,
1210 "port with multiple values not returned with multiple "
1211 "values")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001212
1213 def test_no_return_comment(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001214 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1215 url = "http://foo.bar.com/"
1216 interact_2965(c, url, 'spam=eggs; Version=1; '
1217 'Comment="does anybody read these?"; '
1218 'CommentURL="http://foo.bar.net/comment.html"')
1219 h = interact_2965(c, url)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001220 self.assertNotIn("Comment", h,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001221 "Comment or CommentURL cookie-attributes returned to server")
1222
1223 def test_Cookie_iterator(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001224 cs = CookieJar(DefaultCookiePolicy(rfc2965=True))
1225 # add some random cookies
1226 interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '
1227 'Comment="does anybody read these?"; '
1228 'CommentURL="http://foo.bar.net/comment.html"')
1229 interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")
1230 interact_2965(cs, "http://www.acme.com/blah/",
1231 "foo=bar; secure; Version=1")
1232 interact_2965(cs, "http://www.acme.com/blah/",
1233 "foo=bar; path=/; Version=1")
1234 interact_2965(cs, "http://www.sol.no",
1235 r'bang=wallop; version=1; domain=".sol.no"; '
1236 r'port="90,100, 80,8080"; '
1237 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
1238
1239 versions = [1, 1, 1, 0, 1]
1240 names = ["bang", "foo", "foo", "spam", "foo"]
1241 domains = [".sol.no", "blah.spam.org", "www.acme.com",
1242 "www.acme.com", "www.acme.com"]
1243 paths = ["/", "/", "/", "/blah", "/blah/"]
1244
1245 for i in range(4):
1246 i = 0
1247 for c in cs:
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001248 self.assertIsInstance(c, Cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001249 self.assertEqual(c.version, versions[i])
1250 self.assertEqual(c.name, names[i])
1251 self.assertEqual(c.domain, domains[i])
1252 self.assertEqual(c.path, paths[i])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001253 i = i + 1
1254
1255 def test_parse_ns_headers(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001256 # missing domain value (invalid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001257 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001258 parse_ns_headers(["foo=bar; path=/; domain"]),
1259 [[("foo", "bar"),
1260 ("path", "/"), ("domain", None), ("version", "0")]]
1261 )
1262 # invalid expires value
Ezio Melottib3aedd42010-11-20 19:04:17 +00001263 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001264 parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),
1265 [[("foo", "bar"), ("expires", None), ("version", "0")]]
1266 )
1267 # missing cookie value (valid cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001268 self.assertEqual(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001269 parse_ns_headers(["foo"]),
1270 [[("foo", None), ("version", "0")]]
1271 )
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001272 # missing cookie values for parsed attributes
1273 self.assertEqual(
1274 parse_ns_headers(['foo=bar; expires']),
1275 [[('foo', 'bar'), ('expires', None), ('version', '0')]])
1276 self.assertEqual(
1277 parse_ns_headers(['foo=bar; version']),
1278 [[('foo', 'bar'), ('version', None)]])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001279 # shouldn't add version if header is empty
Ezio Melottib3aedd42010-11-20 19:04:17 +00001280 self.assertEqual(parse_ns_headers([""]), [])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001281
1282 def test_bad_cookie_header(self):
1283
1284 def cookiejar_from_cookie_headers(headers):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001285 c = CookieJar()
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001286 req = urllib.request.Request("http://www.example.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001287 r = FakeResponse(headers, "http://www.example.com/")
1288 c.extract_cookies(r, req)
1289 return c
1290
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001291 future = time2netscape(time.time()+3600)
1292
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001293 # none of these bad headers should cause an exception to be raised
1294 for headers in [
1295 ["Set-Cookie: "], # actually, nothing wrong with this
1296 ["Set-Cookie2: "], # ditto
1297 # missing domain value
1298 ["Set-Cookie2: a=foo; path=/; Version=1; domain"],
1299 # bad max-age
1300 ["Set-Cookie: b=foo; max-age=oops"],
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +00001301 # bad version
1302 ["Set-Cookie: b=foo; version=spam"],
Serhiy Storchaka577fc4e2015-03-13 09:05:01 +02001303 ["Set-Cookie:; Expires=%s" % future],
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001304 ]:
1305 c = cookiejar_from_cookie_headers(headers)
1306 # these bad cookies shouldn't be set
Ezio Melottib3aedd42010-11-20 19:04:17 +00001307 self.assertEqual(len(c), 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001308
1309 # cookie with invalid expires is treated as session cookie
1310 headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
1311 c = cookiejar_from_cookie_headers(headers)
1312 cookie = c._cookies["www.example.com"]["/"]["c"]
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001313 self.assertIsNone(cookie.expires)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001314
1315
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001316class LWPCookieTests(unittest.TestCase):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001317 # Tests taken from libwww-perl, with a few modifications and additions.
1318
1319 def test_netscape_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001320 #-------------------------------------------------------------------
1321 # First we check that it works for the original example at
1322 # http://www.netscape.com/newsref/std/cookie_spec.html
1323
1324 # Client requests a document, and receives in the response:
1325 #
1326 # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT
1327 #
1328 # When client requests a URL in path "/" on this server, it sends:
1329 #
1330 # Cookie: CUSTOMER=WILE_E_COYOTE
1331 #
1332 # Client requests a document, and receives in the response:
1333 #
1334 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1335 #
1336 # When client requests a URL in path "/" on this server, it sends:
1337 #
1338 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1339 #
1340 # Client receives:
1341 #
1342 # Set-Cookie: SHIPPING=FEDEX; path=/fo
1343 #
1344 # When client requests a URL in path "/" on this server, it sends:
1345 #
1346 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
1347 #
1348 # When client requests a URL in path "/foo" on this server, it sends:
1349 #
1350 # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX
1351 #
1352 # The last Cookie is buggy, because both specifications say that the
1353 # most specific cookie must be sent first. SHIPPING=FEDEX is the
1354 # most specific and should thus be first.
1355
1356 year_plus_one = time.localtime()[0] + 1
1357
1358 headers = []
1359
1360 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1361
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001362 #req = urllib.request.Request("http://1.1.1.1/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001363 # headers={"Host": "www.acme.com:80"})
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001364 req = urllib.request.Request("http://www.acme.com:80/",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001365 headers={"Host": "www.acme.com:80"})
1366
1367 headers.append(
1368 "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "
1369 "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)
1370 res = FakeResponse(headers, "http://www.acme.com/")
1371 c.extract_cookies(res, req)
1372
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001373 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001374 c.add_cookie_header(req)
1375
1376 self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")
1377 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
1378
1379 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1380 res = FakeResponse(headers, "http://www.acme.com/")
1381 c.extract_cookies(res, req)
1382
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001383 req = urllib.request.Request("http://www.acme.com/foo/bar")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001384 c.add_cookie_header(req)
1385
1386 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001387 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1388 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001389
1390 headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
1391 res = FakeResponse(headers, "http://www.acme.com")
1392 c.extract_cookies(res, req)
1393
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001394 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001395 c.add_cookie_header(req)
1396
1397 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001398 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1399 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1400 self.assertNotIn("SHIPPING=FEDEX", h)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001401
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001402 req = urllib.request.Request("http://www.acme.com/foo/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001403 c.add_cookie_header(req)
1404
1405 h = req.get_header("Cookie")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001406 self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
1407 self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
1408 self.assertTrue(h.startswith("SHIPPING=FEDEX;"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001409
1410 def test_netscape_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001411 # Second Example transaction sequence:
1412 #
1413 # Assume all mappings from above have been cleared.
1414 #
1415 # Client receives:
1416 #
1417 # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
1418 #
1419 # When client requests a URL in path "/" on this server, it sends:
1420 #
1421 # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001
1422 #
1423 # Client receives:
1424 #
1425 # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
1426 #
1427 # When client requests a URL in path "/ammo" on this server, it sends:
1428 #
1429 # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001
1430 #
1431 # NOTE: There are two name/value pairs named "PART_NUMBER" due to
1432 # the inheritance of the "/" mapping in addition to the "/ammo" mapping.
1433
1434 c = CookieJar()
1435 headers = []
1436
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001437 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001438 headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
1439 res = FakeResponse(headers, "http://www.acme.com/")
1440
1441 c.extract_cookies(res, req)
1442
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001443 req = urllib.request.Request("http://www.acme.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001444 c.add_cookie_header(req)
1445
Ezio Melottib3aedd42010-11-20 19:04:17 +00001446 self.assertEqual(req.get_header("Cookie"),
1447 "PART_NUMBER=ROCKET_LAUNCHER_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001448
1449 headers.append(
1450 "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")
1451 res = FakeResponse(headers, "http://www.acme.com/")
1452 c.extract_cookies(res, req)
1453
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001454 req = urllib.request.Request("http://www.acme.com/ammo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001455 c.add_cookie_header(req)
1456
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001457 self.assertRegex(req.get_header("Cookie"),
1458 r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
1459 "PART_NUMBER=ROCKET_LAUNCHER_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001460
1461 def test_ietf_example_1(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001462 #-------------------------------------------------------------------
1463 # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt
1464 #
1465 # 5. EXAMPLES
1466
1467 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1468
1469 #
1470 # 5.1 Example 1
1471 #
1472 # Most detail of request and response headers has been omitted. Assume
1473 # the user agent has no stored cookies.
1474 #
1475 # 1. User Agent -> Server
1476 #
1477 # POST /acme/login HTTP/1.1
1478 # [form data]
1479 #
1480 # User identifies self via a form.
1481 #
1482 # 2. Server -> User Agent
1483 #
1484 # HTTP/1.1 200 OK
1485 # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
1486 #
1487 # Cookie reflects user's identity.
1488
1489 cookie = interact_2965(
1490 c, 'http://www.acme.com/acme/login',
1491 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001492 self.assertFalse(cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001493
1494 #
1495 # 3. User Agent -> Server
1496 #
1497 # POST /acme/pickitem HTTP/1.1
1498 # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
1499 # [form data]
1500 #
1501 # User selects an item for ``shopping basket.''
1502 #
1503 # 4. Server -> User Agent
1504 #
1505 # HTTP/1.1 200 OK
1506 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1507 # Path="/acme"
1508 #
1509 # Shopping basket contains an item.
1510
1511 cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
1512 'Part_Number="Rocket_Launcher_0001"; '
1513 'Version="1"; Path="/acme"');
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001514 self.assertRegex(cookie,
1515 r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001516
1517 #
1518 # 5. User Agent -> Server
1519 #
1520 # POST /acme/shipping HTTP/1.1
1521 # Cookie: $Version="1";
1522 # Customer="WILE_E_COYOTE"; $Path="/acme";
1523 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1524 # [form data]
1525 #
1526 # User selects shipping method from form.
1527 #
1528 # 6. Server -> User Agent
1529 #
1530 # HTTP/1.1 200 OK
1531 # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"
1532 #
1533 # New cookie reflects shipping method.
1534
1535 cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
1536 'Shipping="FedEx"; Version="1"; Path="/acme"')
1537
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001538 self.assertRegex(cookie, r'^\$Version="?1"?;')
1539 self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;'
R David Murray44b548d2016-09-08 13:59:53 -04001540 r'\s*\$Path="\/acme"')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001541 self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;'
R David Murray44b548d2016-09-08 13:59:53 -04001542 r'\s*\$Path="\/acme"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001543
1544 #
1545 # 7. User Agent -> Server
1546 #
1547 # POST /acme/process HTTP/1.1
1548 # Cookie: $Version="1";
1549 # Customer="WILE_E_COYOTE"; $Path="/acme";
1550 # Part_Number="Rocket_Launcher_0001"; $Path="/acme";
1551 # Shipping="FedEx"; $Path="/acme"
1552 # [form data]
1553 #
1554 # User chooses to process order.
1555 #
1556 # 8. Server -> User Agent
1557 #
1558 # HTTP/1.1 200 OK
1559 #
1560 # Transaction is complete.
1561
1562 cookie = interact_2965(c, "http://www.acme.com/acme/process")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001563 self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"')
1564 self.assertIn("WILE_E_COYOTE", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001565
1566 #
1567 # The user agent makes a series of requests on the origin server, after
1568 # each of which it receives a new cookie. All the cookies have the same
1569 # Path attribute and (default) domain. Because the request URLs all have
1570 # /acme as a prefix, and that matches the Path attribute, each request
1571 # contains all the cookies received so far.
1572
1573 def test_ietf_example_2(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001574 # 5.2 Example 2
1575 #
1576 # This example illustrates the effect of the Path attribute. All detail
1577 # of request and response headers has been omitted. Assume the user agent
1578 # has no stored cookies.
1579
1580 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1581
1582 # Imagine the user agent has received, in response to earlier requests,
1583 # the response headers
1584 #
1585 # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
1586 # Path="/acme"
1587 #
1588 # and
1589 #
1590 # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
1591 # Path="/acme/ammo"
1592
1593 interact_2965(
1594 c, "http://www.acme.com/acme/ammo/specific",
1595 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
1596 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
1597
1598 # A subsequent request by the user agent to the (same) server for URLs of
1599 # the form /acme/ammo/... would include the following request header:
1600 #
1601 # Cookie: $Version="1";
1602 # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
1603 # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1604 #
1605 # Note that the NAME=VALUE pair for the cookie with the more specific Path
1606 # attribute, /acme/ammo, comes before the one with the less specific Path
1607 # attribute, /acme. Further note that the same cookie name appears more
1608 # than once.
1609
1610 cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001611 self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001612
1613 # A subsequent request by the user agent to the (same) server for a URL of
1614 # the form /acme/parts/ would include the following request header:
1615 #
1616 # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
1617 #
1618 # Here, the second cookie's Path attribute /acme/ammo is not a prefix of
1619 # the request URL, /acme/parts/, so the cookie does not get forwarded to
1620 # the server.
1621
1622 cookie = interact_2965(c, "http://www.acme.com/acme/parts/")
Ezio Melottib58e0bd2010-01-23 15:40:09 +00001623 self.assertIn("Rocket_Launcher_0001", cookie)
1624 self.assertNotIn("Riding_Rocket_0023", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001625
1626 def test_rejection(self):
1627 # Test rejection of Set-Cookie2 responses based on domain, path, port.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001628 pol = DefaultCookiePolicy(rfc2965=True)
1629
1630 c = LWPCookieJar(policy=pol)
1631
1632 max_age = "max-age=3600"
1633
1634 # illegal domain (no embedded dots)
1635 cookie = interact_2965(c, "http://www.acme.com",
1636 'foo=bar; domain=".com"; version=1')
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001637 self.assertFalse(c)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001638
1639 # legal domain
1640 cookie = interact_2965(c, "http://www.acme.com",
1641 'ping=pong; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001642 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001643
1644 # illegal domain (host prefix "www.a" contains a dot)
1645 cookie = interact_2965(c, "http://www.a.acme.com",
1646 'whiz=bang; domain="acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001647 self.assertEqual(len(c), 1)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001648
1649 # legal domain
1650 cookie = interact_2965(c, "http://www.a.acme.com",
1651 'wow=flutter; domain=".a.acme.com"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001652 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001653
1654 # can't partially match an IP-address
1655 cookie = interact_2965(c, "http://125.125.125.125",
1656 'zzzz=ping; domain="125.125.125"; version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001657 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001658
1659 # illegal path (must be prefix of request path)
1660 cookie = interact_2965(c, "http://www.sol.no",
1661 'blah=rhubarb; domain=".sol.no"; path="/foo"; '
1662 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001663 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001664
1665 # legal path
1666 cookie = interact_2965(c, "http://www.sol.no/foo/bar",
1667 'bing=bong; domain=".sol.no"; path="/foo"; '
1668 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001669 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001670
1671 # illegal port (request-port not in list)
1672 cookie = interact_2965(c, "http://www.sol.no",
1673 'whiz=ffft; domain=".sol.no"; port="90,100"; '
1674 'version=1')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001675 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001676
1677 # legal port
1678 cookie = interact_2965(
1679 c, "http://www.sol.no",
1680 r'bang=wallop; version=1; domain=".sol.no"; '
1681 r'port="90,100, 80,8080"; '
1682 r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001683 self.assertEqual(len(c), 4)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001684
1685 # port attribute without any value (current port)
1686 cookie = interact_2965(c, "http://www.sol.no",
1687 'foo9=bar; version=1; domain=".sol.no"; port; '
1688 'max-age=100;')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001689 self.assertEqual(len(c), 5)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001690
1691 # encoded path
1692 # LWP has this test, but unescaping allowed path characters seems
1693 # like a bad idea, so I think this should fail:
1694## cookie = interact_2965(c, "http://www.sol.no/foo/",
1695## r'foo8=bar; version=1; path="/%66oo"')
1696 # but this is OK, because '<' is not an allowed HTTP URL path
1697 # character:
1698 cookie = interact_2965(c, "http://www.sol.no/<oo/",
1699 r'foo8=bar; version=1; path="/%3coo"')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001700 self.assertEqual(len(c), 6)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001701
1702 # save and restore
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001703 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001704
1705 try:
1706 c.save(filename, ignore_discard=True)
1707 old = repr(c)
1708
1709 c = LWPCookieJar(policy=pol)
1710 c.load(filename, ignore_discard=True)
1711 finally:
1712 try: os.unlink(filename)
1713 except OSError: pass
1714
Ezio Melottib3aedd42010-11-20 19:04:17 +00001715 self.assertEqual(old, repr(c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001716
1717 def test_url_encoding(self):
1718 # Try some URL encodings of the PATHs.
1719 # (the behaviour here has changed from libwww-perl)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001720 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
Guido van Rossum52dbbb92008-08-18 21:44:30 +00001721 interact_2965(c, "http://www.acme.com/foo%2f%25/"
1722 "%3c%3c%0Anew%C3%A5/%C3%A5",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001723 "foo = bar; version = 1")
1724
1725 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001726 c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001727 'bar=baz; path="/foo/"; version=1');
1728 version_re = re.compile(r'^\$version=\"?1\"?', re.I)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001729 self.assertIn("foo=bar", cookie)
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001730 self.assertRegex(cookie, version_re)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001731
1732 cookie = interact_2965(
Guido van Rossumf520c052007-07-23 03:46:37 +00001733 c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001734 self.assertFalse(cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001735
1736 # unicode URL doesn't raise exception
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001737 cookie = interact_2965(c, "http://www.acme.com/\xfc")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001738
1739 def test_mozilla(self):
1740 # Save / load Mozilla/Netscape cookie file format.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001741 year_plus_one = time.localtime()[0] + 1
1742
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001743 filename = test.support.TESTFN
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001744
1745 c = MozillaCookieJar(filename,
1746 policy=DefaultCookiePolicy(rfc2965=True))
1747 interact_2965(c, "http://www.acme.com/",
1748 "foo1=bar; max-age=100; Version=1")
1749 interact_2965(c, "http://www.acme.com/",
1750 'foo2=bar; port="80"; max-age=100; Discard; Version=1')
1751 interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")
1752
1753 expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
1754 interact_netscape(c, "http://www.foo.com/",
1755 "fooa=bar; %s" % expires)
1756 interact_netscape(c, "http://www.foo.com/",
1757 "foob=bar; Domain=.foo.com; %s" % expires)
1758 interact_netscape(c, "http://www.foo.com/",
1759 "fooc=bar; Domain=www.foo.com; %s" % expires)
1760
1761 def save_and_restore(cj, ignore_discard):
1762 try:
1763 cj.save(ignore_discard=ignore_discard)
1764 new_c = MozillaCookieJar(filename,
1765 DefaultCookiePolicy(rfc2965=True))
1766 new_c.load(ignore_discard=ignore_discard)
1767 finally:
1768 try: os.unlink(filename)
1769 except OSError: pass
1770 return new_c
1771
1772 new_c = save_and_restore(c, True)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001773 self.assertEqual(len(new_c), 6) # none discarded
Benjamin Peterson577473f2010-01-19 00:09:57 +00001774 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001775
1776 new_c = save_and_restore(c, False)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001777 self.assertEqual(len(new_c), 4) # 2 of them discarded on save
Benjamin Peterson577473f2010-01-19 00:09:57 +00001778 self.assertIn("name='foo1', value='bar'", repr(new_c))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001779
1780 def test_netscape_misc(self):
1781 # Some additional Netscape cookies tests.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001782 c = CookieJar()
1783 headers = []
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001784 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001785
1786 # Netscape allows a host part that contains dots
1787 headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
1788 res = FakeResponse(headers, "http://www.acme.com/foo")
1789 c.extract_cookies(res, req)
1790
1791 # and that the domain is the same as the host without adding a leading
1792 # dot to the domain. Should not quote even if strange chars are used
1793 # in the cookie value.
1794 headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
1795 res = FakeResponse(headers, "http://www.acme.com/foo")
1796 c.extract_cookies(res, req)
1797
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001798 req = urllib.request.Request("http://foo.bar.acme.com/foo")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001799 c.add_cookie_header(req)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001800 self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie"))
1801 self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie"))
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001802
1803 def test_intranet_domains_2965(self):
1804 # Test handling of local intranet hostnames without a dot.
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001805 c = CookieJar(DefaultCookiePolicy(rfc2965=True))
1806 interact_2965(c, "http://example/",
1807 "foo1=bar; PORT; Discard; Version=1;")
1808 cookie = interact_2965(c, "http://example/",
1809 'foo2=bar; domain=".local"; Version=1')
Benjamin Peterson577473f2010-01-19 00:09:57 +00001810 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001811
1812 interact_2965(c, "http://example/", 'foo3=bar; Version=1')
1813 cookie = interact_2965(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001814 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001815 self.assertEqual(len(c), 3)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001816
1817 def test_intranet_domains_ns(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001818 c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
1819 interact_netscape(c, "http://example/", "foo1=bar")
1820 cookie = interact_netscape(c, "http://example/",
1821 'foo2=bar; domain=.local')
Ezio Melottib3aedd42010-11-20 19:04:17 +00001822 self.assertEqual(len(c), 2)
Benjamin Peterson577473f2010-01-19 00:09:57 +00001823 self.assertIn("foo1=bar", cookie)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001824
1825 cookie = interact_netscape(c, "http://example/")
Benjamin Peterson577473f2010-01-19 00:09:57 +00001826 self.assertIn("foo2=bar", cookie)
Ezio Melottib3aedd42010-11-20 19:04:17 +00001827 self.assertEqual(len(c), 2)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001828
1829 def test_empty_path(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001830 # Test for empty path
1831 # Broken web-server ORION/1.3.38 returns to the client response like
1832 #
1833 # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
1834 #
1835 # ie. with Path set to nothing.
1836 # In this case, extract_cookies() must set cookie to / (root)
1837 c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
1838 headers = []
1839
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001840 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001841 headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
1842 res = FakeResponse(headers, "http://www.ants.com/")
1843 c.extract_cookies(res, req)
1844
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001845 req = urllib.request.Request("http://www.ants.com/")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001846 c.add_cookie_header(req)
1847
Ezio Melottib3aedd42010-11-20 19:04:17 +00001848 self.assertEqual(req.get_header("Cookie"),
1849 "JSESSIONID=ABCDERANDOM123")
1850 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001851
1852 # missing path in the request URI
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001853 req = urllib.request.Request("http://www.ants.com:8080")
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001854 c.add_cookie_header(req)
1855
Ezio Melottib3aedd42010-11-20 19:04:17 +00001856 self.assertEqual(req.get_header("Cookie"),
1857 "JSESSIONID=ABCDERANDOM123")
1858 self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001859
1860 def test_session_cookies(self):
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001861 year_plus_one = time.localtime()[0] + 1
1862
1863 # Check session cookies are deleted properly by
1864 # CookieJar.clear_session_cookies method
1865
Jeremy Hylton1afc1692008-06-18 20:49:58 +00001866 req = urllib.request.Request('http://www.perlmeister.com/scripts')
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001867 headers = []
1868 headers.append("Set-Cookie: s1=session;Path=/scripts")
1869 headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"
1870 "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %
1871 year_plus_one)
1872 headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "
1873 "02-Feb-%d 23:24:20 GMT" % year_plus_one)
1874 headers.append("Set-Cookie: s2=session;Path=/scripts;"
1875 "Domain=.perlmeister.com")
1876 headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
1877 res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
1878
1879 c = CookieJar()
1880 c.extract_cookies(res, req)
1881 # How many session/permanent cookies do we have?
1882 counter = {"session_after": 0,
1883 "perm_after": 0,
1884 "session_before": 0,
1885 "perm_before": 0}
1886 for cookie in c:
1887 key = "%s_before" % cookie.value
1888 counter[key] = counter[key] + 1
1889 c.clear_session_cookies()
1890 # How many now?
1891 for cookie in c:
1892 key = "%s_after" % cookie.value
1893 counter[key] = counter[key] + 1
1894
Martin Panter46f50722016-05-26 05:35:26 +00001895 # a permanent cookie got lost accidentally
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001896 self.assertEqual(counter["perm_after"], counter["perm_before"])
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001897 # a session cookie hasn't been cleared
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001898 self.assertEqual(counter["session_after"], 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001899 # we didn't have session cookies in the first place
Serhiy Storchaka9d282f62013-11-17 13:45:02 +02001900 self.assertNotEqual(counter["session_before"], 0)
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001901
1902
1903def test_main(verbose=None):
Gregory P. Smith41e6c3d2010-07-19 23:17:22 +00001904 test.support.run_unittest(
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001905 DateTimeTests,
1906 HeaderTests,
1907 CookieTests,
Martin v. Löwisc5574e82005-03-03 10:57:37 +00001908 FileCookieJarTests,
Martin v. Löwis2a6ba902004-05-31 18:22:40 +00001909 LWPCookieTests,
1910 )
1911
1912if __name__ == "__main__":
1913 test_main(verbose=True)