summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_http_cookies.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-07-31 21:04:00 (GMT)
committerGeorg Brandl <georg@python.org>2010-07-31 21:04:00 (GMT)
commit76e155a157e432f85cdd11448cf4904f827cbdbb (patch)
tree4a5e28ed604c25fb927dfd1d645d9b24d32ead14 /Lib/test/test_http_cookies.py
parent7b280e9197fcded4661b90ca064f1a83f2d80608 (diff)
downloadcpython-76e155a157e432f85cdd11448cf4904f827cbdbb.zip
cpython-76e155a157e432f85cdd11448cf4904f827cbdbb.tar.gz
cpython-76e155a157e432f85cdd11448cf4904f827cbdbb.tar.bz2
#3788: more tests for http.cookies, now at 95% coverage. Also bring coding style in the module up to PEP 8, where it does not break backwards compatibility.
Diffstat (limited to 'Lib/test/test_http_cookies.py')
-rw-r--r--Lib/test/test_http_cookies.py115
1 files changed, 98 insertions, 17 deletions
diff --git a/Lib/test/test_http_cookies.py b/Lib/test/test_http_cookies.py
index 4d9c9ca..43ca9f5 100644
--- a/Lib/test/test_http_cookies.py
+++ b/Lib/test/test_http_cookies.py
@@ -19,24 +19,21 @@ class CookieTests(unittest.TestCase):
def test_basic(self):
cases = [
- { 'data': 'chips=ahoy; vienna=finger',
- 'dict': {'chips':'ahoy', 'vienna':'finger'},
- 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>",
- 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger',
- },
-
- { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
- 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'},
- 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''',
- 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
- },
+ {'data': 'chips=ahoy; vienna=finger',
+ 'dict': {'chips':'ahoy', 'vienna':'finger'},
+ 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>",
+ 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger'},
+
+ {'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
+ 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'},
+ 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''',
+ 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"'},
# Check illegal cookies that have an '=' char in an unquoted value
- { 'data': 'keebler=E=mc2',
- 'dict': {'keebler' : 'E=mc2'},
- 'repr': "<SimpleCookie: keebler='E=mc2'>",
- 'output': 'Set-Cookie: keebler=E=mc2',
- }
+ {'data': 'keebler=E=mc2',
+ 'dict': {'keebler' : 'E=mc2'},
+ 'repr': "<SimpleCookie: keebler='E=mc2'>",
+ 'output': 'Set-Cookie: keebler=E=mc2'},
]
for case in cases:
@@ -72,6 +69,26 @@ class CookieTests(unittest.TestCase):
</script>
""")
+ def test_special_attrs(self):
+ # 'expires'
+ C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
+ C['Customer']['expires'] = 0
+ # can't test exact output, it always depends on current date/time
+ self.assertTrue(C.output().endswith('GMT'))
+
+ # 'max-age'
+ C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
+ C['Customer']['max-age'] = 10
+ self.assertEqual(C.output(),
+ 'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10')
+
+ # others
+ C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
+ C['Customer']['secure'] = True
+ C['Customer']['httponly'] = True
+ self.assertEqual(C.output(),
+ 'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
+
def test_quoted_meta(self):
# Try cookie with quoted meta-data
C = cookies.SimpleCookie()
@@ -80,8 +97,72 @@ class CookieTests(unittest.TestCase):
self.assertEqual(C['Customer']['version'], '1')
self.assertEqual(C['Customer']['path'], '/acme')
+ self.assertEqual(C.output(['path']),
+ 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
+ self.assertEqual(C.js_output(), r"""
+ <script type="text/javascript">
+ <!-- begin hiding
+ document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
+ // end hiding -->
+ </script>
+ """)
+ self.assertEqual(C.js_output(['path']), r"""
+ <script type="text/javascript">
+ <!-- begin hiding
+ document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
+ // end hiding -->
+ </script>
+ """)
+
+class MorselTests(unittest.TestCase):
+ """Tests for the Morsel object."""
+
+ def test_reserved_keys(self):
+ M = cookies.Morsel()
+ # tests valid and invalid reserved keys for Morsels
+ for i in M._reserved:
+ # Test that all valid keys are reported as reserved and set them
+ self.assertTrue(M.isReservedKey(i))
+ M[i] = '%s_value' % i
+ for i in M._reserved:
+ # Test that valid key values come out fine
+ self.assertEqual(M[i], '%s_value' % i)
+ for i in "the holy hand grenade".split():
+ # Test that invalid keys raise CookieError
+ self.assertRaises(cookies.CookieError,
+ M.__setitem__, i, '%s_value' % i)
+
+ def test_setter(self):
+ M = cookies.Morsel()
+ # tests the .set method to set keys and their values
+ for i in M._reserved:
+ # Makes sure that all reserved keys can't be set this way
+ self.assertRaises(cookies.CookieError,
+ M.set, i, '%s_value' % i, '%s_value' % i)
+ for i in "thou cast _the- !holy! ^hand| +*grenade~".split():
+ # Try typical use case. Setting decent values.
+ # Check output and js_output.
+ M['path'] = '/foo' # Try a reserved key as well
+ M.set(i, "%s_val" % i, "%s_coded_val" % i)
+ self.assertEqual(
+ M.output(),
+ "Set-Cookie: %s=%s; Path=/foo" % (i, "%s_coded_val" % i))
+ expected_js_output = """
+ <script type="text/javascript">
+ <!-- begin hiding
+ document.cookie = "%s=%s; Path=/foo";
+ // end hiding -->
+ </script>
+ """ % (i, "%s_coded_val" % i)
+ self.assertEqual(M.js_output(), expected_js_output)
+ for i in ["foo bar", "foo@bar"]:
+ # Try some illegal characters
+ self.assertRaises(cookies.CookieError,
+ M.set, i, '%s_value' % i, '%s_value' % i)
+
+
def test_main():
- run_unittest(CookieTests)
+ run_unittest(CookieTests, MorselTests)
run_doctest(cookies)
if __name__ == '__main__':