1 | n/a | # Simple test suite for http/cookies.py |
---|
2 | n/a | |
---|
3 | n/a | import copy |
---|
4 | n/a | from test.support import run_unittest, run_doctest, check_warnings |
---|
5 | n/a | import unittest |
---|
6 | n/a | from http import cookies |
---|
7 | n/a | import pickle |
---|
8 | n/a | import warnings |
---|
9 | n/a | |
---|
10 | n/a | class CookieTests(unittest.TestCase): |
---|
11 | n/a | |
---|
12 | n/a | def test_basic(self): |
---|
13 | n/a | cases = [ |
---|
14 | n/a | {'data': 'chips=ahoy; vienna=finger', |
---|
15 | n/a | 'dict': {'chips':'ahoy', 'vienna':'finger'}, |
---|
16 | n/a | 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>", |
---|
17 | n/a | 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger'}, |
---|
18 | n/a | |
---|
19 | n/a | {'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', |
---|
20 | n/a | 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}, |
---|
21 | n/a | 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''', |
---|
22 | n/a | 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"'}, |
---|
23 | n/a | |
---|
24 | n/a | # Check illegal cookies that have an '=' char in an unquoted value |
---|
25 | n/a | {'data': 'keebler=E=mc2', |
---|
26 | n/a | 'dict': {'keebler' : 'E=mc2'}, |
---|
27 | n/a | 'repr': "<SimpleCookie: keebler='E=mc2'>", |
---|
28 | n/a | 'output': 'Set-Cookie: keebler=E=mc2'}, |
---|
29 | n/a | |
---|
30 | n/a | # Cookies with ':' character in their name. Though not mentioned in |
---|
31 | n/a | # RFC, servers / browsers allow it. |
---|
32 | n/a | |
---|
33 | n/a | {'data': 'key:term=value:term', |
---|
34 | n/a | 'dict': {'key:term' : 'value:term'}, |
---|
35 | n/a | 'repr': "<SimpleCookie: key:term='value:term'>", |
---|
36 | n/a | 'output': 'Set-Cookie: key:term=value:term'}, |
---|
37 | n/a | |
---|
38 | n/a | # issue22931 - Adding '[' and ']' as valid characters in cookie |
---|
39 | n/a | # values as defined in RFC 6265 |
---|
40 | n/a | { |
---|
41 | n/a | 'data': 'a=b; c=[; d=r; f=h', |
---|
42 | n/a | 'dict': {'a':'b', 'c':'[', 'd':'r', 'f':'h'}, |
---|
43 | n/a | 'repr': "<SimpleCookie: a='b' c='[' d='r' f='h'>", |
---|
44 | n/a | 'output': '\n'.join(( |
---|
45 | n/a | 'Set-Cookie: a=b', |
---|
46 | n/a | 'Set-Cookie: c=[', |
---|
47 | n/a | 'Set-Cookie: d=r', |
---|
48 | n/a | 'Set-Cookie: f=h' |
---|
49 | n/a | )) |
---|
50 | n/a | } |
---|
51 | n/a | ] |
---|
52 | n/a | |
---|
53 | n/a | for case in cases: |
---|
54 | n/a | C = cookies.SimpleCookie() |
---|
55 | n/a | C.load(case['data']) |
---|
56 | n/a | self.assertEqual(repr(C), case['repr']) |
---|
57 | n/a | self.assertEqual(C.output(sep='\n'), case['output']) |
---|
58 | n/a | for k, v in sorted(case['dict'].items()): |
---|
59 | n/a | self.assertEqual(C[k].value, v) |
---|
60 | n/a | |
---|
61 | n/a | def test_load(self): |
---|
62 | n/a | C = cookies.SimpleCookie() |
---|
63 | n/a | C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') |
---|
64 | n/a | |
---|
65 | n/a | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
---|
66 | n/a | self.assertEqual(C['Customer']['version'], '1') |
---|
67 | n/a | self.assertEqual(C['Customer']['path'], '/acme') |
---|
68 | n/a | |
---|
69 | n/a | self.assertEqual(C.output(['path']), |
---|
70 | n/a | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
---|
71 | n/a | self.assertEqual(C.js_output(), r""" |
---|
72 | n/a | <script type="text/javascript"> |
---|
73 | n/a | <!-- begin hiding |
---|
74 | n/a | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
---|
75 | n/a | // end hiding --> |
---|
76 | n/a | </script> |
---|
77 | n/a | """) |
---|
78 | n/a | self.assertEqual(C.js_output(['path']), r""" |
---|
79 | n/a | <script type="text/javascript"> |
---|
80 | n/a | <!-- begin hiding |
---|
81 | n/a | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
---|
82 | n/a | // end hiding --> |
---|
83 | n/a | </script> |
---|
84 | n/a | """) |
---|
85 | n/a | |
---|
86 | n/a | def test_extended_encode(self): |
---|
87 | n/a | # Issue 9824: some browsers don't follow the standard; we now |
---|
88 | n/a | # encode , and ; to keep them from tripping up. |
---|
89 | n/a | C = cookies.SimpleCookie() |
---|
90 | n/a | C['val'] = "some,funky;stuff" |
---|
91 | n/a | self.assertEqual(C.output(['val']), |
---|
92 | n/a | 'Set-Cookie: val="some\\054funky\\073stuff"') |
---|
93 | n/a | |
---|
94 | n/a | def test_special_attrs(self): |
---|
95 | n/a | # 'expires' |
---|
96 | n/a | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
---|
97 | n/a | C['Customer']['expires'] = 0 |
---|
98 | n/a | # can't test exact output, it always depends on current date/time |
---|
99 | n/a | self.assertTrue(C.output().endswith('GMT')) |
---|
100 | n/a | |
---|
101 | n/a | # loading 'expires' |
---|
102 | n/a | C = cookies.SimpleCookie() |
---|
103 | n/a | C.load('Customer="W"; expires=Wed, 01 Jan 2010 00:00:00 GMT') |
---|
104 | n/a | self.assertEqual(C['Customer']['expires'], |
---|
105 | n/a | 'Wed, 01 Jan 2010 00:00:00 GMT') |
---|
106 | n/a | C = cookies.SimpleCookie() |
---|
107 | n/a | C.load('Customer="W"; expires=Wed, 01 Jan 98 00:00:00 GMT') |
---|
108 | n/a | self.assertEqual(C['Customer']['expires'], |
---|
109 | n/a | 'Wed, 01 Jan 98 00:00:00 GMT') |
---|
110 | n/a | |
---|
111 | n/a | # 'max-age' |
---|
112 | n/a | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
---|
113 | n/a | C['Customer']['max-age'] = 10 |
---|
114 | n/a | self.assertEqual(C.output(), |
---|
115 | n/a | 'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10') |
---|
116 | n/a | |
---|
117 | n/a | def test_set_secure_httponly_attrs(self): |
---|
118 | n/a | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
---|
119 | n/a | C['Customer']['secure'] = True |
---|
120 | n/a | C['Customer']['httponly'] = True |
---|
121 | n/a | self.assertEqual(C.output(), |
---|
122 | n/a | 'Set-Cookie: Customer="WILE_E_COYOTE"; HttpOnly; Secure') |
---|
123 | n/a | |
---|
124 | n/a | def test_secure_httponly_false_if_not_present(self): |
---|
125 | n/a | C = cookies.SimpleCookie() |
---|
126 | n/a | C.load('eggs=scrambled; Path=/bacon') |
---|
127 | n/a | self.assertFalse(C['eggs']['httponly']) |
---|
128 | n/a | self.assertFalse(C['eggs']['secure']) |
---|
129 | n/a | |
---|
130 | n/a | def test_secure_httponly_true_if_present(self): |
---|
131 | n/a | # Issue 16611 |
---|
132 | n/a | C = cookies.SimpleCookie() |
---|
133 | n/a | C.load('eggs=scrambled; httponly; secure; Path=/bacon') |
---|
134 | n/a | self.assertTrue(C['eggs']['httponly']) |
---|
135 | n/a | self.assertTrue(C['eggs']['secure']) |
---|
136 | n/a | |
---|
137 | n/a | def test_secure_httponly_true_if_have_value(self): |
---|
138 | n/a | # This isn't really valid, but demonstrates what the current code |
---|
139 | n/a | # is expected to do in this case. |
---|
140 | n/a | C = cookies.SimpleCookie() |
---|
141 | n/a | C.load('eggs=scrambled; httponly=foo; secure=bar; Path=/bacon') |
---|
142 | n/a | self.assertTrue(C['eggs']['httponly']) |
---|
143 | n/a | self.assertTrue(C['eggs']['secure']) |
---|
144 | n/a | # Here is what it actually does; don't depend on this behavior. These |
---|
145 | n/a | # checks are testing backward compatibility for issue 16611. |
---|
146 | n/a | self.assertEqual(C['eggs']['httponly'], 'foo') |
---|
147 | n/a | self.assertEqual(C['eggs']['secure'], 'bar') |
---|
148 | n/a | |
---|
149 | n/a | def test_extra_spaces(self): |
---|
150 | n/a | C = cookies.SimpleCookie() |
---|
151 | n/a | C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ') |
---|
152 | n/a | self.assertEqual(C.output(), |
---|
153 | n/a | 'Set-Cookie: eggs=scrambled; Path=bar; Secure\r\nSet-Cookie: foo=foo') |
---|
154 | n/a | |
---|
155 | n/a | def test_quoted_meta(self): |
---|
156 | n/a | # Try cookie with quoted meta-data |
---|
157 | n/a | C = cookies.SimpleCookie() |
---|
158 | n/a | C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') |
---|
159 | n/a | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
---|
160 | n/a | self.assertEqual(C['Customer']['version'], '1') |
---|
161 | n/a | self.assertEqual(C['Customer']['path'], '/acme') |
---|
162 | n/a | |
---|
163 | n/a | self.assertEqual(C.output(['path']), |
---|
164 | n/a | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
---|
165 | n/a | self.assertEqual(C.js_output(), r""" |
---|
166 | n/a | <script type="text/javascript"> |
---|
167 | n/a | <!-- begin hiding |
---|
168 | n/a | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
---|
169 | n/a | // end hiding --> |
---|
170 | n/a | </script> |
---|
171 | n/a | """) |
---|
172 | n/a | self.assertEqual(C.js_output(['path']), r""" |
---|
173 | n/a | <script type="text/javascript"> |
---|
174 | n/a | <!-- begin hiding |
---|
175 | n/a | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
---|
176 | n/a | // end hiding --> |
---|
177 | n/a | </script> |
---|
178 | n/a | """) |
---|
179 | n/a | |
---|
180 | n/a | def test_invalid_cookies(self): |
---|
181 | n/a | # Accepting these could be a security issue |
---|
182 | n/a | C = cookies.SimpleCookie() |
---|
183 | n/a | for s in (']foo=x', '[foo=x', 'blah]foo=x', 'blah[foo=x', |
---|
184 | n/a | 'Set-Cookie: foo=bar', 'Set-Cookie: foo', |
---|
185 | n/a | 'foo=bar; baz', 'baz; foo=bar', |
---|
186 | n/a | 'secure;foo=bar', 'Version=1;foo=bar'): |
---|
187 | n/a | C.load(s) |
---|
188 | n/a | self.assertEqual(dict(C), {}) |
---|
189 | n/a | self.assertEqual(C.output(), '') |
---|
190 | n/a | |
---|
191 | n/a | def test_pickle(self): |
---|
192 | n/a | rawdata = 'Customer="WILE_E_COYOTE"; Path=/acme; Version=1' |
---|
193 | n/a | expected_output = 'Set-Cookie: %s' % rawdata |
---|
194 | n/a | |
---|
195 | n/a | C = cookies.SimpleCookie() |
---|
196 | n/a | C.load(rawdata) |
---|
197 | n/a | self.assertEqual(C.output(), expected_output) |
---|
198 | n/a | |
---|
199 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
200 | n/a | with self.subTest(proto=proto): |
---|
201 | n/a | C1 = pickle.loads(pickle.dumps(C, protocol=proto)) |
---|
202 | n/a | self.assertEqual(C1.output(), expected_output) |
---|
203 | n/a | |
---|
204 | n/a | def test_illegal_chars(self): |
---|
205 | n/a | rawdata = "a=b; c,d=e" |
---|
206 | n/a | C = cookies.SimpleCookie() |
---|
207 | n/a | with self.assertRaises(cookies.CookieError): |
---|
208 | n/a | C.load(rawdata) |
---|
209 | n/a | |
---|
210 | n/a | |
---|
211 | n/a | class MorselTests(unittest.TestCase): |
---|
212 | n/a | """Tests for the Morsel object.""" |
---|
213 | n/a | |
---|
214 | n/a | def test_defaults(self): |
---|
215 | n/a | morsel = cookies.Morsel() |
---|
216 | n/a | self.assertIsNone(morsel.key) |
---|
217 | n/a | self.assertIsNone(morsel.value) |
---|
218 | n/a | self.assertIsNone(morsel.coded_value) |
---|
219 | n/a | self.assertEqual(morsel.keys(), cookies.Morsel._reserved.keys()) |
---|
220 | n/a | for key, val in morsel.items(): |
---|
221 | n/a | self.assertEqual(val, '', key) |
---|
222 | n/a | |
---|
223 | n/a | def test_reserved_keys(self): |
---|
224 | n/a | M = cookies.Morsel() |
---|
225 | n/a | # tests valid and invalid reserved keys for Morsels |
---|
226 | n/a | for i in M._reserved: |
---|
227 | n/a | # Test that all valid keys are reported as reserved and set them |
---|
228 | n/a | self.assertTrue(M.isReservedKey(i)) |
---|
229 | n/a | M[i] = '%s_value' % i |
---|
230 | n/a | for i in M._reserved: |
---|
231 | n/a | # Test that valid key values come out fine |
---|
232 | n/a | self.assertEqual(M[i], '%s_value' % i) |
---|
233 | n/a | for i in "the holy hand grenade".split(): |
---|
234 | n/a | # Test that invalid keys raise CookieError |
---|
235 | n/a | self.assertRaises(cookies.CookieError, |
---|
236 | n/a | M.__setitem__, i, '%s_value' % i) |
---|
237 | n/a | |
---|
238 | n/a | def test_setter(self): |
---|
239 | n/a | M = cookies.Morsel() |
---|
240 | n/a | # tests the .set method to set keys and their values |
---|
241 | n/a | for i in M._reserved: |
---|
242 | n/a | # Makes sure that all reserved keys can't be set this way |
---|
243 | n/a | self.assertRaises(cookies.CookieError, |
---|
244 | n/a | M.set, i, '%s_value' % i, '%s_value' % i) |
---|
245 | n/a | for i in "thou cast _the- !holy! ^hand| +*grenade~".split(): |
---|
246 | n/a | # Try typical use case. Setting decent values. |
---|
247 | n/a | # Check output and js_output. |
---|
248 | n/a | M['path'] = '/foo' # Try a reserved key as well |
---|
249 | n/a | M.set(i, "%s_val" % i, "%s_coded_val" % i) |
---|
250 | n/a | self.assertEqual(M.key, i) |
---|
251 | n/a | self.assertEqual(M.value, "%s_val" % i) |
---|
252 | n/a | self.assertEqual(M.coded_value, "%s_coded_val" % i) |
---|
253 | n/a | self.assertEqual( |
---|
254 | n/a | M.output(), |
---|
255 | n/a | "Set-Cookie: %s=%s; Path=/foo" % (i, "%s_coded_val" % i)) |
---|
256 | n/a | expected_js_output = """ |
---|
257 | n/a | <script type="text/javascript"> |
---|
258 | n/a | <!-- begin hiding |
---|
259 | n/a | document.cookie = "%s=%s; Path=/foo"; |
---|
260 | n/a | // end hiding --> |
---|
261 | n/a | </script> |
---|
262 | n/a | """ % (i, "%s_coded_val" % i) |
---|
263 | n/a | self.assertEqual(M.js_output(), expected_js_output) |
---|
264 | n/a | for i in ["foo bar", "foo@bar"]: |
---|
265 | n/a | # Try some illegal characters |
---|
266 | n/a | self.assertRaises(cookies.CookieError, |
---|
267 | n/a | M.set, i, '%s_value' % i, '%s_value' % i) |
---|
268 | n/a | |
---|
269 | n/a | def test_set_properties(self): |
---|
270 | n/a | morsel = cookies.Morsel() |
---|
271 | n/a | with self.assertRaises(AttributeError): |
---|
272 | n/a | morsel.key = '' |
---|
273 | n/a | with self.assertRaises(AttributeError): |
---|
274 | n/a | morsel.value = '' |
---|
275 | n/a | with self.assertRaises(AttributeError): |
---|
276 | n/a | morsel.coded_value = '' |
---|
277 | n/a | |
---|
278 | n/a | def test_eq(self): |
---|
279 | n/a | base_case = ('key', 'value', '"value"') |
---|
280 | n/a | attribs = { |
---|
281 | n/a | 'path': '/', |
---|
282 | n/a | 'comment': 'foo', |
---|
283 | n/a | 'domain': 'example.com', |
---|
284 | n/a | 'version': 2, |
---|
285 | n/a | } |
---|
286 | n/a | morsel_a = cookies.Morsel() |
---|
287 | n/a | morsel_a.update(attribs) |
---|
288 | n/a | morsel_a.set(*base_case) |
---|
289 | n/a | morsel_b = cookies.Morsel() |
---|
290 | n/a | morsel_b.update(attribs) |
---|
291 | n/a | morsel_b.set(*base_case) |
---|
292 | n/a | self.assertTrue(morsel_a == morsel_b) |
---|
293 | n/a | self.assertFalse(morsel_a != morsel_b) |
---|
294 | n/a | cases = ( |
---|
295 | n/a | ('key', 'value', 'mismatch'), |
---|
296 | n/a | ('key', 'mismatch', '"value"'), |
---|
297 | n/a | ('mismatch', 'value', '"value"'), |
---|
298 | n/a | ) |
---|
299 | n/a | for case_b in cases: |
---|
300 | n/a | with self.subTest(case_b): |
---|
301 | n/a | morsel_b = cookies.Morsel() |
---|
302 | n/a | morsel_b.update(attribs) |
---|
303 | n/a | morsel_b.set(*case_b) |
---|
304 | n/a | self.assertFalse(morsel_a == morsel_b) |
---|
305 | n/a | self.assertTrue(morsel_a != morsel_b) |
---|
306 | n/a | |
---|
307 | n/a | morsel_b = cookies.Morsel() |
---|
308 | n/a | morsel_b.update(attribs) |
---|
309 | n/a | morsel_b.set(*base_case) |
---|
310 | n/a | morsel_b['comment'] = 'bar' |
---|
311 | n/a | self.assertFalse(morsel_a == morsel_b) |
---|
312 | n/a | self.assertTrue(morsel_a != morsel_b) |
---|
313 | n/a | |
---|
314 | n/a | # test mismatched types |
---|
315 | n/a | self.assertFalse(cookies.Morsel() == 1) |
---|
316 | n/a | self.assertTrue(cookies.Morsel() != 1) |
---|
317 | n/a | self.assertFalse(cookies.Morsel() == '') |
---|
318 | n/a | self.assertTrue(cookies.Morsel() != '') |
---|
319 | n/a | items = list(cookies.Morsel().items()) |
---|
320 | n/a | self.assertFalse(cookies.Morsel() == items) |
---|
321 | n/a | self.assertTrue(cookies.Morsel() != items) |
---|
322 | n/a | |
---|
323 | n/a | # morsel/dict |
---|
324 | n/a | morsel = cookies.Morsel() |
---|
325 | n/a | morsel.set(*base_case) |
---|
326 | n/a | morsel.update(attribs) |
---|
327 | n/a | self.assertTrue(morsel == dict(morsel)) |
---|
328 | n/a | self.assertFalse(morsel != dict(morsel)) |
---|
329 | n/a | |
---|
330 | n/a | def test_copy(self): |
---|
331 | n/a | morsel_a = cookies.Morsel() |
---|
332 | n/a | morsel_a.set('foo', 'bar', 'baz') |
---|
333 | n/a | morsel_a.update({ |
---|
334 | n/a | 'version': 2, |
---|
335 | n/a | 'comment': 'foo', |
---|
336 | n/a | }) |
---|
337 | n/a | morsel_b = morsel_a.copy() |
---|
338 | n/a | self.assertIsInstance(morsel_b, cookies.Morsel) |
---|
339 | n/a | self.assertIsNot(morsel_a, morsel_b) |
---|
340 | n/a | self.assertEqual(morsel_a, morsel_b) |
---|
341 | n/a | |
---|
342 | n/a | morsel_b = copy.copy(morsel_a) |
---|
343 | n/a | self.assertIsInstance(morsel_b, cookies.Morsel) |
---|
344 | n/a | self.assertIsNot(morsel_a, morsel_b) |
---|
345 | n/a | self.assertEqual(morsel_a, morsel_b) |
---|
346 | n/a | |
---|
347 | n/a | def test_setitem(self): |
---|
348 | n/a | morsel = cookies.Morsel() |
---|
349 | n/a | morsel['expires'] = 0 |
---|
350 | n/a | self.assertEqual(morsel['expires'], 0) |
---|
351 | n/a | morsel['Version'] = 2 |
---|
352 | n/a | self.assertEqual(morsel['version'], 2) |
---|
353 | n/a | morsel['DOMAIN'] = 'example.com' |
---|
354 | n/a | self.assertEqual(morsel['domain'], 'example.com') |
---|
355 | n/a | |
---|
356 | n/a | with self.assertRaises(cookies.CookieError): |
---|
357 | n/a | morsel['invalid'] = 'value' |
---|
358 | n/a | self.assertNotIn('invalid', morsel) |
---|
359 | n/a | |
---|
360 | n/a | def test_setdefault(self): |
---|
361 | n/a | morsel = cookies.Morsel() |
---|
362 | n/a | morsel.update({ |
---|
363 | n/a | 'domain': 'example.com', |
---|
364 | n/a | 'version': 2, |
---|
365 | n/a | }) |
---|
366 | n/a | # this shouldn't override the default value |
---|
367 | n/a | self.assertEqual(morsel.setdefault('expires', 'value'), '') |
---|
368 | n/a | self.assertEqual(morsel['expires'], '') |
---|
369 | n/a | self.assertEqual(morsel.setdefault('Version', 1), 2) |
---|
370 | n/a | self.assertEqual(morsel['version'], 2) |
---|
371 | n/a | self.assertEqual(morsel.setdefault('DOMAIN', 'value'), 'example.com') |
---|
372 | n/a | self.assertEqual(morsel['domain'], 'example.com') |
---|
373 | n/a | |
---|
374 | n/a | with self.assertRaises(cookies.CookieError): |
---|
375 | n/a | morsel.setdefault('invalid', 'value') |
---|
376 | n/a | self.assertNotIn('invalid', morsel) |
---|
377 | n/a | |
---|
378 | n/a | def test_update(self): |
---|
379 | n/a | attribs = {'expires': 1, 'Version': 2, 'DOMAIN': 'example.com'} |
---|
380 | n/a | # test dict update |
---|
381 | n/a | morsel = cookies.Morsel() |
---|
382 | n/a | morsel.update(attribs) |
---|
383 | n/a | self.assertEqual(morsel['expires'], 1) |
---|
384 | n/a | self.assertEqual(morsel['version'], 2) |
---|
385 | n/a | self.assertEqual(morsel['domain'], 'example.com') |
---|
386 | n/a | # test iterable update |
---|
387 | n/a | morsel = cookies.Morsel() |
---|
388 | n/a | morsel.update(list(attribs.items())) |
---|
389 | n/a | self.assertEqual(morsel['expires'], 1) |
---|
390 | n/a | self.assertEqual(morsel['version'], 2) |
---|
391 | n/a | self.assertEqual(morsel['domain'], 'example.com') |
---|
392 | n/a | # test iterator update |
---|
393 | n/a | morsel = cookies.Morsel() |
---|
394 | n/a | morsel.update((k, v) for k, v in attribs.items()) |
---|
395 | n/a | self.assertEqual(morsel['expires'], 1) |
---|
396 | n/a | self.assertEqual(morsel['version'], 2) |
---|
397 | n/a | self.assertEqual(morsel['domain'], 'example.com') |
---|
398 | n/a | |
---|
399 | n/a | with self.assertRaises(cookies.CookieError): |
---|
400 | n/a | morsel.update({'invalid': 'value'}) |
---|
401 | n/a | self.assertNotIn('invalid', morsel) |
---|
402 | n/a | self.assertRaises(TypeError, morsel.update) |
---|
403 | n/a | self.assertRaises(TypeError, morsel.update, 0) |
---|
404 | n/a | |
---|
405 | n/a | def test_pickle(self): |
---|
406 | n/a | morsel_a = cookies.Morsel() |
---|
407 | n/a | morsel_a.set('foo', 'bar', 'baz') |
---|
408 | n/a | morsel_a.update({ |
---|
409 | n/a | 'version': 2, |
---|
410 | n/a | 'comment': 'foo', |
---|
411 | n/a | }) |
---|
412 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
413 | n/a | with self.subTest(proto=proto): |
---|
414 | n/a | morsel_b = pickle.loads(pickle.dumps(morsel_a, proto)) |
---|
415 | n/a | self.assertIsInstance(morsel_b, cookies.Morsel) |
---|
416 | n/a | self.assertEqual(morsel_b, morsel_a) |
---|
417 | n/a | self.assertEqual(str(morsel_b), str(morsel_a)) |
---|
418 | n/a | |
---|
419 | n/a | def test_repr(self): |
---|
420 | n/a | morsel = cookies.Morsel() |
---|
421 | n/a | self.assertEqual(repr(morsel), '<Morsel: None=None>') |
---|
422 | n/a | self.assertEqual(str(morsel), 'Set-Cookie: None=None') |
---|
423 | n/a | morsel.set('key', 'val', 'coded_val') |
---|
424 | n/a | self.assertEqual(repr(morsel), '<Morsel: key=coded_val>') |
---|
425 | n/a | self.assertEqual(str(morsel), 'Set-Cookie: key=coded_val') |
---|
426 | n/a | morsel.update({ |
---|
427 | n/a | 'path': '/', |
---|
428 | n/a | 'comment': 'foo', |
---|
429 | n/a | 'domain': 'example.com', |
---|
430 | n/a | 'max-age': 0, |
---|
431 | n/a | 'secure': 0, |
---|
432 | n/a | 'version': 1, |
---|
433 | n/a | }) |
---|
434 | n/a | self.assertEqual(repr(morsel), |
---|
435 | n/a | '<Morsel: key=coded_val; Comment=foo; Domain=example.com; ' |
---|
436 | n/a | 'Max-Age=0; Path=/; Version=1>') |
---|
437 | n/a | self.assertEqual(str(morsel), |
---|
438 | n/a | 'Set-Cookie: key=coded_val; Comment=foo; Domain=example.com; ' |
---|
439 | n/a | 'Max-Age=0; Path=/; Version=1') |
---|
440 | n/a | morsel['secure'] = True |
---|
441 | n/a | morsel['httponly'] = 1 |
---|
442 | n/a | self.assertEqual(repr(morsel), |
---|
443 | n/a | '<Morsel: key=coded_val; Comment=foo; Domain=example.com; ' |
---|
444 | n/a | 'HttpOnly; Max-Age=0; Path=/; Secure; Version=1>') |
---|
445 | n/a | self.assertEqual(str(morsel), |
---|
446 | n/a | 'Set-Cookie: key=coded_val; Comment=foo; Domain=example.com; ' |
---|
447 | n/a | 'HttpOnly; Max-Age=0; Path=/; Secure; Version=1') |
---|
448 | n/a | |
---|
449 | n/a | morsel = cookies.Morsel() |
---|
450 | n/a | morsel.set('key', 'val', 'coded_val') |
---|
451 | n/a | morsel['expires'] = 0 |
---|
452 | n/a | self.assertRegex(repr(morsel), |
---|
453 | n/a | r'<Morsel: key=coded_val; ' |
---|
454 | n/a | r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+>') |
---|
455 | n/a | self.assertRegex(str(morsel), |
---|
456 | n/a | r'Set-Cookie: key=coded_val; ' |
---|
457 | n/a | r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+') |
---|
458 | n/a | |
---|
459 | n/a | def test_main(): |
---|
460 | n/a | run_unittest(CookieTests, MorselTests) |
---|
461 | n/a | run_doctest(cookies) |
---|
462 | n/a | |
---|
463 | n/a | if __name__ == '__main__': |
---|
464 | n/a | test_main() |
---|