blob: 41ba60f9d2a4545a6b4e5ada825b9ba0b3bd678e [file] [log] [blame]
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +00001# Simple test suite for Cookie.py
2
Florent Xicluna6257a7b2010-03-31 22:01:03 +00003from test.test_support import run_unittest, run_doctest, check_warnings
Georg Brandla962eb32006-10-29 19:51:16 +00004import unittest
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +00005import Cookie
6
Andrew M. Kuchling44644322002-12-29 16:45:06 +00007
Georg Brandla962eb32006-10-29 19:51:16 +00008class 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 },
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000017
Georg Brandla962eb32006-10-29 19:51:16 +000018 { '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 },
Andrew M. Kuchling8b94b1c2001-02-21 01:17:54 +000023
Georg Brandla962eb32006-10-29 19:51:16 +000024 # 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 ]
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000031
Georg Brandla962eb32006-10-29 19:51:16 +000032 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)
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000039
Georg Brandla962eb32006-10-29 19:51:16 +000040 def test_load(self):
41 C = Cookie.SimpleCookie()
42 C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000043
Georg Brandla962eb32006-10-29 19:51:16 +000044 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
45 self.assertEqual(C['Customer']['version'], '1')
46 self.assertEqual(C['Customer']['path'], '/acme')
Moshe Zadka514a1022000-08-19 15:57:33 +000047
Georg Brandla962eb32006-10-29 19:51:16 +000048 self.assertEqual(C.output(['path']),
49 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000050 self.assertEqual(C.js_output(), r"""
Georg Brandla962eb32006-10-29 19:51:16 +000051 <script type="text/javascript">
52 <!-- begin hiding
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000053 document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
Georg Brandla962eb32006-10-29 19:51:16 +000054 // end hiding -->
55 </script>
56 """)
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000057 self.assertEqual(C.js_output(['path']), r"""
Georg Brandla962eb32006-10-29 19:51:16 +000058 <script type="text/javascript">
59 <!-- begin hiding
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000060 document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
Georg Brandla962eb32006-10-29 19:51:16 +000061 // end hiding -->
62 </script>
63 """)
Moshe Zadka514a1022000-08-19 15:57:33 +000064
Georg Brandl78e69572010-08-01 18:52:52 +000065 # loading 'expires'
66 C = Cookie.SimpleCookie()
Senthil Kumaranf439a362012-05-20 12:02:44 +080067 C.load('Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000068 self.assertEqual(C['Customer']['expires'],
Senthil Kumaranf439a362012-05-20 12:02:44 +080069 'Wed, 01 Jan 2010 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000070 C = Cookie.SimpleCookie()
Senthil Kumaranf439a362012-05-20 12:02:44 +080071 C.load('Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000072 self.assertEqual(C['Customer']['expires'],
Senthil Kumaranf439a362012-05-20 12:02:44 +080073 'Wed, 01 Jan 98 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000074
R. David Murray08fc7012010-12-28 19:11:03 +000075 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
Berker Peksagcf0a7062014-07-02 10:48:27 +030083 def test_set_secure_httponly_attrs(self):
84 C = Cookie.SimpleCookie('Customer="WILE_E_COYOTE"')
85 C['Customer']['secure'] = True
86 C['Customer']['httponly'] = True
87 self.assertEqual(C.output(),
88 'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
89
90 def test_secure_httponly_false_if_not_present(self):
91 C = Cookie.SimpleCookie()
92 C.load('eggs=scrambled; Path=/bacon')
93 self.assertFalse(C['eggs']['httponly'])
94 self.assertFalse(C['eggs']['secure'])
95
96 def test_secure_httponly_true_if_present(self):
97 # Issue 16611
98 C = Cookie.SimpleCookie()
99 C.load('eggs=scrambled; httponly; secure; Path=/bacon')
100 self.assertTrue(C['eggs']['httponly'])
101 self.assertTrue(C['eggs']['secure'])
102
103 def test_secure_httponly_true_if_have_value(self):
104 # This isn't really valid, but demonstrates what the current code
105 # is expected to do in this case.
106 C = Cookie.SimpleCookie()
107 C.load('eggs=scrambled; httponly=foo; secure=bar; Path=/bacon')
108 self.assertTrue(C['eggs']['httponly'])
109 self.assertTrue(C['eggs']['secure'])
110 # Here is what it actually does; don't depend on this behavior. These
111 # checks are testing backward compatibility for issue 16611.
112 self.assertEqual(C['eggs']['httponly'], 'foo')
113 self.assertEqual(C['eggs']['secure'], 'bar')
114
115 def test_bad_attrs(self):
116 # Issue 16611: make sure we don't break backward compatibility.
117 C = Cookie.SimpleCookie()
118 C.load('cookie=with; invalid; version; second=cookie;')
119 self.assertEqual(C.output(),
120 'Set-Cookie: cookie=with\r\nSet-Cookie: second=cookie')
121
122 def test_extra_spaces(self):
123 C = Cookie.SimpleCookie()
124 C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ')
125 self.assertEqual(C.output(),
126 'Set-Cookie: eggs=scrambled; Path=bar; secure\r\nSet-Cookie: foo=foo')
127
Georg Brandla962eb32006-10-29 19:51:16 +0000128 def test_quoted_meta(self):
129 # Try cookie with quoted meta-data
130 C = Cookie.SimpleCookie()
131 C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
132 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
133 self.assertEqual(C['Customer']['version'], '1')
134 self.assertEqual(C['Customer']['path'], '/acme')
Tim Peterseb26f952001-04-06 21:20:58 +0000135
Guido van Rossumc9cdd0c2014-09-16 15:45:36 -0700136 def test_invalid_cookies(self):
137 # Accepting these could be a security issue
138 C = Cookie.SimpleCookie()
139 for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x'):
140 C.load(s)
141 self.assertEqual(dict(C), {})
142 self.assertEqual(C.output(), '')
143
144
Georg Brandla962eb32006-10-29 19:51:16 +0000145def test_main():
146 run_unittest(CookieTests)
Benjamin Petersonbf67ba82012-10-16 09:51:46 -0400147 if Cookie.__doc__ is not None:
148 with check_warnings(('.+Cookie class is insecure; do not use it',
149 DeprecationWarning)):
150 run_doctest(Cookie)
Georg Brandla962eb32006-10-29 19:51:16 +0000151
152if __name__ == '__main__':
153 test_main()