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