Commit 76e155a1 authored by Georg Brandl's avatar Georg Brandl

#3788: more tests for http.cookies, now at 95% coverage. Also bring coding...

#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.
parent 7b280e91
This diff is collapsed.
...@@ -19,24 +19,21 @@ class CookieTests(unittest.TestCase): ...@@ -19,24 +19,21 @@ class CookieTests(unittest.TestCase):
def test_basic(self): def test_basic(self):
cases = [ cases = [
{ 'data': 'chips=ahoy; vienna=finger', {'data': 'chips=ahoy; vienna=finger',
'dict': {'chips':'ahoy', 'vienna':'finger'}, 'dict': {'chips':'ahoy', 'vienna':'finger'},
'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>", 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>",
'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger', 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger'},
},
{'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
{ 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'},
'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}, 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''',
'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''', 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"'},
'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
},
# Check illegal cookies that have an '=' char in an unquoted value # Check illegal cookies that have an '=' char in an unquoted value
{ 'data': 'keebler=E=mc2', {'data': 'keebler=E=mc2',
'dict': {'keebler' : 'E=mc2'}, 'dict': {'keebler' : 'E=mc2'},
'repr': "<SimpleCookie: keebler='E=mc2'>", 'repr': "<SimpleCookie: keebler='E=mc2'>",
'output': 'Set-Cookie: keebler=E=mc2', 'output': 'Set-Cookie: keebler=E=mc2'},
}
] ]
for case in cases: for case in cases:
...@@ -72,6 +69,26 @@ class CookieTests(unittest.TestCase): ...@@ -72,6 +69,26 @@ class CookieTests(unittest.TestCase):
</script> </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): def test_quoted_meta(self):
# Try cookie with quoted meta-data # Try cookie with quoted meta-data
C = cookies.SimpleCookie() C = cookies.SimpleCookie()
...@@ -80,8 +97,72 @@ class CookieTests(unittest.TestCase): ...@@ -80,8 +97,72 @@ class CookieTests(unittest.TestCase):
self.assertEqual(C['Customer']['version'], '1') self.assertEqual(C['Customer']['version'], '1')
self.assertEqual(C['Customer']['path'], '/acme') 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(): def test_main():
run_unittest(CookieTests) run_unittest(CookieTests, MorselTests)
run_doctest(cookies) run_doctest(cookies)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment