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 | |
Brett Cannon | d3791ed | 2010-03-20 21:51:10 +0000 | [diff] [blame] | 3 | from test.support import run_unittest, run_doctest, check_warnings |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4 | import unittest |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 5 | from http import cookies |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 6 | |
Andrew M. Kuchling | 4464432 | 2002-12-29 16:45:06 +0000 | [diff] [blame] | 7 | import warnings |
Andrew M. Kuchling | 4464432 | 2002-12-29 16:45:06 +0000 | [diff] [blame] | 8 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | class CookieTests(unittest.TestCase): |
Brett Cannon | d3791ed | 2010-03-20 21:51:10 +0000 | [diff] [blame] | 10 | |
| 11 | def setUp(self): |
| 12 | self._warnings_manager = check_warnings() |
| 13 | self._warnings_manager.__enter__() |
| 14 | warnings.filterwarnings("ignore", ".* class is insecure.*", |
| 15 | DeprecationWarning) |
| 16 | |
| 17 | def tearDown(self): |
| 18 | self._warnings_manager.__exit__(None, None, None) |
| 19 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | def test_basic(self): |
| 21 | cases = [ |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 22 | {'data': 'chips=ahoy; vienna=finger', |
| 23 | 'dict': {'chips':'ahoy', 'vienna':'finger'}, |
| 24 | 'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>", |
| 25 | 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger'}, |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 27 | {'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', |
| 28 | 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}, |
| 29 | 'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''', |
| 30 | 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"'}, |
Andrew M. Kuchling | 8b94b1c | 2001-02-21 01:17:54 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | # Check illegal cookies that have an '=' char in an unquoted value |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 33 | {'data': 'keebler=E=mc2', |
| 34 | 'dict': {'keebler' : 'E=mc2'}, |
| 35 | 'repr': "<SimpleCookie: keebler='E=mc2'>", |
| 36 | 'output': 'Set-Cookie: keebler=E=mc2'}, |
Senthil Kumaran | 3a441c1 | 2012-04-22 09:19:04 +0800 | [diff] [blame] | 37 | |
| 38 | # Cookies with ':' character in their name. Though not mentioned in |
| 39 | # RFC, servers / browsers allow it. |
| 40 | |
| 41 | {'data': 'key:term=value:term', |
| 42 | 'dict': {'key:term' : 'value:term'}, |
| 43 | 'repr': "<SimpleCookie: key:term='value:term'>", |
| 44 | 'output': 'Set-Cookie: key:term=value:term'}, |
| 45 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 46 | ] |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 47 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 48 | for case in cases: |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 49 | C = cookies.SimpleCookie() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 50 | C.load(case['data']) |
| 51 | self.assertEqual(repr(C), case['repr']) |
| 52 | self.assertEqual(C.output(sep='\n'), case['output']) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 53 | for k, v in sorted(case['dict'].items()): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 54 | self.assertEqual(C[k].value, v) |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 55 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | def test_load(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 57 | C = cookies.SimpleCookie() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 58 | C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 59 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 60 | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
| 61 | self.assertEqual(C['Customer']['version'], '1') |
| 62 | self.assertEqual(C['Customer']['path'], '/acme') |
Moshe Zadka | 514a102 | 2000-08-19 15:57:33 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 64 | self.assertEqual(C.output(['path']), |
| 65 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 66 | self.assertEqual(C.js_output(), r""" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 67 | <script type="text/javascript"> |
| 68 | <!-- begin hiding |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 69 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 70 | // end hiding --> |
| 71 | </script> |
| 72 | """) |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 73 | self.assertEqual(C.js_output(['path']), r""" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 74 | <script type="text/javascript"> |
| 75 | <!-- begin hiding |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 76 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 77 | // end hiding --> |
| 78 | </script> |
| 79 | """) |
Moshe Zadka | 514a102 | 2000-08-19 15:57:33 +0000 | [diff] [blame] | 80 | |
R. David Murray | e05ca2a | 2010-12-28 18:54:13 +0000 | [diff] [blame] | 81 | def test_extended_encode(self): |
| 82 | # Issue 9824: some browsers don't follow the standard; we now |
| 83 | # encode , and ; to keep them from tripping up. |
| 84 | C = cookies.SimpleCookie() |
| 85 | C['val'] = "some,funky;stuff" |
| 86 | self.assertEqual(C.output(['val']), |
| 87 | 'Set-Cookie: val="some\\054funky\\073stuff"') |
| 88 | |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 89 | def test_special_attrs(self): |
| 90 | # 'expires' |
| 91 | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
| 92 | C['Customer']['expires'] = 0 |
| 93 | # can't test exact output, it always depends on current date/time |
| 94 | self.assertTrue(C.output().endswith('GMT')) |
| 95 | |
Georg Brandl | b16e38b | 2010-08-01 09:06:34 +0000 | [diff] [blame] | 96 | # loading 'expires' |
| 97 | C = cookies.SimpleCookie() |
Senthil Kumaran | 00c2ec2 | 2012-05-20 12:05:16 +0800 | [diff] [blame] | 98 | 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] | 99 | self.assertEqual(C['Customer']['expires'], |
Senthil Kumaran | 00c2ec2 | 2012-05-20 12:05:16 +0800 | [diff] [blame] | 100 | 'Wed, 01 Jan 2010 00:00:00 GMT') |
Georg Brandl | b16e38b | 2010-08-01 09:06:34 +0000 | [diff] [blame] | 101 | C = cookies.SimpleCookie() |
Senthil Kumaran | 00c2ec2 | 2012-05-20 12:05:16 +0800 | [diff] [blame] | 102 | 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] | 103 | self.assertEqual(C['Customer']['expires'], |
Senthil Kumaran | 00c2ec2 | 2012-05-20 12:05:16 +0800 | [diff] [blame] | 104 | 'Wed, 01 Jan 98 00:00:00 GMT') |
Georg Brandl | b16e38b | 2010-08-01 09:06:34 +0000 | [diff] [blame] | 105 | |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 106 | # 'max-age' |
| 107 | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
| 108 | C['Customer']['max-age'] = 10 |
| 109 | self.assertEqual(C.output(), |
| 110 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10') |
| 111 | |
R David Murray | cd0f74b | 2013-08-25 11:09:02 -0400 | [diff] [blame] | 112 | def test_set_secure_httponly_attrs(self): |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 113 | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
| 114 | C['Customer']['secure'] = True |
| 115 | C['Customer']['httponly'] = True |
| 116 | self.assertEqual(C.output(), |
| 117 | 'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure') |
| 118 | |
R David Murray | cd0f74b | 2013-08-25 11:09:02 -0400 | [diff] [blame] | 119 | def test_secure_httponly_false_if_not_present(self): |
| 120 | C = cookies.SimpleCookie() |
| 121 | C.load('eggs=scrambled; Path=/bacon') |
| 122 | self.assertFalse(C['eggs']['httponly']) |
| 123 | self.assertFalse(C['eggs']['secure']) |
| 124 | |
| 125 | def test_secure_httponly_true_if_present(self): |
| 126 | # Issue 16611 |
| 127 | C = cookies.SimpleCookie() |
| 128 | C.load('eggs=scrambled; httponly; secure; Path=/bacon') |
| 129 | self.assertTrue(C['eggs']['httponly']) |
| 130 | self.assertTrue(C['eggs']['secure']) |
| 131 | |
| 132 | def test_secure_httponly_true_if_have_value(self): |
| 133 | # This isn't really valid, but demonstrates what the current code |
| 134 | # is expected to do in this case. |
| 135 | C = cookies.SimpleCookie() |
| 136 | C.load('eggs=scrambled; httponly=foo; secure=bar; Path=/bacon') |
| 137 | self.assertTrue(C['eggs']['httponly']) |
| 138 | self.assertTrue(C['eggs']['secure']) |
| 139 | # Here is what it actually does; don't depend on this behavior. These |
| 140 | # checks are testing backward compatibility for issue 16611. |
| 141 | self.assertEqual(C['eggs']['httponly'], 'foo') |
| 142 | self.assertEqual(C['eggs']['secure'], 'bar') |
| 143 | |
| 144 | def test_bad_attrs(self): |
| 145 | # issue 16611: make sure we don't break backward compatibility. |
| 146 | C = cookies.SimpleCookie() |
| 147 | C.load('cookie=with; invalid; version; second=cookie;') |
| 148 | self.assertEqual(C.output(), |
| 149 | 'Set-Cookie: cookie=with\r\nSet-Cookie: second=cookie') |
| 150 | |
| 151 | def test_extra_spaces(self): |
| 152 | C = cookies.SimpleCookie() |
| 153 | C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ') |
| 154 | self.assertEqual(C.output(), |
| 155 | 'Set-Cookie: eggs=scrambled; Path=bar; secure\r\nSet-Cookie: foo=foo') |
| 156 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 157 | def test_quoted_meta(self): |
| 158 | # Try cookie with quoted meta-data |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 159 | C = cookies.SimpleCookie() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 160 | C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') |
| 161 | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
| 162 | self.assertEqual(C['Customer']['version'], '1') |
| 163 | self.assertEqual(C['Customer']['path'], '/acme') |
Tim Peters | eb26f95 | 2001-04-06 21:20:58 +0000 | [diff] [blame] | 164 | |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 165 | self.assertEqual(C.output(['path']), |
| 166 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
| 167 | self.assertEqual(C.js_output(), r""" |
| 168 | <script type="text/javascript"> |
| 169 | <!-- begin hiding |
| 170 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
| 171 | // end hiding --> |
| 172 | </script> |
| 173 | """) |
| 174 | self.assertEqual(C.js_output(['path']), r""" |
| 175 | <script type="text/javascript"> |
| 176 | <!-- begin hiding |
| 177 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
| 178 | // end hiding --> |
| 179 | </script> |
| 180 | """) |
| 181 | |
| 182 | class MorselTests(unittest.TestCase): |
| 183 | """Tests for the Morsel object.""" |
| 184 | |
| 185 | def test_reserved_keys(self): |
| 186 | M = cookies.Morsel() |
| 187 | # tests valid and invalid reserved keys for Morsels |
| 188 | for i in M._reserved: |
| 189 | # Test that all valid keys are reported as reserved and set them |
| 190 | self.assertTrue(M.isReservedKey(i)) |
| 191 | M[i] = '%s_value' % i |
| 192 | for i in M._reserved: |
| 193 | # Test that valid key values come out fine |
| 194 | self.assertEqual(M[i], '%s_value' % i) |
| 195 | for i in "the holy hand grenade".split(): |
| 196 | # Test that invalid keys raise CookieError |
| 197 | self.assertRaises(cookies.CookieError, |
| 198 | M.__setitem__, i, '%s_value' % i) |
| 199 | |
| 200 | def test_setter(self): |
| 201 | M = cookies.Morsel() |
| 202 | # tests the .set method to set keys and their values |
| 203 | for i in M._reserved: |
| 204 | # Makes sure that all reserved keys can't be set this way |
| 205 | self.assertRaises(cookies.CookieError, |
| 206 | M.set, i, '%s_value' % i, '%s_value' % i) |
| 207 | for i in "thou cast _the- !holy! ^hand| +*grenade~".split(): |
| 208 | # Try typical use case. Setting decent values. |
| 209 | # Check output and js_output. |
| 210 | M['path'] = '/foo' # Try a reserved key as well |
| 211 | M.set(i, "%s_val" % i, "%s_coded_val" % i) |
| 212 | self.assertEqual( |
| 213 | M.output(), |
| 214 | "Set-Cookie: %s=%s; Path=/foo" % (i, "%s_coded_val" % i)) |
| 215 | expected_js_output = """ |
| 216 | <script type="text/javascript"> |
| 217 | <!-- begin hiding |
| 218 | document.cookie = "%s=%s; Path=/foo"; |
| 219 | // end hiding --> |
| 220 | </script> |
| 221 | """ % (i, "%s_coded_val" % i) |
| 222 | self.assertEqual(M.js_output(), expected_js_output) |
| 223 | for i in ["foo bar", "foo@bar"]: |
| 224 | # Try some illegal characters |
| 225 | self.assertRaises(cookies.CookieError, |
| 226 | M.set, i, '%s_value' % i, '%s_value' % i) |
| 227 | |
| 228 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 229 | def test_main(): |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 230 | run_unittest(CookieTests, MorselTests) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 231 | run_doctest(cookies) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 232 | |
| 233 | if __name__ == '__main__': |
| 234 | test_main() |