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