diff -r 1b9d8be8b07e Lib/test/test_http_cookiejar.py --- a/Lib/test/test_http_cookiejar.py Thu Nov 14 23:50:51 2013 +0200 +++ b/Lib/test/test_http_cookiejar.py Fri Nov 15 11:48:45 2013 +0200 @@ -75,12 +75,9 @@ "%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 [ @@ -134,12 +131,9 @@ test_t = 760233600 # assume broken POSIX counting of seconds for s in tests: - t = iso2time(s) - t2 = iso2time(s.lower()) - t3 = iso2time(s.upper()) - - self.assertTrue(t == t2 == t3 == test_t, - "'%s' => %s, %s, %s (%s)" % (s, t, t2, t3, test_t)) + 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 [ @@ -411,7 +405,7 @@ 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 @@ -421,10 +415,10 @@ 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"; ' @@ -466,7 +460,7 @@ 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 @@ -490,26 +484,26 @@ 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 @@ -679,12 +673,12 @@ 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") @@ -698,39 +692,39 @@ 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 @@ -877,7 +871,7 @@ 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( @@ -901,8 +895,8 @@ 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"]) @@ -917,7 +911,7 @@ 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: @@ -935,8 +929,8 @@ 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, @@ -1034,8 +1028,7 @@ '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): @@ -1063,7 +1056,7 @@ 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]) @@ -1118,7 +1111,7 @@ 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): @@ -1297,7 +1290,7 @@ 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 @@ -1370,8 +1363,8 @@ 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) + re.search(r'Shipping="?FedEx"?;\s*\$Path="\/acme"', cookie)) + self.assertIn("WILE_E_COYOTE", cookie) # # The user agent makes a series of requests on the origin server, after @@ -1445,7 +1438,7 @@ # 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", @@ -1542,7 +1535,7 @@ 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") @@ -1703,13 +1696,12 @@ 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):