PageRenderTime 436ms CodeModel.GetById 222ms app.highlight 8ms RepoModel.GetById 204ms app.codeStats 0ms

/Lib/test/test_cookie.py

http://unladen-swallow.googlecode.com/
Python | 82 lines | 76 code | 4 blank | 2 comment | 0 complexity | 9600d074e1e865bcfaf637e24a1faa5a MD5 | raw file
 1# Simple test suite for Cookie.py
 2
 3from test.test_support import run_unittest, run_doctest
 4import unittest
 5import Cookie
 6
 7import warnings
 8warnings.filterwarnings("ignore",
 9                        ".* class is insecure.*",
10                        DeprecationWarning)
11
12class CookieTests(unittest.TestCase):
13    # Currently this only tests SimpleCookie
14    def test_basic(self):
15        cases = [
16            { 'data': 'chips=ahoy; vienna=finger',
17              'dict': {'chips':'ahoy', 'vienna':'finger'},
18              'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>",
19              'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger',
20            },
21
22            { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
23              'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'},
24              'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''',
25              'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
26            },
27
28            # Check illegal cookies that have an '=' char in an unquoted value
29            { 'data': 'keebler=E=mc2',
30              'dict': {'keebler' : 'E=mc2'},
31              'repr': "<SimpleCookie: keebler='E=mc2'>",
32              'output': 'Set-Cookie: keebler=E=mc2',
33            }
34        ]
35
36        for case in cases:
37            C = Cookie.SimpleCookie()
38            C.load(case['data'])
39            self.assertEqual(repr(C), case['repr'])
40            self.assertEqual(C.output(sep='\n'), case['output'])
41            for k, v in sorted(case['dict'].iteritems()):
42                self.assertEqual(C[k].value, v)
43
44    def test_load(self):
45        C = Cookie.SimpleCookie()
46        C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
47
48        self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
49        self.assertEqual(C['Customer']['version'], '1')
50        self.assertEqual(C['Customer']['path'], '/acme')
51
52        self.assertEqual(C.output(['path']),
53            'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
54        self.assertEqual(C.js_output(), """
55        <script type="text/javascript">
56        <!-- begin hiding
57        document.cookie = "Customer="WILE_E_COYOTE"; Path=/acme; Version=1";
58        // end hiding -->
59        </script>
60        """)
61        self.assertEqual(C.js_output(['path']), """
62        <script type="text/javascript">
63        <!-- begin hiding
64        document.cookie = "Customer="WILE_E_COYOTE"; Path=/acme";
65        // end hiding -->
66        </script>
67        """)
68
69    def test_quoted_meta(self):
70        # Try cookie with quoted meta-data
71        C = Cookie.SimpleCookie()
72        C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
73        self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
74        self.assertEqual(C['Customer']['version'], '1')
75        self.assertEqual(C['Customer']['path'], '/acme')
76
77def test_main():
78    run_unittest(CookieTests)
79    run_doctest(Cookie)
80
81if __name__ == '__main__':
82    test_main()