+++ /dev/null
-# -*- coding: latin-1 -*-\r
-"""Tests for cookielib.py."""\r
-\r
-import cookielib\r
-import os\r
-import re\r
-import time\r
-\r
-from unittest import TestCase\r
-\r
-from test import test_support\r
-\r
-\r
-class DateTimeTests(TestCase):\r
-\r
- def test_time2isoz(self):\r
- from cookielib import time2isoz\r
-\r
- base = 1019227000\r
- day = 24*3600\r
- self.assertEqual(time2isoz(base), "2002-04-19 14:36:40Z")\r
- self.assertEqual(time2isoz(base+day), "2002-04-20 14:36:40Z")\r
- self.assertEqual(time2isoz(base+2*day), "2002-04-21 14:36:40Z")\r
- self.assertEqual(time2isoz(base+3*day), "2002-04-22 14:36:40Z")\r
-\r
- az = time2isoz()\r
- bz = time2isoz(500000)\r
- for text in (az, bz):\r
- self.assertTrue(re.search(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$", text),\r
- "bad time2isoz format: %s %s" % (az, bz))\r
-\r
- def test_http2time(self):\r
- from cookielib import http2time\r
-\r
- def parse_date(text):\r
- return time.gmtime(http2time(text))[:6]\r
-\r
- self.assertEqual(parse_date("01 Jan 2001"), (2001, 1, 1, 0, 0, 0.0))\r
-\r
- # this test will break around year 2070\r
- self.assertEqual(parse_date("03-Feb-20"), (2020, 2, 3, 0, 0, 0.0))\r
-\r
- # this test will break around year 2048\r
- self.assertEqual(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0))\r
-\r
- def test_http2time_formats(self):\r
- from cookielib import http2time, time2isoz\r
-\r
- # test http2time for supported dates. Test cases with 2 digit year\r
- # will probably break in year 2044.\r
- tests = [\r
- 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format\r
- 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format\r
- 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format\r
-\r
- '03 Feb 1994 00:00:00 GMT', # HTTP format (no weekday)\r
- '03-Feb-94 00:00:00 GMT', # old rfc850 (no weekday)\r
- '03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday)\r
- '03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds)\r
- '03-Feb-1994 00:00', # broken rfc850 (no weekday, no seconds, no tz)\r
-\r
- '03-Feb-94', # old rfc850 HTTP format (no weekday, no time)\r
- '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)\r
- '03 Feb 1994', # proposed new HTTP format (no weekday, no time)\r
-\r
- # A few tests with extra space at various places\r
- ' 03 Feb 1994 0:00 ',\r
- ' 03-Feb-1994 ',\r
- ]\r
-\r
- test_t = 760233600 # assume broken POSIX counting of seconds\r
- result = time2isoz(test_t)\r
- expected = "1994-02-03 00:00:00Z"\r
- self.assertEqual(result, expected,\r
- "%s => '%s' (%s)" % (test_t, result, expected))\r
-\r
- for s in tests:\r
- t = http2time(s)\r
- t2 = http2time(s.lower())\r
- t3 = http2time(s.upper())\r
-\r
- self.assertTrue(t == t2 == t3 == test_t,\r
- "'%s' => %s, %s, %s (%s)" % (s, t, t2, t3, test_t))\r
-\r
- def test_http2time_garbage(self):\r
- from cookielib import http2time\r
-\r
- for test in [\r
- '',\r
- 'Garbage',\r
- 'Mandag 16. September 1996',\r
- '01-00-1980',\r
- '01-13-1980',\r
- '00-01-1980',\r
- '32-01-1980',\r
- '01-01-1980 25:00:00',\r
- '01-01-1980 00:61:00',\r
- '01-01-1980 00:00:62',\r
- ]:\r
- self.assertTrue(http2time(test) is None,\r
- "http2time(%s) is not None\n"\r
- "http2time(test) %s" % (test, http2time(test))\r
- )\r
-\r
-\r
-class HeaderTests(TestCase):\r
-\r
- def test_parse_ns_headers_expires(self):\r
- from cookielib import parse_ns_headers\r
-\r
- # quotes should be stripped\r
- expected = [[('foo', 'bar'), ('expires', 2209069412L), ('version', '0')]]\r
- for hdr in [\r
- 'foo=bar; expires=01 Jan 2040 22:23:32 GMT',\r
- 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"',\r
- ]:\r
- self.assertEqual(parse_ns_headers([hdr]), expected)\r
-\r
- def test_parse_ns_headers_version(self):\r
- from cookielib import parse_ns_headers\r
-\r
- # quotes should be stripped\r
- expected = [[('foo', 'bar'), ('version', '1')]]\r
- for hdr in [\r
- 'foo=bar; version="1"',\r
- 'foo=bar; Version="1"',\r
- ]:\r
- self.assertEqual(parse_ns_headers([hdr]), expected)\r
-\r
- def test_parse_ns_headers_special_names(self):\r
- # names such as 'expires' are not special in first name=value pair\r
- # of Set-Cookie: header\r
- from cookielib import parse_ns_headers\r
-\r
- # Cookie with name 'expires'\r
- hdr = 'expires=01 Jan 2040 22:23:32 GMT'\r
- expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]\r
- self.assertEqual(parse_ns_headers([hdr]), expected)\r
-\r
- def test_join_header_words(self):\r
- from cookielib import join_header_words\r
-\r
- joined = join_header_words([[("foo", None), ("bar", "baz")]])\r
- self.assertEqual(joined, "foo; bar=baz")\r
-\r
- self.assertEqual(join_header_words([[]]), "")\r
-\r
- def test_split_header_words(self):\r
- from cookielib import split_header_words\r
-\r
- tests = [\r
- ("foo", [[("foo", None)]]),\r
- ("foo=bar", [[("foo", "bar")]]),\r
- (" foo ", [[("foo", None)]]),\r
- (" foo= ", [[("foo", "")]]),\r
- (" foo=", [[("foo", "")]]),\r
- (" foo= ; ", [[("foo", "")]]),\r
- (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),\r
- ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),\r
- # doesn't really matter if this next fails, but it works ATM\r
- ("foo= bar=baz", [[("foo", "bar=baz")]]),\r
- ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),\r
- ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),\r
- ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),\r
- (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',\r
- [[("foo", None), ("bar", "baz")],\r
- [("spam", "")], [("foo", ',;"')], [("bar", "")]]),\r
- ]\r
-\r
- for arg, expect in tests:\r
- try:\r
- result = split_header_words([arg])\r
- except:\r
- import traceback, StringIO\r
- f = StringIO.StringIO()\r
- traceback.print_exc(None, f)\r
- result = "(error -- traceback follows)\n\n%s" % f.getvalue()\r
- self.assertEqual(result, expect, """\r
-When parsing: '%s'\r
-Expected: '%s'\r
-Got: '%s'\r
-""" % (arg, expect, result))\r
-\r
- def test_roundtrip(self):\r
- from cookielib import split_header_words, join_header_words\r
-\r
- tests = [\r
- ("foo", "foo"),\r
- ("foo=bar", "foo=bar"),\r
- (" foo ", "foo"),\r
- ("foo=", 'foo=""'),\r
- ("foo=bar bar=baz", "foo=bar; bar=baz"),\r
- ("foo=bar;bar=baz", "foo=bar; bar=baz"),\r
- ('foo bar baz', "foo; bar; baz"),\r
- (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),\r
- ('foo,,,bar', 'foo, bar'),\r
- ('foo=bar,bar=baz', 'foo=bar, bar=baz'),\r
-\r
- ('text/html; charset=iso-8859-1',\r
- 'text/html; charset="iso-8859-1"'),\r
-\r
- ('foo="bar"; port="80,81"; discard, bar=baz',\r
- 'foo=bar; port="80,81"; discard, bar=baz'),\r
-\r
- (r'Basic realm="\"foo\\\\bar\""',\r
- r'Basic; realm="\"foo\\\\bar\""')\r
- ]\r
-\r
- for arg, expect in tests:\r
- input = split_header_words([arg])\r
- res = join_header_words(input)\r
- self.assertEqual(res, expect, """\r
-When parsing: '%s'\r
-Expected: '%s'\r
-Got: '%s'\r
-Input was: '%s'\r
-""" % (arg, expect, res, input))\r
-\r
-\r
-class FakeResponse:\r
- def __init__(self, headers=[], url=None):\r
- """\r
- headers: list of RFC822-style 'Key: value' strings\r
- """\r
- import mimetools, StringIO\r
- f = StringIO.StringIO("\n".join(headers))\r
- self._headers = mimetools.Message(f)\r
- self._url = url\r
- def info(self): return self._headers\r
-\r
-def interact_2965(cookiejar, url, *set_cookie_hdrs):\r
- return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2")\r
-\r
-def interact_netscape(cookiejar, url, *set_cookie_hdrs):\r
- return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")\r
-\r
-def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):\r
- """Perform a single request / response cycle, returning Cookie: header."""\r
- from urllib2 import Request\r
- req = Request(url)\r
- cookiejar.add_cookie_header(req)\r
- cookie_hdr = req.get_header("Cookie", "")\r
- headers = []\r
- for hdr in set_cookie_hdrs:\r
- headers.append("%s: %s" % (hdr_name, hdr))\r
- res = FakeResponse(headers, url)\r
- cookiejar.extract_cookies(res, req)\r
- return cookie_hdr\r
-\r
-\r
-class FileCookieJarTests(TestCase):\r
- def test_lwp_valueless_cookie(self):\r
- # cookies with no value should be saved and loaded consistently\r
- from cookielib import LWPCookieJar\r
- filename = test_support.TESTFN\r
- c = LWPCookieJar()\r
- interact_netscape(c, "http://www.acme.com/", 'boo')\r
- self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)\r
- try:\r
- c.save(filename, ignore_discard=True)\r
- c = LWPCookieJar()\r
- c.load(filename, ignore_discard=True)\r
- finally:\r
- try: os.unlink(filename)\r
- except OSError: pass\r
- self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)\r
-\r
- def test_bad_magic(self):\r
- from cookielib import LWPCookieJar, MozillaCookieJar, LoadError\r
- # IOErrors (eg. file doesn't exist) are allowed to propagate\r
- filename = test_support.TESTFN\r
- for cookiejar_class in LWPCookieJar, MozillaCookieJar:\r
- c = cookiejar_class()\r
- try:\r
- c.load(filename="for this test to work, a file with this "\r
- "filename should not exist")\r
- except IOError, exc:\r
- # exactly IOError, not LoadError\r
- self.assertEqual(exc.__class__, IOError)\r
- else:\r
- self.fail("expected IOError for invalid filename")\r
- # Invalid contents of cookies file (eg. bad magic string)\r
- # causes a LoadError.\r
- try:\r
- f = open(filename, "w")\r
- f.write("oops\n")\r
- for cookiejar_class in LWPCookieJar, MozillaCookieJar:\r
- c = cookiejar_class()\r
- self.assertRaises(LoadError, c.load, filename)\r
- finally:\r
- try: os.unlink(filename)\r
- except OSError: pass\r
-\r
-class CookieTests(TestCase):\r
- # XXX\r
- # Get rid of string comparisons where not actually testing str / repr.\r
- # .clear() etc.\r
- # IP addresses like 50 (single number, no dot) and domain-matching\r
- # functions (and is_HDN)? See draft RFC 2965 errata.\r
- # Strictness switches\r
- # is_third_party()\r
- # unverifiability / third-party blocking\r
- # Netscape cookies work the same as RFC 2965 with regard to port.\r
- # Set-Cookie with negative max age.\r
- # If turn RFC 2965 handling off, Set-Cookie2 cookies should not clobber\r
- # Set-Cookie cookies.\r
- # Cookie2 should be sent if *any* cookies are not V1 (ie. V0 OR V2 etc.).\r
- # Cookies (V1 and V0) with no expiry date should be set to be discarded.\r
- # RFC 2965 Quoting:\r
- # Should accept unquoted cookie-attribute values? check errata draft.\r
- # Which are required on the way in and out?\r
- # Should always return quoted cookie-attribute values?\r
- # Proper testing of when RFC 2965 clobbers Netscape (waiting for errata).\r
- # Path-match on return (same for V0 and V1).\r
- # RFC 2965 acceptance and returning rules\r
- # Set-Cookie2 without version attribute is rejected.\r
-\r
- # Netscape peculiarities list from Ronald Tschalar.\r
- # The first two still need tests, the rest are covered.\r
-## - Quoting: only quotes around the expires value are recognized as such\r
-## (and yes, some folks quote the expires value); quotes around any other\r
-## value are treated as part of the value.\r
-## - White space: white space around names and values is ignored\r
-## - Default path: if no path parameter is given, the path defaults to the\r
-## path in the request-uri up to, but not including, the last '/'. Note\r
-## that this is entirely different from what the spec says.\r
-## - Commas and other delimiters: Netscape just parses until the next ';'.\r
-## This means it will allow commas etc inside values (and yes, both\r
-## commas and equals are commonly appear in the cookie value). This also\r
-## means that if you fold multiple Set-Cookie header fields into one,\r
-## comma-separated list, it'll be a headache to parse (at least my head\r
-## starts hurting everytime I think of that code).\r
-## - Expires: You'll get all sorts of date formats in the expires,\r
-## including emtpy expires attributes ("expires="). Be as flexible as you\r
-## can, and certainly don't expect the weekday to be there; if you can't\r
-## parse it, just ignore it and pretend it's a session cookie.\r
-## - Domain-matching: Netscape uses the 2-dot rule for _all_ domains, not\r
-## just the 7 special TLD's listed in their spec. And folks rely on\r
-## that...\r
-\r
- def test_domain_return_ok(self):\r
- # test optimization: .domain_return_ok() should filter out most\r
- # domains in the CookieJar before we try to access them (because that\r
- # may require disk access -- in particular, with MSIECookieJar)\r
- # This is only a rough check for performance reasons, so it's not too\r
- # critical as long as it's sufficiently liberal.\r
- import cookielib, urllib2\r
- pol = cookielib.DefaultCookiePolicy()\r
- for url, domain, ok in [\r
- ("http://foo.bar.com/", "blah.com", False),\r
- ("http://foo.bar.com/", "rhubarb.blah.com", False),\r
- ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),\r
- ("http://foo.bar.com/", ".foo.bar.com", True),\r
- ("http://foo.bar.com/", "foo.bar.com", True),\r
- ("http://foo.bar.com/", ".bar.com", True),\r
- ("http://foo.bar.com/", "com", True),\r
- ("http://foo.com/", "rhubarb.foo.com", False),\r
- ("http://foo.com/", ".foo.com", True),\r
- ("http://foo.com/", "foo.com", True),\r
- ("http://foo.com/", "com", True),\r
- ("http://foo/", "rhubarb.foo", False),\r
- ("http://foo/", ".foo", True),\r
- ("http://foo/", "foo", True),\r
- ("http://foo/", "foo.local", True),\r
- ("http://foo/", ".local", True),\r
- ]:\r
- request = urllib2.Request(url)\r
- r = pol.domain_return_ok(domain, request)\r
- if ok: self.assertTrue(r)\r
- else: self.assertTrue(not r)\r
-\r
- def test_missing_value(self):\r
- from cookielib import MozillaCookieJar, lwp_cookie_str\r
-\r
- # missing = sign in Cookie: header is regarded by Mozilla as a missing\r
- # name, and by cookielib as a missing value\r
- filename = test_support.TESTFN\r
- c = MozillaCookieJar(filename)\r
- interact_netscape(c, "http://www.acme.com/", 'eggs')\r
- interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')\r
- cookie = c._cookies["www.acme.com"]["/"]["eggs"]\r
- self.assertTrue(cookie.value is None)\r
- self.assertEqual(cookie.name, "eggs")\r
- cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']\r
- self.assertTrue(cookie.value is None)\r
- self.assertEqual(cookie.name, '"spam"')\r
- self.assertEqual(lwp_cookie_str(cookie), (\r
- r'"spam"; path="/foo/"; domain="www.acme.com"; '\r
- 'path_spec; discard; version=0'))\r
- old_str = repr(c)\r
- c.save(ignore_expires=True, ignore_discard=True)\r
- try:\r
- c = MozillaCookieJar(filename)\r
- c.revert(ignore_expires=True, ignore_discard=True)\r
- finally:\r
- os.unlink(c.filename)\r
- # cookies unchanged apart from lost info re. whether path was specified\r
- self.assertEqual(\r
- repr(c),\r
- re.sub("path_specified=%s" % True, "path_specified=%s" % False,\r
- old_str)\r
- )\r
- self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),\r
- '"spam"; eggs')\r
-\r
- def test_rfc2109_handling(self):\r
- # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,\r
- # dependent on policy settings\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- for rfc2109_as_netscape, rfc2965, version in [\r
- # default according to rfc2965 if not explicitly specified\r
- (None, False, 0),\r
- (None, True, 1),\r
- # explicit rfc2109_as_netscape\r
- (False, False, None), # version None here means no cookie stored\r
- (False, True, 1),\r
- (True, False, 0),\r
- (True, True, 0),\r
- ]:\r
- policy = DefaultCookiePolicy(\r
- rfc2109_as_netscape=rfc2109_as_netscape,\r
- rfc2965=rfc2965)\r
- c = CookieJar(policy)\r
- interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")\r
- try:\r
- cookie = c._cookies["www.example.com"]["/"]["ni"]\r
- except KeyError:\r
- self.assertTrue(version is None) # didn't expect a stored cookie\r
- else:\r
- self.assertEqual(cookie.version, version)\r
- # 2965 cookies are unaffected\r
- interact_2965(c, "http://www.example.com/",\r
- "foo=bar; Version=1")\r
- if rfc2965:\r
- cookie2965 = c._cookies["www.example.com"]["/"]["foo"]\r
- self.assertEqual(cookie2965.version, 1)\r
-\r
- def test_ns_parser(self):\r
- from cookielib import CookieJar, DEFAULT_HTTP_PORT\r
-\r
- c = CookieJar()\r
- interact_netscape(c, "http://www.acme.com/",\r
- 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')\r
- interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080')\r
- interact_netscape(c, "http://www.acme.com:80/", 'nini=ni')\r
- interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=')\r
- interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; '\r
- 'expires="Foo Bar 25 33:22:11 3022"')\r
-\r
- cookie = c._cookies[".acme.com"]["/"]["spam"]\r
- self.assertEqual(cookie.domain, ".acme.com")\r
- self.assertTrue(cookie.domain_specified)\r
- self.assertEqual(cookie.port, DEFAULT_HTTP_PORT)\r
- self.assertTrue(not cookie.port_specified)\r
- # case is preserved\r
- self.assertTrue(cookie.has_nonstandard_attr("blArgh") and\r
- not cookie.has_nonstandard_attr("blargh"))\r
-\r
- cookie = c._cookies["www.acme.com"]["/"]["ni"]\r
- self.assertEqual(cookie.domain, "www.acme.com")\r
- self.assertTrue(not cookie.domain_specified)\r
- self.assertEqual(cookie.port, "80,8080")\r
- self.assertTrue(cookie.port_specified)\r
-\r
- cookie = c._cookies["www.acme.com"]["/"]["nini"]\r
- self.assertTrue(cookie.port is None)\r
- self.assertTrue(not cookie.port_specified)\r
-\r
- # invalid expires should not cause cookie to be dropped\r
- foo = c._cookies["www.acme.com"]["/"]["foo"]\r
- spam = c._cookies["www.acme.com"]["/"]["foo"]\r
- self.assertTrue(foo.expires is None)\r
- self.assertTrue(spam.expires is None)\r
-\r
- def test_ns_parser_special_names(self):\r
- # names such as 'expires' are not special in first name=value pair\r
- # of Set-Cookie: header\r
- from cookielib import CookieJar\r
-\r
- c = CookieJar()\r
- interact_netscape(c, "http://www.acme.com/", 'expires=eggs')\r
- interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')\r
-\r
- cookies = c._cookies["www.acme.com"]["/"]\r
- self.assertTrue('expires' in cookies)\r
- self.assertTrue('version' in cookies)\r
-\r
- def test_expires(self):\r
- from cookielib import time2netscape, CookieJar\r
-\r
- # if expires is in future, keep cookie...\r
- c = CookieJar()\r
- future = time2netscape(time.time()+3600)\r
- interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %\r
- future)\r
- self.assertEqual(len(c), 1)\r
- now = time2netscape(time.time()-1)\r
- # ... and if in past or present, discard it\r
- interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %\r
- now)\r
- h = interact_netscape(c, "http://www.acme.com/")\r
- self.assertEqual(len(c), 1)\r
- self.assertTrue('spam="bar"' in h and "foo" not in h)\r
-\r
- # max-age takes precedence over expires, and zero max-age is request to\r
- # delete both new cookie and any old matching cookie\r
- interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %\r
- future)\r
- interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %\r
- future)\r
- self.assertEqual(len(c), 3)\r
- interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '\r
- 'expires=%s; max-age=0' % future)\r
- interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '\r
- 'max-age=0; expires=%s' % future)\r
- h = interact_netscape(c, "http://www.acme.com/")\r
- self.assertEqual(len(c), 1)\r
-\r
- # test expiry at end of session for cookies with no expires attribute\r
- interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')\r
- self.assertEqual(len(c), 2)\r
- c.clear_session_cookies()\r
- self.assertEqual(len(c), 1)\r
- self.assertIn('spam="bar"', h)\r
-\r
- # XXX RFC 2965 expiry rules (some apply to V0 too)\r
-\r
- def test_default_path(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- # RFC 2965\r
- pol = DefaultCookiePolicy(rfc2965=True)\r
-\r
- c = CookieJar(pol)\r
- interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')\r
- self.assertIn("/", c._cookies["www.acme.com"])\r
-\r
- c = CookieJar(pol)\r
- interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"')\r
- self.assertIn("/", c._cookies["www.acme.com"])\r
-\r
- c = CookieJar(pol)\r
- interact_2965(c, "http://www.acme.com/blah/rhubarb",\r
- 'eggs="bar"; Version="1"')\r
- self.assertIn("/blah/", c._cookies["www.acme.com"])\r
-\r
- c = CookieJar(pol)\r
- interact_2965(c, "http://www.acme.com/blah/rhubarb/",\r
- 'eggs="bar"; Version="1"')\r
- self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"])\r
-\r
- # Netscape\r
-\r
- c = CookieJar()\r
- interact_netscape(c, "http://www.acme.com/", 'spam="bar"')\r
- self.assertIn("/", c._cookies["www.acme.com"])\r
-\r
- c = CookieJar()\r
- interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')\r
- self.assertIn("/", c._cookies["www.acme.com"])\r
-\r
- c = CookieJar()\r
- interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')\r
- self.assertIn("/blah", c._cookies["www.acme.com"])\r
-\r
- c = CookieJar()\r
- interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')\r
- self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])\r
-\r
- def test_default_path_with_query(self):\r
- cj = cookielib.CookieJar()\r
- uri = "http://example.com/?spam/eggs"\r
- value = 'eggs="bar"'\r
- interact_netscape(cj, uri, value)\r
- # default path does not include query, so is "/", not "/?spam"\r
- self.assertIn("/", cj._cookies["example.com"])\r
- # cookie is sent back to the same URI\r
- self.assertEqual(interact_netscape(cj, uri), value)\r
-\r
- def test_escape_path(self):\r
- from cookielib import escape_path\r
- cases = [\r
- # quoted safe\r
- ("/foo%2f/bar", "/foo%2F/bar"),\r
- ("/foo%2F/bar", "/foo%2F/bar"),\r
- # quoted %\r
- ("/foo%%/bar", "/foo%%/bar"),\r
- # quoted unsafe\r
- ("/fo%19o/bar", "/fo%19o/bar"),\r
- ("/fo%7do/bar", "/fo%7Do/bar"),\r
- # unquoted safe\r
- ("/foo/bar&", "/foo/bar&"),\r
- ("/foo//bar", "/foo//bar"),\r
- ("\176/foo/bar", "\176/foo/bar"),\r
- # unquoted unsafe\r
- ("/foo\031/bar", "/foo%19/bar"),\r
- ("/\175foo/bar", "/%7Dfoo/bar"),\r
- # unicode\r
- (u"/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded\r
- ]\r
- for arg, result in cases:\r
- self.assertEqual(escape_path(arg), result)\r
-\r
- def test_request_path(self):\r
- from urllib2 import Request\r
- from cookielib import request_path\r
- # with parameters\r
- req = Request("http://www.example.com/rheum/rhaponticum;"\r
- "foo=bar;sing=song?apples=pears&spam=eggs#ni")\r
- self.assertEqual(request_path(req),\r
- "/rheum/rhaponticum;foo=bar;sing=song")\r
- # without parameters\r
- req = Request("http://www.example.com/rheum/rhaponticum?"\r
- "apples=pears&spam=eggs#ni")\r
- self.assertEqual(request_path(req), "/rheum/rhaponticum")\r
- # missing final slash\r
- req = Request("http://www.example.com")\r
- self.assertEqual(request_path(req), "/")\r
-\r
- def test_request_port(self):\r
- from urllib2 import Request\r
- from cookielib import request_port, DEFAULT_HTTP_PORT\r
- req = Request("http://www.acme.com:1234/",\r
- headers={"Host": "www.acme.com:4321"})\r
- self.assertEqual(request_port(req), "1234")\r
- req = Request("http://www.acme.com/",\r
- headers={"Host": "www.acme.com:4321"})\r
- self.assertEqual(request_port(req), DEFAULT_HTTP_PORT)\r
-\r
- def test_request_host(self):\r
- from urllib2 import Request\r
- from cookielib import request_host\r
- # this request is illegal (RFC2616, 14.2.3)\r
- req = Request("http://1.1.1.1/",\r
- headers={"Host": "www.acme.com:80"})\r
- # libwww-perl wants this response, but that seems wrong (RFC 2616,\r
- # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)\r
- #self.assertEqual(request_host(req), "www.acme.com")\r
- self.assertEqual(request_host(req), "1.1.1.1")\r
- req = Request("http://www.acme.com/",\r
- headers={"Host": "irrelevant.com"})\r
- self.assertEqual(request_host(req), "www.acme.com")\r
- # not actually sure this one is valid Request object, so maybe should\r
- # remove test for no host in url in request_host function?\r
- req = Request("/resource.html",\r
- headers={"Host": "www.acme.com"})\r
- self.assertEqual(request_host(req), "www.acme.com")\r
- # port shouldn't be in request-host\r
- req = Request("http://www.acme.com:2345/resource.html",\r
- headers={"Host": "www.acme.com:5432"})\r
- self.assertEqual(request_host(req), "www.acme.com")\r
-\r
- def test_is_HDN(self):\r
- from cookielib import is_HDN\r
- self.assertTrue(is_HDN("foo.bar.com"))\r
- self.assertTrue(is_HDN("1foo2.3bar4.5com"))\r
- self.assertTrue(not is_HDN("192.168.1.1"))\r
- self.assertTrue(not is_HDN(""))\r
- self.assertTrue(not is_HDN("."))\r
- self.assertTrue(not is_HDN(".foo.bar.com"))\r
- self.assertTrue(not is_HDN("..foo"))\r
- self.assertTrue(not is_HDN("foo."))\r
-\r
- def test_reach(self):\r
- from cookielib import reach\r
- self.assertEqual(reach("www.acme.com"), ".acme.com")\r
- self.assertEqual(reach("acme.com"), "acme.com")\r
- self.assertEqual(reach("acme.local"), ".local")\r
- self.assertEqual(reach(".local"), ".local")\r
- self.assertEqual(reach(".com"), ".com")\r
- self.assertEqual(reach("."), ".")\r
- self.assertEqual(reach(""), "")\r
- self.assertEqual(reach("192.168.0.1"), "192.168.0.1")\r
-\r
- def test_domain_match(self):\r
- from cookielib import domain_match, user_domain_match\r
- self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))\r
- self.assertTrue(not domain_match("192.168.1.1", ".168.1.1"))\r
- self.assertTrue(domain_match("x.y.com", "x.Y.com"))\r
- self.assertTrue(domain_match("x.y.com", ".Y.com"))\r
- self.assertTrue(not domain_match("x.y.com", "Y.com"))\r
- self.assertTrue(domain_match("a.b.c.com", ".c.com"))\r
- self.assertTrue(not domain_match(".c.com", "a.b.c.com"))\r
- self.assertTrue(domain_match("example.local", ".local"))\r
- self.assertTrue(not domain_match("blah.blah", ""))\r
- self.assertTrue(not domain_match("", ".rhubarb.rhubarb"))\r
- self.assertTrue(domain_match("", ""))\r
-\r
- self.assertTrue(user_domain_match("acme.com", "acme.com"))\r
- self.assertTrue(not user_domain_match("acme.com", ".acme.com"))\r
- self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))\r
- self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))\r
- self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))\r
- self.assertTrue(user_domain_match("x.y.com", ".Y.com"))\r
- self.assertTrue(not user_domain_match("x.y.com", "Y.com"))\r
- self.assertTrue(user_domain_match("y.com", "Y.com"))\r
- self.assertTrue(not user_domain_match(".y.com", "Y.com"))\r
- self.assertTrue(user_domain_match(".y.com", ".Y.com"))\r
- self.assertTrue(user_domain_match("x.y.com", ".com"))\r
- self.assertTrue(not user_domain_match("x.y.com", "com"))\r
- self.assertTrue(not user_domain_match("x.y.com", "m"))\r
- self.assertTrue(not user_domain_match("x.y.com", ".m"))\r
- self.assertTrue(not user_domain_match("x.y.com", ""))\r
- self.assertTrue(not user_domain_match("x.y.com", "."))\r
- self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))\r
- # not both HDNs, so must string-compare equal to match\r
- self.assertTrue(not user_domain_match("192.168.1.1", ".168.1.1"))\r
- self.assertTrue(not user_domain_match("192.168.1.1", "."))\r
- # empty string is a special case\r
- self.assertTrue(not user_domain_match("192.168.1.1", ""))\r
-\r
- def test_wrong_domain(self):\r
- # Cookies whose effective request-host name does not domain-match the\r
- # domain are rejected.\r
-\r
- # XXX far from complete\r
- from cookielib import CookieJar\r
- c = CookieJar()\r
- interact_2965(c, "http://www.nasty.com/",\r
- 'foo=bar; domain=friendly.org; Version="1"')\r
- self.assertEqual(len(c), 0)\r
-\r
- def test_strict_domain(self):\r
- # Cookies whose domain is a country-code tld like .co.uk should\r
- # not be set if CookiePolicy.strict_domain is true.\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- cp = DefaultCookiePolicy(strict_domain=True)\r
- cj = CookieJar(policy=cp)\r
- interact_netscape(cj, "http://example.co.uk/", 'no=problemo')\r
- interact_netscape(cj, "http://example.co.uk/",\r
- 'okey=dokey; Domain=.example.co.uk')\r
- self.assertEqual(len(cj), 2)\r
- for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:\r
- interact_netscape(cj, "http://example.%s/" % pseudo_tld,\r
- 'spam=eggs; Domain=.co.uk')\r
- self.assertEqual(len(cj), 2)\r
-\r
- def test_two_component_domain_ns(self):\r
- # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain\r
- # should all get accepted, as should .acme.com, acme.com and no domain\r
- # for 2-component domains like acme.com.\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- c = CookieJar()\r
-\r
- # two-component V0 domain is OK\r
- interact_netscape(c, "http://foo.net/", 'ns=bar')\r
- self.assertEqual(len(c), 1)\r
- self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar")\r
- self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar")\r
- # *will* be returned to any other domain (unlike RFC 2965)...\r
- self.assertEqual(interact_netscape(c, "http://www.foo.net/"),\r
- "ns=bar")\r
- # ...unless requested otherwise\r
- pol = DefaultCookiePolicy(\r
- strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)\r
- c.set_policy(pol)\r
- self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "")\r
-\r
- # unlike RFC 2965, even explicit two-component domain is OK,\r
- # because .foo.net matches foo.net\r
- interact_netscape(c, "http://foo.net/foo/",\r
- 'spam1=eggs; domain=foo.net')\r
- # even if starts with a dot -- in NS rules, .foo.net matches foo.net!\r
- interact_netscape(c, "http://foo.net/foo/bar/",\r
- 'spam2=eggs; domain=.foo.net')\r
- self.assertEqual(len(c), 3)\r
- self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value,\r
- "eggs")\r
- self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,\r
- "eggs")\r
- self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"),\r
- "spam2=eggs; spam1=eggs; ns=bar")\r
-\r
- # top-level domain is too general\r
- interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')\r
- self.assertEqual(len(c), 3)\r
-\r
-## # Netscape protocol doesn't allow non-special top level domains (such\r
-## # as co.uk) in the domain attribute unless there are at least three\r
-## # dots in it.\r
- # Oh yes it does! Real implementations don't check this, and real\r
- # cookies (of course) rely on that behaviour.\r
- interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk')\r
-## self.assertEqual(len(c), 2)\r
- self.assertEqual(len(c), 4)\r
-\r
- def test_two_component_domain_rfc2965(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- pol = DefaultCookiePolicy(rfc2965=True)\r
- c = CookieJar(pol)\r
-\r
- # two-component V1 domain is OK\r
- interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')\r
- self.assertEqual(len(c), 1)\r
- self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar")\r
- self.assertEqual(interact_2965(c, "http://foo.net/"),\r
- "$Version=1; foo=bar")\r
- # won't be returned to any other domain (because domain was implied)\r
- self.assertEqual(interact_2965(c, "http://www.foo.net/"), "")\r
-\r
- # unless domain is given explicitly, because then it must be\r
- # rewritten to start with a dot: foo.net --> .foo.net, which does\r
- # not domain-match foo.net\r
- interact_2965(c, "http://foo.net/foo",\r
- 'spam=eggs; domain=foo.net; path=/foo; Version="1"')\r
- self.assertEqual(len(c), 1)\r
- self.assertEqual(interact_2965(c, "http://foo.net/foo"),\r
- "$Version=1; foo=bar")\r
-\r
- # explicit foo.net from three-component domain www.foo.net *does* get\r
- # set, because .foo.net domain-matches .foo.net\r
- interact_2965(c, "http://www.foo.net/foo/",\r
- 'spam=eggs; domain=foo.net; Version="1"')\r
- self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value,\r
- "eggs")\r
- self.assertEqual(len(c), 2)\r
- self.assertEqual(interact_2965(c, "http://foo.net/foo/"),\r
- "$Version=1; foo=bar")\r
- self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"),\r
- '$Version=1; spam=eggs; $Domain="foo.net"')\r
-\r
- # top-level domain is too general\r
- interact_2965(c, "http://foo.net/",\r
- 'ni="ni"; domain=".net"; Version="1"')\r
- self.assertEqual(len(c), 2)\r
-\r
- # RFC 2965 doesn't require blocking this\r
- interact_2965(c, "http://foo.co.uk/",\r
- 'nasty=trick; domain=.co.uk; Version="1"')\r
- self.assertEqual(len(c), 3)\r
-\r
- def test_domain_allow(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
- from urllib2 import Request\r
-\r
- c = CookieJar(policy=DefaultCookiePolicy(\r
- blocked_domains=["acme.com"],\r
- allowed_domains=["www.acme.com"]))\r
-\r
- req = Request("http://acme.com/")\r
- headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]\r
- res = FakeResponse(headers, "http://acme.com/")\r
- c.extract_cookies(res, req)\r
- self.assertEqual(len(c), 0)\r
-\r
- req = Request("http://www.acme.com/")\r
- res = FakeResponse(headers, "http://www.acme.com/")\r
- c.extract_cookies(res, req)\r
- self.assertEqual(len(c), 1)\r
-\r
- req = Request("http://www.coyote.com/")\r
- res = FakeResponse(headers, "http://www.coyote.com/")\r
- c.extract_cookies(res, req)\r
- self.assertEqual(len(c), 1)\r
-\r
- # set a cookie with non-allowed domain...\r
- req = Request("http://www.coyote.com/")\r
- res = FakeResponse(headers, "http://www.coyote.com/")\r
- cookies = c.make_cookies(res, req)\r
- c.set_cookie(cookies[0])\r
- self.assertEqual(len(c), 2)\r
- # ... and check is doesn't get returned\r
- c.add_cookie_header(req)\r
- self.assertTrue(not req.has_header("Cookie"))\r
-\r
- def test_domain_block(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
- from urllib2 import Request\r
-\r
- pol = DefaultCookiePolicy(\r
- rfc2965=True, blocked_domains=[".acme.com"])\r
- c = CookieJar(policy=pol)\r
- headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]\r
-\r
- req = Request("http://www.acme.com/")\r
- res = FakeResponse(headers, "http://www.acme.com/")\r
- c.extract_cookies(res, req)\r
- self.assertEqual(len(c), 0)\r
-\r
- p = pol.set_blocked_domains(["acme.com"])\r
- c.extract_cookies(res, req)\r
- self.assertEqual(len(c), 1)\r
-\r
- c.clear()\r
- req = Request("http://www.roadrunner.net/")\r
- res = FakeResponse(headers, "http://www.roadrunner.net/")\r
- c.extract_cookies(res, req)\r
- self.assertEqual(len(c), 1)\r
- req = Request("http://www.roadrunner.net/")\r
- c.add_cookie_header(req)\r
- self.assertTrue((req.has_header("Cookie") and\r
- req.has_header("Cookie2")))\r
-\r
- c.clear()\r
- pol.set_blocked_domains([".acme.com"])\r
- c.extract_cookies(res, req)\r
- self.assertEqual(len(c), 1)\r
-\r
- # set a cookie with blocked domain...\r
- req = Request("http://www.acme.com/")\r
- res = FakeResponse(headers, "http://www.acme.com/")\r
- cookies = c.make_cookies(res, req)\r
- c.set_cookie(cookies[0])\r
- self.assertEqual(len(c), 2)\r
- # ... and check is doesn't get returned\r
- c.add_cookie_header(req)\r
- self.assertTrue(not req.has_header("Cookie"))\r
-\r
- def test_secure(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- for ns in True, False:\r
- for whitespace in " ", "":\r
- c = CookieJar()\r
- if ns:\r
- pol = DefaultCookiePolicy(rfc2965=False)\r
- int = interact_netscape\r
- vs = ""\r
- else:\r
- pol = DefaultCookiePolicy(rfc2965=True)\r
- int = interact_2965\r
- vs = "; Version=1"\r
- c.set_policy(pol)\r
- url = "http://www.acme.com/"\r
- int(c, url, "foo1=bar%s%s" % (vs, whitespace))\r
- int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))\r
- self.assertTrue(\r
- not c._cookies["www.acme.com"]["/"]["foo1"].secure,\r
- "non-secure cookie registered secure")\r
- self.assertTrue(\r
- c._cookies["www.acme.com"]["/"]["foo2"].secure,\r
- "secure cookie registered non-secure")\r
-\r
- def test_quote_cookie_value(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
- c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))\r
- interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')\r
- h = interact_2965(c, "http://www.acme.com/")\r
- self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r')\r
-\r
- def test_missing_final_slash(self):\r
- # Missing slash from request URL's abs_path should be assumed present.\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
- from urllib2 import Request\r
- url = "http://www.acme.com"\r
- c = CookieJar(DefaultCookiePolicy(rfc2965=True))\r
- interact_2965(c, url, "foo=bar; Version=1")\r
- req = Request(url)\r
- self.assertEqual(len(c), 1)\r
- c.add_cookie_header(req)\r
- self.assertTrue(req.has_header("Cookie"))\r
-\r
- def test_domain_mirror(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- pol = DefaultCookiePolicy(rfc2965=True)\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, "spam=eggs; Version=1")\r
- h = interact_2965(c, url)\r
- self.assertNotIn("Domain", h,\r
- "absent domain returned with domain present")\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')\r
- h = interact_2965(c, url)\r
- self.assertIn('$Domain=".bar.com"', h, "domain not returned")\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- # note missing initial dot in Domain\r
- interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')\r
- h = interact_2965(c, url)\r
- self.assertIn('$Domain="bar.com"', h, "domain not returned")\r
-\r
- def test_path_mirror(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- pol = DefaultCookiePolicy(rfc2965=True)\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, "spam=eggs; Version=1")\r
- h = interact_2965(c, url)\r
- self.assertNotIn("Path", h, "absent path returned with path present")\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, 'spam=eggs; Version=1; Path=/')\r
- h = interact_2965(c, url)\r
- self.assertIn('$Path="/"', h, "path not returned")\r
-\r
- def test_port_mirror(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- pol = DefaultCookiePolicy(rfc2965=True)\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, "spam=eggs; Version=1")\r
- h = interact_2965(c, url)\r
- self.assertNotIn("Port", h, "absent port returned with port present")\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, "spam=eggs; Version=1; Port")\r
- h = interact_2965(c, url)\r
- self.assertTrue(re.search("\$Port([^=]|$)", h),\r
- "port with no value not returned with no value")\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')\r
- h = interact_2965(c, url)\r
- self.assertIn('$Port="80"', h,\r
- "port with single value not returned with single value")\r
-\r
- c = CookieJar(pol)\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')\r
- h = interact_2965(c, url)\r
- self.assertIn('$Port="80,8080"', h,\r
- "port with multiple values not returned with multiple "\r
- "values")\r
-\r
- def test_no_return_comment(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- c = CookieJar(DefaultCookiePolicy(rfc2965=True))\r
- url = "http://foo.bar.com/"\r
- interact_2965(c, url, 'spam=eggs; Version=1; '\r
- 'Comment="does anybody read these?"; '\r
- 'CommentURL="http://foo.bar.net/comment.html"')\r
- h = interact_2965(c, url)\r
- self.assertTrue(\r
- "Comment" not in h,\r
- "Comment or CommentURL cookie-attributes returned to server")\r
-\r
- def test_Cookie_iterator(self):\r
- from cookielib import CookieJar, Cookie, DefaultCookiePolicy\r
-\r
- cs = CookieJar(DefaultCookiePolicy(rfc2965=True))\r
- # add some random cookies\r
- interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '\r
- 'Comment="does anybody read these?"; '\r
- 'CommentURL="http://foo.bar.net/comment.html"')\r
- interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")\r
- interact_2965(cs, "http://www.acme.com/blah/",\r
- "foo=bar; secure; Version=1")\r
- interact_2965(cs, "http://www.acme.com/blah/",\r
- "foo=bar; path=/; Version=1")\r
- interact_2965(cs, "http://www.sol.no",\r
- r'bang=wallop; version=1; domain=".sol.no"; '\r
- r'port="90,100, 80,8080"; '\r
- r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')\r
-\r
- versions = [1, 1, 1, 0, 1]\r
- names = ["bang", "foo", "foo", "spam", "foo"]\r
- domains = [".sol.no", "blah.spam.org", "www.acme.com",\r
- "www.acme.com", "www.acme.com"]\r
- paths = ["/", "/", "/", "/blah", "/blah/"]\r
-\r
- for i in range(4):\r
- i = 0\r
- for c in cs:\r
- self.assertIsInstance(c, Cookie)\r
- self.assertEqual(c.version, versions[i])\r
- self.assertEqual(c.name, names[i])\r
- self.assertEqual(c.domain, domains[i])\r
- self.assertEqual(c.path, paths[i])\r
- i = i + 1\r
-\r
- def test_parse_ns_headers(self):\r
- from cookielib import parse_ns_headers\r
-\r
- # missing domain value (invalid cookie)\r
- self.assertEqual(\r
- parse_ns_headers(["foo=bar; path=/; domain"]),\r
- [[("foo", "bar"),\r
- ("path", "/"), ("domain", None), ("version", "0")]]\r
- )\r
- # invalid expires value\r
- self.assertEqual(\r
- parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),\r
- [[("foo", "bar"), ("expires", None), ("version", "0")]]\r
- )\r
- # missing cookie value (valid cookie)\r
- self.assertEqual(\r
- parse_ns_headers(["foo"]),\r
- [[("foo", None), ("version", "0")]]\r
- )\r
- # shouldn't add version if header is empty\r
- self.assertEqual(parse_ns_headers([""]), [])\r
-\r
- def test_bad_cookie_header(self):\r
-\r
- def cookiejar_from_cookie_headers(headers):\r
- from cookielib import CookieJar\r
- from urllib2 import Request\r
- c = CookieJar()\r
- req = Request("http://www.example.com/")\r
- r = FakeResponse(headers, "http://www.example.com/")\r
- c.extract_cookies(r, req)\r
- return c\r
-\r
- # none of these bad headers should cause an exception to be raised\r
- for headers in [\r
- ["Set-Cookie: "], # actually, nothing wrong with this\r
- ["Set-Cookie2: "], # ditto\r
- # missing domain value\r
- ["Set-Cookie2: a=foo; path=/; Version=1; domain"],\r
- # bad max-age\r
- ["Set-Cookie: b=foo; max-age=oops"],\r
- # bad version\r
- ["Set-Cookie: b=foo; version=spam"],\r
- ]:\r
- c = cookiejar_from_cookie_headers(headers)\r
- # these bad cookies shouldn't be set\r
- self.assertEqual(len(c), 0)\r
-\r
- # cookie with invalid expires is treated as session cookie\r
- headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]\r
- c = cookiejar_from_cookie_headers(headers)\r
- cookie = c._cookies["www.example.com"]["/"]["c"]\r
- self.assertTrue(cookie.expires is None)\r
-\r
-\r
-class LWPCookieTests(TestCase):\r
- # Tests taken from libwww-perl, with a few modifications and additions.\r
-\r
- def test_netscape_example_1(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
- from urllib2 import Request\r
-\r
- #-------------------------------------------------------------------\r
- # First we check that it works for the original example at\r
- # http://www.netscape.com/newsref/std/cookie_spec.html\r
-\r
- # Client requests a document, and receives in the response:\r
- #\r
- # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT\r
- #\r
- # When client requests a URL in path "/" on this server, it sends:\r
- #\r
- # Cookie: CUSTOMER=WILE_E_COYOTE\r
- #\r
- # Client requests a document, and receives in the response:\r
- #\r
- # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/\r
- #\r
- # When client requests a URL in path "/" on this server, it sends:\r
- #\r
- # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001\r
- #\r
- # Client receives:\r
- #\r
- # Set-Cookie: SHIPPING=FEDEX; path=/fo\r
- #\r
- # When client requests a URL in path "/" on this server, it sends:\r
- #\r
- # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001\r
- #\r
- # When client requests a URL in path "/foo" on this server, it sends:\r
- #\r
- # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX\r
- #\r
- # The last Cookie is buggy, because both specifications say that the\r
- # most specific cookie must be sent first. SHIPPING=FEDEX is the\r
- # most specific and should thus be first.\r
-\r
- year_plus_one = time.localtime()[0] + 1\r
-\r
- headers = []\r
-\r
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))\r
-\r
- #req = Request("http://1.1.1.1/",\r
- # headers={"Host": "www.acme.com:80"})\r
- req = Request("http://www.acme.com:80/",\r
- headers={"Host": "www.acme.com:80"})\r
-\r
- headers.append(\r
- "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "\r
- "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)\r
- res = FakeResponse(headers, "http://www.acme.com/")\r
- c.extract_cookies(res, req)\r
-\r
- req = Request("http://www.acme.com/")\r
- c.add_cookie_header(req)\r
-\r
- self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")\r
- self.assertEqual(req.get_header("Cookie2"), '$Version="1"')\r
-\r
- headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")\r
- res = FakeResponse(headers, "http://www.acme.com/")\r
- c.extract_cookies(res, req)\r
-\r
- req = Request("http://www.acme.com/foo/bar")\r
- c.add_cookie_header(req)\r
-\r
- h = req.get_header("Cookie")\r
- self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)\r
- self.assertIn("CUSTOMER=WILE_E_COYOTE", h)\r
-\r
- headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')\r
- res = FakeResponse(headers, "http://www.acme.com")\r
- c.extract_cookies(res, req)\r
-\r
- req = Request("http://www.acme.com/")\r
- c.add_cookie_header(req)\r
-\r
- h = req.get_header("Cookie")\r
- self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)\r
- self.assertIn("CUSTOMER=WILE_E_COYOTE", h)\r
- self.assertNotIn("SHIPPING=FEDEX", h)\r
-\r
- req = Request("http://www.acme.com/foo/")\r
- c.add_cookie_header(req)\r
-\r
- h = req.get_header("Cookie")\r
- self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)\r
- self.assertIn("CUSTOMER=WILE_E_COYOTE", h)\r
- self.assertTrue(h.startswith("SHIPPING=FEDEX;"))\r
-\r
- def test_netscape_example_2(self):\r
- from cookielib import CookieJar\r
- from urllib2 import Request\r
-\r
- # Second Example transaction sequence:\r
- #\r
- # Assume all mappings from above have been cleared.\r
- #\r
- # Client receives:\r
- #\r
- # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/\r
- #\r
- # When client requests a URL in path "/" on this server, it sends:\r
- #\r
- # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001\r
- #\r
- # Client receives:\r
- #\r
- # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo\r
- #\r
- # When client requests a URL in path "/ammo" on this server, it sends:\r
- #\r
- # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001\r
- #\r
- # NOTE: There are two name/value pairs named "PART_NUMBER" due to\r
- # the inheritance of the "/" mapping in addition to the "/ammo" mapping.\r
-\r
- c = CookieJar()\r
- headers = []\r
-\r
- req = Request("http://www.acme.com/")\r
- headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")\r
- res = FakeResponse(headers, "http://www.acme.com/")\r
-\r
- c.extract_cookies(res, req)\r
-\r
- req = Request("http://www.acme.com/")\r
- c.add_cookie_header(req)\r
-\r
- self.assertEqual(req.get_header("Cookie"),\r
- "PART_NUMBER=ROCKET_LAUNCHER_0001")\r
-\r
- headers.append(\r
- "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")\r
- res = FakeResponse(headers, "http://www.acme.com/")\r
- c.extract_cookies(res, req)\r
-\r
- req = Request("http://www.acme.com/ammo")\r
- c.add_cookie_header(req)\r
-\r
- self.assertTrue(re.search(r"PART_NUMBER=RIDING_ROCKET_0023;\s*"\r
- "PART_NUMBER=ROCKET_LAUNCHER_0001",\r
- req.get_header("Cookie")))\r
-\r
- def test_ietf_example_1(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
- #-------------------------------------------------------------------\r
- # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt\r
- #\r
- # 5. EXAMPLES\r
-\r
- c = CookieJar(DefaultCookiePolicy(rfc2965=True))\r
-\r
- #\r
- # 5.1 Example 1\r
- #\r
- # Most detail of request and response headers has been omitted. Assume\r
- # the user agent has no stored cookies.\r
- #\r
- # 1. User Agent -> Server\r
- #\r
- # POST /acme/login HTTP/1.1\r
- # [form data]\r
- #\r
- # User identifies self via a form.\r
- #\r
- # 2. Server -> User Agent\r
- #\r
- # HTTP/1.1 200 OK\r
- # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r
- #\r
- # Cookie reflects user's identity.\r
-\r
- cookie = interact_2965(\r
- c, 'http://www.acme.com/acme/login',\r
- 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')\r
- self.assertTrue(not cookie)\r
-\r
- #\r
- # 3. User Agent -> Server\r
- #\r
- # POST /acme/pickitem HTTP/1.1\r
- # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"\r
- # [form data]\r
- #\r
- # User selects an item for ``shopping basket.''\r
- #\r
- # 4. Server -> User Agent\r
- #\r
- # HTTP/1.1 200 OK\r
- # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";\r
- # Path="/acme"\r
- #\r
- # Shopping basket contains an item.\r
-\r
- cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',\r
- 'Part_Number="Rocket_Launcher_0001"; '\r
- 'Version="1"; Path="/acme"');\r
- self.assertTrue(re.search(\r
- r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$',\r
- cookie))\r
-\r
- #\r
- # 5. User Agent -> Server\r
- #\r
- # POST /acme/shipping HTTP/1.1\r
- # Cookie: $Version="1";\r
- # Customer="WILE_E_COYOTE"; $Path="/acme";\r
- # Part_Number="Rocket_Launcher_0001"; $Path="/acme"\r
- # [form data]\r
- #\r
- # User selects shipping method from form.\r
- #\r
- # 6. Server -> User Agent\r
- #\r
- # HTTP/1.1 200 OK\r
- # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"\r
- #\r
- # New cookie reflects shipping method.\r
-\r
- cookie = interact_2965(c, "http://www.acme.com/acme/shipping",\r
- 'Shipping="FedEx"; Version="1"; Path="/acme"')\r
-\r
- self.assertTrue(re.search(r'^\$Version="?1"?;', cookie))\r
- self.assertTrue(re.search(r'Part_Number="?Rocket_Launcher_0001"?;'\r
- '\s*\$Path="\/acme"', cookie))\r
- self.assertTrue(re.search(r'Customer="?WILE_E_COYOTE"?;\s*\$Path="\/acme"',\r
- cookie))\r
-\r
- #\r
- # 7. User Agent -> Server\r
- #\r
- # POST /acme/process HTTP/1.1\r
- # Cookie: $Version="1";\r
- # Customer="WILE_E_COYOTE"; $Path="/acme";\r
- # Part_Number="Rocket_Launcher_0001"; $Path="/acme";\r
- # Shipping="FedEx"; $Path="/acme"\r
- # [form data]\r
- #\r
- # User chooses to process order.\r
- #\r
- # 8. Server -> User Agent\r
- #\r
- # HTTP/1.1 200 OK\r
- #\r
- # Transaction is complete.\r
-\r
- cookie = interact_2965(c, "http://www.acme.com/acme/process")\r
- self.assertTrue(\r
- re.search(r'Shipping="?FedEx"?;\s*\$Path="\/acme"', cookie) and\r
- "WILE_E_COYOTE" in cookie)\r
-\r
- #\r
- # The user agent makes a series of requests on the origin server, after\r
- # each of which it receives a new cookie. All the cookies have the same\r
- # Path attribute and (default) domain. Because the request URLs all have\r
- # /acme as a prefix, and that matches the Path attribute, each request\r
- # contains all the cookies received so far.\r
-\r
- def test_ietf_example_2(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- # 5.2 Example 2\r
- #\r
- # This example illustrates the effect of the Path attribute. All detail\r
- # of request and response headers has been omitted. Assume the user agent\r
- # has no stored cookies.\r
-\r
- c = CookieJar(DefaultCookiePolicy(rfc2965=True))\r
-\r
- # Imagine the user agent has received, in response to earlier requests,\r
- # the response headers\r
- #\r
- # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";\r
- # Path="/acme"\r
- #\r
- # and\r
- #\r
- # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";\r
- # Path="/acme/ammo"\r
-\r
- interact_2965(\r
- c, "http://www.acme.com/acme/ammo/specific",\r
- 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',\r
- 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')\r
-\r
- # A subsequent request by the user agent to the (same) server for URLs of\r
- # the form /acme/ammo/... would include the following request header:\r
- #\r
- # Cookie: $Version="1";\r
- # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";\r
- # Part_Number="Rocket_Launcher_0001"; $Path="/acme"\r
- #\r
- # Note that the NAME=VALUE pair for the cookie with the more specific Path\r
- # attribute, /acme/ammo, comes before the one with the less specific Path\r
- # attribute, /acme. Further note that the same cookie name appears more\r
- # than once.\r
-\r
- cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")\r
- self.assertTrue(\r
- re.search(r"Riding_Rocket_0023.*Rocket_Launcher_0001", cookie))\r
-\r
- # A subsequent request by the user agent to the (same) server for a URL of\r
- # the form /acme/parts/ would include the following request header:\r
- #\r
- # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"\r
- #\r
- # Here, the second cookie's Path attribute /acme/ammo is not a prefix of\r
- # the request URL, /acme/parts/, so the cookie does not get forwarded to\r
- # the server.\r
-\r
- cookie = interact_2965(c, "http://www.acme.com/acme/parts/")\r
- self.assertIn("Rocket_Launcher_0001", cookie)\r
- self.assertNotIn("Riding_Rocket_0023", cookie)\r
-\r
- def test_rejection(self):\r
- # Test rejection of Set-Cookie2 responses based on domain, path, port.\r
- from cookielib import DefaultCookiePolicy, LWPCookieJar\r
-\r
- pol = DefaultCookiePolicy(rfc2965=True)\r
-\r
- c = LWPCookieJar(policy=pol)\r
-\r
- max_age = "max-age=3600"\r
-\r
- # illegal domain (no embedded dots)\r
- cookie = interact_2965(c, "http://www.acme.com",\r
- 'foo=bar; domain=".com"; version=1')\r
- self.assertTrue(not c)\r
-\r
- # legal domain\r
- cookie = interact_2965(c, "http://www.acme.com",\r
- 'ping=pong; domain="acme.com"; version=1')\r
- self.assertEqual(len(c), 1)\r
-\r
- # illegal domain (host prefix "www.a" contains a dot)\r
- cookie = interact_2965(c, "http://www.a.acme.com",\r
- 'whiz=bang; domain="acme.com"; version=1')\r
- self.assertEqual(len(c), 1)\r
-\r
- # legal domain\r
- cookie = interact_2965(c, "http://www.a.acme.com",\r
- 'wow=flutter; domain=".a.acme.com"; version=1')\r
- self.assertEqual(len(c), 2)\r
-\r
- # can't partially match an IP-address\r
- cookie = interact_2965(c, "http://125.125.125.125",\r
- 'zzzz=ping; domain="125.125.125"; version=1')\r
- self.assertEqual(len(c), 2)\r
-\r
- # illegal path (must be prefix of request path)\r
- cookie = interact_2965(c, "http://www.sol.no",\r
- 'blah=rhubarb; domain=".sol.no"; path="/foo"; '\r
- 'version=1')\r
- self.assertEqual(len(c), 2)\r
-\r
- # legal path\r
- cookie = interact_2965(c, "http://www.sol.no/foo/bar",\r
- 'bing=bong; domain=".sol.no"; path="/foo"; '\r
- 'version=1')\r
- self.assertEqual(len(c), 3)\r
-\r
- # illegal port (request-port not in list)\r
- cookie = interact_2965(c, "http://www.sol.no",\r
- 'whiz=ffft; domain=".sol.no"; port="90,100"; '\r
- 'version=1')\r
- self.assertEqual(len(c), 3)\r
-\r
- # legal port\r
- cookie = interact_2965(\r
- c, "http://www.sol.no",\r
- r'bang=wallop; version=1; domain=".sol.no"; '\r
- r'port="90,100, 80,8080"; '\r
- r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')\r
- self.assertEqual(len(c), 4)\r
-\r
- # port attribute without any value (current port)\r
- cookie = interact_2965(c, "http://www.sol.no",\r
- 'foo9=bar; version=1; domain=".sol.no"; port; '\r
- 'max-age=100;')\r
- self.assertEqual(len(c), 5)\r
-\r
- # encoded path\r
- # LWP has this test, but unescaping allowed path characters seems\r
- # like a bad idea, so I think this should fail:\r
-## cookie = interact_2965(c, "http://www.sol.no/foo/",\r
-## r'foo8=bar; version=1; path="/%66oo"')\r
- # but this is OK, because '<' is not an allowed HTTP URL path\r
- # character:\r
- cookie = interact_2965(c, "http://www.sol.no/<oo/",\r
- r'foo8=bar; version=1; path="/%3coo"')\r
- self.assertEqual(len(c), 6)\r
-\r
- # save and restore\r
- filename = test_support.TESTFN\r
-\r
- try:\r
- c.save(filename, ignore_discard=True)\r
- old = repr(c)\r
-\r
- c = LWPCookieJar(policy=pol)\r
- c.load(filename, ignore_discard=True)\r
- finally:\r
- try: os.unlink(filename)\r
- except OSError: pass\r
-\r
- self.assertEqual(old, repr(c))\r
-\r
- def test_url_encoding(self):\r
- # Try some URL encodings of the PATHs.\r
- # (the behaviour here has changed from libwww-perl)\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- c = CookieJar(DefaultCookiePolicy(rfc2965=True))\r
- interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5",\r
- "foo = bar; version = 1")\r
-\r
- cookie = interact_2965(\r
- c, "http://www.acme.com/foo%2f%25/<<%0anewå/æøå",\r
- 'bar=baz; path="/foo/"; version=1');\r
- version_re = re.compile(r'^\$version=\"?1\"?', re.I)\r
- self.assertTrue("foo=bar" in cookie and version_re.search(cookie))\r
-\r
- cookie = interact_2965(\r
- c, "http://www.acme.com/foo/%25/<<%0anewå/æøå")\r
- self.assertTrue(not cookie)\r
-\r
- # unicode URL doesn't raise exception\r
- cookie = interact_2965(c, u"http://www.acme.com/\xfc")\r
-\r
- def test_mozilla(self):\r
- # Save / load Mozilla/Netscape cookie file format.\r
- from cookielib import MozillaCookieJar, DefaultCookiePolicy\r
-\r
- year_plus_one = time.localtime()[0] + 1\r
-\r
- filename = test_support.TESTFN\r
-\r
- c = MozillaCookieJar(filename,\r
- policy=DefaultCookiePolicy(rfc2965=True))\r
- interact_2965(c, "http://www.acme.com/",\r
- "foo1=bar; max-age=100; Version=1")\r
- interact_2965(c, "http://www.acme.com/",\r
- 'foo2=bar; port="80"; max-age=100; Discard; Version=1')\r
- interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")\r
-\r
- expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)\r
- interact_netscape(c, "http://www.foo.com/",\r
- "fooa=bar; %s" % expires)\r
- interact_netscape(c, "http://www.foo.com/",\r
- "foob=bar; Domain=.foo.com; %s" % expires)\r
- interact_netscape(c, "http://www.foo.com/",\r
- "fooc=bar; Domain=www.foo.com; %s" % expires)\r
-\r
- def save_and_restore(cj, ignore_discard):\r
- try:\r
- cj.save(ignore_discard=ignore_discard)\r
- new_c = MozillaCookieJar(filename,\r
- DefaultCookiePolicy(rfc2965=True))\r
- new_c.load(ignore_discard=ignore_discard)\r
- finally:\r
- try: os.unlink(filename)\r
- except OSError: pass\r
- return new_c\r
-\r
- new_c = save_and_restore(c, True)\r
- self.assertEqual(len(new_c), 6) # none discarded\r
- self.assertIn("name='foo1', value='bar'", repr(new_c))\r
-\r
- new_c = save_and_restore(c, False)\r
- self.assertEqual(len(new_c), 4) # 2 of them discarded on save\r
- self.assertIn("name='foo1', value='bar'", repr(new_c))\r
-\r
- def test_netscape_misc(self):\r
- # Some additional Netscape cookies tests.\r
- from cookielib import CookieJar\r
- from urllib2 import Request\r
-\r
- c = CookieJar()\r
- headers = []\r
- req = Request("http://foo.bar.acme.com/foo")\r
-\r
- # Netscape allows a host part that contains dots\r
- headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")\r
- res = FakeResponse(headers, "http://www.acme.com/foo")\r
- c.extract_cookies(res, req)\r
-\r
- # and that the domain is the same as the host without adding a leading\r
- # dot to the domain. Should not quote even if strange chars are used\r
- # in the cookie value.\r
- headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")\r
- res = FakeResponse(headers, "http://www.acme.com/foo")\r
- c.extract_cookies(res, req)\r
-\r
- req = Request("http://foo.bar.acme.com/foo")\r
- c.add_cookie_header(req)\r
- self.assertTrue(\r
- "PART_NUMBER=3,4" in req.get_header("Cookie") and\r
- "Customer=WILE_E_COYOTE" in req.get_header("Cookie"))\r
-\r
- def test_intranet_domains_2965(self):\r
- # Test handling of local intranet hostnames without a dot.\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- c = CookieJar(DefaultCookiePolicy(rfc2965=True))\r
- interact_2965(c, "http://example/",\r
- "foo1=bar; PORT; Discard; Version=1;")\r
- cookie = interact_2965(c, "http://example/",\r
- 'foo2=bar; domain=".local"; Version=1')\r
- self.assertIn("foo1=bar", cookie)\r
-\r
- interact_2965(c, "http://example/", 'foo3=bar; Version=1')\r
- cookie = interact_2965(c, "http://example/")\r
- self.assertIn("foo2=bar", cookie)\r
- self.assertEqual(len(c), 3)\r
-\r
- def test_intranet_domains_ns(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
-\r
- c = CookieJar(DefaultCookiePolicy(rfc2965 = False))\r
- interact_netscape(c, "http://example/", "foo1=bar")\r
- cookie = interact_netscape(c, "http://example/",\r
- 'foo2=bar; domain=.local')\r
- self.assertEqual(len(c), 2)\r
- self.assertIn("foo1=bar", cookie)\r
-\r
- cookie = interact_netscape(c, "http://example/")\r
- self.assertIn("foo2=bar", cookie)\r
- self.assertEqual(len(c), 2)\r
-\r
- def test_empty_path(self):\r
- from cookielib import CookieJar, DefaultCookiePolicy\r
- from urllib2 import Request\r
-\r
- # Test for empty path\r
- # Broken web-server ORION/1.3.38 returns to the client response like\r
- #\r
- # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=\r
- #\r
- # ie. with Path set to nothing.\r
- # In this case, extract_cookies() must set cookie to / (root)\r
- c = CookieJar(DefaultCookiePolicy(rfc2965 = True))\r
- headers = []\r
-\r
- req = Request("http://www.ants.com/")\r
- headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")\r
- res = FakeResponse(headers, "http://www.ants.com/")\r
- c.extract_cookies(res, req)\r
-\r
- req = Request("http://www.ants.com/")\r
- c.add_cookie_header(req)\r
-\r
- self.assertEqual(req.get_header("Cookie"),\r
- "JSESSIONID=ABCDERANDOM123")\r
- self.assertEqual(req.get_header("Cookie2"), '$Version="1"')\r
-\r
- # missing path in the request URI\r
- req = Request("http://www.ants.com:8080")\r
- c.add_cookie_header(req)\r
-\r
- self.assertEqual(req.get_header("Cookie"),\r
- "JSESSIONID=ABCDERANDOM123")\r
- self.assertEqual(req.get_header("Cookie2"), '$Version="1"')\r
-\r
- def test_session_cookies(self):\r
- from cookielib import CookieJar\r
- from urllib2 import Request\r
-\r
- year_plus_one = time.localtime()[0] + 1\r
-\r
- # Check session cookies are deleted properly by\r
- # CookieJar.clear_session_cookies method\r
-\r
- req = Request('http://www.perlmeister.com/scripts')\r
- headers = []\r
- headers.append("Set-Cookie: s1=session;Path=/scripts")\r
- headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"\r
- "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %\r
- year_plus_one)\r
- headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "\r
- "02-Feb-%d 23:24:20 GMT" % year_plus_one)\r
- headers.append("Set-Cookie: s2=session;Path=/scripts;"\r
- "Domain=.perlmeister.com")\r
- headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')\r
- res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')\r
-\r
- c = CookieJar()\r
- c.extract_cookies(res, req)\r
- # How many session/permanent cookies do we have?\r
- counter = {"session_after": 0,\r
- "perm_after": 0,\r
- "session_before": 0,\r
- "perm_before": 0}\r
- for cookie in c:\r
- key = "%s_before" % cookie.value\r
- counter[key] = counter[key] + 1\r
- c.clear_session_cookies()\r
- # How many now?\r
- for cookie in c:\r
- key = "%s_after" % cookie.value\r
- counter[key] = counter[key] + 1\r
-\r
- self.assertTrue(not (\r
- # a permanent cookie got lost accidentally\r
- counter["perm_after"] != counter["perm_before"] or\r
- # a session cookie hasn't been cleared\r
- counter["session_after"] != 0 or\r
- # we didn't have session cookies in the first place\r
- counter["session_before"] == 0))\r
-\r
-\r
-def test_main(verbose=None):\r
- test_support.run_unittest(\r
- DateTimeTests,\r
- HeaderTests,\r
- CookieTests,\r
- FileCookieJarTests,\r
- LWPCookieTests,\r
- )\r
-\r
-if __name__ == "__main__":\r
- test_main(verbose=True)\r