1 # Simple test suite for Cookie.py
3 from test
.test_support
import run_unittest
, run_doctest
, check_warnings
8 class CookieTests(unittest
.TestCase
):
9 # Currently this only tests SimpleCookie
12 { 'data': 'chips=ahoy; vienna=finger',
13 'dict': {'chips':'ahoy', 'vienna':'finger'},
14 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>",
15 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger',
18 { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
19 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'},
20 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''',
21 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
24 # Check illegal cookies that have an '=' char in an unquoted value
25 { 'data': 'keebler=E=mc2',
26 'dict': {'keebler' : 'E=mc2'},
27 'repr': "<SimpleCookie: keebler='E=mc2'>",
28 'output': 'Set-Cookie: keebler=E=mc2',
33 C
= Cookie
.SimpleCookie()
35 self
.assertEqual(repr(C
), case
['repr'])
36 self
.assertEqual(C
.output(sep
='\n'), case
['output'])
37 for k
, v
in sorted(case
['dict'].iteritems()):
38 self
.assertEqual(C
[k
].value
, v
)
41 C
= Cookie
.SimpleCookie()
42 C
.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
44 self
.assertEqual(C
['Customer'].value
, 'WILE_E_COYOTE')
45 self
.assertEqual(C
['Customer']['version'], '1')
46 self
.assertEqual(C
['Customer']['path'], '/acme')
48 self
.assertEqual(C
.output(['path']),
49 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
50 self
.assertEqual(C
.js_output(), r
"""
51 <script type="text/javascript">
53 document.cookie = "Customer=\"WILE_E_COYOTE
\"; Path
=/acme
; Version
=1";
57 self.assertEqual(C.js_output(['path']), r"""
58 <script type="text
/javascript
">
60 document.cookie = "Customer
=\"WILE_E_COYOTE
\"; Path
=/acme
";
65 def test_quoted_meta(self):
66 # Try cookie with quoted meta-data
67 C = Cookie.SimpleCookie()
68 C.load('Customer="WILE_E_COYOTE
"; Version="1"; Path="/acme
"')
69 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
70 self.assertEqual(C['Customer']['version'], '1')
71 self.assertEqual(C['Customer']['path'], '/acme')
74 run_unittest(CookieTests)
75 with check_warnings(('.+Cookie class is insecure; do not use it',
79 if __name__ == '__main__':