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'}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 37 | ] |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 38 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 39 | for case in cases: |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 40 | C = cookies.SimpleCookie() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 41 | C.load(case['data']) |
| 42 | self.assertEqual(repr(C), case['repr']) |
| 43 | self.assertEqual(C.output(sep='\n'), case['output']) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 44 | for k, v in sorted(case['dict'].items()): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 45 | self.assertEqual(C[k].value, v) |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 46 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 47 | def test_load(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 48 | C = cookies.SimpleCookie() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 49 | C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') |
Andrew M. Kuchling | cdec8c7 | 2000-08-19 15:21:12 +0000 | [diff] [blame] | 50 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 51 | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
| 52 | self.assertEqual(C['Customer']['version'], '1') |
| 53 | self.assertEqual(C['Customer']['path'], '/acme') |
Moshe Zadka | 514a102 | 2000-08-19 15:57:33 +0000 | [diff] [blame] | 54 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 55 | self.assertEqual(C.output(['path']), |
| 56 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 57 | self.assertEqual(C.js_output(), r""" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 58 | <script type="text/javascript"> |
| 59 | <!-- begin hiding |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 60 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | // end hiding --> |
| 62 | </script> |
| 63 | """) |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 64 | self.assertEqual(C.js_output(['path']), r""" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 65 | <script type="text/javascript"> |
| 66 | <!-- begin hiding |
Senthil Kumaran | 3e2ea79 | 2009-04-02 03:02:03 +0000 | [diff] [blame] | 67 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 68 | // end hiding --> |
| 69 | </script> |
| 70 | """) |
Moshe Zadka | 514a102 | 2000-08-19 15:57:33 +0000 | [diff] [blame] | 71 | |
R. David Murray | e05ca2a | 2010-12-28 18:54:13 +0000 | [diff] [blame] | 72 | def test_extended_encode(self): |
| 73 | # Issue 9824: some browsers don't follow the standard; we now |
| 74 | # encode , and ; to keep them from tripping up. |
| 75 | C = cookies.SimpleCookie() |
| 76 | C['val'] = "some,funky;stuff" |
| 77 | self.assertEqual(C.output(['val']), |
| 78 | 'Set-Cookie: val="some\\054funky\\073stuff"') |
| 79 | |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 80 | def test_special_attrs(self): |
| 81 | # 'expires' |
| 82 | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
| 83 | C['Customer']['expires'] = 0 |
| 84 | # can't test exact output, it always depends on current date/time |
| 85 | self.assertTrue(C.output().endswith('GMT')) |
| 86 | |
Georg Brandl | b16e38b | 2010-08-01 09:06:34 +0000 | [diff] [blame] | 87 | # loading 'expires' |
| 88 | C = cookies.SimpleCookie() |
| 89 | C.load('Customer="W"; expires=Wed, 01-Jan-2010 00:00:00 GMT') |
| 90 | self.assertEqual(C['Customer']['expires'], |
| 91 | 'Wed, 01-Jan-2010 00:00:00 GMT') |
| 92 | C = cookies.SimpleCookie() |
| 93 | C.load('Customer="W"; expires=Wed, 01-Jan-98 00:00:00 GMT') |
| 94 | self.assertEqual(C['Customer']['expires'], |
| 95 | 'Wed, 01-Jan-98 00:00:00 GMT') |
| 96 | |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 97 | # 'max-age' |
| 98 | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
| 99 | C['Customer']['max-age'] = 10 |
| 100 | self.assertEqual(C.output(), |
| 101 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10') |
| 102 | |
| 103 | # others |
| 104 | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
| 105 | C['Customer']['secure'] = True |
| 106 | C['Customer']['httponly'] = True |
| 107 | self.assertEqual(C.output(), |
| 108 | 'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure') |
| 109 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 110 | def test_quoted_meta(self): |
| 111 | # Try cookie with quoted meta-data |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 112 | C = cookies.SimpleCookie() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 113 | C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') |
| 114 | self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') |
| 115 | self.assertEqual(C['Customer']['version'], '1') |
| 116 | self.assertEqual(C['Customer']['path'], '/acme') |
Tim Peters | eb26f95 | 2001-04-06 21:20:58 +0000 | [diff] [blame] | 117 | |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 118 | self.assertEqual(C.output(['path']), |
| 119 | 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') |
| 120 | self.assertEqual(C.js_output(), r""" |
| 121 | <script type="text/javascript"> |
| 122 | <!-- begin hiding |
| 123 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1"; |
| 124 | // end hiding --> |
| 125 | </script> |
| 126 | """) |
| 127 | self.assertEqual(C.js_output(['path']), r""" |
| 128 | <script type="text/javascript"> |
| 129 | <!-- begin hiding |
| 130 | document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme"; |
| 131 | // end hiding --> |
| 132 | </script> |
| 133 | """) |
| 134 | |
| 135 | class MorselTests(unittest.TestCase): |
| 136 | """Tests for the Morsel object.""" |
| 137 | |
| 138 | def test_reserved_keys(self): |
| 139 | M = cookies.Morsel() |
| 140 | # tests valid and invalid reserved keys for Morsels |
| 141 | for i in M._reserved: |
| 142 | # Test that all valid keys are reported as reserved and set them |
| 143 | self.assertTrue(M.isReservedKey(i)) |
| 144 | M[i] = '%s_value' % i |
| 145 | for i in M._reserved: |
| 146 | # Test that valid key values come out fine |
| 147 | self.assertEqual(M[i], '%s_value' % i) |
| 148 | for i in "the holy hand grenade".split(): |
| 149 | # Test that invalid keys raise CookieError |
| 150 | self.assertRaises(cookies.CookieError, |
| 151 | M.__setitem__, i, '%s_value' % i) |
| 152 | |
| 153 | def test_setter(self): |
| 154 | M = cookies.Morsel() |
| 155 | # tests the .set method to set keys and their values |
| 156 | for i in M._reserved: |
| 157 | # Makes sure that all reserved keys can't be set this way |
| 158 | self.assertRaises(cookies.CookieError, |
| 159 | M.set, i, '%s_value' % i, '%s_value' % i) |
| 160 | for i in "thou cast _the- !holy! ^hand| +*grenade~".split(): |
| 161 | # Try typical use case. Setting decent values. |
| 162 | # Check output and js_output. |
| 163 | M['path'] = '/foo' # Try a reserved key as well |
| 164 | M.set(i, "%s_val" % i, "%s_coded_val" % i) |
| 165 | self.assertEqual( |
| 166 | M.output(), |
| 167 | "Set-Cookie: %s=%s; Path=/foo" % (i, "%s_coded_val" % i)) |
| 168 | expected_js_output = """ |
| 169 | <script type="text/javascript"> |
| 170 | <!-- begin hiding |
| 171 | document.cookie = "%s=%s; Path=/foo"; |
| 172 | // end hiding --> |
| 173 | </script> |
| 174 | """ % (i, "%s_coded_val" % i) |
| 175 | self.assertEqual(M.js_output(), expected_js_output) |
| 176 | for i in ["foo bar", "foo@bar"]: |
| 177 | # Try some illegal characters |
| 178 | self.assertRaises(cookies.CookieError, |
| 179 | M.set, i, '%s_value' % i, '%s_value' % i) |
| 180 | |
| 181 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 182 | def test_main(): |
Georg Brandl | 76e155a | 2010-07-31 21:04:00 +0000 | [diff] [blame] | 183 | run_unittest(CookieTests, MorselTests) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 184 | run_doctest(cookies) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 185 | |
| 186 | if __name__ == '__main__': |
| 187 | test_main() |