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