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