blob: 404190123fae9983eccb331eb3aa1c26ed1ab326 [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
Serhiy Storchaka186c5f02014-11-02 22:35:47 +02006import pickle
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +00007
Andrew M. Kuchling44644322002-12-29 16:45:06 +00008
Georg Brandla962eb32006-10-29 19:51:16 +00009class CookieTests(unittest.TestCase):
10 # Currently this only tests SimpleCookie
11 def test_basic(self):
12 cases = [
13 { 'data': 'chips=ahoy; vienna=finger',
14 'dict': {'chips':'ahoy', 'vienna':'finger'},
15 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>",
16 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger',
17 },
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000018
Georg Brandla962eb32006-10-29 19:51:16 +000019 { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
20 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'},
21 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''',
22 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
23 },
Andrew M. Kuchling8b94b1c2001-02-21 01:17:54 +000024
Georg Brandla962eb32006-10-29 19:51:16 +000025 # Check illegal cookies that have an '=' char in an unquoted value
26 { 'data': 'keebler=E=mc2',
27 'dict': {'keebler' : 'E=mc2'},
28 'repr': "<SimpleCookie: keebler='E=mc2'>",
29 'output': 'Set-Cookie: keebler=E=mc2',
Benjamin Petersonec7abfb2015-05-23 10:46:25 -050030 },
31
32 # issue22931 - Adding '[' and ']' as valid characters in cookie
33 # values as defined in RFC 6265
34 {
35 'data': 'a=b; c=[; d=r; f=h',
36 'dict': {'a':'b', 'c':'[', 'd':'r', 'f':'h'},
37 'repr': "<SimpleCookie: a='b' c='[' d='r' f='h'>",
38 'output': '\n'.join((
39 'Set-Cookie: a=b',
40 'Set-Cookie: c=[',
41 'Set-Cookie: d=r',
42 'Set-Cookie: f=h'
43 ))
Georg Brandla962eb32006-10-29 19:51:16 +000044 }
45 ]
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000046
Georg Brandla962eb32006-10-29 19:51:16 +000047 for case in cases:
48 C = Cookie.SimpleCookie()
49 C.load(case['data'])
50 self.assertEqual(repr(C), case['repr'])
51 self.assertEqual(C.output(sep='\n'), case['output'])
52 for k, v in sorted(case['dict'].iteritems()):
53 self.assertEqual(C[k].value, v)
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000054
Georg Brandla962eb32006-10-29 19:51:16 +000055 def test_load(self):
56 C = Cookie.SimpleCookie()
57 C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
Andrew M. Kuchlingcdec8c72000-08-19 15:21:12 +000058
Georg Brandla962eb32006-10-29 19:51:16 +000059 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
60 self.assertEqual(C['Customer']['version'], '1')
61 self.assertEqual(C['Customer']['path'], '/acme')
Moshe Zadka514a1022000-08-19 15:57:33 +000062
Georg Brandla962eb32006-10-29 19:51:16 +000063 self.assertEqual(C.output(['path']),
64 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000065 self.assertEqual(C.js_output(), r"""
Georg Brandla962eb32006-10-29 19:51:16 +000066 <script type="text/javascript">
67 <!-- begin hiding
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000068 document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
Georg Brandla962eb32006-10-29 19:51:16 +000069 // end hiding -->
70 </script>
71 """)
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000072 self.assertEqual(C.js_output(['path']), r"""
Georg Brandla962eb32006-10-29 19:51:16 +000073 <script type="text/javascript">
74 <!-- begin hiding
Senthil Kumaranc730a6a2009-04-02 03:00:34 +000075 document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
Georg Brandla962eb32006-10-29 19:51:16 +000076 // end hiding -->
77 </script>
78 """)
Moshe Zadka514a1022000-08-19 15:57:33 +000079
Georg Brandl78e69572010-08-01 18:52:52 +000080 # loading 'expires'
81 C = Cookie.SimpleCookie()
Senthil Kumaranf439a362012-05-20 12:02:44 +080082 C.load('Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000083 self.assertEqual(C['Customer']['expires'],
Senthil Kumaranf439a362012-05-20 12:02:44 +080084 'Wed, 01 Jan 2010 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000085 C = Cookie.SimpleCookie()
Senthil Kumaranf439a362012-05-20 12:02:44 +080086 C.load('Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000087 self.assertEqual(C['Customer']['expires'],
Senthil Kumaranf439a362012-05-20 12:02:44 +080088 'Wed, 01 Jan 98 00:00:00 GMT')
Georg Brandl78e69572010-08-01 18:52:52 +000089
R. David Murray08fc7012010-12-28 19:11:03 +000090 def test_extended_encode(self):
91 # Issue 9824: some browsers don't follow the standard; we now
92 # encode , and ; to keep them from tripping up.
93 C = Cookie.SimpleCookie()
94 C['val'] = "some,funky;stuff"
95 self.assertEqual(C.output(['val']),
96 'Set-Cookie: val="some\\054funky\\073stuff"')
97
Berker Peksagcf0a7062014-07-02 10:48:27 +030098 def test_set_secure_httponly_attrs(self):
99 C = Cookie.SimpleCookie('Customer="WILE_E_COYOTE"')
100 C['Customer']['secure'] = True
101 C['Customer']['httponly'] = True
102 self.assertEqual(C.output(),
103 'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
104
105 def test_secure_httponly_false_if_not_present(self):
106 C = Cookie.SimpleCookie()
107 C.load('eggs=scrambled; Path=/bacon')
108 self.assertFalse(C['eggs']['httponly'])
109 self.assertFalse(C['eggs']['secure'])
110
111 def test_secure_httponly_true_if_present(self):
112 # Issue 16611
113 C = Cookie.SimpleCookie()
114 C.load('eggs=scrambled; httponly; secure; Path=/bacon')
115 self.assertTrue(C['eggs']['httponly'])
116 self.assertTrue(C['eggs']['secure'])
117
118 def test_secure_httponly_true_if_have_value(self):
119 # This isn't really valid, but demonstrates what the current code
120 # is expected to do in this case.
121 C = Cookie.SimpleCookie()
122 C.load('eggs=scrambled; httponly=foo; secure=bar; Path=/bacon')
123 self.assertTrue(C['eggs']['httponly'])
124 self.assertTrue(C['eggs']['secure'])
125 # Here is what it actually does; don't depend on this behavior. These
126 # checks are testing backward compatibility for issue 16611.
127 self.assertEqual(C['eggs']['httponly'], 'foo')
128 self.assertEqual(C['eggs']['secure'], 'bar')
129
130 def test_bad_attrs(self):
131 # Issue 16611: make sure we don't break backward compatibility.
132 C = Cookie.SimpleCookie()
133 C.load('cookie=with; invalid; version; second=cookie;')
134 self.assertEqual(C.output(),
135 'Set-Cookie: cookie=with\r\nSet-Cookie: second=cookie')
136
137 def test_extra_spaces(self):
138 C = Cookie.SimpleCookie()
139 C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ')
140 self.assertEqual(C.output(),
141 'Set-Cookie: eggs=scrambled; Path=bar; secure\r\nSet-Cookie: foo=foo')
142
Georg Brandla962eb32006-10-29 19:51:16 +0000143 def test_quoted_meta(self):
144 # Try cookie with quoted meta-data
145 C = Cookie.SimpleCookie()
146 C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
147 self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
148 self.assertEqual(C['Customer']['version'], '1')
149 self.assertEqual(C['Customer']['path'], '/acme')
Tim Peterseb26f952001-04-06 21:20:58 +0000150
Guido van Rossumc9cdd0c2014-09-16 15:45:36 -0700151 def test_invalid_cookies(self):
152 # Accepting these could be a security issue
153 C = Cookie.SimpleCookie()
154 for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x'):
155 C.load(s)
156 self.assertEqual(dict(C), {})
157 self.assertEqual(C.output(), '')
158
Serhiy Storchaka186c5f02014-11-02 22:35:47 +0200159 def test_pickle(self):
160 rawdata = 'Customer="WILE_E_COYOTE"; Path=/acme; Version=1'
161 expected_output = 'Set-Cookie: %s' % rawdata
162
163 C = Cookie.SimpleCookie()
164 C.load(rawdata)
165 self.assertEqual(C.output(), expected_output)
166
167 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
168 C1 = pickle.loads(pickle.dumps(C, protocol=proto))
169 self.assertEqual(C1.output(), expected_output)
170
Guido van Rossumc9cdd0c2014-09-16 15:45:36 -0700171
Georg Brandla962eb32006-10-29 19:51:16 +0000172def test_main():
173 run_unittest(CookieTests)
Benjamin Petersonbf67ba82012-10-16 09:51:46 -0400174 if Cookie.__doc__ is not None:
175 with check_warnings(('.+Cookie class is insecure; do not use it',
176 DeprecationWarning)):
177 run_doctest(Cookie)
Georg Brandla962eb32006-10-29 19:51:16 +0000178
179if __name__ == '__main__':
180 test_main()