1 | # Simple test suite for Cookie.py
|
---|
2 |
|
---|
3 | from test.test_support import run_unittest, run_doctest, check_warnings
|
---|
4 | import unittest
|
---|
5 | import Cookie
|
---|
6 |
|
---|
7 |
|
---|
8 | class CookieTests(unittest.TestCase):
|
---|
9 | # Currently this only tests SimpleCookie
|
---|
10 | def test_basic(self):
|
---|
11 | cases = [
|
---|
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',
|
---|
16 | },
|
---|
17 |
|
---|
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;"',
|
---|
22 | },
|
---|
23 |
|
---|
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',
|
---|
29 | }
|
---|
30 | ]
|
---|
31 |
|
---|
32 | for case in cases:
|
---|
33 | C = Cookie.SimpleCookie()
|
---|
34 | C.load(case['data'])
|
---|
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)
|
---|
39 |
|
---|
40 | def test_load(self):
|
---|
41 | C = Cookie.SimpleCookie()
|
---|
42 | C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
|
---|
43 |
|
---|
44 | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
|
---|
45 | self.assertEqual(C['Customer']['version'], '1')
|
---|
46 | self.assertEqual(C['Customer']['path'], '/acme')
|
---|
47 |
|
---|
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">
|
---|
52 | <!-- begin hiding
|
---|
53 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
|
---|
54 | // end hiding -->
|
---|
55 | </script>
|
---|
56 | """)
|
---|
57 | self.assertEqual(C.js_output(['path']), r"""
|
---|
58 | <script type="text/javascript">
|
---|
59 | <!-- begin hiding
|
---|
60 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
|
---|
61 | // end hiding -->
|
---|
62 | </script>
|
---|
63 | """)
|
---|
64 |
|
---|
65 | # loading 'expires'
|
---|
66 | C = Cookie.SimpleCookie()
|
---|
67 | C.load('Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMT')
|
---|
68 | self.assertEqual(C['Customer']['expires'],
|
---|
69 | 'Wed, 01 Jan 2010 00:00:00 GMT')
|
---|
70 | C = Cookie.SimpleCookie()
|
---|
71 | C.load('Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMT')
|
---|
72 | self.assertEqual(C['Customer']['expires'],
|
---|
73 | 'Wed, 01 Jan 98 00:00:00 GMT')
|
---|
74 |
|
---|
75 | def test_extended_encode(self):
|
---|
76 | # Issue 9824: some browsers don't follow the standard; we now
|
---|
77 | # encode , and ; to keep them from tripping up.
|
---|
78 | C = Cookie.SimpleCookie()
|
---|
79 | C['val'] = "some,funky;stuff"
|
---|
80 | self.assertEqual(C.output(['val']),
|
---|
81 | 'Set-Cookie: val="some\\054funky\\073stuff"')
|
---|
82 |
|
---|
83 | def test_quoted_meta(self):
|
---|
84 | # Try cookie with quoted meta-data
|
---|
85 | C = Cookie.SimpleCookie()
|
---|
86 | C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
|
---|
87 | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
|
---|
88 | self.assertEqual(C['Customer']['version'], '1')
|
---|
89 | self.assertEqual(C['Customer']['path'], '/acme')
|
---|
90 |
|
---|
91 | def test_main():
|
---|
92 | run_unittest(CookieTests)
|
---|
93 | if Cookie.__doc__ is not None:
|
---|
94 | with check_warnings(('.+Cookie class is insecure; do not use it',
|
---|
95 | DeprecationWarning)):
|
---|
96 | run_doctest(Cookie)
|
---|
97 |
|
---|
98 | if __name__ == '__main__':
|
---|
99 | test_main()
|
---|