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) |
Miss Islington (bot) | 44cb89a | 2019-09-13 04:47:51 -0700 | [diff] [blame] | 588 | |
| 589 | with test.support.check_no_warnings(self): |
| 590 | headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"] |
| 591 | req = urllib.request.Request("http://www.coyote.com/") |
| 592 | res = FakeResponse(headers, "http://www.coyote.com/") |
| 593 | cookies = c.make_cookies(res, req) |
| 594 | self.assertEqual(len(cookies), 1) |
| 595 | self.assertEqual(time2netscape(cookies[0].expires), future) |
| 596 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 597 | interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' % |
| 598 | future) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 599 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 600 | now = time2netscape(time.time()-1) |
| 601 | # ... and if in past or present, discard it |
| 602 | interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' % |
| 603 | now) |
| 604 | h = interact_netscape(c, "http://www.acme.com/") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 605 | self.assertEqual(len(c), 1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 606 | self.assertIn('spam="bar"', h) |
| 607 | self.assertNotIn("foo", h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 608 | |
| 609 | # max-age takes precedence over expires, and zero max-age is request to |
| 610 | # delete both new cookie and any old matching cookie |
| 611 | interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' % |
| 612 | future) |
| 613 | interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' % |
| 614 | future) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 615 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 616 | interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; ' |
| 617 | 'expires=%s; max-age=0' % future) |
| 618 | interact_netscape(c, "http://www.acme.com/", 'bar="bar"; ' |
| 619 | 'max-age=0; expires=%s' % future) |
| 620 | h = interact_netscape(c, "http://www.acme.com/") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 621 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 622 | |
| 623 | # test expiry at end of session for cookies with no expires attribute |
| 624 | interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 625 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 626 | c.clear_session_cookies() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 627 | self.assertEqual(len(c), 1) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 628 | self.assertIn('spam="bar"', h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 629 | |
Robert Collins | a0e5d98 | 2015-08-04 10:06:29 +1200 | [diff] [blame] | 630 | # test if fractional expiry is accepted |
| 631 | cookie = Cookie(0, "name", "value", |
| 632 | None, False, "www.python.org", |
| 633 | True, False, "/", |
| 634 | False, False, "1444312383.018307", |
| 635 | False, None, None, |
| 636 | {}) |
| 637 | self.assertEqual(cookie.expires, 1444312383) |
| 638 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 639 | # XXX RFC 2965 expiry rules (some apply to V0 too) |
| 640 | |
| 641 | def test_default_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 642 | # RFC 2965 |
| 643 | pol = DefaultCookiePolicy(rfc2965=True) |
| 644 | |
| 645 | c = CookieJar(pol) |
| 646 | interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 647 | self.assertIn("/", 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", 'eggs="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 651 | self.assertIn("/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 652 | |
| 653 | c = CookieJar(pol) |
| 654 | interact_2965(c, "http://www.acme.com/blah/rhubarb", |
| 655 | 'eggs="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 656 | self.assertIn("/blah/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 657 | |
| 658 | c = CookieJar(pol) |
| 659 | interact_2965(c, "http://www.acme.com/blah/rhubarb/", |
| 660 | 'eggs="bar"; Version="1"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 661 | self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 662 | |
| 663 | # Netscape |
| 664 | |
| 665 | c = CookieJar() |
| 666 | interact_netscape(c, "http://www.acme.com/", 'spam="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 667 | self.assertIn("/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 668 | |
| 669 | c = CookieJar() |
| 670 | interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 671 | self.assertIn("/", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 672 | |
| 673 | c = CookieJar() |
| 674 | interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 675 | self.assertIn("/blah", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 676 | |
| 677 | c = CookieJar() |
| 678 | interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 679 | self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 680 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 681 | def test_default_path_with_query(self): |
| 682 | cj = CookieJar() |
| 683 | uri = "http://example.com/?spam/eggs" |
| 684 | value = 'eggs="bar"' |
| 685 | interact_netscape(cj, uri, value) |
| 686 | # Default path does not include query, so is "/", not "/?spam". |
| 687 | self.assertIn("/", cj._cookies["example.com"]) |
| 688 | # Cookie is sent back to the same URI. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 689 | self.assertEqual(interact_netscape(cj, uri), value) |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 690 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 691 | def test_escape_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 692 | cases = [ |
| 693 | # quoted safe |
| 694 | ("/foo%2f/bar", "/foo%2F/bar"), |
| 695 | ("/foo%2F/bar", "/foo%2F/bar"), |
| 696 | # quoted % |
| 697 | ("/foo%%/bar", "/foo%%/bar"), |
| 698 | # quoted unsafe |
| 699 | ("/fo%19o/bar", "/fo%19o/bar"), |
| 700 | ("/fo%7do/bar", "/fo%7Do/bar"), |
| 701 | # unquoted safe |
| 702 | ("/foo/bar&", "/foo/bar&"), |
| 703 | ("/foo//bar", "/foo//bar"), |
| 704 | ("\176/foo/bar", "\176/foo/bar"), |
| 705 | # unquoted unsafe |
| 706 | ("/foo\031/bar", "/foo%19/bar"), |
| 707 | ("/\175foo/bar", "/%7Dfoo/bar"), |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 708 | # unicode, latin-1 range |
| 709 | ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 710 | # unicode |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 711 | ("/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] | 712 | ] |
| 713 | for arg, result in cases: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 714 | self.assertEqual(escape_path(arg), result) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 715 | |
| 716 | def test_request_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 717 | # with parameters |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 718 | req = urllib.request.Request( |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 719 | "http://www.example.com/rheum/rhaponticum;" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 720 | "foo=bar;sing=song?apples=pears&spam=eggs#ni") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 721 | self.assertEqual(request_path(req), |
| 722 | "/rheum/rhaponticum;foo=bar;sing=song") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 723 | # without parameters |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 724 | req = urllib.request.Request( |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 725 | "http://www.example.com/rheum/rhaponticum?" |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 726 | "apples=pears&spam=eggs#ni") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 727 | self.assertEqual(request_path(req), "/rheum/rhaponticum") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 728 | # missing final slash |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 729 | req = urllib.request.Request("http://www.example.com") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 730 | self.assertEqual(request_path(req), "/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 731 | |
Xtreak | 0e1f1f0 | 2019-03-10 22:42:28 +0530 | [diff] [blame] | 732 | def test_path_prefix_match(self): |
| 733 | pol = DefaultCookiePolicy() |
| 734 | strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True) |
| 735 | |
| 736 | c = CookieJar(pol) |
| 737 | base_url = "http://bar.com" |
| 738 | interact_netscape(c, base_url, 'spam=eggs; Path=/foo') |
| 739 | cookie = c._cookies['bar.com']['/foo']['spam'] |
| 740 | |
| 741 | for path, ok in [('/foo', True), |
| 742 | ('/foo/', True), |
| 743 | ('/foo/bar', True), |
| 744 | ('/', False), |
| 745 | ('/foobad/foo', False)]: |
| 746 | url = f'{base_url}{path}' |
| 747 | req = urllib.request.Request(url) |
| 748 | h = interact_netscape(c, url) |
| 749 | if ok: |
| 750 | self.assertIn('spam=eggs', h, f"cookie not set for {path}") |
| 751 | self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req)) |
| 752 | else: |
| 753 | self.assertNotIn('spam=eggs', h, f"cookie set for {path}") |
| 754 | self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req)) |
| 755 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 756 | def test_request_port(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 757 | req = urllib.request.Request("http://www.acme.com:1234/", |
| 758 | headers={"Host": "www.acme.com:4321"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 759 | self.assertEqual(request_port(req), "1234") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 760 | req = urllib.request.Request("http://www.acme.com/", |
| 761 | headers={"Host": "www.acme.com:4321"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 762 | self.assertEqual(request_port(req), DEFAULT_HTTP_PORT) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 763 | |
| 764 | def test_request_host(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 765 | # this request is illegal (RFC2616, 14.2.3) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 766 | req = urllib.request.Request("http://1.1.1.1/", |
| 767 | headers={"Host": "www.acme.com:80"}) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 768 | # libwww-perl wants this response, but that seems wrong (RFC 2616, |
| 769 | # section 5.2, point 1., and RFC 2965 section 1, paragraph 3) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 770 | #self.assertEqual(request_host(req), "www.acme.com") |
| 771 | self.assertEqual(request_host(req), "1.1.1.1") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 772 | req = urllib.request.Request("http://www.acme.com/", |
| 773 | headers={"Host": "irrelevant.com"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 774 | self.assertEqual(request_host(req), "www.acme.com") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 775 | # port shouldn't be in request-host |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 776 | req = urllib.request.Request("http://www.acme.com:2345/resource.html", |
| 777 | headers={"Host": "www.acme.com:5432"}) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 778 | self.assertEqual(request_host(req), "www.acme.com") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 779 | |
| 780 | def test_is_HDN(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 781 | self.assertTrue(is_HDN("foo.bar.com")) |
| 782 | self.assertTrue(is_HDN("1foo2.3bar4.5com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 783 | self.assertFalse(is_HDN("192.168.1.1")) |
| 784 | self.assertFalse(is_HDN("")) |
| 785 | self.assertFalse(is_HDN(".")) |
| 786 | self.assertFalse(is_HDN(".foo.bar.com")) |
| 787 | self.assertFalse(is_HDN("..foo")) |
| 788 | self.assertFalse(is_HDN("foo.")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 789 | |
| 790 | def test_reach(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 791 | self.assertEqual(reach("www.acme.com"), ".acme.com") |
| 792 | self.assertEqual(reach("acme.com"), "acme.com") |
| 793 | self.assertEqual(reach("acme.local"), ".local") |
| 794 | self.assertEqual(reach(".local"), ".local") |
| 795 | self.assertEqual(reach(".com"), ".com") |
| 796 | self.assertEqual(reach("."), ".") |
| 797 | self.assertEqual(reach(""), "") |
| 798 | self.assertEqual(reach("192.168.0.1"), "192.168.0.1") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 799 | |
| 800 | def test_domain_match(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 801 | self.assertTrue(domain_match("192.168.1.1", "192.168.1.1")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 802 | self.assertFalse(domain_match("192.168.1.1", ".168.1.1")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 803 | self.assertTrue(domain_match("x.y.com", "x.Y.com")) |
| 804 | self.assertTrue(domain_match("x.y.com", ".Y.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 805 | self.assertFalse(domain_match("x.y.com", "Y.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 806 | self.assertTrue(domain_match("a.b.c.com", ".c.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 807 | self.assertFalse(domain_match(".c.com", "a.b.c.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 808 | self.assertTrue(domain_match("example.local", ".local")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 809 | self.assertFalse(domain_match("blah.blah", "")) |
| 810 | self.assertFalse(domain_match("", ".rhubarb.rhubarb")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 811 | self.assertTrue(domain_match("", "")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 812 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 813 | self.assertTrue(user_domain_match("acme.com", "acme.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 814 | self.assertFalse(user_domain_match("acme.com", ".acme.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 815 | self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com")) |
| 816 | self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com")) |
| 817 | self.assertTrue(user_domain_match("x.y.com", "x.Y.com")) |
| 818 | self.assertTrue(user_domain_match("x.y.com", ".Y.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 819 | self.assertFalse(user_domain_match("x.y.com", "Y.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 820 | self.assertTrue(user_domain_match("y.com", "Y.com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 821 | self.assertFalse(user_domain_match(".y.com", "Y.com")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 822 | self.assertTrue(user_domain_match(".y.com", ".Y.com")) |
| 823 | self.assertTrue(user_domain_match("x.y.com", ".com")) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 824 | self.assertFalse(user_domain_match("x.y.com", "com")) |
| 825 | self.assertFalse(user_domain_match("x.y.com", "m")) |
| 826 | self.assertFalse(user_domain_match("x.y.com", ".m")) |
| 827 | self.assertFalse(user_domain_match("x.y.com", "")) |
| 828 | self.assertFalse(user_domain_match("x.y.com", ".")) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 829 | 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] | 830 | # not both HDNs, so must string-compare equal to match |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 831 | self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1")) |
| 832 | self.assertFalse(user_domain_match("192.168.1.1", ".")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 833 | # empty string is a special case |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 834 | self.assertFalse(user_domain_match("192.168.1.1", "")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 835 | |
| 836 | def test_wrong_domain(self): |
| 837 | # Cookies whose effective request-host name does not domain-match the |
| 838 | # domain are rejected. |
| 839 | |
| 840 | # XXX far from complete |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 841 | c = CookieJar() |
| 842 | interact_2965(c, "http://www.nasty.com/", |
| 843 | 'foo=bar; domain=friendly.org; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 844 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 845 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 846 | def test_strict_domain(self): |
| 847 | # Cookies whose domain is a country-code tld like .co.uk should |
| 848 | # not be set if CookiePolicy.strict_domain is true. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 849 | cp = DefaultCookiePolicy(strict_domain=True) |
| 850 | cj = CookieJar(policy=cp) |
| 851 | interact_netscape(cj, "http://example.co.uk/", 'no=problemo') |
| 852 | interact_netscape(cj, "http://example.co.uk/", |
| 853 | 'okey=dokey; Domain=.example.co.uk') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 854 | self.assertEqual(len(cj), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 855 | for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]: |
| 856 | interact_netscape(cj, "http://example.%s/" % pseudo_tld, |
| 857 | 'spam=eggs; Domain=.co.uk') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 858 | self.assertEqual(len(cj), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 859 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 860 | def test_two_component_domain_ns(self): |
| 861 | # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain |
| 862 | # should all get accepted, as should .acme.com, acme.com and no domain |
| 863 | # for 2-component domains like acme.com. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 864 | c = CookieJar() |
| 865 | |
| 866 | # two-component V0 domain is OK |
| 867 | interact_netscape(c, "http://foo.net/", 'ns=bar') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 868 | self.assertEqual(len(c), 1) |
| 869 | self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar") |
| 870 | self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 871 | # *will* be returned to any other domain (unlike RFC 2965)... |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 872 | self.assertEqual(interact_netscape(c, "http://www.foo.net/"), |
| 873 | "ns=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 874 | # ...unless requested otherwise |
| 875 | pol = DefaultCookiePolicy( |
| 876 | strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain) |
| 877 | c.set_policy(pol) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 878 | self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 879 | |
| 880 | # unlike RFC 2965, even explicit two-component domain is OK, |
| 881 | # because .foo.net matches foo.net |
| 882 | interact_netscape(c, "http://foo.net/foo/", |
| 883 | 'spam1=eggs; domain=foo.net') |
| 884 | # even if starts with a dot -- in NS rules, .foo.net matches foo.net! |
| 885 | interact_netscape(c, "http://foo.net/foo/bar/", |
| 886 | 'spam2=eggs; domain=.foo.net') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 887 | self.assertEqual(len(c), 3) |
| 888 | self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value, |
| 889 | "eggs") |
| 890 | self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value, |
| 891 | "eggs") |
| 892 | self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"), |
| 893 | "spam2=eggs; spam1=eggs; ns=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 894 | |
| 895 | # top-level domain is too general |
| 896 | interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 897 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 898 | |
| 899 | ## # Netscape protocol doesn't allow non-special top level domains (such |
| 900 | ## # as co.uk) in the domain attribute unless there are at least three |
| 901 | ## # dots in it. |
| 902 | # Oh yes it does! Real implementations don't check this, and real |
| 903 | # cookies (of course) rely on that behaviour. |
| 904 | interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 905 | ## self.assertEqual(len(c), 2) |
| 906 | self.assertEqual(len(c), 4) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 907 | |
| 908 | def test_two_component_domain_rfc2965(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 909 | pol = DefaultCookiePolicy(rfc2965=True) |
| 910 | c = CookieJar(pol) |
| 911 | |
| 912 | # two-component V1 domain is OK |
| 913 | interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 914 | self.assertEqual(len(c), 1) |
| 915 | self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar") |
| 916 | self.assertEqual(interact_2965(c, "http://foo.net/"), |
| 917 | "$Version=1; foo=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 918 | # won't be returned to any other domain (because domain was implied) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 919 | self.assertEqual(interact_2965(c, "http://www.foo.net/"), "") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 920 | |
| 921 | # unless domain is given explicitly, because then it must be |
| 922 | # rewritten to start with a dot: foo.net --> .foo.net, which does |
| 923 | # not domain-match foo.net |
| 924 | interact_2965(c, "http://foo.net/foo", |
| 925 | 'spam=eggs; domain=foo.net; path=/foo; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 926 | self.assertEqual(len(c), 1) |
| 927 | self.assertEqual(interact_2965(c, "http://foo.net/foo"), |
| 928 | "$Version=1; foo=bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 929 | |
| 930 | # explicit foo.net from three-component domain www.foo.net *does* get |
| 931 | # set, because .foo.net domain-matches .foo.net |
| 932 | interact_2965(c, "http://www.foo.net/foo/", |
| 933 | 'spam=eggs; domain=foo.net; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 934 | self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value, |
| 935 | "eggs") |
| 936 | self.assertEqual(len(c), 2) |
| 937 | self.assertEqual(interact_2965(c, "http://foo.net/foo/"), |
| 938 | "$Version=1; foo=bar") |
| 939 | self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"), |
| 940 | '$Version=1; spam=eggs; $Domain="foo.net"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 941 | |
| 942 | # top-level domain is too general |
| 943 | interact_2965(c, "http://foo.net/", |
| 944 | 'ni="ni"; domain=".net"; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 945 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 946 | |
| 947 | # RFC 2965 doesn't require blocking this |
| 948 | interact_2965(c, "http://foo.co.uk/", |
| 949 | 'nasty=trick; domain=.co.uk; Version="1"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 950 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 951 | |
| 952 | def test_domain_allow(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 953 | c = CookieJar(policy=DefaultCookiePolicy( |
| 954 | blocked_domains=["acme.com"], |
| 955 | allowed_domains=["www.acme.com"])) |
| 956 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 957 | req = urllib.request.Request("http://acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 958 | headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"] |
| 959 | res = FakeResponse(headers, "http://acme.com/") |
| 960 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 961 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 962 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 963 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 964 | res = FakeResponse(headers, "http://www.acme.com/") |
| 965 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 966 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 967 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 968 | req = urllib.request.Request("http://www.coyote.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 969 | res = FakeResponse(headers, "http://www.coyote.com/") |
| 970 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 971 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 972 | |
| 973 | # set a cookie with non-allowed domain... |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 974 | req = urllib.request.Request("http://www.coyote.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 975 | res = FakeResponse(headers, "http://www.coyote.com/") |
| 976 | cookies = c.make_cookies(res, req) |
| 977 | c.set_cookie(cookies[0]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 978 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 979 | # ... and check is doesn't get returned |
| 980 | c.add_cookie_header(req) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 981 | self.assertFalse(req.has_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 982 | |
| 983 | def test_domain_block(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 984 | pol = DefaultCookiePolicy( |
| 985 | rfc2965=True, blocked_domains=[".acme.com"]) |
| 986 | c = CookieJar(policy=pol) |
| 987 | headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"] |
| 988 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 989 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 990 | res = FakeResponse(headers, "http://www.acme.com/") |
| 991 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 992 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 993 | |
| 994 | p = pol.set_blocked_domains(["acme.com"]) |
| 995 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 996 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 997 | |
| 998 | c.clear() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 999 | req = urllib.request.Request("http://www.roadrunner.net/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1000 | res = FakeResponse(headers, "http://www.roadrunner.net/") |
| 1001 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1002 | self.assertEqual(len(c), 1) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1003 | req = urllib.request.Request("http://www.roadrunner.net/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1004 | c.add_cookie_header(req) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1005 | self.assertTrue(req.has_header("Cookie")) |
| 1006 | self.assertTrue(req.has_header("Cookie2")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1007 | |
| 1008 | c.clear() |
| 1009 | pol.set_blocked_domains([".acme.com"]) |
| 1010 | c.extract_cookies(res, req) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1011 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1012 | |
| 1013 | # set a cookie with blocked domain... |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1014 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1015 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1016 | cookies = c.make_cookies(res, req) |
| 1017 | c.set_cookie(cookies[0]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1018 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1019 | # ... and check is doesn't get returned |
| 1020 | c.add_cookie_header(req) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1021 | self.assertFalse(req.has_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1022 | |
Xtreak | ca7fe50 | 2019-03-10 07:39:48 +0530 | [diff] [blame] | 1023 | c.clear() |
| 1024 | |
| 1025 | pol.set_blocked_domains([]) |
| 1026 | req = urllib.request.Request("http://acme.com/") |
| 1027 | res = FakeResponse(headers, "http://acme.com/") |
| 1028 | cookies = c.make_cookies(res, req) |
| 1029 | c.extract_cookies(res, req) |
| 1030 | self.assertEqual(len(c), 1) |
| 1031 | |
| 1032 | req = urllib.request.Request("http://acme.com/") |
| 1033 | c.add_cookie_header(req) |
| 1034 | self.assertTrue(req.has_header("Cookie")) |
| 1035 | |
| 1036 | req = urllib.request.Request("http://badacme.com/") |
| 1037 | c.add_cookie_header(req) |
| 1038 | self.assertFalse(pol.return_ok(cookies[0], req)) |
| 1039 | self.assertFalse(req.has_header("Cookie")) |
| 1040 | |
| 1041 | p = pol.set_blocked_domains(["acme.com"]) |
| 1042 | req = urllib.request.Request("http://acme.com/") |
| 1043 | c.add_cookie_header(req) |
| 1044 | self.assertFalse(req.has_header("Cookie")) |
| 1045 | |
| 1046 | req = urllib.request.Request("http://badacme.com/") |
| 1047 | c.add_cookie_header(req) |
| 1048 | self.assertFalse(req.has_header("Cookie")) |
| 1049 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1050 | def test_secure(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1051 | for ns in True, False: |
| 1052 | for whitespace in " ", "": |
| 1053 | c = CookieJar() |
| 1054 | if ns: |
| 1055 | pol = DefaultCookiePolicy(rfc2965=False) |
| 1056 | int = interact_netscape |
| 1057 | vs = "" |
| 1058 | else: |
| 1059 | pol = DefaultCookiePolicy(rfc2965=True) |
| 1060 | int = interact_2965 |
| 1061 | vs = "; Version=1" |
| 1062 | c.set_policy(pol) |
| 1063 | url = "http://www.acme.com/" |
| 1064 | int(c, url, "foo1=bar%s%s" % (vs, whitespace)) |
| 1065 | int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace)) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1066 | self.assertFalse( |
| 1067 | c._cookies["www.acme.com"]["/"]["foo1"].secure, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1068 | "non-secure cookie registered secure") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1069 | self.assertTrue( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1070 | c._cookies["www.acme.com"]["/"]["foo2"].secure, |
| 1071 | "secure cookie registered non-secure") |
| 1072 | |
Paul Bailey | 4c33997 | 2018-10-08 13:49:29 -0500 | [diff] [blame] | 1073 | def test_secure_block(self): |
| 1074 | pol = DefaultCookiePolicy() |
| 1075 | c = CookieJar(policy=pol) |
| 1076 | |
| 1077 | headers = ["Set-Cookie: session=narf; secure; path=/"] |
| 1078 | req = urllib.request.Request("https://www.acme.com/") |
| 1079 | res = FakeResponse(headers, "https://www.acme.com/") |
| 1080 | c.extract_cookies(res, req) |
| 1081 | self.assertEqual(len(c), 1) |
| 1082 | |
| 1083 | req = urllib.request.Request("https://www.acme.com/") |
| 1084 | c.add_cookie_header(req) |
| 1085 | self.assertTrue(req.has_header("Cookie")) |
| 1086 | |
| 1087 | req = urllib.request.Request("http://www.acme.com/") |
| 1088 | c.add_cookie_header(req) |
| 1089 | self.assertFalse(req.has_header("Cookie")) |
| 1090 | |
| 1091 | # secure websocket protocol |
| 1092 | req = urllib.request.Request("wss://www.acme.com/") |
| 1093 | c.add_cookie_header(req) |
| 1094 | self.assertTrue(req.has_header("Cookie")) |
| 1095 | |
| 1096 | # non-secure websocket protocol |
| 1097 | req = urllib.request.Request("ws://www.acme.com/") |
| 1098 | c.add_cookie_header(req) |
| 1099 | self.assertFalse(req.has_header("Cookie")) |
| 1100 | |
| 1101 | def test_custom_secure_protocols(self): |
| 1102 | pol = DefaultCookiePolicy(secure_protocols=["foos"]) |
| 1103 | c = CookieJar(policy=pol) |
| 1104 | |
| 1105 | headers = ["Set-Cookie: session=narf; secure; path=/"] |
| 1106 | req = urllib.request.Request("https://www.acme.com/") |
| 1107 | res = FakeResponse(headers, "https://www.acme.com/") |
| 1108 | c.extract_cookies(res, req) |
| 1109 | self.assertEqual(len(c), 1) |
| 1110 | |
| 1111 | # test https removed from secure protocol list |
| 1112 | req = urllib.request.Request("https://www.acme.com/") |
| 1113 | c.add_cookie_header(req) |
| 1114 | self.assertFalse(req.has_header("Cookie")) |
| 1115 | |
| 1116 | req = urllib.request.Request("http://www.acme.com/") |
| 1117 | c.add_cookie_header(req) |
| 1118 | self.assertFalse(req.has_header("Cookie")) |
| 1119 | |
| 1120 | req = urllib.request.Request("foos://www.acme.com/") |
| 1121 | c.add_cookie_header(req) |
| 1122 | self.assertTrue(req.has_header("Cookie")) |
| 1123 | |
| 1124 | req = urllib.request.Request("foo://www.acme.com/") |
| 1125 | c.add_cookie_header(req) |
| 1126 | self.assertFalse(req.has_header("Cookie")) |
| 1127 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1128 | def test_quote_cookie_value(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1129 | c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True)) |
| 1130 | interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1') |
| 1131 | h = interact_2965(c, "http://www.acme.com/") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1132 | self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1133 | |
| 1134 | def test_missing_final_slash(self): |
| 1135 | # 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] | 1136 | url = "http://www.acme.com" |
| 1137 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1138 | interact_2965(c, url, "foo=bar; Version=1") |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1139 | req = urllib.request.Request(url) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1140 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1141 | c.add_cookie_header(req) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1142 | self.assertTrue(req.has_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1143 | |
| 1144 | def test_domain_mirror(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1145 | pol = DefaultCookiePolicy(rfc2965=True) |
| 1146 | |
| 1147 | c = CookieJar(pol) |
| 1148 | url = "http://foo.bar.com/" |
| 1149 | interact_2965(c, url, "spam=eggs; Version=1") |
| 1150 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1151 | self.assertNotIn("Domain", h, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1152 | "absent domain returned with domain present") |
| 1153 | |
| 1154 | c = CookieJar(pol) |
| 1155 | url = "http://foo.bar.com/" |
| 1156 | interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com') |
| 1157 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1158 | self.assertIn('$Domain=".bar.com"', h, "domain not returned") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1159 | |
| 1160 | c = CookieJar(pol) |
| 1161 | url = "http://foo.bar.com/" |
| 1162 | # note missing initial dot in Domain |
| 1163 | interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com') |
| 1164 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1165 | self.assertIn('$Domain="bar.com"', h, "domain not returned") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1166 | |
| 1167 | def test_path_mirror(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1168 | pol = DefaultCookiePolicy(rfc2965=True) |
| 1169 | |
| 1170 | c = CookieJar(pol) |
| 1171 | url = "http://foo.bar.com/" |
| 1172 | interact_2965(c, url, "spam=eggs; Version=1") |
| 1173 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1174 | self.assertNotIn("Path", h, "absent path returned with path present") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1175 | |
| 1176 | c = CookieJar(pol) |
| 1177 | url = "http://foo.bar.com/" |
| 1178 | interact_2965(c, url, 'spam=eggs; Version=1; Path=/') |
| 1179 | h = interact_2965(c, url) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1180 | self.assertIn('$Path="/"', h, "path not returned") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1181 | |
| 1182 | def test_port_mirror(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1183 | pol = DefaultCookiePolicy(rfc2965=True) |
| 1184 | |
| 1185 | c = CookieJar(pol) |
| 1186 | url = "http://foo.bar.com/" |
| 1187 | interact_2965(c, url, "spam=eggs; Version=1") |
| 1188 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1189 | self.assertNotIn("Port", h, "absent port returned with port present") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1190 | |
| 1191 | c = CookieJar(pol) |
| 1192 | url = "http://foo.bar.com/" |
| 1193 | interact_2965(c, url, "spam=eggs; Version=1; Port") |
| 1194 | h = interact_2965(c, url) |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1195 | self.assertRegex(h, r"\$Port([^=]|$)", |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1196 | "port with no value not returned with no value") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1197 | |
| 1198 | c = CookieJar(pol) |
| 1199 | url = "http://foo.bar.com/" |
| 1200 | interact_2965(c, url, 'spam=eggs; Version=1; Port="80"') |
| 1201 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1202 | self.assertIn('$Port="80"', h, |
| 1203 | "port with single value not returned with single value") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1204 | |
| 1205 | c = CookieJar(pol) |
| 1206 | url = "http://foo.bar.com/" |
| 1207 | interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"') |
| 1208 | h = interact_2965(c, url) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1209 | self.assertIn('$Port="80,8080"', h, |
| 1210 | "port with multiple values not returned with multiple " |
| 1211 | "values") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1212 | |
| 1213 | def test_no_return_comment(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1214 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1215 | url = "http://foo.bar.com/" |
| 1216 | interact_2965(c, url, 'spam=eggs; Version=1; ' |
| 1217 | 'Comment="does anybody read these?"; ' |
| 1218 | 'CommentURL="http://foo.bar.net/comment.html"') |
| 1219 | h = interact_2965(c, url) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1220 | self.assertNotIn("Comment", h, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1221 | "Comment or CommentURL cookie-attributes returned to server") |
| 1222 | |
| 1223 | def test_Cookie_iterator(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1224 | cs = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1225 | # add some random cookies |
| 1226 | interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; ' |
| 1227 | 'Comment="does anybody read these?"; ' |
| 1228 | 'CommentURL="http://foo.bar.net/comment.html"') |
| 1229 | interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure") |
| 1230 | interact_2965(cs, "http://www.acme.com/blah/", |
| 1231 | "foo=bar; secure; Version=1") |
| 1232 | interact_2965(cs, "http://www.acme.com/blah/", |
| 1233 | "foo=bar; path=/; Version=1") |
| 1234 | interact_2965(cs, "http://www.sol.no", |
| 1235 | r'bang=wallop; version=1; domain=".sol.no"; ' |
| 1236 | r'port="90,100, 80,8080"; ' |
| 1237 | r'max-age=100; Comment = "Just kidding! (\"|\\\\) "') |
| 1238 | |
| 1239 | versions = [1, 1, 1, 0, 1] |
| 1240 | names = ["bang", "foo", "foo", "spam", "foo"] |
| 1241 | domains = [".sol.no", "blah.spam.org", "www.acme.com", |
| 1242 | "www.acme.com", "www.acme.com"] |
| 1243 | paths = ["/", "/", "/", "/blah", "/blah/"] |
| 1244 | |
| 1245 | for i in range(4): |
| 1246 | i = 0 |
| 1247 | for c in cs: |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1248 | self.assertIsInstance(c, Cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1249 | self.assertEqual(c.version, versions[i]) |
| 1250 | self.assertEqual(c.name, names[i]) |
| 1251 | self.assertEqual(c.domain, domains[i]) |
| 1252 | self.assertEqual(c.path, paths[i]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1253 | i = i + 1 |
| 1254 | |
| 1255 | def test_parse_ns_headers(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1256 | # missing domain value (invalid cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1257 | self.assertEqual( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1258 | parse_ns_headers(["foo=bar; path=/; domain"]), |
| 1259 | [[("foo", "bar"), |
| 1260 | ("path", "/"), ("domain", None), ("version", "0")]] |
| 1261 | ) |
| 1262 | # invalid expires value |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1263 | self.assertEqual( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1264 | parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]), |
| 1265 | [[("foo", "bar"), ("expires", None), ("version", "0")]] |
| 1266 | ) |
| 1267 | # missing cookie value (valid cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1268 | self.assertEqual( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1269 | parse_ns_headers(["foo"]), |
| 1270 | [[("foo", None), ("version", "0")]] |
| 1271 | ) |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 1272 | # missing cookie values for parsed attributes |
| 1273 | self.assertEqual( |
| 1274 | parse_ns_headers(['foo=bar; expires']), |
| 1275 | [[('foo', 'bar'), ('expires', None), ('version', '0')]]) |
| 1276 | self.assertEqual( |
| 1277 | parse_ns_headers(['foo=bar; version']), |
| 1278 | [[('foo', 'bar'), ('version', None)]]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1279 | # shouldn't add version if header is empty |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1280 | self.assertEqual(parse_ns_headers([""]), []) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1281 | |
| 1282 | def test_bad_cookie_header(self): |
| 1283 | |
| 1284 | def cookiejar_from_cookie_headers(headers): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1285 | c = CookieJar() |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1286 | req = urllib.request.Request("http://www.example.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1287 | r = FakeResponse(headers, "http://www.example.com/") |
| 1288 | c.extract_cookies(r, req) |
| 1289 | return c |
| 1290 | |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 1291 | future = time2netscape(time.time()+3600) |
| 1292 | |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1293 | # none of these bad headers should cause an exception to be raised |
| 1294 | for headers in [ |
| 1295 | ["Set-Cookie: "], # actually, nothing wrong with this |
| 1296 | ["Set-Cookie2: "], # ditto |
| 1297 | # missing domain value |
| 1298 | ["Set-Cookie2: a=foo; path=/; Version=1; domain"], |
| 1299 | # bad max-age |
| 1300 | ["Set-Cookie: b=foo; max-age=oops"], |
Benjamin Peterson | 3e5cd1d | 2010-06-27 21:45:24 +0000 | [diff] [blame] | 1301 | # bad version |
| 1302 | ["Set-Cookie: b=foo; version=spam"], |
Serhiy Storchaka | 577fc4e | 2015-03-13 09:05:01 +0200 | [diff] [blame] | 1303 | ["Set-Cookie:; Expires=%s" % future], |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1304 | ]: |
| 1305 | c = cookiejar_from_cookie_headers(headers) |
| 1306 | # these bad cookies shouldn't be set |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1307 | self.assertEqual(len(c), 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1308 | |
| 1309 | # cookie with invalid expires is treated as session cookie |
| 1310 | headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"] |
| 1311 | c = cookiejar_from_cookie_headers(headers) |
| 1312 | cookie = c._cookies["www.example.com"]["/"]["c"] |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1313 | self.assertIsNone(cookie.expires) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1314 | |
| 1315 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1316 | class LWPCookieTests(unittest.TestCase): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1317 | # Tests taken from libwww-perl, with a few modifications and additions. |
| 1318 | |
| 1319 | def test_netscape_example_1(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1320 | #------------------------------------------------------------------- |
| 1321 | # First we check that it works for the original example at |
| 1322 | # http://www.netscape.com/newsref/std/cookie_spec.html |
| 1323 | |
| 1324 | # Client requests a document, and receives in the response: |
| 1325 | # |
| 1326 | # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT |
| 1327 | # |
| 1328 | # When client requests a URL in path "/" on this server, it sends: |
| 1329 | # |
| 1330 | # Cookie: CUSTOMER=WILE_E_COYOTE |
| 1331 | # |
| 1332 | # Client requests a document, and receives in the response: |
| 1333 | # |
| 1334 | # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/ |
| 1335 | # |
| 1336 | # When client requests a URL in path "/" on this server, it sends: |
| 1337 | # |
| 1338 | # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1339 | # |
| 1340 | # Client receives: |
| 1341 | # |
| 1342 | # Set-Cookie: SHIPPING=FEDEX; path=/fo |
| 1343 | # |
| 1344 | # When client requests a URL in path "/" on this server, it sends: |
| 1345 | # |
| 1346 | # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1347 | # |
| 1348 | # When client requests a URL in path "/foo" on this server, it sends: |
| 1349 | # |
| 1350 | # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX |
| 1351 | # |
| 1352 | # The last Cookie is buggy, because both specifications say that the |
| 1353 | # most specific cookie must be sent first. SHIPPING=FEDEX is the |
| 1354 | # most specific and should thus be first. |
| 1355 | |
| 1356 | year_plus_one = time.localtime()[0] + 1 |
| 1357 | |
| 1358 | headers = [] |
| 1359 | |
| 1360 | c = CookieJar(DefaultCookiePolicy(rfc2965 = True)) |
| 1361 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1362 | #req = urllib.request.Request("http://1.1.1.1/", |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1363 | # headers={"Host": "www.acme.com:80"}) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1364 | req = urllib.request.Request("http://www.acme.com:80/", |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1365 | headers={"Host": "www.acme.com:80"}) |
| 1366 | |
| 1367 | headers.append( |
| 1368 | "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; " |
| 1369 | "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one) |
| 1370 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1371 | c.extract_cookies(res, req) |
| 1372 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1373 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1374 | c.add_cookie_header(req) |
| 1375 | |
| 1376 | self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE") |
| 1377 | self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
| 1378 | |
| 1379 | headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/") |
| 1380 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1381 | c.extract_cookies(res, req) |
| 1382 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1383 | req = urllib.request.Request("http://www.acme.com/foo/bar") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1384 | c.add_cookie_header(req) |
| 1385 | |
| 1386 | h = req.get_header("Cookie") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1387 | self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1388 | self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1389 | |
| 1390 | headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo') |
| 1391 | res = FakeResponse(headers, "http://www.acme.com") |
| 1392 | c.extract_cookies(res, req) |
| 1393 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1394 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1395 | c.add_cookie_header(req) |
| 1396 | |
| 1397 | h = req.get_header("Cookie") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1398 | self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1399 | self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
| 1400 | self.assertNotIn("SHIPPING=FEDEX", h) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1401 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1402 | req = urllib.request.Request("http://www.acme.com/foo/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1403 | c.add_cookie_header(req) |
| 1404 | |
| 1405 | h = req.get_header("Cookie") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1406 | self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1407 | self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
| 1408 | self.assertTrue(h.startswith("SHIPPING=FEDEX;")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1409 | |
| 1410 | def test_netscape_example_2(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1411 | # Second Example transaction sequence: |
| 1412 | # |
| 1413 | # Assume all mappings from above have been cleared. |
| 1414 | # |
| 1415 | # Client receives: |
| 1416 | # |
| 1417 | # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/ |
| 1418 | # |
| 1419 | # When client requests a URL in path "/" on this server, it sends: |
| 1420 | # |
| 1421 | # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1422 | # |
| 1423 | # Client receives: |
| 1424 | # |
| 1425 | # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo |
| 1426 | # |
| 1427 | # When client requests a URL in path "/ammo" on this server, it sends: |
| 1428 | # |
| 1429 | # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001 |
| 1430 | # |
| 1431 | # NOTE: There are two name/value pairs named "PART_NUMBER" due to |
| 1432 | # the inheritance of the "/" mapping in addition to the "/ammo" mapping. |
| 1433 | |
| 1434 | c = CookieJar() |
| 1435 | headers = [] |
| 1436 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1437 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1438 | headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/") |
| 1439 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1440 | |
| 1441 | c.extract_cookies(res, req) |
| 1442 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1443 | req = urllib.request.Request("http://www.acme.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1444 | c.add_cookie_header(req) |
| 1445 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1446 | self.assertEqual(req.get_header("Cookie"), |
| 1447 | "PART_NUMBER=ROCKET_LAUNCHER_0001") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1448 | |
| 1449 | headers.append( |
| 1450 | "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo") |
| 1451 | res = FakeResponse(headers, "http://www.acme.com/") |
| 1452 | c.extract_cookies(res, req) |
| 1453 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1454 | req = urllib.request.Request("http://www.acme.com/ammo") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1455 | c.add_cookie_header(req) |
| 1456 | |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1457 | self.assertRegex(req.get_header("Cookie"), |
| 1458 | r"PART_NUMBER=RIDING_ROCKET_0023;\s*" |
| 1459 | "PART_NUMBER=ROCKET_LAUNCHER_0001") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1460 | |
| 1461 | def test_ietf_example_1(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1462 | #------------------------------------------------------------------- |
| 1463 | # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt |
| 1464 | # |
| 1465 | # 5. EXAMPLES |
| 1466 | |
| 1467 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1468 | |
| 1469 | # |
| 1470 | # 5.1 Example 1 |
| 1471 | # |
| 1472 | # Most detail of request and response headers has been omitted. Assume |
| 1473 | # the user agent has no stored cookies. |
| 1474 | # |
| 1475 | # 1. User Agent -> Server |
| 1476 | # |
| 1477 | # POST /acme/login HTTP/1.1 |
| 1478 | # [form data] |
| 1479 | # |
| 1480 | # User identifies self via a form. |
| 1481 | # |
| 1482 | # 2. Server -> User Agent |
| 1483 | # |
| 1484 | # HTTP/1.1 200 OK |
| 1485 | # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme" |
| 1486 | # |
| 1487 | # Cookie reflects user's identity. |
| 1488 | |
| 1489 | cookie = interact_2965( |
| 1490 | c, 'http://www.acme.com/acme/login', |
| 1491 | 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1492 | self.assertFalse(cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1493 | |
| 1494 | # |
| 1495 | # 3. User Agent -> Server |
| 1496 | # |
| 1497 | # POST /acme/pickitem HTTP/1.1 |
| 1498 | # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme" |
| 1499 | # [form data] |
| 1500 | # |
| 1501 | # User selects an item for ``shopping basket.'' |
| 1502 | # |
| 1503 | # 4. Server -> User Agent |
| 1504 | # |
| 1505 | # HTTP/1.1 200 OK |
| 1506 | # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1"; |
| 1507 | # Path="/acme" |
| 1508 | # |
| 1509 | # Shopping basket contains an item. |
| 1510 | |
| 1511 | cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem', |
| 1512 | 'Part_Number="Rocket_Launcher_0001"; ' |
| 1513 | 'Version="1"; Path="/acme"'); |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1514 | self.assertRegex(cookie, |
| 1515 | r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1516 | |
| 1517 | # |
| 1518 | # 5. User Agent -> Server |
| 1519 | # |
| 1520 | # POST /acme/shipping HTTP/1.1 |
| 1521 | # Cookie: $Version="1"; |
| 1522 | # Customer="WILE_E_COYOTE"; $Path="/acme"; |
| 1523 | # Part_Number="Rocket_Launcher_0001"; $Path="/acme" |
| 1524 | # [form data] |
| 1525 | # |
| 1526 | # User selects shipping method from form. |
| 1527 | # |
| 1528 | # 6. Server -> User Agent |
| 1529 | # |
| 1530 | # HTTP/1.1 200 OK |
| 1531 | # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme" |
| 1532 | # |
| 1533 | # New cookie reflects shipping method. |
| 1534 | |
| 1535 | cookie = interact_2965(c, "http://www.acme.com/acme/shipping", |
| 1536 | 'Shipping="FedEx"; Version="1"; Path="/acme"') |
| 1537 | |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1538 | self.assertRegex(cookie, r'^\$Version="?1"?;') |
| 1539 | self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1540 | r'\s*\$Path="\/acme"') |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1541 | self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1542 | r'\s*\$Path="\/acme"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1543 | |
| 1544 | # |
| 1545 | # 7. User Agent -> Server |
| 1546 | # |
| 1547 | # POST /acme/process HTTP/1.1 |
| 1548 | # Cookie: $Version="1"; |
| 1549 | # Customer="WILE_E_COYOTE"; $Path="/acme"; |
| 1550 | # Part_Number="Rocket_Launcher_0001"; $Path="/acme"; |
| 1551 | # Shipping="FedEx"; $Path="/acme" |
| 1552 | # [form data] |
| 1553 | # |
| 1554 | # User chooses to process order. |
| 1555 | # |
| 1556 | # 8. Server -> User Agent |
| 1557 | # |
| 1558 | # HTTP/1.1 200 OK |
| 1559 | # |
| 1560 | # Transaction is complete. |
| 1561 | |
| 1562 | cookie = interact_2965(c, "http://www.acme.com/acme/process") |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1563 | self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"') |
| 1564 | self.assertIn("WILE_E_COYOTE", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1565 | |
| 1566 | # |
| 1567 | # The user agent makes a series of requests on the origin server, after |
| 1568 | # each of which it receives a new cookie. All the cookies have the same |
| 1569 | # Path attribute and (default) domain. Because the request URLs all have |
| 1570 | # /acme as a prefix, and that matches the Path attribute, each request |
| 1571 | # contains all the cookies received so far. |
| 1572 | |
| 1573 | def test_ietf_example_2(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1574 | # 5.2 Example 2 |
| 1575 | # |
| 1576 | # This example illustrates the effect of the Path attribute. All detail |
| 1577 | # of request and response headers has been omitted. Assume the user agent |
| 1578 | # has no stored cookies. |
| 1579 | |
| 1580 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1581 | |
| 1582 | # Imagine the user agent has received, in response to earlier requests, |
| 1583 | # the response headers |
| 1584 | # |
| 1585 | # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1"; |
| 1586 | # Path="/acme" |
| 1587 | # |
| 1588 | # and |
| 1589 | # |
| 1590 | # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1"; |
| 1591 | # Path="/acme/ammo" |
| 1592 | |
| 1593 | interact_2965( |
| 1594 | c, "http://www.acme.com/acme/ammo/specific", |
| 1595 | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"', |
| 1596 | 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"') |
| 1597 | |
| 1598 | # A subsequent request by the user agent to the (same) server for URLs of |
| 1599 | # the form /acme/ammo/... would include the following request header: |
| 1600 | # |
| 1601 | # Cookie: $Version="1"; |
| 1602 | # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo"; |
| 1603 | # Part_Number="Rocket_Launcher_0001"; $Path="/acme" |
| 1604 | # |
| 1605 | # Note that the NAME=VALUE pair for the cookie with the more specific Path |
| 1606 | # attribute, /acme/ammo, comes before the one with the less specific Path |
| 1607 | # attribute, /acme. Further note that the same cookie name appears more |
| 1608 | # than once. |
| 1609 | |
| 1610 | cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...") |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1611 | self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1612 | |
| 1613 | # A subsequent request by the user agent to the (same) server for a URL of |
| 1614 | # the form /acme/parts/ would include the following request header: |
| 1615 | # |
| 1616 | # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme" |
| 1617 | # |
| 1618 | # Here, the second cookie's Path attribute /acme/ammo is not a prefix of |
| 1619 | # the request URL, /acme/parts/, so the cookie does not get forwarded to |
| 1620 | # the server. |
| 1621 | |
| 1622 | cookie = interact_2965(c, "http://www.acme.com/acme/parts/") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 1623 | self.assertIn("Rocket_Launcher_0001", cookie) |
| 1624 | self.assertNotIn("Riding_Rocket_0023", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1625 | |
| 1626 | def test_rejection(self): |
| 1627 | # 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] | 1628 | pol = DefaultCookiePolicy(rfc2965=True) |
| 1629 | |
| 1630 | c = LWPCookieJar(policy=pol) |
| 1631 | |
| 1632 | max_age = "max-age=3600" |
| 1633 | |
| 1634 | # illegal domain (no embedded dots) |
| 1635 | cookie = interact_2965(c, "http://www.acme.com", |
| 1636 | 'foo=bar; domain=".com"; version=1') |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1637 | self.assertFalse(c) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1638 | |
| 1639 | # legal domain |
| 1640 | cookie = interact_2965(c, "http://www.acme.com", |
| 1641 | 'ping=pong; domain="acme.com"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1642 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1643 | |
| 1644 | # illegal domain (host prefix "www.a" contains a dot) |
| 1645 | cookie = interact_2965(c, "http://www.a.acme.com", |
| 1646 | 'whiz=bang; domain="acme.com"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1647 | self.assertEqual(len(c), 1) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1648 | |
| 1649 | # legal domain |
| 1650 | cookie = interact_2965(c, "http://www.a.acme.com", |
| 1651 | 'wow=flutter; domain=".a.acme.com"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1652 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1653 | |
| 1654 | # can't partially match an IP-address |
| 1655 | cookie = interact_2965(c, "http://125.125.125.125", |
| 1656 | 'zzzz=ping; domain="125.125.125"; version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1657 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1658 | |
| 1659 | # illegal path (must be prefix of request path) |
| 1660 | cookie = interact_2965(c, "http://www.sol.no", |
| 1661 | 'blah=rhubarb; domain=".sol.no"; path="/foo"; ' |
| 1662 | 'version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1663 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1664 | |
| 1665 | # legal path |
| 1666 | cookie = interact_2965(c, "http://www.sol.no/foo/bar", |
| 1667 | 'bing=bong; domain=".sol.no"; path="/foo"; ' |
| 1668 | 'version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1669 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1670 | |
| 1671 | # illegal port (request-port not in list) |
| 1672 | cookie = interact_2965(c, "http://www.sol.no", |
| 1673 | 'whiz=ffft; domain=".sol.no"; port="90,100"; ' |
| 1674 | 'version=1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1675 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1676 | |
| 1677 | # legal port |
| 1678 | cookie = interact_2965( |
| 1679 | c, "http://www.sol.no", |
| 1680 | r'bang=wallop; version=1; domain=".sol.no"; ' |
| 1681 | r'port="90,100, 80,8080"; ' |
| 1682 | r'max-age=100; Comment = "Just kidding! (\"|\\\\) "') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1683 | self.assertEqual(len(c), 4) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1684 | |
| 1685 | # port attribute without any value (current port) |
| 1686 | cookie = interact_2965(c, "http://www.sol.no", |
| 1687 | 'foo9=bar; version=1; domain=".sol.no"; port; ' |
| 1688 | 'max-age=100;') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1689 | self.assertEqual(len(c), 5) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1690 | |
| 1691 | # encoded path |
| 1692 | # LWP has this test, but unescaping allowed path characters seems |
| 1693 | # like a bad idea, so I think this should fail: |
| 1694 | ## cookie = interact_2965(c, "http://www.sol.no/foo/", |
| 1695 | ## r'foo8=bar; version=1; path="/%66oo"') |
| 1696 | # but this is OK, because '<' is not an allowed HTTP URL path |
| 1697 | # character: |
| 1698 | cookie = interact_2965(c, "http://www.sol.no/<oo/", |
| 1699 | r'foo8=bar; version=1; path="/%3coo"') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1700 | self.assertEqual(len(c), 6) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1701 | |
| 1702 | # save and restore |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1703 | filename = test.support.TESTFN |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1704 | |
| 1705 | try: |
| 1706 | c.save(filename, ignore_discard=True) |
| 1707 | old = repr(c) |
| 1708 | |
| 1709 | c = LWPCookieJar(policy=pol) |
| 1710 | c.load(filename, ignore_discard=True) |
| 1711 | finally: |
| 1712 | try: os.unlink(filename) |
| 1713 | except OSError: pass |
| 1714 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1715 | self.assertEqual(old, repr(c)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1716 | |
| 1717 | def test_url_encoding(self): |
| 1718 | # Try some URL encodings of the PATHs. |
| 1719 | # (the behaviour here has changed from libwww-perl) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1720 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 1721 | interact_2965(c, "http://www.acme.com/foo%2f%25/" |
| 1722 | "%3c%3c%0Anew%C3%A5/%C3%A5", |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1723 | "foo = bar; version = 1") |
| 1724 | |
| 1725 | cookie = interact_2965( |
Guido van Rossum | f520c05 | 2007-07-23 03:46:37 +0000 | [diff] [blame] | 1726 | 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] | 1727 | 'bar=baz; path="/foo/"; version=1'); |
| 1728 | version_re = re.compile(r'^\$version=\"?1\"?', re.I) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1729 | self.assertIn("foo=bar", cookie) |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1730 | self.assertRegex(cookie, version_re) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1731 | |
| 1732 | cookie = interact_2965( |
Guido van Rossum | f520c05 | 2007-07-23 03:46:37 +0000 | [diff] [blame] | 1733 | c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345") |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1734 | self.assertFalse(cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1735 | |
| 1736 | # unicode URL doesn't raise exception |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 1737 | cookie = interact_2965(c, "http://www.acme.com/\xfc") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1738 | |
| 1739 | def test_mozilla(self): |
| 1740 | # Save / load Mozilla/Netscape cookie file format. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1741 | year_plus_one = time.localtime()[0] + 1 |
| 1742 | |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1743 | filename = test.support.TESTFN |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1744 | |
| 1745 | c = MozillaCookieJar(filename, |
| 1746 | policy=DefaultCookiePolicy(rfc2965=True)) |
| 1747 | interact_2965(c, "http://www.acme.com/", |
| 1748 | "foo1=bar; max-age=100; Version=1") |
| 1749 | interact_2965(c, "http://www.acme.com/", |
| 1750 | 'foo2=bar; port="80"; max-age=100; Discard; Version=1') |
| 1751 | interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1") |
| 1752 | |
| 1753 | expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,) |
| 1754 | interact_netscape(c, "http://www.foo.com/", |
| 1755 | "fooa=bar; %s" % expires) |
| 1756 | interact_netscape(c, "http://www.foo.com/", |
| 1757 | "foob=bar; Domain=.foo.com; %s" % expires) |
| 1758 | interact_netscape(c, "http://www.foo.com/", |
| 1759 | "fooc=bar; Domain=www.foo.com; %s" % expires) |
| 1760 | |
| 1761 | def save_and_restore(cj, ignore_discard): |
| 1762 | try: |
| 1763 | cj.save(ignore_discard=ignore_discard) |
| 1764 | new_c = MozillaCookieJar(filename, |
| 1765 | DefaultCookiePolicy(rfc2965=True)) |
| 1766 | new_c.load(ignore_discard=ignore_discard) |
| 1767 | finally: |
| 1768 | try: os.unlink(filename) |
| 1769 | except OSError: pass |
| 1770 | return new_c |
| 1771 | |
| 1772 | new_c = save_and_restore(c, True) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1773 | self.assertEqual(len(new_c), 6) # none discarded |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1774 | self.assertIn("name='foo1', value='bar'", repr(new_c)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1775 | |
| 1776 | new_c = save_and_restore(c, False) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1777 | self.assertEqual(len(new_c), 4) # 2 of them discarded on save |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1778 | self.assertIn("name='foo1', value='bar'", repr(new_c)) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1779 | |
| 1780 | def test_netscape_misc(self): |
| 1781 | # Some additional Netscape cookies tests. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1782 | c = CookieJar() |
| 1783 | headers = [] |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1784 | req = urllib.request.Request("http://foo.bar.acme.com/foo") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1785 | |
| 1786 | # Netscape allows a host part that contains dots |
| 1787 | headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com") |
| 1788 | res = FakeResponse(headers, "http://www.acme.com/foo") |
| 1789 | c.extract_cookies(res, req) |
| 1790 | |
| 1791 | # and that the domain is the same as the host without adding a leading |
| 1792 | # dot to the domain. Should not quote even if strange chars are used |
| 1793 | # in the cookie value. |
| 1794 | headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com") |
| 1795 | res = FakeResponse(headers, "http://www.acme.com/foo") |
| 1796 | c.extract_cookies(res, req) |
| 1797 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1798 | req = urllib.request.Request("http://foo.bar.acme.com/foo") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1799 | c.add_cookie_header(req) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1800 | self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie")) |
| 1801 | self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie")) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1802 | |
| 1803 | def test_intranet_domains_2965(self): |
| 1804 | # Test handling of local intranet hostnames without a dot. |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1805 | c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1806 | interact_2965(c, "http://example/", |
| 1807 | "foo1=bar; PORT; Discard; Version=1;") |
| 1808 | cookie = interact_2965(c, "http://example/", |
| 1809 | 'foo2=bar; domain=".local"; Version=1') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1810 | self.assertIn("foo1=bar", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1811 | |
| 1812 | interact_2965(c, "http://example/", 'foo3=bar; Version=1') |
| 1813 | cookie = interact_2965(c, "http://example/") |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1814 | self.assertIn("foo2=bar", cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1815 | self.assertEqual(len(c), 3) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1816 | |
| 1817 | def test_intranet_domains_ns(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1818 | c = CookieJar(DefaultCookiePolicy(rfc2965 = False)) |
| 1819 | interact_netscape(c, "http://example/", "foo1=bar") |
| 1820 | cookie = interact_netscape(c, "http://example/", |
| 1821 | 'foo2=bar; domain=.local') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1822 | self.assertEqual(len(c), 2) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1823 | self.assertIn("foo1=bar", cookie) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1824 | |
| 1825 | cookie = interact_netscape(c, "http://example/") |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 1826 | self.assertIn("foo2=bar", cookie) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1827 | self.assertEqual(len(c), 2) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1828 | |
| 1829 | def test_empty_path(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1830 | # Test for empty path |
| 1831 | # Broken web-server ORION/1.3.38 returns to the client response like |
| 1832 | # |
| 1833 | # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path= |
| 1834 | # |
| 1835 | # ie. with Path set to nothing. |
| 1836 | # In this case, extract_cookies() must set cookie to / (root) |
| 1837 | c = CookieJar(DefaultCookiePolicy(rfc2965 = True)) |
| 1838 | headers = [] |
| 1839 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1840 | req = urllib.request.Request("http://www.ants.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1841 | headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=") |
| 1842 | res = FakeResponse(headers, "http://www.ants.com/") |
| 1843 | c.extract_cookies(res, req) |
| 1844 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1845 | req = urllib.request.Request("http://www.ants.com/") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1846 | c.add_cookie_header(req) |
| 1847 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1848 | self.assertEqual(req.get_header("Cookie"), |
| 1849 | "JSESSIONID=ABCDERANDOM123") |
| 1850 | self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1851 | |
| 1852 | # missing path in the request URI |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1853 | req = urllib.request.Request("http://www.ants.com:8080") |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1854 | c.add_cookie_header(req) |
| 1855 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1856 | self.assertEqual(req.get_header("Cookie"), |
| 1857 | "JSESSIONID=ABCDERANDOM123") |
| 1858 | self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1859 | |
| 1860 | def test_session_cookies(self): |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1861 | year_plus_one = time.localtime()[0] + 1 |
| 1862 | |
| 1863 | # Check session cookies are deleted properly by |
| 1864 | # CookieJar.clear_session_cookies method |
| 1865 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 1866 | req = urllib.request.Request('http://www.perlmeister.com/scripts') |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1867 | headers = [] |
| 1868 | headers.append("Set-Cookie: s1=session;Path=/scripts") |
| 1869 | headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;" |
| 1870 | "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" % |
| 1871 | year_plus_one) |
| 1872 | headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, " |
| 1873 | "02-Feb-%d 23:24:20 GMT" % year_plus_one) |
| 1874 | headers.append("Set-Cookie: s2=session;Path=/scripts;" |
| 1875 | "Domain=.perlmeister.com") |
| 1876 | headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"') |
| 1877 | res = FakeResponse(headers, 'http://www.perlmeister.com/scripts') |
| 1878 | |
| 1879 | c = CookieJar() |
| 1880 | c.extract_cookies(res, req) |
| 1881 | # How many session/permanent cookies do we have? |
| 1882 | counter = {"session_after": 0, |
| 1883 | "perm_after": 0, |
| 1884 | "session_before": 0, |
| 1885 | "perm_before": 0} |
| 1886 | for cookie in c: |
| 1887 | key = "%s_before" % cookie.value |
| 1888 | counter[key] = counter[key] + 1 |
| 1889 | c.clear_session_cookies() |
| 1890 | # How many now? |
| 1891 | for cookie in c: |
| 1892 | key = "%s_after" % cookie.value |
| 1893 | counter[key] = counter[key] + 1 |
| 1894 | |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 1895 | # a permanent cookie got lost accidentally |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1896 | self.assertEqual(counter["perm_after"], counter["perm_before"]) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1897 | # a session cookie hasn't been cleared |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1898 | self.assertEqual(counter["session_after"], 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1899 | # we didn't have session cookies in the first place |
Serhiy Storchaka | 9d282f6 | 2013-11-17 13:45:02 +0200 | [diff] [blame] | 1900 | self.assertNotEqual(counter["session_before"], 0) |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1901 | |
| 1902 | |
| 1903 | def test_main(verbose=None): |
Gregory P. Smith | 41e6c3d | 2010-07-19 23:17:22 +0000 | [diff] [blame] | 1904 | test.support.run_unittest( |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1905 | DateTimeTests, |
| 1906 | HeaderTests, |
| 1907 | CookieTests, |
Martin v. Löwis | c5574e8 | 2005-03-03 10:57:37 +0000 | [diff] [blame] | 1908 | FileCookieJarTests, |
Martin v. Löwis | 2a6ba90 | 2004-05-31 18:22:40 +0000 | [diff] [blame] | 1909 | LWPCookieTests, |
| 1910 | ) |
| 1911 | |
| 1912 | if __name__ == "__main__": |
| 1913 | test_main(verbose=True) |