diff options
Diffstat (limited to 'Lib/test/test_http_cookiejar.py')
| -rw-r--r-- | Lib/test/test_http_cookiejar.py | 117 |
1 files changed, 65 insertions, 52 deletions
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 3e85f62..712a4ae 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -29,7 +29,7 @@ class DateTimeTests(unittest.TestCase): bz = time2isoz(500000) for text in (az, bz): self.assertTrue(re.search(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$", text), - "bad time2isoz format: %s %s" % (az, bz)) + "bad time2isoz format: %s %s" % (az, bz)) def test_http2time(self): def parse_date(text): @@ -100,6 +100,7 @@ class DateTimeTests(unittest.TestCase): class HeaderTests(unittest.TestCase): + def test_parse_ns_headers(self): # quotes should be stripped expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]] @@ -109,6 +110,16 @@ class HeaderTests(unittest.TestCase): ]: self.assertEqual(parse_ns_headers([hdr]), expected) + def test_parse_ns_headers_version(self): + + # quotes should be stripped + expected = [[('foo', 'bar'), ('version', '1')]] + for hdr in [ + 'foo=bar; version="1"', + 'foo=bar; Version="1"', + ]: + self.assertEqual(parse_ns_headers([hdr]), expected) + def test_parse_ns_headers_special_names(self): # names such as 'expires' are not special in first name=value pair # of Set-Cookie: header @@ -252,11 +263,11 @@ class FileCookieJarTests(unittest.TestCase): # Invalid contents of cookies file (eg. bad magic string) # causes a LoadError. try: - f = open(filename, "w") - f.write("oops\n") - for cookiejar_class in LWPCookieJar, MozillaCookieJar: - c = cookiejar_class() - self.assertRaises(LoadError, c.load, filename) + with open(filename, "w") as f: + f.write("oops\n") + for cookiejar_class in LWPCookieJar, MozillaCookieJar: + c = cookiejar_class() + self.assertRaises(LoadError, c.load, filename) finally: try: os.unlink(filename) except OSError: pass @@ -444,8 +455,8 @@ class CookieTests(unittest.TestCase): interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs') cookies = c._cookies["www.acme.com"]["/"] - self.assertTrue('expires' in cookies) - self.assertTrue('version' in cookies) + self.assertIn('expires', cookies) + self.assertIn('version', cookies) def test_expires(self): # if expires is in future, keep cookie... @@ -460,7 +471,8 @@ class CookieTests(unittest.TestCase): now) h = interact_netscape(c, "http://www.acme.com/") self.assertEqual(len(c), 1) - self.assertTrue('spam="bar"' in h and "foo" not in h) + self.assertIn('spam="bar"', h) + self.assertNotIn("foo", h) # max-age takes precedence over expires, and zero max-age is request to # delete both new cookie and any old matching cookie @@ -481,7 +493,7 @@ class CookieTests(unittest.TestCase): self.assertEqual(len(c), 2) c.clear_session_cookies() self.assertEqual(len(c), 1) - self.assertTrue('spam="bar"' in h) + self.assertIn('spam="bar"', h) # XXX RFC 2965 expiry rules (some apply to V0 too) @@ -491,39 +503,39 @@ class CookieTests(unittest.TestCase): c = CookieJar(pol) interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"') - self.assertTrue("/" in c._cookies["www.acme.com"]) + self.assertIn("/", c._cookies["www.acme.com"]) c = CookieJar(pol) interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"') - self.assertTrue("/" in c._cookies["www.acme.com"]) + self.assertIn("/", c._cookies["www.acme.com"]) c = CookieJar(pol) interact_2965(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"; Version="1"') - self.assertTrue("/blah/" in c._cookies["www.acme.com"]) + self.assertIn("/blah/", c._cookies["www.acme.com"]) c = CookieJar(pol) interact_2965(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"; Version="1"') - self.assertTrue("/blah/rhubarb/" in c._cookies["www.acme.com"]) + self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"]) # Netscape c = CookieJar() interact_netscape(c, "http://www.acme.com/", 'spam="bar"') - self.assertTrue("/" in c._cookies["www.acme.com"]) + self.assertIn("/", c._cookies["www.acme.com"]) c = CookieJar() interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"') - self.assertTrue("/" in c._cookies["www.acme.com"]) + self.assertIn("/", c._cookies["www.acme.com"]) c = CookieJar() interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"') - self.assertTrue("/blah" in c._cookies["www.acme.com"]) + self.assertIn("/blah", c._cookies["www.acme.com"]) c = CookieJar() interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"') - self.assertTrue("/blah/rhubarb" in c._cookies["www.acme.com"]) + self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"]) def test_default_path_with_query(self): cj = CookieJar() @@ -566,7 +578,7 @@ class CookieTests(unittest.TestCase): "http://www.example.com/rheum/rhaponticum;" "foo=bar;sing=song?apples=pears&spam=eggs#ni") self.assertEqual(request_path(req), - "/rheum/rhaponticum;foo=bar;sing=song") + "/rheum/rhaponticum;foo=bar;sing=song") # without parameters req = urllib.request.Request( "http://www.example.com/rheum/rhaponticum?" @@ -889,21 +901,21 @@ class CookieTests(unittest.TestCase): url = "http://foo.bar.com/" interact_2965(c, url, "spam=eggs; Version=1") h = interact_2965(c, url) - self.assertTrue("Domain" not in h, + self.assertNotIn("Domain", h, "absent domain returned with domain present") c = CookieJar(pol) url = "http://foo.bar.com/" interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com') h = interact_2965(c, url) - self.assertTrue('$Domain=".bar.com"' in h, "domain not returned") + self.assertIn('$Domain=".bar.com"', h, "domain not returned") c = CookieJar(pol) url = "http://foo.bar.com/" # note missing initial dot in Domain interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com') h = interact_2965(c, url) - self.assertTrue('$Domain="bar.com"' in h, "domain not returned") + self.assertIn('$Domain="bar.com"', h, "domain not returned") def test_path_mirror(self): pol = DefaultCookiePolicy(rfc2965=True) @@ -912,14 +924,13 @@ class CookieTests(unittest.TestCase): url = "http://foo.bar.com/" interact_2965(c, url, "spam=eggs; Version=1") h = interact_2965(c, url) - self.assertTrue("Path" not in h, - "absent path returned with path present") + self.assertNotIn("Path", h, "absent path returned with path present") c = CookieJar(pol) url = "http://foo.bar.com/" interact_2965(c, url, 'spam=eggs; Version=1; Path=/') h = interact_2965(c, url) - self.assertTrue('$Path="/"' in h, "path not returned") + self.assertIn('$Path="/"', h, "path not returned") def test_port_mirror(self): pol = DefaultCookiePolicy(rfc2965=True) @@ -928,8 +939,7 @@ class CookieTests(unittest.TestCase): url = "http://foo.bar.com/" interact_2965(c, url, "spam=eggs; Version=1") h = interact_2965(c, url) - self.assertTrue("Port" not in h, - "absent port returned with port present") + self.assertNotIn("Port", h, "absent port returned with port present") c = CookieJar(pol) url = "http://foo.bar.com/" @@ -942,16 +952,16 @@ class CookieTests(unittest.TestCase): url = "http://foo.bar.com/" interact_2965(c, url, 'spam=eggs; Version=1; Port="80"') h = interact_2965(c, url) - self.assertTrue('$Port="80"' in h, - "port with single value not returned with single value") + self.assertIn('$Port="80"', h, + "port with single value not returned with single value") c = CookieJar(pol) url = "http://foo.bar.com/" interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"') h = interact_2965(c, url) - self.assertTrue('$Port="80,8080"' in h, - "port with multiple values not returned with multiple " - "values") + self.assertIn('$Port="80,8080"', h, + "port with multiple values not returned with multiple " + "values") def test_no_return_comment(self): c = CookieJar(DefaultCookiePolicy(rfc2965=True)) @@ -1033,6 +1043,8 @@ class CookieTests(unittest.TestCase): ["Set-Cookie2: a=foo; path=/; Version=1; domain"], # bad max-age ["Set-Cookie: b=foo; max-age=oops"], + # bad version + ["Set-Cookie: b=foo; version=spam"], ]: c = cookiejar_from_cookie_headers(headers) # these bad cookies shouldn't be set @@ -1116,8 +1128,8 @@ class LWPCookieTests(unittest.TestCase): c.add_cookie_header(req) h = req.get_header("Cookie") - self.assertTrue("PART_NUMBER=ROCKET_LAUNCHER_0001" in h and - "CUSTOMER=WILE_E_COYOTE" in h) + self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) + self.assertIn("CUSTOMER=WILE_E_COYOTE", h) headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo') res = FakeResponse(headers, "http://www.acme.com") @@ -1127,17 +1139,17 @@ class LWPCookieTests(unittest.TestCase): c.add_cookie_header(req) h = req.get_header("Cookie") - self.assertTrue("PART_NUMBER=ROCKET_LAUNCHER_0001" in h and - "CUSTOMER=WILE_E_COYOTE" in h and - "SHIPPING=FEDEX" not in h) + self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) + self.assertIn("CUSTOMER=WILE_E_COYOTE", h) + self.assertNotIn("SHIPPING=FEDEX", h) req = urllib.request.Request("http://www.acme.com/foo/") c.add_cookie_header(req) h = req.get_header("Cookie") - self.assertTrue(("PART_NUMBER=ROCKET_LAUNCHER_0001" in h and - "CUSTOMER=WILE_E_COYOTE" in h and - h.startswith("SHIPPING=FEDEX;"))) + self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) + self.assertIn("CUSTOMER=WILE_E_COYOTE", h) + self.assertTrue(h.startswith("SHIPPING=FEDEX;")) def test_netscape_example_2(self): # Second Example transaction sequence: @@ -1355,8 +1367,8 @@ class LWPCookieTests(unittest.TestCase): # the server. cookie = interact_2965(c, "http://www.acme.com/acme/parts/") - self.assertTrue("Rocket_Launcher_0001" in cookie and - "Riding_Rocket_0023" not in cookie) + self.assertIn("Rocket_Launcher_0001", cookie) + self.assertNotIn("Riding_Rocket_0023", cookie) def test_rejection(self): # Test rejection of Set-Cookie2 responses based on domain, path, port. @@ -1461,7 +1473,8 @@ class LWPCookieTests(unittest.TestCase): c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345", 'bar=baz; path="/foo/"; version=1'); version_re = re.compile(r'^\$version=\"?1\"?', re.I) - self.assertTrue("foo=bar" in cookie and version_re.search(cookie)) + self.assertIn("foo=bar", cookie) + self.assertTrue(version_re.search(cookie)) cookie = interact_2965( c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345") @@ -1505,11 +1518,11 @@ class LWPCookieTests(unittest.TestCase): new_c = save_and_restore(c, True) self.assertEqual(len(new_c), 6) # none discarded - self.assertTrue("name='foo1', value='bar'" in repr(new_c)) + self.assertIn("name='foo1', value='bar'", repr(new_c)) new_c = save_and_restore(c, False) self.assertEqual(len(new_c), 4) # 2 of them discarded on save - self.assertTrue("name='foo1', value='bar'" in repr(new_c)) + self.assertIn("name='foo1', value='bar'", repr(new_c)) def test_netscape_misc(self): # Some additional Netscape cookies tests. @@ -1531,9 +1544,8 @@ class LWPCookieTests(unittest.TestCase): req = urllib.request.Request("http://foo.bar.acme.com/foo") c.add_cookie_header(req) - self.assertTrue( - "PART_NUMBER=3,4" in req.get_header("Cookie") and - "Customer=WILE_E_COYOTE" in req.get_header("Cookie")) + self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie")) + self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie")) def test_intranet_domains_2965(self): # Test handling of local intranet hostnames without a dot. @@ -1542,11 +1554,12 @@ class LWPCookieTests(unittest.TestCase): "foo1=bar; PORT; Discard; Version=1;") cookie = interact_2965(c, "http://example/", 'foo2=bar; domain=".local"; Version=1') - self.assertTrue("foo1=bar" in cookie) + self.assertIn("foo1=bar", cookie) interact_2965(c, "http://example/", 'foo3=bar; Version=1') cookie = interact_2965(c, "http://example/") - self.assertTrue("foo2=bar" in cookie and len(c) == 3) + self.assertIn("foo2=bar", cookie) + self.assertEqual(len(c), 3) def test_intranet_domains_ns(self): c = CookieJar(DefaultCookiePolicy(rfc2965 = False)) @@ -1554,10 +1567,10 @@ class LWPCookieTests(unittest.TestCase): cookie = interact_netscape(c, "http://example/", 'foo2=bar; domain=.local') self.assertEqual(len(c), 2) - self.assertTrue("foo1=bar" in cookie) + self.assertIn("foo1=bar", cookie) cookie = interact_netscape(c, "http://example/") - self.assertTrue("foo2=bar" in cookie) + self.assertIn("foo2=bar", cookie) self.assertEqual(len(c), 2) def test_empty_path(self): |
