Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 1 | """Tests for http/cookiejar.py.""" |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 2 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 3 | import os |
| 4 | import re |
| 5 | import test.support |
| 6 | import time |
| 7 | import unittest |
| 8 | import urllib.request |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 9 | |
Ezio Melotti | 1d237e5 | 2013-08-10 18:20:09 +0300 | [diff] [blame] | 10 | from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape, |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 11 | parse_ns_headers, join_header_words, split_header_words, Cookie, |
| 12 | CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, |
| 13 | LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path, |
| 14 | reach, is_HDN, domain_match, user_domain_match, request_path, |
| 15 | request_port, request_host) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 16 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 17 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 18 | class DateTimeTests(unittest.TestCase): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 19 | |
| 20 | def test_time2isoz(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 21 | base = 1019227000 |
| 22 | day = 24*3600 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 23 | self.assertEqual(time2isoz(base), "2002-04-19 14:36:40Z") |
| 24 | self.assertEqual(time2isoz(base+day), "2002-04-20 14:36:40Z") |
| 25 | self.assertEqual(time2isoz(base+2*day), "2002-04-21 14:36:40Z") |
| 26 | self.assertEqual(time2isoz(base+3*day), "2002-04-22 14:36:40Z") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 27 | |
| 28 | az = time2isoz() |
| 29 | bz = time2isoz(500000) |
| 30 | for text in (az, bz): |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 31 | self.assertRegex(text, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$", |
| 32 | "bad time2isoz format: %s %s" % (az, bz)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 33 | |
| 34 | def test_http2time(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 35 | def parse_date(text): |
| 36 | return time.gmtime(http2time(text))[:6] |
| 37 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 38 | self.assertEqual(parse_date("01 Jan 2001"), (2001, 1, 1, 0, 0, 0.0)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 39 | |
| 40 | # this test will break around year 2070 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 41 | self.assertEqual(parse_date("03-Feb-20"), (2020, 2, 3, 0, 0, 0.0)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 42 | |
| 43 | # this test will break around year 2048 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 44 | self.assertEqual(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 45 | |
| 46 | def test_http2time_formats(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 47 | # test http2time for supported dates. Test cases with 2 digit year |
| 48 | # will probably break in year 2044. |
| 49 | tests = [ |
| 50 | 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format |
| 51 | 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format |
| 52 | 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format |
| 53 | |
| 54 | '03 Feb 1994 00:00:00 GMT', # HTTP format (no weekday) |
| 55 | '03-Feb-94 00:00:00 GMT', # old rfc850 (no weekday) |
| 56 | '03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday) |
| 57 | '03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds) |
| 58 | '03-Feb-1994 00:00', # broken rfc850 (no weekday, no seconds, no tz) |
Ezio Melotti | 7ac17f8 | 2013-08-10 18:07:25 +0300 | [diff] [blame] | 59 | '02-Feb-1994 24:00', # broken rfc850 (no weekday, no seconds, |
| 60 | # no tz) using hour 24 with yesterday date |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 61 | |
| 62 | '03-Feb-94', # old rfc850 HTTP format (no weekday, no time) |
| 63 | '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time) |
| 64 | '03 Feb 1994', # proposed new HTTP format (no weekday, no time) |
| 65 | |
| 66 | # A few tests with extra space at various places |
| 67 | ' 03 Feb 1994 0:00 ', |
| 68 | ' 03-Feb-1994 ', |
| 69 | ] |
| 70 | |
| 71 | test_t = 760233600 # assume broken POSIX counting of seconds |
| 72 | result = time2isoz(test_t) |
| 73 | expected = "1994-02-03 00:00:00Z" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 74 | self.assertEqual(result, expected, |
| 75 | "%s => '%s' (%s)" % (test_t, result, expected)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 76 | |
| 77 | for s in tests: |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 78 | self.assertEqual(http2time(s), test_t, s) |
| 79 | self.assertEqual(http2time(s.lower()), test_t, s.lower()) |
| 80 | self.assertEqual(http2time(s.upper()), test_t, s.upper()) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 81 | |
| 82 | def test_http2time_garbage(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 83 | for test in [ |
| 84 | '', |
| 85 | 'Garbage', |
| 86 | 'Mandag 16. September 1996', |
| 87 | '01-00-1980', |
| 88 | '01-13-1980', |
| 89 | '00-01-1980', |
| 90 | '32-01-1980', |
| 91 | '01-01-1980 25:00:00', |
| 92 | '01-01-1980 00:61:00', |
| 93 | '01-01-1980 00:00:62', |
| 94 | ]: |
Ezio Melotti | 1d237e5 | 2013-08-10 18:20:09 +0300 | [diff] [blame] | 95 | self.assertIsNone(http2time(test), |
| 96 | "http2time(%s) is not None\n" |
| 97 | "http2time(test) %s" % (test, http2time(test))) |
| 98 | |
| 99 | def test_iso2time(self): |
| 100 | def parse_date(text): |
| 101 | return time.gmtime(iso2time(text))[:6] |
| 102 | |
| 103 | # ISO 8601 compact format |
| 104 | self.assertEqual(parse_date("19940203T141529Z"), |
| 105 | (1994, 2, 3, 14, 15, 29)) |
| 106 | |
| 107 | # ISO 8601 with time behind UTC |
| 108 | self.assertEqual(parse_date("1994-02-03 07:15:29 -0700"), |
| 109 | (1994, 2, 3, 14, 15, 29)) |
| 110 | |
| 111 | # ISO 8601 with time ahead of UTC |
| 112 | self.assertEqual(parse_date("1994-02-03 19:45:29 +0530"), |
| 113 | (1994, 2, 3, 14, 15, 29)) |
| 114 | |
| 115 | def test_iso2time_formats(self): |
| 116 | # test iso2time for supported dates. |
| 117 | tests = [ |
| 118 | '1994-02-03 00:00:00 -0000', # ISO 8601 format |
| 119 | '1994-02-03 00:00:00 +0000', # ISO 8601 format |
| 120 | '1994-02-03 00:00:00', # zone is optional |
| 121 | '1994-02-03', # only date |
| 122 | '1994-02-03T00:00:00', # Use T as separator |
| 123 | '19940203', # only date |
| 124 | '1994-02-02 24:00:00', # using hour-24 yesterday date |
| 125 | '19940203T000000Z', # ISO 8601 compact format |
| 126 | |
| 127 | # A few tests with extra space at various places |
| 128 | ' 1994-02-03 ', |
| 129 | ' 1994-02-03T00:00:00 ', |
| 130 | ] |
| 131 | |
| 132 | test_t = 760233600 # assume broken POSIX counting of seconds |
| 133 | for s in tests: |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 134 | self.assertEqual(iso2time(s), test_t, s) |
| 135 | self.assertEqual(iso2time(s.lower()), test_t, s.lower()) |
| 136 | self.assertEqual(iso2time(s.upper()), test_t, s.upper()) |
Ezio Melotti | 1d237e5 | 2013-08-10 18:20:09 +0300 | [diff] [blame] | 137 | |
| 138 | def test_iso2time_garbage(self): |
| 139 | for test in [ |
| 140 | '', |
| 141 | 'Garbage', |
| 142 | 'Thursday, 03-Feb-94 00:00:00 GMT', |
| 143 | '1980-00-01', |
| 144 | '1980-13-01', |
| 145 | '1980-01-00', |
| 146 | '1980-01-32', |
| 147 | '1980-01-01 25:00:00', |
| 148 | '1980-01-01 00:61:00', |
| 149 | '01-01-1980 00:00:62', |
| 150 | '01-01-1980T00:00:62', |
| 151 | '19800101T250000Z' |
| 152 | '1980-01-01 00:00:00 -2500', |
| 153 | ]: |
| 154 | self.assertIsNone(iso2time(test), |
| 155 | "iso2time(%s) is not None\n" |
| 156 | "iso2time(test) %s" % (test, iso2time(test))) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 157 | |
| 158 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 159 | class HeaderTests(unittest.TestCase): |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 160 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 161 | def test_parse_ns_headers(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 162 | # quotes should be stripped |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 163 | expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]] |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 164 | for hdr in [ |
Martin v. Löwis | 4ea3ead | 2005-03-03 10:48:12 +0000 | [diff] [blame] | 165 | 'foo=bar; expires=01 Jan 2040 22:23:32 GMT', |
| 166 | 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"', |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 167 | ]: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 168 | self.assertEqual(parse_ns_headers([hdr]), expected) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 169 | |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 170 | def test_parse_ns_headers_version(self): |
| 171 | |
| 172 | # quotes should be stripped |
| 173 | expected = [[('foo', 'bar'), ('version', '1')]] |
| 174 | for hdr in [ |
| 175 | 'foo=bar; version="1"', |
| 176 | 'foo=bar; Version="1"', |
| 177 | ]: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 178 | self.assertEqual(parse_ns_headers([hdr]), expected) |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 179 | |
Martin v. Löwis | 4ea3ead | 2005-03-03 10:48:12 +0000 | [diff] [blame] | 180 | def test_parse_ns_headers_special_names(self): |
| 181 | # names such as 'expires' are not special in first name=value pair |
| 182 | # of Set-Cookie: header |
Martin v. Löwis | 4ea3ead | 2005-03-03 10:48:12 +0000 | [diff] [blame] | 183 | # Cookie with name 'expires' |
| 184 | hdr = 'expires=01 Jan 2040 22:23:32 GMT' |
| 185 | expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 186 | self.assertEqual(parse_ns_headers([hdr]), expected) |
Martin v. Löwis | 4ea3ead | 2005-03-03 10:48:12 +0000 | [diff] [blame] | 187 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 188 | def test_join_header_words(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 189 | joined = join_header_words([[("foo", None), ("bar", "baz")]]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 190 | self.assertEqual(joined, "foo; bar=baz") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 191 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 192 | self.assertEqual(join_header_words([[]]), "") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 193 | |
| 194 | def test_split_header_words(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 195 | tests = [ |
| 196 | ("foo", [[("foo", None)]]), |
| 197 | ("foo=bar", [[("foo", "bar")]]), |
| 198 | (" foo ", [[("foo", None)]]), |
| 199 | (" foo= ", [[("foo", "")]]), |
| 200 | (" foo=", [[("foo", "")]]), |
| 201 | (" foo= ; ", [[("foo", "")]]), |
| 202 | (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]), |
| 203 | ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]), |
| 204 | # doesn't really matter if this next fails, but it works ATM |
| 205 | ("foo= bar=baz", [[("foo", "bar=baz")]]), |
| 206 | ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]), |
| 207 | ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]), |
| 208 | ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]), |
| 209 | (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ', |
| 210 | [[("foo", None), ("bar", "baz")], |
| 211 | [("spam", "")], [("foo", ',;"')], [("bar", "")]]), |
| 212 | ] |
| 213 | |
| 214 | for arg, expect in tests: |
| 215 | try: |
| 216 | result = split_header_words([arg]) |
| 217 | except: |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 218 | import traceback, io |
| 219 | f = io.StringIO() |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 220 | traceback.print_exc(None, f) |
| 221 | result = "(error -- traceback follows)\n\n%s" % f.getvalue() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 222 | self.assertEqual(result, expect, """ |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 223 | When parsing: '%s' |
| 224 | Expected: '%s' |
| 225 | Got: '%s' |
| 226 | """ % (arg, expect, result)) |
| 227 | |
| 228 | def test_roundtrip(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 229 | tests = [ |
| 230 | ("foo", "foo"), |
| 231 | ("foo=bar", "foo=bar"), |
| 232 | (" foo ", "foo"), |
| 233 | ("foo=", 'foo=""'), |
| 234 | ("foo=bar bar=baz", "foo=bar; bar=baz"), |
| 235 | ("foo=bar;bar=baz", "foo=bar; bar=baz"), |
| 236 | ('foo bar baz', "foo; bar; baz"), |
| 237 | (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'), |
| 238 | ('foo,,,bar', 'foo, bar'), |
| 239 | ('foo=bar,bar=baz', 'foo=bar, bar=baz'), |
| 240 | |
| 241 | ('text/html; charset=iso-8859-1', |
| 242 | 'text/html; charset="iso-8859-1"'), |
| 243 | |
| 244 | ('foo="bar"; port="80,81"; discard, bar=baz', |
| 245 | 'foo=bar; port="80,81"; discard, bar=baz'), |
| 246 | |
| 247 | (r'Basic realm="\"foo\\\\bar\""', |
| 248 | r'Basic; realm="\"foo\\\\bar\""') |
| 249 | ] |
| 250 | |
| 251 | for arg, expect in tests: |
| 252 | input = split_header_words([arg]) |
| 253 | res = join_header_words(input) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 254 | self.assertEqual(res, expect, """ |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 255 | When parsing: '%s' |
| 256 | Expected: '%s' |
| 257 | Got: '%s' |
| 258 | Input was: '%s' |
| 259 | """ % (arg, expect, res, input)) |
| 260 | |
| 261 | |
| 262 | class FakeResponse: |
| 263 | def __init__(self, headers=[], url=None): |
| 264 | """ |
| 265 | headers: list of RFC822-style 'Key: value' strings |
| 266 | """ |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 267 | import email |
| 268 | self._headers = email.message_from_string("\n".join(headers)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 269 | self._url = url |
| 270 | def info(self): return self._headers |
| 271 | |
| 272 | def interact_2965(cookiejar, url, *set_cookie_hdrs): |
| 273 | return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2") |
| 274 | |
| 275 | def interact_netscape(cookiejar, url, *set_cookie_hdrs): |
| 276 | return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie") |
| 277 | |
| 278 | def _interact(cookiejar, url, set_cookie_hdrs, hdr_name): |
| 279 | """Perform a single request / response cycle, returning Cookie: header.""" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 280 | req = urllib.request.Request(url) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 281 | cookiejar.add_cookie_header(req) |
| 282 | cookie_hdr = req.get_header("Cookie", "") |
| 283 | headers = [] |
| 284 | for hdr in set_cookie_hdrs: |
| 285 | headers.append("%s: %s" % (hdr_name, hdr)) |
| 286 | res = FakeResponse(headers, url) |
| 287 | cookiejar.extract_cookies(res, req) |
| 288 | return cookie_hdr |
| 289 | |
| 290 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 291 | class FileCookieJarTests(unittest.TestCase): |
Martin v. Löwis | c5574e8 | 2005-03-03 10:57:37 +0000 | [diff] [blame] | 292 | def test_lwp_valueless_cookie(self): |
| 293 | # cookies with no value should be saved and loaded consistently |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 294 | filename = test.support.TESTFN |
Martin v. Löwis | c5574e8 | 2005-03-03 10:57:37 +0000 | [diff] [blame] | 295 | c = LWPCookieJar() |
| 296 | interact_netscape(c, "http://www.acme.com/", 'boo') |
| 297 | self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None) |
| 298 | try: |
| 299 | c.save(filename, ignore_discard=True) |
| 300 | c = LWPCookieJar() |
| 301 | c.load(filename, ignore_discard=True) |
| 302 | finally: |
| 303 | try: os.unlink(filename) |
| 304 | except OSError: pass |
| 305 | self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None) |
| 306 | |
Neal Norwitz | 3e7de59 | 2005-12-23 21:24:35 +0000 | [diff] [blame] | 307 | def test_bad_magic(self): |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 308 | # OSErrors (eg. file doesn't exist) are allowed to propagate |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 309 | filename = test.support.TESTFN |
Neal Norwitz | 3e7de59 | 2005-12-23 21:24:35 +0000 | [diff] [blame] | 310 | for cookiejar_class in LWPCookieJar, MozillaCookieJar: |
| 311 | c = cookiejar_class() |
| 312 | try: |
| 313 | c.load(filename="for this test to work, a file with this " |
| 314 | "filename should not exist") |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 315 | except OSError as exc: |
| 316 | # an OSError subclass (likely FileNotFoundError), but not |
| 317 | # LoadError |
| 318 | self.assertIsNot(exc.__class__, LoadError) |
Neal Norwitz | 3e7de59 | 2005-12-23 21:24:35 +0000 | [diff] [blame] | 319 | else: |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 320 | self.fail("expected OSError for invalid filename") |
Neal Norwitz | 3e7de59 | 2005-12-23 21:24:35 +0000 | [diff] [blame] | 321 | # Invalid contents of cookies file (eg. bad magic string) |
| 322 | # causes a LoadError. |
| 323 | try: |
Brett Cannon | 7f462fc | 2010-10-29 23:27:39 +0000 | [diff] [blame] | 324 | with open(filename, "w") as f: |
| 325 | f.write("oops\n") |
| 326 | for cookiejar_class in LWPCookieJar, MozillaCookieJar: |
| 327 | c = cookiejar_class() |
| 328 | self.assertRaises(LoadError, c.load, filename) |
Neal Norwitz | 3e7de59 | 2005-12-23 21:24:35 +0000 | [diff] [blame] | 329 | finally: |
| 330 | try: os.unlink(filename) |
| 331 | except OSError: pass |
Martin v. Löwis | c5574e8 | 2005-03-03 10:57:37 +0000 | [diff] [blame] | 332 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 333 | class CookieTests(unittest.TestCase): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 334 | # XXX |
| 335 | # Get rid of string comparisons where not actually testing str / repr. |
| 336 | # .clear() etc. |
| 337 | # IP addresses like 50 (single number, no dot) and domain-matching |
| 338 | # functions (and is_HDN)? See draft RFC 2965 errata. |
| 339 | # Strictness switches |
| 340 | # is_third_party() |
| 341 | # unverifiability / third-party blocking |
| 342 | # Netscape cookies work the same as RFC 2965 with regard to port. |
| 343 | # Set-Cookie with negative max age. |
| 344 | # If turn RFC 2965 handling off, Set-Cookie2 cookies should not clobber |
| 345 | # Set-Cookie cookies. |
| 346 | # Cookie2 should be sent if *any* cookies are not V1 (ie. V0 OR V2 etc.). |
| 347 | # Cookies (V1 and V0) with no expiry date should be set to be discarded. |
| 348 | # RFC 2965 Quoting: |
| 349 | # Should accept unquoted cookie-attribute values? check errata draft. |
| 350 | # Which are required on the way in and out? |
| 351 | # Should always return quoted cookie-attribute values? |
| 352 | # Proper testing of when RFC 2965 clobbers Netscape (waiting for errata). |
| 353 | # Path-match on return (same for V0 and V1). |
| 354 | # RFC 2965 acceptance and returning rules |
| 355 | # Set-Cookie2 without version attribute is rejected. |
| 356 | |
| 357 | # Netscape peculiarities list from Ronald Tschalar. |
| 358 | # The first two still need tests, the rest are covered. |
| 359 | ## - Quoting: only quotes around the expires value are recognized as such |
| 360 | ## (and yes, some folks quote the expires value); quotes around any other |
| 361 | ## value are treated as part of the value. |
| 362 | ## - White space: white space around names and values is ignored |
| 363 | ## - Default path: if no path parameter is given, the path defaults to the |
| 364 | ## path in the request-uri up to, but not including, the last '/'. Note |
| 365 | ## that this is entirely different from what the spec says. |
| 366 | ## - Commas and other delimiters: Netscape just parses until the next ';'. |
| 367 | ## This means it will allow commas etc inside values (and yes, both |
| 368 | ## commas and equals are commonly appear in the cookie value). This also |
| 369 | ## means that if you fold multiple Set-Cookie header fields into one, |
| 370 | ## comma-separated list, it'll be a headache to parse (at least my head |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 371 | ## starts hurting every time I think of that code). |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 372 | ## - Expires: You'll get all sorts of date formats in the expires, |
| 373 | ## including emtpy expires attributes ("expires="). Be as flexible as you |
| 374 | ## can, and certainly don't expect the weekday to be there; if you can't |
| 375 | ## parse it, just ignore it and pretend it's a session cookie. |
| 376 | ## - Domain-matching: Netscape uses the 2-dot rule for _all_ domains, not |
| 377 | ## just the 7 special TLD's listed in their spec. And folks rely on |
| 378 | ## that... |
| 379 | |
| 380 | def test_domain_return_ok(self): |
| 381 | # test optimization: .domain_return_ok() should filter out most |
| 382 | # domains in the CookieJar before we try to access them (because that |
| 383 | # may require disk access -- in particular, with MSIECookieJar) |
| 384 | # This is only a rough check for performance reasons, so it's not too |
| 385 | # critical as long as it's sufficiently liberal. |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 386 | pol = DefaultCookiePolicy() |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 387 | for url, domain, ok in [ |
| 388 | ("http://foo.bar.com/", "blah.com", False), |
| 389 | ("http://foo.bar.com/", "rhubarb.blah.com", False), |
| 390 | ("http://foo.bar.com/", "rhubarb.foo.bar.com", False), |
| 391 | ("http://foo.bar.com/", ".foo.bar.com", True), |
| 392 | ("http://foo.bar.com/", "foo.bar.com", True), |
| 393 | ("http://foo.bar.com/", ".bar.com", True), |
| 394 | ("http://foo.bar.com/", "com", True), |
| 395 | ("http://foo.com/", "rhubarb.foo.com", False), |
| 396 | ("http://foo.com/", ".foo.com", True), |
| 397 | ("http://foo.com/", "foo.com", True), |
| 398 | ("http://foo.com/", "com", True), |
| 399 | ("http://foo/", "rhubarb.foo", False), |
| 400 | ("http://foo/", ".foo", True), |
| 401 | ("http://foo/", "foo", True), |
| 402 | ("http://foo/", "foo.local", True), |
| 403 | ("http://foo/", ".local", True), |
| 404 | ]: |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 405 | request = urllib.request.Request(url) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 406 | r = pol.domain_return_ok(domain, request) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 407 | if ok: self.assertTrue(r) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 408 | else: self.assertFalse(r) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 409 | |
| 410 | def test_missing_value(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 411 | # missing = sign in Cookie: header is regarded by Mozilla as a missing |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 412 | # name, and by http.cookiejar as a missing value |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 413 | filename = test.support.TESTFN |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 414 | c = MozillaCookieJar(filename) |
| 415 | interact_netscape(c, "http://www.acme.com/", 'eggs') |
| 416 | interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/') |
| 417 | cookie = c._cookies["www.acme.com"]["/"]["eggs"] |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 418 | self.assertIsNone(cookie.value) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 419 | self.assertEqual(cookie.name, "eggs") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 420 | cookie = c._cookies["www.acme.com"]['/foo/']['"spam"'] |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 421 | self.assertIsNone(cookie.value) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 422 | self.assertEqual(cookie.name, '"spam"') |
| 423 | self.assertEqual(lwp_cookie_str(cookie), ( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 424 | r'"spam"; path="/foo/"; domain="www.acme.com"; ' |
| 425 | 'path_spec; discard; version=0')) |
| 426 | old_str = repr(c) |
| 427 | c.save(ignore_expires=True, ignore_discard=True) |
| 428 | try: |
| 429 | c = MozillaCookieJar(filename) |
| 430 | c.revert(ignore_expires=True, ignore_discard=True) |
| 431 | finally: |
| 432 | os.unlink(c.filename) |
| 433 | # cookies unchanged apart from lost info re. whether path was specified |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 434 | self.assertEqual( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 435 | repr(c), |
| 436 | re.sub("path_specified=%s" % True, "path_specified=%s" % False, |
| 437 | old_str) |
| 438 | ) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 439 | self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"), |
| 440 | '"spam"; eggs') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 441 | |
Neal Norwitz | 71dad72 | 2005-12-23 21:43:48 +0000 | [diff] [blame] | 442 | def test_rfc2109_handling(self): |
| 443 | # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies, |
| 444 | # dependent on policy settings |
Neal Norwitz | 71dad72 | 2005-12-23 21:43:48 +0000 | [diff] [blame] | 445 | for rfc2109_as_netscape, rfc2965, version in [ |
| 446 | # default according to rfc2965 if not explicitly specified |
| 447 | (None, False, 0), |
| 448 | (None, True, 1), |
| 449 | # explicit rfc2109_as_netscape |
| 450 | (False, False, None), # version None here means no cookie stored |
| 451 | (False, True, 1), |
| 452 | (True, False, 0), |
| 453 | (True, True, 0), |
| 454 | ]: |
| 455 | policy = DefaultCookiePolicy( |
| 456 | rfc2109_as_netscape=rfc2109_as_netscape, |
| 457 | rfc2965=rfc2965) |
| 458 | c = CookieJar(policy) |
| 459 | interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1") |
| 460 | try: |
| 461 | cookie = c._cookies["www.example.com"]["/"]["ni"] |
| 462 | except KeyError: |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 463 | self.assertIsNone(version) # didn't expect a stored cookie |
Neal Norwitz | 71dad72 | 2005-12-23 21:43:48 +0000 | [diff] [blame] | 464 | else: |
| 465 | self.assertEqual(cookie.version, version) |
| 466 | # 2965 cookies are unaffected |
| 467 | interact_2965(c, "http://www.example.com/", |
| 468 | "foo=bar; Version=1") |
| 469 | if rfc2965: |
| 470 | cookie2965 = c._cookies["www.example.com"]["/"]["foo"] |
| 471 | self.assertEqual(cookie2965.version, 1) |
| 472 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 473 | def test_ns_parser(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 474 | c = CookieJar() |
| 475 | interact_netscape(c, "http://www.acme.com/", |
| 476 | 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"') |
| 477 | interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080') |
| 478 | interact_netscape(c, "http://www.acme.com:80/", 'nini=ni') |
| 479 | interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=') |
| 480 | interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; ' |
| 481 | 'expires="Foo Bar 25 33:22:11 3022"') |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 482 | interact_netscape(c, 'http://www.acme.com/', 'fortytwo=') |
| 483 | interact_netscape(c, 'http://www.acme.com/', '=unladenswallow') |
| 484 | interact_netscape(c, 'http://www.acme.com/', 'holyhandgrenade') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 485 | |
| 486 | cookie = c._cookies[".acme.com"]["/"]["spam"] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 487 | self.assertEqual(cookie.domain, ".acme.com") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 488 | self.assertTrue(cookie.domain_specified) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 489 | self.assertEqual(cookie.port, DEFAULT_HTTP_PORT) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 490 | self.assertFalse(cookie.port_specified) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 491 | # case is preserved |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 492 | self.assertTrue(cookie.has_nonstandard_attr("blArgh")) |
| 493 | self.assertFalse(cookie.has_nonstandard_attr("blargh")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 494 | |
| 495 | cookie = c._cookies["www.acme.com"]["/"]["ni"] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 496 | self.assertEqual(cookie.domain, "www.acme.com") |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 497 | self.assertFalse(cookie.domain_specified) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 498 | self.assertEqual(cookie.port, "80,8080") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 499 | self.assertTrue(cookie.port_specified) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 500 | |
| 501 | cookie = c._cookies["www.acme.com"]["/"]["nini"] |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 502 | self.assertIsNone(cookie.port) |
| 503 | self.assertFalse(cookie.port_specified) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 504 | |
| 505 | # invalid expires should not cause cookie to be dropped |
| 506 | foo = c._cookies["www.acme.com"]["/"]["foo"] |
| 507 | spam = c._cookies["www.acme.com"]["/"]["foo"] |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 508 | self.assertIsNone(foo.expires) |
| 509 | self.assertIsNone(spam.expires) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 510 | |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 511 | cookie = c._cookies['www.acme.com']['/']['fortytwo'] |
| 512 | self.assertIsNotNone(cookie.value) |
| 513 | self.assertEqual(cookie.value, '') |
| 514 | |
| 515 | # there should be a distinction between a present but empty value |
| 516 | # (above) and a value that's entirely missing (below) |
| 517 | |
| 518 | cookie = c._cookies['www.acme.com']['/']['holyhandgrenade'] |
| 519 | self.assertIsNone(cookie.value) |
| 520 | |
Martin v. Löwis | 4ea3ead | 2005-03-03 10:48:12 +0000 | [diff] [blame] | 521 | def test_ns_parser_special_names(self): |
| 522 | # names such as 'expires' are not special in first name=value pair |
| 523 | # of Set-Cookie: header |
Martin v. Löwis | 4ea3ead | 2005-03-03 10:48:12 +0000 | [diff] [blame] | 524 | c = CookieJar() |
| 525 | interact_netscape(c, "http://www.acme.com/", 'expires=eggs') |
| 526 | interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs') |
| 527 | |
| 528 | cookies = c._cookies["www.acme.com"]["/"] |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 529 | self.assertIn('expires', cookies) |
| 530 | self.assertIn('version', cookies) |
Martin v. Löwis | 4ea3ead | 2005-03-03 10:48:12 +0000 | [diff] [blame] | 531 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 532 | def test_expires(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 533 | # if expires is in future, keep cookie... |
| 534 | c = CookieJar() |
| 535 | future = time2netscape(time.time()+3600) |
| 536 | interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' % |
| 537 | future) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 538 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 539 | now = time2netscape(time.time()-1) |
| 540 | # ... and if in past or present, discard it |
| 541 | interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' % |
| 542 | now) |
| 543 | h = interact_netscape(c, "http://www.acme.com/") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 544 | self.assertEqual(len(c), 1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 545 | self.assertIn('spam="bar"', h) |
| 546 | self.assertNotIn("foo", h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 547 | |
| 548 | # max-age takes precedence over expires, and zero max-age is request to |
| 549 | # delete both new cookie and any old matching cookie |
| 550 | interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' % |
| 551 | future) |
| 552 | interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' % |
| 553 | future) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 554 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 555 | interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; ' |
| 556 | 'expires=%s; max-age=0' % future) |
| 557 | interact_netscape(c, "http://www.acme.com/", 'bar="bar"; ' |
| 558 | 'max-age=0; expires=%s' % future) |
| 559 | h = interact_netscape(c, "http://www.acme.com/") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 560 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 561 | |
| 562 | # test expiry at end of session for cookies with no expires attribute |
| 563 | interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 564 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 565 | c.clear_session_cookies() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 566 | self.assertEqual(len(c), 1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 567 | self.assertIn('spam="bar"', h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 568 | |
| 569 | # XXX RFC 2965 expiry rules (some apply to V0 too) |
| 570 | |
| 571 | def test_default_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 572 | # RFC 2965 |
| 573 | pol = DefaultCookiePolicy(rfc2965=True) |
| 574 | |
| 575 | c = CookieJar(pol) |
| 576 | interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 577 | self.assertIn("/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 578 | |
| 579 | c = CookieJar(pol) |
| 580 | interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 581 | self.assertIn("/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 582 | |
| 583 | c = CookieJar(pol) |
| 584 | interact_2965(c, "http://www.acme.com/blah/rhubarb", |
| 585 | 'eggs="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 586 | self.assertIn("/blah/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 587 | |
| 588 | c = CookieJar(pol) |
| 589 | interact_2965(c, "http://www.acme.com/blah/rhubarb/", |
| 590 | 'eggs="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 591 | self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 592 | |
| 593 | # Netscape |
| 594 | |
| 595 | c = CookieJar() |
| 596 | interact_netscape(c, "http://www.acme.com/", 'spam="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 597 | self.assertIn("/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 598 | |
| 599 | c = CookieJar() |
| 600 | interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 601 | self.assertIn("/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 602 | |
| 603 | c = CookieJar() |
| 604 | interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 605 | self.assertIn("/blah", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 606 | |
| 607 | c = CookieJar() |
| 608 | interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 609 | self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 610 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 611 | def test_default_path_with_query(self): |
| 612 | cj = CookieJar() |
| 613 | uri = "http://example.com/?spam/eggs" |
| 614 | value = 'eggs="bar"' |
| 615 | interact_netscape(cj, uri, value) |
| 616 | # Default path does not include query, so is "/", not "/?spam". |
| 617 | self.assertIn("/", cj._cookies["example.com"]) |
| 618 | # Cookie is sent back to the same URI. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 619 | self.assertEqual(interact_netscape(cj, uri), value) |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 620 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 621 | def test_escape_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 622 | cases = [ |
| 623 | # quoted safe |
| 624 | ("/foo%2f/bar", "/foo%2F/bar"), |
| 625 | ("/foo%2F/bar", "/foo%2F/bar"), |
| 626 | # quoted % |
| 627 | ("/foo%%/bar", "/foo%%/bar"), |
| 628 | # quoted unsafe |
| 629 | ("/fo%19o/bar", "/fo%19o/bar"), |
| 630 | ("/fo%7do/bar", "/fo%7Do/bar"), |
| 631 | # unquoted safe |
| 632 | ("/foo/bar&", "/foo/bar&"), |
| 633 | ("/foo//bar", "/foo//bar"), |
| 634 | ("\176/foo/bar", "\176/foo/bar"), |
| 635 | # unquoted unsafe |
| 636 | ("/foo\031/bar", "/foo%19/bar"), |
| 637 | ("/\175foo/bar", "/%7Dfoo/bar"), |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 638 | # unicode, latin-1 range |
| 639 | ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 640 | # unicode |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 641 | ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 642 | ] |
| 643 | for arg, result in cases: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 644 | self.assertEqual(escape_path(arg), result) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 645 | |
| 646 | def test_request_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 647 | # with parameters |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 648 | req = urllib.request.Request( |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 649 | "http://www.example.com/rheum/rhaponticum;" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 650 | "foo=bar;sing=song?apples=pears&spam=eggs#ni") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 651 | self.assertEqual(request_path(req), |
| 652 | "/rheum/rhaponticum;foo=bar;sing=song") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 653 | # without parameters |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 654 | req = urllib.request.Request( |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 655 | "http://www.example.com/rheum/rhaponticum?" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 656 | "apples=pears&spam=eggs#ni") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 657 | self.assertEqual(request_path(req), "/rheum/rhaponticum") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 658 | # missing final slash |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 659 | req = urllib.request.Request("http://www.example.com") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 660 | self.assertEqual(request_path(req), "/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 661 | |
| 662 | def test_request_port(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 663 | req = urllib.request.Request("http://www.acme.com:1234/", |
| 664 | headers={"Host": "www.acme.com:4321"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 665 | self.assertEqual(request_port(req), "1234") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 666 | req = urllib.request.Request("http://www.acme.com/", |
| 667 | headers={"Host": "www.acme.com:4321"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 668 | self.assertEqual(request_port(req), DEFAULT_HTTP_PORT) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 669 | |
| 670 | def test_request_host(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 671 | # this request is illegal (RFC2616, 14.2.3) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 672 | req = urllib.request.Request("http://1.1.1.1/", |
| 673 | headers={"Host": "www.acme.com:80"}) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 674 | # libwww-perl wants this response, but that seems wrong (RFC 2616, |
| 675 | # section 5.2, point 1., and RFC 2965 section 1, paragraph 3) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 676 | #self.assertEqual(request_host(req), "www.acme.com") |
| 677 | self.assertEqual(request_host(req), "1.1.1.1") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 678 | req = urllib.request.Request("http://www.acme.com/", |
| 679 | headers={"Host": "irrelevant.com"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 680 | self.assertEqual(request_host(req), "www.acme.com") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 681 | # port shouldn't be in request-host |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 682 | req = urllib.request.Request("http://www.acme.com:2345/resource.html", |
| 683 | headers={"Host": "www.acme.com:5432"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 684 | self.assertEqual(request_host(req), "www.acme.com") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 685 | |
| 686 | def test_is_HDN(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 687 | self.assertTrue(is_HDN("foo.bar.com")) |
| 688 | self.assertTrue(is_HDN("1foo2.3bar4.5com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 689 | self.assertFalse(is_HDN("192.168.1.1")) |
| 690 | self.assertFalse(is_HDN("")) |
| 691 | self.assertFalse(is_HDN(".")) |
| 692 | self.assertFalse(is_HDN(".foo.bar.com")) |
| 693 | self.assertFalse(is_HDN("..foo")) |
| 694 | self.assertFalse(is_HDN("foo.")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 695 | |
| 696 | def test_reach(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 697 | self.assertEqual(reach("www.acme.com"), ".acme.com") |
| 698 | self.assertEqual(reach("acme.com"), "acme.com") |
| 699 | self.assertEqual(reach("acme.local"), ".local") |
| 700 | self.assertEqual(reach(".local"), ".local") |
| 701 | self.assertEqual(reach(".com"), ".com") |
| 702 | self.assertEqual(reach("."), ".") |
| 703 | self.assertEqual(reach(""), "") |
| 704 | self.assertEqual(reach("192.168.0.1"), "192.168.0.1") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 705 | |
| 706 | def test_domain_match(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 707 | self.assertTrue(domain_match("192.168.1.1", "192.168.1.1")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 708 | self.assertFalse(domain_match("192.168.1.1", ".168.1.1")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 709 | self.assertTrue(domain_match("x.y.com", "x.Y.com")) |
| 710 | self.assertTrue(domain_match("x.y.com", ".Y.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 711 | self.assertFalse(domain_match("x.y.com", "Y.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 712 | self.assertTrue(domain_match("a.b.c.com", ".c.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 713 | self.assertFalse(domain_match(".c.com", "a.b.c.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 714 | self.assertTrue(domain_match("example.local", ".local")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 715 | self.assertFalse(domain_match("blah.blah", "")) |
| 716 | self.assertFalse(domain_match("", ".rhubarb.rhubarb")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 717 | self.assertTrue(domain_match("", "")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 718 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 719 | self.assertTrue(user_domain_match("acme.com", "acme.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 720 | self.assertFalse(user_domain_match("acme.com", ".acme.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 721 | self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com")) |
| 722 | self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com")) |
| 723 | self.assertTrue(user_domain_match("x.y.com", "x.Y.com")) |
| 724 | self.assertTrue(user_domain_match("x.y.com", ".Y.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 725 | self.assertFalse(user_domain_match("x.y.com", "Y.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 726 | self.assertTrue(user_domain_match("y.com", "Y.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 727 | self.assertFalse(user_domain_match(".y.com", "Y.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 728 | self.assertTrue(user_domain_match(".y.com", ".Y.com")) |
| 729 | self.assertTrue(user_domain_match("x.y.com", ".com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 730 | self.assertFalse(user_domain_match("x.y.com", "com")) |
| 731 | self.assertFalse(user_domain_match("x.y.com", "m")) |
| 732 | self.assertFalse(user_domain_match("x.y.com", ".m")) |
| 733 | self.assertFalse(user_domain_match("x.y.com", "")) |
| 734 | self.assertFalse(user_domain_match("x.y.com", ".")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 735 | self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 736 | # not both HDNs, so must string-compare equal to match |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 737 | self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1")) |
| 738 | self.assertFalse(user_domain_match("192.168.1.1", ".")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 739 | # empty string is a special case |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 740 | self.assertFalse(user_domain_match("192.168.1.1", "")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 741 | |
| 742 | def test_wrong_domain(self): |
| 743 | # Cookies whose effective request-host name does not domain-match the |
| 744 | # domain are rejected. |
| 745 | |
| 746 | # XXX far from complete |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 747 | c = CookieJar() |
| 748 | interact_2965(c, "http://www.nasty.com/", |
| 749 | 'foo=bar; domain=friendly.org; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 750 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 751 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 752 | def test_strict_domain(self): |
| 753 | # Cookies whose domain is a country-code tld like .co.uk should |
| 754 | # not be set if CookiePolicy.strict_domain is true. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 755 | cp = DefaultCookiePolicy(strict_domain=True) |
| 756 | cj = CookieJar(policy=cp) |
| 757 | interact_netscape(cj, "http://example.co.uk/", 'no=problemo') |
| 758 | interact_netscape(cj, "http://example.co.uk/", |
| 759 | 'okey=dokey; Domain=.example.co.uk') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 760 | self.assertEqual(len(cj), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 761 | for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]: |
| 762 | interact_netscape(cj, "http://example.%s/" % pseudo_tld, |
| 763 | 'spam=eggs; Domain=.co.uk') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 764 | self.assertEqual(len(cj), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 765 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 766 | def test_two_component_domain_ns(self): |
| 767 | # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain |
| 768 | # should all get accepted, as should .acme.com, acme.com and no domain |
| 769 | # for 2-component domains like acme.com. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 770 | c = CookieJar() |
| 771 | |
| 772 | # two-component V0 domain is OK |
| 773 | interact_netscape(c, "http://foo.net/", 'ns=bar') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 774 | self.assertEqual(len(c), 1) |
| 775 | self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar") |
| 776 | self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 777 | # *will* be returned to any other domain (unlike RFC 2965)... |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 778 | self.assertEqual(interact_netscape(c, "http://www.foo.net/"), |
| 779 | "ns=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 780 | # ...unless requested otherwise |
| 781 | pol = DefaultCookiePolicy( |
| 782 | strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain) |
| 783 | c.set_policy(pol) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 784 | self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 785 | |
| 786 | # unlike RFC 2965, even explicit two-component domain is OK, |
| 787 | # because .foo.net matches foo.net |
| 788 | interact_netscape(c, "http://foo.net/foo/", |
| 789 | 'spam1=eggs; domain=foo.net') |
| 790 | # even if starts with a dot -- in NS rules, .foo.net matches foo.net! |
| 791 | interact_netscape(c, "http://foo.net/foo/bar/", |
| 792 | 'spam2=eggs; domain=.foo.net') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 793 | self.assertEqual(len(c), 3) |
| 794 | self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value, |
| 795 | "eggs") |
| 796 | self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value, |
| 797 | "eggs") |
| 798 | self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"), |
| 799 | "spam2=eggs; spam1=eggs; ns=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 800 | |
| 801 | # top-level domain is too general |
| 802 | interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 803 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 804 | |
| 805 | ## # Netscape protocol doesn't allow non-special top level domains (such |
| 806 | ## # as co.uk) in the domain attribute unless there are at least three |
| 807 | ## # dots in it. |
| 808 | # Oh yes it does! Real implementations don't check this, and real |
| 809 | # cookies (of course) rely on that behaviour. |
| 810 | interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 811 | ## self.assertEqual(len(c), 2) |
| 812 | self.assertEqual(len(c), 4) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 813 | |
| 814 | def test_two_component_domain_rfc2965(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 815 | pol = DefaultCookiePolicy(rfc2965=True) |
| 816 | c = CookieJar(pol) |
| 817 | |
| 818 | # two-component V1 domain is OK |
| 819 | interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 820 | self.assertEqual(len(c), 1) |
| 821 | self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar") |
| 822 | self.assertEqual(interact_2965(c, "http://foo.net/"), |
| 823 | "$Version=1; foo=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 824 | # won't be returned to any other domain (because domain was implied) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 825 | self.assertEqual(interact_2965(c, "http://www.foo.net/"), "") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 826 | |
| 827 | # unless domain is given explicitly, because then it must be |
| 828 | # rewritten to start with a dot: foo.net --> .foo.net, which does |
| 829 | # not domain-match foo.net |
| 830 | interact_2965(c, "http://foo.net/foo", |
| 831 | 'spam=eggs; domain=foo.net; path=/foo; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 832 | self.assertEqual(len(c), 1) |
| 833 | self.assertEqual(interact_2965(c, "http://foo.net/foo"), |
| 834 | "$Version=1; foo=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 835 | |
| 836 | # explicit foo.net from three-component domain www.foo.net *does* get |
| 837 | # set, because .foo.net domain-matches .foo.net |
| 838 | interact_2965(c, "http://www.foo.net/foo/", |
| 839 | 'spam=eggs; domain=foo.net; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 840 | self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value, |
| 841 | "eggs") |
| 842 | self.assertEqual(len(c), 2) |
| 843 | self.assertEqual(interact_2965(c, "http://foo.net/foo/"), |
| 844 | "$Version=1; foo=bar") |
| 845 | self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"), |
| 846 | '$Version=1; spam=eggs; $Domain="foo.net"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 847 | |
| 848 | # top-level domain is too general |
| 849 | interact_2965(c, "http://foo.net/", |
| 850 | 'ni="ni"; domain=".net"; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 851 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 852 | |
| 853 | # RFC 2965 doesn't require blocking this |
| 854 | interact_2965(c, "http://foo.co.uk/", |
| 855 | 'nasty=trick; domain=.co.uk; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 856 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 857 | |
| 858 | def test_domain_allow(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 859 | c = CookieJar(policy=DefaultCookiePolicy( |
| 860 | blocked_domains=["acme.com"], |
| 861 | allowed_domains=["www.acme.com"])) |
| 862 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 863 | req = urllib.request.Request("http://acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 864 | headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"] |
| 865 | res = FakeResponse(headers, "http://acme.com/") |
| 866 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 867 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 868 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 869 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 870 | res = FakeResponse(headers, "http://www.acme.com/") |
| 871 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 872 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 873 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 874 | req = urllib.request.Request("http://www.coyote.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 875 | res = FakeResponse(headers, "http://www.coyote.com/") |
| 876 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 877 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 878 | |
| 879 | # set a cookie with non-allowed domain... |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 880 | req = urllib.request.Request("http://www.coyote.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 881 | res = FakeResponse(headers, "http://www.coyote.com/") |
| 882 | cookies = c.make_cookies(res, req) |
| 883 | c.set_cookie(cookies[0]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 884 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 885 | # ... and check is doesn't get returned |
| 886 | c.add_cookie_header(req) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 887 | self.assertFalse(req.has_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 888 | |
| 889 | def test_domain_block(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 890 | pol = DefaultCookiePolicy( |
| 891 | rfc2965=True, blocked_domains=[".acme.com"]) |
| 892 | c = CookieJar(policy=pol) |
| 893 | headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"] |
| 894 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 895 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 896 | res = FakeResponse(headers, "http://www.acme.com/") |
| 897 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 898 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 899 | |
| 900 | p = pol.set_blocked_domains(["acme.com"]) |
| 901 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 902 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 903 | |
| 904 | c.clear() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 905 | req = urllib.request.Request("http://www.roadrunner.net/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 906 | res = FakeResponse(headers, "http://www.roadrunner.net/") |
| 907 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 908 | self.assertEqual(len(c), 1) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 909 | req = urllib.request.Request("http://www.roadrunner.net/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 910 | c.add_cookie_header(req) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 911 | self.assertTrue(req.has_header("Cookie")) |
| 912 | self.assertTrue(req.has_header("Cookie2")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 913 | |
| 914 | c.clear() |
| 915 | pol.set_blocked_domains([".acme.com"]) |
| 916 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 917 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 918 | |
| 919 | # set a cookie with blocked domain... |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 920 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 921 | res = FakeResponse(headers, "http://www.acme.com/") |
| 922 | cookies = c.make_cookies(res, req) |
| 923 | c.set_cookie(cookies[0]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 924 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 925 | # ... and check is doesn't get returned |
| 926 | c.add_cookie_header(req) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 927 | self.assertFalse(req.has_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 928 | |
| 929 | def test_secure(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 930 | for ns in True, False: |
| 931 | for whitespace in " ", "": |
| 932 | c = CookieJar() |
| 933 | if ns: |
| 934 | pol = DefaultCookiePolicy(rfc2965=False) |
| 935 | int = interact_netscape |
| 936 | vs = "" |
| 937 | else: |
| 938 | pol = DefaultCookiePolicy(rfc2965=True) |
| 939 | int = interact_2965 |
| 940 | vs = "; Version=1" |
| 941 | c.set_policy(pol) |
| 942 | url = "http://www.acme.com/" |
| 943 | int(c, url, "foo1=bar%s%s" % (vs, whitespace)) |
| 944 | int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace)) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 945 | self.assertFalse( |
| 946 | c._cookies["www.acme.com"]["/"]["foo1"].secure, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 947 | "non-secure cookie registered secure") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 948 | self.assertTrue( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 949 | c._cookies["www.acme.com"]["/"]["foo2"].secure, |
| 950 | "secure cookie registered non-secure") |
| 951 | |
| 952 | def test_quote_cookie_value(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 953 | c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True)) |
| 954 | interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1') |
| 955 | h = interact_2965(c, "http://www.acme.com/") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 956 | self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 957 | |
| 958 | def test_missing_final_slash(self): |
| 959 | # Missing slash from request URL's abs_path should be assumed present. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 960 | url = "http://www.acme.com" |
| 961 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 962 | interact_2965(c, url, "foo=bar; Version=1") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 963 | req = urllib.request.Request(url) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 964 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 965 | c.add_cookie_header(req) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 966 | self.assertTrue(req.has_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 967 | |
| 968 | def test_domain_mirror(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 969 | pol = DefaultCookiePolicy(rfc2965=True) |
| 970 | |
| 971 | c = CookieJar(pol) |
| 972 | url = "http://foo.bar.com/" |
| 973 | interact_2965(c, url, "spam=eggs; Version=1") |
| 974 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 975 | self.assertNotIn("Domain", h, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 976 | "absent domain returned with domain present") |
| 977 | |
| 978 | c = CookieJar(pol) |
| 979 | url = "http://foo.bar.com/" |
| 980 | interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com') |
| 981 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 982 | self.assertIn('$Domain=".bar.com"', h, "domain not returned") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 983 | |
| 984 | c = CookieJar(pol) |
| 985 | url = "http://foo.bar.com/" |
| 986 | # note missing initial dot in Domain |
| 987 | interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com') |
| 988 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 989 | self.assertIn('$Domain="bar.com"', h, "domain not returned") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 990 | |
| 991 | def test_path_mirror(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 992 | pol = DefaultCookiePolicy(rfc2965=True) |
| 993 | |
| 994 | c = CookieJar(pol) |
| 995 | url = "http://foo.bar.com/" |
| 996 | interact_2965(c, url, "spam=eggs; Version=1") |
| 997 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 998 | self.assertNotIn("Path", h, "absent path returned with path present") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 999 | |
| 1000 | c = CookieJar(pol) |
| 1001 | url = "http://foo.bar.com/" |
| 1002 | interact_2965(c, url, 'spam=eggs; Version=1; Path=/') |
| 1003 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1004 | self.assertIn('$Path="/"', h, "path not returned") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1005 | |
| 1006 | def test_port_mirror(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1007 | pol = DefaultCookiePolicy(rfc2965=True) |
| 1008 | |
| 1009 | c = CookieJar(pol) |
| 1010 | url = "http://foo.bar.com/" |
| 1011 | interact_2965(c, url, "spam=eggs; Version=1") |
| 1012 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1013 | self.assertNotIn("Port", h, "absent port returned with port present") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1014 | |
| 1015 | c = CookieJar(pol) |
| 1016 | url = "http://foo.bar.com/" |
| 1017 | interact_2965(c, url, "spam=eggs; Version=1; Port") |
| 1018 | h = interact_2965(c, url) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1019 | self.assertRegex(h, "\$Port([^=]|$)", |
| 1020 | "port with no value not returned with no value") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1021 | |
| 1022 | c = CookieJar(pol) |
| 1023 | url = "http://foo.bar.com/" |
| 1024 | interact_2965(c, url, 'spam=eggs; Version=1; Port="80"') |
| 1025 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1026 | self.assertIn('$Port="80"', h, |
| 1027 | "port with single value not returned with single value") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1028 | |
| 1029 | c = CookieJar(pol) |
| 1030 | url = "http://foo.bar.com/" |
| 1031 | interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"') |
| 1032 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1033 | self.assertIn('$Port="80,8080"', h, |
| 1034 | "port with multiple values not returned with multiple " |
| 1035 | "values") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1036 | |
| 1037 | def test_no_return_comment(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1038 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1039 | url = "http://foo.bar.com/" |
| 1040 | interact_2965(c, url, 'spam=eggs; Version=1; ' |
| 1041 | 'Comment="does anybody read these?"; ' |
| 1042 | 'CommentURL="http://foo.bar.net/comment.html"') |
| 1043 | h = interact_2965(c, url) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1044 | self.assertNotIn("Comment", h, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1045 | "Comment or CommentURL cookie-attributes returned to server") |
| 1046 | |
| 1047 | def test_Cookie_iterator(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1048 | cs = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1049 | # add some random cookies |
| 1050 | interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; ' |
| 1051 | 'Comment="does anybody read these?"; ' |
| 1052 | 'CommentURL="http://foo.bar.net/comment.html"') |
| 1053 | interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure") |
| 1054 | interact_2965(cs, "http://www.acme.com/blah/", |
| 1055 | "foo=bar; secure; Version=1") |
| 1056 | interact_2965(cs, "http://www.acme.com/blah/", |
| 1057 | "foo=bar; path=/; Version=1") |
| 1058 | interact_2965(cs, "http://www.sol.no", |
| 1059 | r'bang=wallop; version=1; domain=".sol.no"; ' |
| 1060 | r'port="90,100, 80,8080"; ' |
| 1061 | r'max-age=100; Comment = "Just kidding! (\"|\\\\) "') |
| 1062 | |
| 1063 | versions = [1, 1, 1, 0, 1] |
| 1064 | names = ["bang", "foo", "foo", "spam", "foo"] |
| 1065 | domains = [".sol.no", "blah.spam.org", "www.acme.com", |
| 1066 | "www.acme.com", "www.acme.com"] |
| 1067 | paths = ["/", "/", "/", "/blah", "/blah/"] |
| 1068 | |
| 1069 | for i in range(4): |
| 1070 | i = 0 |
| 1071 | for c in cs: |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1072 | self.assertIsInstance(c, Cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1073 | self.assertEqual(c.version, versions[i]) |
| 1074 | self.assertEqual(c.name, names[i]) |
| 1075 | self.assertEqual(c.domain, domains[i]) |
| 1076 | self.assertEqual(c.path, paths[i]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1077 | i = i + 1 |
| 1078 | |
| 1079 | def test_parse_ns_headers(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1080 | # missing domain value (invalid cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1081 | self.assertEqual( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1082 | parse_ns_headers(["foo=bar; path=/; domain"]), |
| 1083 | [[("foo", "bar"), |
| 1084 | ("path", "/"), ("domain", None), ("version", "0")]] |
| 1085 | ) |
| 1086 | # invalid expires value |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1087 | self.assertEqual( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1088 | parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]), |
| 1089 | [[("foo", "bar"), ("expires", None), ("version", "0")]] |
| 1090 | ) |
| 1091 | # missing cookie value (valid cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1092 | self.assertEqual( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1093 | parse_ns_headers(["foo"]), |
| 1094 | [[("foo", None), ("version", "0")]] |
| 1095 | ) |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 1096 | # missing cookie values for parsed attributes |
| 1097 | self.assertEqual( |
| 1098 | parse_ns_headers(['foo=bar; expires']), |
| 1099 | [[('foo', 'bar'), ('expires', None), ('version', '0')]]) |
| 1100 | self.assertEqual( |
| 1101 | parse_ns_headers(['foo=bar; version']), |
| 1102 | [[('foo', 'bar'), ('version', None)]]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1103 | # shouldn't add version if header is empty |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1104 | self.assertEqual(parse_ns_headers([""]), []) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1105 | |
| 1106 | def test_bad_cookie_header(self): |
| 1107 | |
| 1108 | def cookiejar_from_cookie_headers(headers): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1109 | c = CookieJar() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1110 | req = urllib.request.Request("http://www.example.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1111 | r = FakeResponse(headers, "http://www.example.com/") |
| 1112 | c.extract_cookies(r, req) |
| 1113 | return c |
| 1114 | |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 1115 | future = time2netscape(time.time()+3600) |
| 1116 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1117 | # none of these bad headers should cause an exception to be raised |
| 1118 | for headers in [ |
| 1119 | ["Set-Cookie: "], # actually, nothing wrong with this |
| 1120 | ["Set-Cookie2: "], # ditto |
| 1121 | # missing domain value |
| 1122 | ["Set-Cookie2: a=foo; path=/; Version=1; domain"], |
| 1123 | # bad max-age |
| 1124 | ["Set-Cookie: b=foo; max-age=oops"], |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 1125 | # bad version |
| 1126 | ["Set-Cookie: b=foo; version=spam"], |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 1127 | ["Set-Cookie:; Expires=%s" % future], |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1128 | ]: |
| 1129 | c = cookiejar_from_cookie_headers(headers) |
| 1130 | # these bad cookies shouldn't be set |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1131 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1132 | |
| 1133 | # cookie with invalid expires is treated as session cookie |
| 1134 | headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"] |
| 1135 | c = cookiejar_from_cookie_headers(headers) |
| 1136 | cookie = c._cookies["www.example.com"]["/"]["c"] |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1137 | self.assertIsNone(cookie.expires) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1138 | |
| 1139 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1140 | class LWPCookieTests(unittest.TestCase): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1141 | # Tests taken from libwww-perl, with a few modifications and additions. |
| 1142 | |
| 1143 | def test_netscape_example_1(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1144 | #------------------------------------------------------------------- |
| 1145 | # First we check that it works for the original example at |
| 1146 | # http://www.netscape.com/newsref/std/cookie_spec.html |
| 1147 | |
| 1148 | # Client requests a document, and receives in the response: |
| 1149 | # |
| 1150 | # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT |
| 1151 | # |
| 1152 | # When client requests a URL in path "/" on this server, it sends: |
| 1153 | # |
| 1154 | # Cookie: CUSTOMER=WILE_E_COYOTE |
| 1155 | # |
| 1156 | # Client requests a document, and receives in the response: |
| 1157 | # |
| 1158 | # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/ |
| 1159 | # |
| 1160 | # When client requests a URL in path "/" on this server, it sends: |
| 1161 | # |
| 1162 | # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1163 | # |
| 1164 | # Client receives: |
| 1165 | # |
| 1166 | # Set-Cookie: SHIPPING=FEDEX; path=/fo |
| 1167 | # |
| 1168 | # When client requests a URL in path "/" on this server, it sends: |
| 1169 | # |
| 1170 | # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1171 | # |
| 1172 | # When client requests a URL in path "/foo" on this server, it sends: |
| 1173 | # |
| 1174 | # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX |
| 1175 | # |
| 1176 | # The last Cookie is buggy, because both specifications say that the |
| 1177 | # most specific cookie must be sent first. SHIPPING=FEDEX is the |
| 1178 | # most specific and should thus be first. |
| 1179 | |
| 1180 | year_plus_one = time.localtime()[0] + 1 |
| 1181 | |
| 1182 | headers = [] |
| 1183 | |
| 1184 | c = CookieJar(DefaultCookiePolicy(rfc2965 = True)) |
| 1185 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1186 | #req = urllib.request.Request("http://1.1.1.1/", |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1187 | # headers={"Host": "www.acme.com:80"}) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1188 | req = urllib.request.Request("http://www.acme.com:80/", |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1189 | headers={"Host": "www.acme.com:80"}) |
| 1190 | |
| 1191 | headers.append( |
| 1192 | "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; " |
| 1193 | "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one) |
| 1194 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1195 | c.extract_cookies(res, req) |
| 1196 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1197 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1198 | c.add_cookie_header(req) |
| 1199 | |
| 1200 | self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE") |
| 1201 | self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
| 1202 | |
| 1203 | headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/") |
| 1204 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1205 | c.extract_cookies(res, req) |
| 1206 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1207 | req = urllib.request.Request("http://www.acme.com/foo/bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1208 | c.add_cookie_header(req) |
| 1209 | |
| 1210 | h = req.get_header("Cookie") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1211 | self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1212 | self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1213 | |
| 1214 | headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo') |
| 1215 | res = FakeResponse(headers, "http://www.acme.com") |
| 1216 | c.extract_cookies(res, req) |
| 1217 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1218 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1219 | c.add_cookie_header(req) |
| 1220 | |
| 1221 | h = req.get_header("Cookie") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1222 | self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1223 | self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
| 1224 | self.assertNotIn("SHIPPING=FEDEX", h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1225 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1226 | req = urllib.request.Request("http://www.acme.com/foo/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1227 | c.add_cookie_header(req) |
| 1228 | |
| 1229 | h = req.get_header("Cookie") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1230 | self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1231 | self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
| 1232 | self.assertTrue(h.startswith("SHIPPING=FEDEX;")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1233 | |
| 1234 | def test_netscape_example_2(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1235 | # Second Example transaction sequence: |
| 1236 | # |
| 1237 | # Assume all mappings from above have been cleared. |
| 1238 | # |
| 1239 | # Client receives: |
| 1240 | # |
| 1241 | # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/ |
| 1242 | # |
| 1243 | # When client requests a URL in path "/" on this server, it sends: |
| 1244 | # |
| 1245 | # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1246 | # |
| 1247 | # Client receives: |
| 1248 | # |
| 1249 | # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo |
| 1250 | # |
| 1251 | # When client requests a URL in path "/ammo" on this server, it sends: |
| 1252 | # |
| 1253 | # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1254 | # |
| 1255 | # NOTE: There are two name/value pairs named "PART_NUMBER" due to |
| 1256 | # the inheritance of the "/" mapping in addition to the "/ammo" mapping. |
| 1257 | |
| 1258 | c = CookieJar() |
| 1259 | headers = [] |
| 1260 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1261 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1262 | headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/") |
| 1263 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1264 | |
| 1265 | c.extract_cookies(res, req) |
| 1266 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1267 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1268 | c.add_cookie_header(req) |
| 1269 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1270 | self.assertEqual(req.get_header("Cookie"), |
| 1271 | "PART_NUMBER=ROCKET_LAUNCHER_0001") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1272 | |
| 1273 | headers.append( |
| 1274 | "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo") |
| 1275 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1276 | c.extract_cookies(res, req) |
| 1277 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1278 | req = urllib.request.Request("http://www.acme.com/ammo") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1279 | c.add_cookie_header(req) |
| 1280 | |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1281 | self.assertRegex(req.get_header("Cookie"), |
| 1282 | r"PART_NUMBER=RIDING_ROCKET_0023;\s*" |
| 1283 | "PART_NUMBER=ROCKET_LAUNCHER_0001") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1284 | |
| 1285 | def test_ietf_example_1(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1286 | #------------------------------------------------------------------- |
| 1287 | # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt |
| 1288 | # |
| 1289 | # 5. EXAMPLES |
| 1290 | |
| 1291 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1292 | |
| 1293 | # |
| 1294 | # 5.1 Example 1 |
| 1295 | # |
| 1296 | # Most detail of request and response headers has been omitted. Assume |
| 1297 | # the user agent has no stored cookies. |
| 1298 | # |
| 1299 | # 1. User Agent -> Server |
| 1300 | # |
| 1301 | # POST /acme/login HTTP/1.1 |
| 1302 | # [form data] |
| 1303 | # |
| 1304 | # User identifies self via a form. |
| 1305 | # |
| 1306 | # 2. Server -> User Agent |
| 1307 | # |
| 1308 | # HTTP/1.1 200 OK |
| 1309 | # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme" |
| 1310 | # |
| 1311 | # Cookie reflects user's identity. |
| 1312 | |
| 1313 | cookie = interact_2965( |
| 1314 | c, 'http://www.acme.com/acme/login', |
| 1315 | 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1316 | self.assertFalse(cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1317 | |
| 1318 | # |
| 1319 | # 3. User Agent -> Server |
| 1320 | # |
| 1321 | # POST /acme/pickitem HTTP/1.1 |
| 1322 | # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme" |
| 1323 | # [form data] |
| 1324 | # |
| 1325 | # User selects an item for ``shopping basket.'' |
| 1326 | # |
| 1327 | # 4. Server -> User Agent |
| 1328 | # |
| 1329 | # HTTP/1.1 200 OK |
| 1330 | # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1"; |
| 1331 | # Path="/acme" |
| 1332 | # |
| 1333 | # Shopping basket contains an item. |
| 1334 | |
| 1335 | cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem', |
| 1336 | 'Part_Number="Rocket_Launcher_0001"; ' |
| 1337 | 'Version="1"; Path="/acme"'); |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1338 | self.assertRegex(cookie, |
| 1339 | r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1340 | |
| 1341 | # |
| 1342 | # 5. User Agent -> Server |
| 1343 | # |
| 1344 | # POST /acme/shipping HTTP/1.1 |
| 1345 | # Cookie: $Version="1"; |
| 1346 | # Customer="WILE_E_COYOTE"; $Path="/acme"; |
| 1347 | # Part_Number="Rocket_Launcher_0001"; $Path="/acme" |
| 1348 | # [form data] |
| 1349 | # |
| 1350 | # User selects shipping method from form. |
| 1351 | # |
| 1352 | # 6. Server -> User Agent |
| 1353 | # |
| 1354 | # HTTP/1.1 200 OK |
| 1355 | # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme" |
| 1356 | # |
| 1357 | # New cookie reflects shipping method. |
| 1358 | |
| 1359 | cookie = interact_2965(c, "http://www.acme.com/acme/shipping", |
| 1360 | 'Shipping="FedEx"; Version="1"; Path="/acme"') |
| 1361 | |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1362 | self.assertRegex(cookie, r'^\$Version="?1"?;') |
| 1363 | self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;' |
| 1364 | '\s*\$Path="\/acme"') |
| 1365 | self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;' |
| 1366 | '\s*\$Path="\/acme"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1367 | |
| 1368 | # |
| 1369 | # 7. User Agent -> Server |
| 1370 | # |
| 1371 | # POST /acme/process HTTP/1.1 |
| 1372 | # Cookie: $Version="1"; |
| 1373 | # Customer="WILE_E_COYOTE"; $Path="/acme"; |
| 1374 | # Part_Number="Rocket_Launcher_0001"; $Path="/acme"; |
| 1375 | # Shipping="FedEx"; $Path="/acme" |
| 1376 | # [form data] |
| 1377 | # |
| 1378 | # User chooses to process order. |
| 1379 | # |
| 1380 | # 8. Server -> User Agent |
| 1381 | # |
| 1382 | # HTTP/1.1 200 OK |
| 1383 | # |
| 1384 | # Transaction is complete. |
| 1385 | |
| 1386 | cookie = interact_2965(c, "http://www.acme.com/acme/process") |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1387 | self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"') |
| 1388 | self.assertIn("WILE_E_COYOTE", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1389 | |
| 1390 | # |
| 1391 | # The user agent makes a series of requests on the origin server, after |
| 1392 | # each of which it receives a new cookie. All the cookies have the same |
| 1393 | # Path attribute and (default) domain. Because the request URLs all have |
| 1394 | # /acme as a prefix, and that matches the Path attribute, each request |
| 1395 | # contains all the cookies received so far. |
| 1396 | |
| 1397 | def test_ietf_example_2(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1398 | # 5.2 Example 2 |
| 1399 | # |
| 1400 | # This example illustrates the effect of the Path attribute. All detail |
| 1401 | # of request and response headers has been omitted. Assume the user agent |
| 1402 | # has no stored cookies. |
| 1403 | |
| 1404 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1405 | |
| 1406 | # Imagine the user agent has received, in response to earlier requests, |
| 1407 | # the response headers |
| 1408 | # |
| 1409 | # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1"; |
| 1410 | # Path="/acme" |
| 1411 | # |
| 1412 | # and |
| 1413 | # |
| 1414 | # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1"; |
| 1415 | # Path="/acme/ammo" |
| 1416 | |
| 1417 | interact_2965( |
| 1418 | c, "http://www.acme.com/acme/ammo/specific", |
| 1419 | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"', |
| 1420 | 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"') |
| 1421 | |
| 1422 | # A subsequent request by the user agent to the (same) server for URLs of |
| 1423 | # the form /acme/ammo/... would include the following request header: |
| 1424 | # |
| 1425 | # Cookie: $Version="1"; |
| 1426 | # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo"; |
| 1427 | # Part_Number="Rocket_Launcher_0001"; $Path="/acme" |
| 1428 | # |
| 1429 | # Note that the NAME=VALUE pair for the cookie with the more specific Path |
| 1430 | # attribute, /acme/ammo, comes before the one with the less specific Path |
| 1431 | # attribute, /acme. Further note that the same cookie name appears more |
| 1432 | # than once. |
| 1433 | |
| 1434 | cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...") |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1435 | self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1436 | |
| 1437 | # A subsequent request by the user agent to the (same) server for a URL of |
| 1438 | # the form /acme/parts/ would include the following request header: |
| 1439 | # |
| 1440 | # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme" |
| 1441 | # |
| 1442 | # Here, the second cookie's Path attribute /acme/ammo is not a prefix of |
| 1443 | # the request URL, /acme/parts/, so the cookie does not get forwarded to |
| 1444 | # the server. |
| 1445 | |
| 1446 | cookie = interact_2965(c, "http://www.acme.com/acme/parts/") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1447 | self.assertIn("Rocket_Launcher_0001", cookie) |
| 1448 | self.assertNotIn("Riding_Rocket_0023", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1449 | |
| 1450 | def test_rejection(self): |
| 1451 | # Test rejection of Set-Cookie2 responses based on domain, path, port. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1452 | pol = DefaultCookiePolicy(rfc2965=True) |
| 1453 | |
| 1454 | c = LWPCookieJar(policy=pol) |
| 1455 | |
| 1456 | max_age = "max-age=3600" |
| 1457 | |
| 1458 | # illegal domain (no embedded dots) |
| 1459 | cookie = interact_2965(c, "http://www.acme.com", |
| 1460 | 'foo=bar; domain=".com"; version=1') |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1461 | self.assertFalse(c) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1462 | |
| 1463 | # legal domain |
| 1464 | cookie = interact_2965(c, "http://www.acme.com", |
| 1465 | 'ping=pong; domain="acme.com"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1466 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1467 | |
| 1468 | # illegal domain (host prefix "www.a" contains a dot) |
| 1469 | cookie = interact_2965(c, "http://www.a.acme.com", |
| 1470 | 'whiz=bang; domain="acme.com"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1471 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1472 | |
| 1473 | # legal domain |
| 1474 | cookie = interact_2965(c, "http://www.a.acme.com", |
| 1475 | 'wow=flutter; domain=".a.acme.com"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1476 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1477 | |
| 1478 | # can't partially match an IP-address |
| 1479 | cookie = interact_2965(c, "http://125.125.125.125", |
| 1480 | 'zzzz=ping; domain="125.125.125"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1481 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1482 | |
| 1483 | # illegal path (must be prefix of request path) |
| 1484 | cookie = interact_2965(c, "http://www.sol.no", |
| 1485 | 'blah=rhubarb; domain=".sol.no"; path="/foo"; ' |
| 1486 | 'version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1487 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1488 | |
| 1489 | # legal path |
| 1490 | cookie = interact_2965(c, "http://www.sol.no/foo/bar", |
| 1491 | 'bing=bong; domain=".sol.no"; path="/foo"; ' |
| 1492 | 'version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1493 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1494 | |
| 1495 | # illegal port (request-port not in list) |
| 1496 | cookie = interact_2965(c, "http://www.sol.no", |
| 1497 | 'whiz=ffft; domain=".sol.no"; port="90,100"; ' |
| 1498 | 'version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1499 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1500 | |
| 1501 | # legal port |
| 1502 | cookie = interact_2965( |
| 1503 | c, "http://www.sol.no", |
| 1504 | r'bang=wallop; version=1; domain=".sol.no"; ' |
| 1505 | r'port="90,100, 80,8080"; ' |
| 1506 | r'max-age=100; Comment = "Just kidding! (\"|\\\\) "') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1507 | self.assertEqual(len(c), 4) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1508 | |
| 1509 | # port attribute without any value (current port) |
| 1510 | cookie = interact_2965(c, "http://www.sol.no", |
| 1511 | 'foo9=bar; version=1; domain=".sol.no"; port; ' |
| 1512 | 'max-age=100;') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1513 | self.assertEqual(len(c), 5) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1514 | |
| 1515 | # encoded path |
| 1516 | # LWP has this test, but unescaping allowed path characters seems |
| 1517 | # like a bad idea, so I think this should fail: |
| 1518 | ## cookie = interact_2965(c, "http://www.sol.no/foo/", |
| 1519 | ## r'foo8=bar; version=1; path="/%66oo"') |
| 1520 | # but this is OK, because '<' is not an allowed HTTP URL path |
| 1521 | # character: |
| 1522 | cookie = interact_2965(c, "http://www.sol.no/<oo/", |
| 1523 | r'foo8=bar; version=1; path="/%3coo"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1524 | self.assertEqual(len(c), 6) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1525 | |
| 1526 | # save and restore |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1527 | filename = test.support.TESTFN |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1528 | |
| 1529 | try: |
| 1530 | c.save(filename, ignore_discard=True) |
| 1531 | old = repr(c) |
| 1532 | |
| 1533 | c = LWPCookieJar(policy=pol) |
| 1534 | c.load(filename, ignore_discard=True) |
| 1535 | finally: |
| 1536 | try: os.unlink(filename) |
| 1537 | except OSError: pass |
| 1538 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1539 | self.assertEqual(old, repr(c)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1540 | |
| 1541 | def test_url_encoding(self): |
| 1542 | # Try some URL encodings of the PATHs. |
| 1543 | # (the behaviour here has changed from libwww-perl) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1544 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 1545 | interact_2965(c, "http://www.acme.com/foo%2f%25/" |
| 1546 | "%3c%3c%0Anew%C3%A5/%C3%A5", |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1547 | "foo = bar; version = 1") |
| 1548 | |
| 1549 | cookie = interact_2965( |
Guido van Rossum | f520c05 | 2007-07-23 03:46:37 +0000 | [diff] [blame] | 1550 | c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345", |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1551 | 'bar=baz; path="/foo/"; version=1'); |
| 1552 | version_re = re.compile(r'^\$version=\"?1\"?', re.I) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1553 | self.assertIn("foo=bar", cookie) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1554 | self.assertRegex(cookie, version_re) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1555 | |
| 1556 | cookie = interact_2965( |
Guido van Rossum | f520c05 | 2007-07-23 03:46:37 +0000 | [diff] [blame] | 1557 | c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345") |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1558 | self.assertFalse(cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1559 | |
| 1560 | # unicode URL doesn't raise exception |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1561 | cookie = interact_2965(c, "http://www.acme.com/\xfc") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1562 | |
| 1563 | def test_mozilla(self): |
| 1564 | # Save / load Mozilla/Netscape cookie file format. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1565 | year_plus_one = time.localtime()[0] + 1 |
| 1566 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1567 | filename = test.support.TESTFN |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1568 | |
| 1569 | c = MozillaCookieJar(filename, |
| 1570 | policy=DefaultCookiePolicy(rfc2965=True)) |
| 1571 | interact_2965(c, "http://www.acme.com/", |
| 1572 | "foo1=bar; max-age=100; Version=1") |
| 1573 | interact_2965(c, "http://www.acme.com/", |
| 1574 | 'foo2=bar; port="80"; max-age=100; Discard; Version=1') |
| 1575 | interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1") |
| 1576 | |
| 1577 | expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,) |
| 1578 | interact_netscape(c, "http://www.foo.com/", |
| 1579 | "fooa=bar; %s" % expires) |
| 1580 | interact_netscape(c, "http://www.foo.com/", |
| 1581 | "foob=bar; Domain=.foo.com; %s" % expires) |
| 1582 | interact_netscape(c, "http://www.foo.com/", |
| 1583 | "fooc=bar; Domain=www.foo.com; %s" % expires) |
| 1584 | |
| 1585 | def save_and_restore(cj, ignore_discard): |
| 1586 | try: |
| 1587 | cj.save(ignore_discard=ignore_discard) |
| 1588 | new_c = MozillaCookieJar(filename, |
| 1589 | DefaultCookiePolicy(rfc2965=True)) |
| 1590 | new_c.load(ignore_discard=ignore_discard) |
| 1591 | finally: |
| 1592 | try: os.unlink(filename) |
| 1593 | except OSError: pass |
| 1594 | return new_c |
| 1595 | |
| 1596 | new_c = save_and_restore(c, True) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1597 | self.assertEqual(len(new_c), 6) # none discarded |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1598 | self.assertIn("name='foo1', value='bar'", repr(new_c)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1599 | |
| 1600 | new_c = save_and_restore(c, False) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1601 | self.assertEqual(len(new_c), 4) # 2 of them discarded on save |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1602 | self.assertIn("name='foo1', value='bar'", repr(new_c)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1603 | |
| 1604 | def test_netscape_misc(self): |
| 1605 | # Some additional Netscape cookies tests. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1606 | c = CookieJar() |
| 1607 | headers = [] |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1608 | req = urllib.request.Request("http://foo.bar.acme.com/foo") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1609 | |
| 1610 | # Netscape allows a host part that contains dots |
| 1611 | headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com") |
| 1612 | res = FakeResponse(headers, "http://www.acme.com/foo") |
| 1613 | c.extract_cookies(res, req) |
| 1614 | |
| 1615 | # and that the domain is the same as the host without adding a leading |
| 1616 | # dot to the domain. Should not quote even if strange chars are used |
| 1617 | # in the cookie value. |
| 1618 | headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com") |
| 1619 | res = FakeResponse(headers, "http://www.acme.com/foo") |
| 1620 | c.extract_cookies(res, req) |
| 1621 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1622 | req = urllib.request.Request("http://foo.bar.acme.com/foo") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1623 | c.add_cookie_header(req) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1624 | self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie")) |
| 1625 | self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1626 | |
| 1627 | def test_intranet_domains_2965(self): |
| 1628 | # Test handling of local intranet hostnames without a dot. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1629 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1630 | interact_2965(c, "http://example/", |
| 1631 | "foo1=bar; PORT; Discard; Version=1;") |
| 1632 | cookie = interact_2965(c, "http://example/", |
| 1633 | 'foo2=bar; domain=".local"; Version=1') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1634 | self.assertIn("foo1=bar", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1635 | |
| 1636 | interact_2965(c, "http://example/", 'foo3=bar; Version=1') |
| 1637 | cookie = interact_2965(c, "http://example/") |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1638 | self.assertIn("foo2=bar", cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1639 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1640 | |
| 1641 | def test_intranet_domains_ns(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1642 | c = CookieJar(DefaultCookiePolicy(rfc2965 = False)) |
| 1643 | interact_netscape(c, "http://example/", "foo1=bar") |
| 1644 | cookie = interact_netscape(c, "http://example/", |
| 1645 | 'foo2=bar; domain=.local') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1646 | self.assertEqual(len(c), 2) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1647 | self.assertIn("foo1=bar", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1648 | |
| 1649 | cookie = interact_netscape(c, "http://example/") |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1650 | self.assertIn("foo2=bar", cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1651 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1652 | |
| 1653 | def test_empty_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1654 | # Test for empty path |
| 1655 | # Broken web-server ORION/1.3.38 returns to the client response like |
| 1656 | # |
| 1657 | # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path= |
| 1658 | # |
| 1659 | # ie. with Path set to nothing. |
| 1660 | # In this case, extract_cookies() must set cookie to / (root) |
| 1661 | c = CookieJar(DefaultCookiePolicy(rfc2965 = True)) |
| 1662 | headers = [] |
| 1663 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1664 | req = urllib.request.Request("http://www.ants.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1665 | headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=") |
| 1666 | res = FakeResponse(headers, "http://www.ants.com/") |
| 1667 | c.extract_cookies(res, req) |
| 1668 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1669 | req = urllib.request.Request("http://www.ants.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1670 | c.add_cookie_header(req) |
| 1671 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1672 | self.assertEqual(req.get_header("Cookie"), |
| 1673 | "JSESSIONID=ABCDERANDOM123") |
| 1674 | self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1675 | |
| 1676 | # missing path in the request URI |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1677 | req = urllib.request.Request("http://www.ants.com:8080") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1678 | c.add_cookie_header(req) |
| 1679 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1680 | self.assertEqual(req.get_header("Cookie"), |
| 1681 | "JSESSIONID=ABCDERANDOM123") |
| 1682 | self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1683 | |
| 1684 | def test_session_cookies(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1685 | year_plus_one = time.localtime()[0] + 1 |
| 1686 | |
| 1687 | # Check session cookies are deleted properly by |
| 1688 | # CookieJar.clear_session_cookies method |
| 1689 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1690 | req = urllib.request.Request('http://www.perlmeister.com/scripts') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1691 | headers = [] |
| 1692 | headers.append("Set-Cookie: s1=session;Path=/scripts") |
| 1693 | headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;" |
| 1694 | "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" % |
| 1695 | year_plus_one) |
| 1696 | headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, " |
| 1697 | "02-Feb-%d 23:24:20 GMT" % year_plus_one) |
| 1698 | headers.append("Set-Cookie: s2=session;Path=/scripts;" |
| 1699 | "Domain=.perlmeister.com") |
| 1700 | headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"') |
| 1701 | res = FakeResponse(headers, 'http://www.perlmeister.com/scripts') |
| 1702 | |
| 1703 | c = CookieJar() |
| 1704 | c.extract_cookies(res, req) |
| 1705 | # How many session/permanent cookies do we have? |
| 1706 | counter = {"session_after": 0, |
| 1707 | "perm_after": 0, |
| 1708 | "session_before": 0, |
| 1709 | "perm_before": 0} |
| 1710 | for cookie in c: |
| 1711 | key = "%s_before" % cookie.value |
| 1712 | counter[key] = counter[key] + 1 |
| 1713 | c.clear_session_cookies() |
| 1714 | # How many now? |
| 1715 | for cookie in c: |
| 1716 | key = "%s_after" % cookie.value |
| 1717 | counter[key] = counter[key] + 1 |
| 1718 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1719 | # a permanent cookie got lost accidently |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1720 | self.assertEqual(counter["perm_after"], counter["perm_before"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1721 | # a session cookie hasn't been cleared |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1722 | self.assertEqual(counter["session_after"], 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1723 | # we didn't have session cookies in the first place |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1724 | self.assertNotEqual(counter["session_before"], 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1725 | |
| 1726 | |
| 1727 | def test_main(verbose=None): |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1728 | test.support.run_unittest( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1729 | DateTimeTests, |
| 1730 | HeaderTests, |
| 1731 | CookieTests, |
Martin v. Löwis | c5574e8 | 2005-03-03 10:57:37 +0000 | [diff] [blame] | 1732 | FileCookieJarTests, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1733 | LWPCookieTests, |
| 1734 | ) |
| 1735 | |
| 1736 | if __name__ == "__main__": |
| 1737 | test_main(verbose=True) |