summaryrefslogtreecommitdiff
path: root/Lib/test/test_http_cookiejar.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_http_cookiejar.py')
-rw-r--r--Lib/test/test_http_cookiejar.py231
1 files changed, 142 insertions, 89 deletions
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
index 41e0dfde33..fb66f6f2df 100644
--- a/Lib/test/test_http_cookiejar.py
+++ b/Lib/test/test_http_cookiejar.py
@@ -7,7 +7,7 @@ import time
import unittest
import urllib.request
-from http.cookiejar import (time2isoz, http2time, time2netscape,
+from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
parse_ns_headers, join_header_words, split_header_words, Cookie,
CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar,
LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path,
@@ -28,8 +28,8 @@ class DateTimeTests(unittest.TestCase):
az = time2isoz()
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))
+ self.assertRegex(text, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$",
+ "bad time2isoz format: %s %s" % (az, bz))
def test_http2time(self):
def parse_date(text):
@@ -56,6 +56,8 @@ class DateTimeTests(unittest.TestCase):
'03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday)
'03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds)
'03-Feb-1994 00:00', # broken rfc850 (no weekday, no seconds, no tz)
+ '02-Feb-1994 24:00', # broken rfc850 (no weekday, no seconds,
+ # no tz) using hour 24 with yesterday date
'03-Feb-94', # old rfc850 HTTP format (no weekday, no time)
'03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)
@@ -73,12 +75,9 @@ class DateTimeTests(unittest.TestCase):
"%s => '%s' (%s)" % (test_t, result, expected))
for s in tests:
- t = http2time(s)
- t2 = http2time(s.lower())
- t3 = http2time(s.upper())
-
- self.assertTrue(t == t2 == t3 == test_t,
- "'%s' => %s, %s, %s (%s)" % (s, t, t2, t3, test_t))
+ self.assertEqual(http2time(s), test_t, s)
+ self.assertEqual(http2time(s.lower()), test_t, s.lower())
+ self.assertEqual(http2time(s.upper()), test_t, s.upper())
def test_http2time_garbage(self):
for test in [
@@ -93,10 +92,68 @@ class DateTimeTests(unittest.TestCase):
'01-01-1980 00:61:00',
'01-01-1980 00:00:62',
]:
- self.assertTrue(http2time(test) is None,
- "http2time(%s) is not None\n"
- "http2time(test) %s" % (test, http2time(test))
- )
+ self.assertIsNone(http2time(test),
+ "http2time(%s) is not None\n"
+ "http2time(test) %s" % (test, http2time(test)))
+
+ def test_iso2time(self):
+ def parse_date(text):
+ return time.gmtime(iso2time(text))[:6]
+
+ # ISO 8601 compact format
+ self.assertEqual(parse_date("19940203T141529Z"),
+ (1994, 2, 3, 14, 15, 29))
+
+ # ISO 8601 with time behind UTC
+ self.assertEqual(parse_date("1994-02-03 07:15:29 -0700"),
+ (1994, 2, 3, 14, 15, 29))
+
+ # ISO 8601 with time ahead of UTC
+ self.assertEqual(parse_date("1994-02-03 19:45:29 +0530"),
+ (1994, 2, 3, 14, 15, 29))
+
+ def test_iso2time_formats(self):
+ # test iso2time for supported dates.
+ tests = [
+ '1994-02-03 00:00:00 -0000', # ISO 8601 format
+ '1994-02-03 00:00:00 +0000', # ISO 8601 format
+ '1994-02-03 00:00:00', # zone is optional
+ '1994-02-03', # only date
+ '1994-02-03T00:00:00', # Use T as separator
+ '19940203', # only date
+ '1994-02-02 24:00:00', # using hour-24 yesterday date
+ '19940203T000000Z', # ISO 8601 compact format
+
+ # A few tests with extra space at various places
+ ' 1994-02-03 ',
+ ' 1994-02-03T00:00:00 ',
+ ]
+
+ test_t = 760233600 # assume broken POSIX counting of seconds
+ for s in tests:
+ self.assertEqual(iso2time(s), test_t, s)
+ self.assertEqual(iso2time(s.lower()), test_t, s.lower())
+ self.assertEqual(iso2time(s.upper()), test_t, s.upper())
+
+ def test_iso2time_garbage(self):
+ for test in [
+ '',
+ 'Garbage',
+ 'Thursday, 03-Feb-94 00:00:00 GMT',
+ '1980-00-01',
+ '1980-13-01',
+ '1980-01-00',
+ '1980-01-32',
+ '1980-01-01 25:00:00',
+ '1980-01-01 00:61:00',
+ '01-01-1980 00:00:62',
+ '01-01-1980T00:00:62',
+ '19800101T250000Z'
+ '1980-01-01 00:00:00 -2500',
+ ]:
+ self.assertIsNone(iso2time(test),
+ "iso2time(%s) is not None\n"
+ "iso2time(test) %s" % (test, iso2time(test)))
class HeaderTests(unittest.TestCase):
@@ -248,18 +305,19 @@ class FileCookieJarTests(unittest.TestCase):
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
def test_bad_magic(self):
- # IOErrors (eg. file doesn't exist) are allowed to propagate
+ # OSErrors (eg. file doesn't exist) are allowed to propagate
filename = test.support.TESTFN
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class()
try:
c.load(filename="for this test to work, a file with this "
"filename should not exist")
- except IOError as exc:
- # exactly IOError, not LoadError
- self.assertIs(exc.__class__, IOError)
+ except OSError as exc:
+ # an OSError subclass (likely FileNotFoundError), but not
+ # LoadError
+ self.assertIsNot(exc.__class__, LoadError)
else:
- self.fail("expected IOError for invalid filename")
+ self.fail("expected OSError for invalid filename")
# Invalid contents of cookies file (eg. bad magic string)
# causes a LoadError.
try:
@@ -310,7 +368,7 @@ class CookieTests(unittest.TestCase):
## commas and equals are commonly appear in the cookie value). This also
## means that if you fold multiple Set-Cookie header fields into one,
## comma-separated list, it'll be a headache to parse (at least my head
-## starts hurting everytime I think of that code).
+## starts hurting every time I think of that code).
## - Expires: You'll get all sorts of date formats in the expires,
## including emtpy expires attributes ("expires="). Be as flexible as you
## can, and certainly don't expect the weekday to be there; if you can't
@@ -347,7 +405,7 @@ class CookieTests(unittest.TestCase):
request = urllib.request.Request(url)
r = pol.domain_return_ok(domain, request)
if ok: self.assertTrue(r)
- else: self.assertTrue(not r)
+ else: self.assertFalse(r)
def test_missing_value(self):
# missing = sign in Cookie: header is regarded by Mozilla as a missing
@@ -357,10 +415,10 @@ class CookieTests(unittest.TestCase):
interact_netscape(c, "http://www.acme.com/", 'eggs')
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
cookie = c._cookies["www.acme.com"]["/"]["eggs"]
- self.assertTrue(cookie.value is None)
+ self.assertIsNone(cookie.value)
self.assertEqual(cookie.name, "eggs")
cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
- self.assertTrue(cookie.value is None)
+ self.assertIsNone(cookie.value)
self.assertEqual(cookie.name, '"spam"')
self.assertEqual(lwp_cookie_str(cookie), (
r'"spam"; path="/foo/"; domain="www.acme.com"; '
@@ -402,7 +460,7 @@ class CookieTests(unittest.TestCase):
try:
cookie = c._cookies["www.example.com"]["/"]["ni"]
except KeyError:
- self.assertTrue(version is None) # didn't expect a stored cookie
+ self.assertIsNone(version) # didn't expect a stored cookie
else:
self.assertEqual(cookie.version, version)
# 2965 cookies are unaffected
@@ -426,26 +484,26 @@ class CookieTests(unittest.TestCase):
self.assertEqual(cookie.domain, ".acme.com")
self.assertTrue(cookie.domain_specified)
self.assertEqual(cookie.port, DEFAULT_HTTP_PORT)
- self.assertTrue(not cookie.port_specified)
+ self.assertFalse(cookie.port_specified)
# case is preserved
- self.assertTrue(cookie.has_nonstandard_attr("blArgh") and
- not cookie.has_nonstandard_attr("blargh"))
+ self.assertTrue(cookie.has_nonstandard_attr("blArgh"))
+ self.assertFalse(cookie.has_nonstandard_attr("blargh"))
cookie = c._cookies["www.acme.com"]["/"]["ni"]
self.assertEqual(cookie.domain, "www.acme.com")
- self.assertTrue(not cookie.domain_specified)
+ self.assertFalse(cookie.domain_specified)
self.assertEqual(cookie.port, "80,8080")
self.assertTrue(cookie.port_specified)
cookie = c._cookies["www.acme.com"]["/"]["nini"]
- self.assertTrue(cookie.port is None)
- self.assertTrue(not cookie.port_specified)
+ self.assertIsNone(cookie.port)
+ self.assertFalse(cookie.port_specified)
# invalid expires should not cause cookie to be dropped
foo = c._cookies["www.acme.com"]["/"]["foo"]
spam = c._cookies["www.acme.com"]["/"]["foo"]
- self.assertTrue(foo.expires is None)
- self.assertTrue(spam.expires is None)
+ self.assertIsNone(foo.expires)
+ self.assertIsNone(spam.expires)
def test_ns_parser_special_names(self):
# names such as 'expires' are not special in first name=value pair
@@ -615,12 +673,12 @@ class CookieTests(unittest.TestCase):
def test_is_HDN(self):
self.assertTrue(is_HDN("foo.bar.com"))
self.assertTrue(is_HDN("1foo2.3bar4.5com"))
- self.assertTrue(not is_HDN("192.168.1.1"))
- self.assertTrue(not is_HDN(""))
- self.assertTrue(not is_HDN("."))
- self.assertTrue(not is_HDN(".foo.bar.com"))
- self.assertTrue(not is_HDN("..foo"))
- self.assertTrue(not is_HDN("foo."))
+ self.assertFalse(is_HDN("192.168.1.1"))
+ self.assertFalse(is_HDN(""))
+ self.assertFalse(is_HDN("."))
+ self.assertFalse(is_HDN(".foo.bar.com"))
+ self.assertFalse(is_HDN("..foo"))
+ self.assertFalse(is_HDN("foo."))
def test_reach(self):
self.assertEqual(reach("www.acme.com"), ".acme.com")
@@ -634,39 +692,39 @@ class CookieTests(unittest.TestCase):
def test_domain_match(self):
self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))
- self.assertTrue(not domain_match("192.168.1.1", ".168.1.1"))
+ self.assertFalse(domain_match("192.168.1.1", ".168.1.1"))
self.assertTrue(domain_match("x.y.com", "x.Y.com"))
self.assertTrue(domain_match("x.y.com", ".Y.com"))
- self.assertTrue(not domain_match("x.y.com", "Y.com"))
+ self.assertFalse(domain_match("x.y.com", "Y.com"))
self.assertTrue(domain_match("a.b.c.com", ".c.com"))
- self.assertTrue(not domain_match(".c.com", "a.b.c.com"))
+ self.assertFalse(domain_match(".c.com", "a.b.c.com"))
self.assertTrue(domain_match("example.local", ".local"))
- self.assertTrue(not domain_match("blah.blah", ""))
- self.assertTrue(not domain_match("", ".rhubarb.rhubarb"))
+ self.assertFalse(domain_match("blah.blah", ""))
+ self.assertFalse(domain_match("", ".rhubarb.rhubarb"))
self.assertTrue(domain_match("", ""))
self.assertTrue(user_domain_match("acme.com", "acme.com"))
- self.assertTrue(not user_domain_match("acme.com", ".acme.com"))
+ self.assertFalse(user_domain_match("acme.com", ".acme.com"))
self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))
self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))
self.assertTrue(user_domain_match("x.y.com", ".Y.com"))
- self.assertTrue(not user_domain_match("x.y.com", "Y.com"))
+ self.assertFalse(user_domain_match("x.y.com", "Y.com"))
self.assertTrue(user_domain_match("y.com", "Y.com"))
- self.assertTrue(not user_domain_match(".y.com", "Y.com"))
+ self.assertFalse(user_domain_match(".y.com", "Y.com"))
self.assertTrue(user_domain_match(".y.com", ".Y.com"))
self.assertTrue(user_domain_match("x.y.com", ".com"))
- self.assertTrue(not user_domain_match("x.y.com", "com"))
- self.assertTrue(not user_domain_match("x.y.com", "m"))
- self.assertTrue(not user_domain_match("x.y.com", ".m"))
- self.assertTrue(not user_domain_match("x.y.com", ""))
- self.assertTrue(not user_domain_match("x.y.com", "."))
+ self.assertFalse(user_domain_match("x.y.com", "com"))
+ self.assertFalse(user_domain_match("x.y.com", "m"))
+ self.assertFalse(user_domain_match("x.y.com", ".m"))
+ self.assertFalse(user_domain_match("x.y.com", ""))
+ self.assertFalse(user_domain_match("x.y.com", "."))
self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))
# not both HDNs, so must string-compare equal to match
- self.assertTrue(not user_domain_match("192.168.1.1", ".168.1.1"))
- self.assertTrue(not user_domain_match("192.168.1.1", "."))
+ self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1"))
+ self.assertFalse(user_domain_match("192.168.1.1", "."))
# empty string is a special case
- self.assertTrue(not user_domain_match("192.168.1.1", ""))
+ self.assertFalse(user_domain_match("192.168.1.1", ""))
def test_wrong_domain(self):
# Cookies whose effective request-host name does not domain-match the
@@ -813,7 +871,7 @@ class CookieTests(unittest.TestCase):
self.assertEqual(len(c), 2)
# ... and check is doesn't get returned
c.add_cookie_header(req)
- self.assertTrue(not req.has_header("Cookie"))
+ self.assertFalse(req.has_header("Cookie"))
def test_domain_block(self):
pol = DefaultCookiePolicy(
@@ -837,8 +895,8 @@ class CookieTests(unittest.TestCase):
self.assertEqual(len(c), 1)
req = urllib.request.Request("http://www.roadrunner.net/")
c.add_cookie_header(req)
- self.assertTrue((req.has_header("Cookie") and
- req.has_header("Cookie2")))
+ self.assertTrue(req.has_header("Cookie"))
+ self.assertTrue(req.has_header("Cookie2"))
c.clear()
pol.set_blocked_domains([".acme.com"])
@@ -853,7 +911,7 @@ class CookieTests(unittest.TestCase):
self.assertEqual(len(c), 2)
# ... and check is doesn't get returned
c.add_cookie_header(req)
- self.assertTrue(not req.has_header("Cookie"))
+ self.assertFalse(req.has_header("Cookie"))
def test_secure(self):
for ns in True, False:
@@ -871,8 +929,8 @@ class CookieTests(unittest.TestCase):
url = "http://www.acme.com/"
int(c, url, "foo1=bar%s%s" % (vs, whitespace))
int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
- self.assertTrue(
- not c._cookies["www.acme.com"]["/"]["foo1"].secure,
+ self.assertFalse(
+ c._cookies["www.acme.com"]["/"]["foo1"].secure,
"non-secure cookie registered secure")
self.assertTrue(
c._cookies["www.acme.com"]["/"]["foo2"].secure,
@@ -945,8 +1003,8 @@ class CookieTests(unittest.TestCase):
url = "http://foo.bar.com/"
interact_2965(c, url, "spam=eggs; Version=1; Port")
h = interact_2965(c, url)
- self.assertTrue(re.search("\$Port([^=]|$)", h),
- "port with no value not returned with no value")
+ self.assertRegex(h, "\$Port([^=]|$)",
+ "port with no value not returned with no value")
c = CookieJar(pol)
url = "http://foo.bar.com/"
@@ -970,8 +1028,7 @@ class CookieTests(unittest.TestCase):
'Comment="does anybody read these?"; '
'CommentURL="http://foo.bar.net/comment.html"')
h = interact_2965(c, url)
- self.assertTrue(
- "Comment" not in h,
+ self.assertNotIn("Comment", h,
"Comment or CommentURL cookie-attributes returned to server")
def test_Cookie_iterator(self):
@@ -999,7 +1056,7 @@ class CookieTests(unittest.TestCase):
for i in range(4):
i = 0
for c in cs:
- self.assertTrue(isinstance(c, Cookie))
+ self.assertIsInstance(c, Cookie)
self.assertEqual(c.version, versions[i])
self.assertEqual(c.name, names[i])
self.assertEqual(c.domain, domains[i])
@@ -1054,7 +1111,7 @@ class CookieTests(unittest.TestCase):
headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
c = cookiejar_from_cookie_headers(headers)
cookie = c._cookies["www.example.com"]["/"]["c"]
- self.assertTrue(cookie.expires is None)
+ self.assertIsNone(cookie.expires)
class LWPCookieTests(unittest.TestCase):
@@ -1198,9 +1255,9 @@ class LWPCookieTests(unittest.TestCase):
req = urllib.request.Request("http://www.acme.com/ammo")
c.add_cookie_header(req)
- self.assertTrue(re.search(r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
- "PART_NUMBER=ROCKET_LAUNCHER_0001",
- req.get_header("Cookie")))
+ self.assertRegex(req.get_header("Cookie"),
+ r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
+ "PART_NUMBER=ROCKET_LAUNCHER_0001")
def test_ietf_example_1(self):
#-------------------------------------------------------------------
@@ -1233,7 +1290,7 @@ class LWPCookieTests(unittest.TestCase):
cookie = interact_2965(
c, 'http://www.acme.com/acme/login',
'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
- self.assertTrue(not cookie)
+ self.assertFalse(cookie)
#
# 3. User Agent -> Server
@@ -1255,9 +1312,8 @@ class LWPCookieTests(unittest.TestCase):
cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
'Part_Number="Rocket_Launcher_0001"; '
'Version="1"; Path="/acme"');
- self.assertTrue(re.search(
- r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$',
- cookie))
+ self.assertRegex(cookie,
+ r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$')
#
# 5. User Agent -> Server
@@ -1280,11 +1336,11 @@ class LWPCookieTests(unittest.TestCase):
cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
'Shipping="FedEx"; Version="1"; Path="/acme"')
- self.assertTrue(re.search(r'^\$Version="?1"?;', cookie))
- self.assertTrue(re.search(r'Part_Number="?Rocket_Launcher_0001"?;'
- '\s*\$Path="\/acme"', cookie))
- self.assertTrue(re.search(r'Customer="?WILE_E_COYOTE"?;\s*\$Path="\/acme"',
- cookie))
+ self.assertRegex(cookie, r'^\$Version="?1"?;')
+ self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;'
+ '\s*\$Path="\/acme"')
+ self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;'
+ '\s*\$Path="\/acme"')
#
# 7. User Agent -> Server
@@ -1305,9 +1361,8 @@ class LWPCookieTests(unittest.TestCase):
# Transaction is complete.
cookie = interact_2965(c, "http://www.acme.com/acme/process")
- self.assertTrue(
- re.search(r'Shipping="?FedEx"?;\s*\$Path="\/acme"', cookie) and
- "WILE_E_COYOTE" in cookie)
+ self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"')
+ self.assertIn("WILE_E_COYOTE", cookie)
#
# The user agent makes a series of requests on the origin server, after
@@ -1354,8 +1409,7 @@ class LWPCookieTests(unittest.TestCase):
# than once.
cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
- self.assertTrue(
- re.search(r"Riding_Rocket_0023.*Rocket_Launcher_0001", cookie))
+ self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001")
# A subsequent request by the user agent to the (same) server for a URL of
# the form /acme/parts/ would include the following request header:
@@ -1381,7 +1435,7 @@ class LWPCookieTests(unittest.TestCase):
# illegal domain (no embedded dots)
cookie = interact_2965(c, "http://www.acme.com",
'foo=bar; domain=".com"; version=1')
- self.assertTrue(not c)
+ self.assertFalse(c)
# legal domain
cookie = interact_2965(c, "http://www.acme.com",
@@ -1474,11 +1528,11 @@ class LWPCookieTests(unittest.TestCase):
'bar=baz; path="/foo/"; version=1');
version_re = re.compile(r'^\$version=\"?1\"?', re.I)
self.assertIn("foo=bar", cookie)
- self.assertTrue(version_re.search(cookie))
+ self.assertRegex(cookie, version_re)
cookie = interact_2965(
c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")
- self.assertTrue(not cookie)
+ self.assertFalse(cookie)
# unicode URL doesn't raise exception
cookie = interact_2965(c, "http://www.acme.com/\xfc")
@@ -1639,13 +1693,12 @@ class LWPCookieTests(unittest.TestCase):
key = "%s_after" % cookie.value
counter[key] = counter[key] + 1
- self.assertTrue(not (
# a permanent cookie got lost accidently
- counter["perm_after"] != counter["perm_before"] or
+ self.assertEqual(counter["perm_after"], counter["perm_before"])
# a session cookie hasn't been cleared
- counter["session_after"] != 0 or
+ self.assertEqual(counter["session_after"], 0)
# we didn't have session cookies in the first place
- counter["session_before"] == 0))
+ self.assertNotEqual(counter["session_before"], 0)
def test_main(verbose=None):