mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 02:15:10 +00:00 
			
		
		
		
	 cea7e55998
			
		
	
	
		cea7e55998
		
	
	
	
	
		
			
			svn+ssh://svn.python.org/python/branches/py3k ........ r83393 | georg.brandl | 2010-08-01 10:35:29 +0200 (So, 01 Aug 2010) | 1 line #1690103: fix initial namespace for code run with trace.main(). ........ r83396 | georg.brandl | 2010-08-01 10:52:32 +0200 (So, 01 Aug 2010) | 1 line #4810: document "--" option separator in timeit help. ........ r83398 | georg.brandl | 2010-08-01 11:06:34 +0200 (So, 01 Aug 2010) | 1 line #8826: the "expires" attribute value is a date string with spaces, but apparently not all user-agents put it in quotes. Handle that as a special case. ........ r83404 | georg.brandl | 2010-08-01 16:25:22 +0200 (So, 01 Aug 2010) | 1 line #6439: fix argument type for PySys_SetArgvEx() and Py_SetProgramName() in Demo/embed code. ........ r83405 | georg.brandl | 2010-08-01 16:38:17 +0200 (So, 01 Aug 2010) | 1 line #4943: do not try to include drive letters (and colons) when looking for a probably module name. ........ r83408 | georg.brandl | 2010-08-01 17:30:56 +0200 (So, 01 Aug 2010) | 1 line #5551: symbolic links never can be mount points. Fixes the fix for #1713. ........
		
			
				
	
	
		
			111 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
	
		
			4.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| # Simple test suite for http/cookies.py
 | |
| 
 | |
| from test.support import run_unittest, run_doctest
 | |
| import unittest
 | |
| from http import cookies
 | |
| 
 | |
| import warnings
 | |
| warnings.filterwarnings("ignore",
 | |
|                         ".* class is insecure.*",
 | |
|                         DeprecationWarning)
 | |
| 
 | |
| class CookieTests(unittest.TestCase):
 | |
|     def test_basic(self):
 | |
|         cases = [
 | |
|             { 'data': 'chips=ahoy; vienna=finger',
 | |
|               'dict': {'chips':'ahoy', 'vienna':'finger'},
 | |
|               'repr': "<SimpleCookie: chips='ahoy' vienna='finger'>",
 | |
|               'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger',
 | |
|             },
 | |
| 
 | |
|             { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
 | |
|               'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'},
 | |
|               'repr': '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\n;'>''',
 | |
|               'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"',
 | |
|             },
 | |
| 
 | |
|             # Check illegal cookies that have an '=' char in an unquoted value
 | |
|             { 'data': 'keebler=E=mc2',
 | |
|               'dict': {'keebler' : 'E=mc2'},
 | |
|               'repr': "<SimpleCookie: keebler='E=mc2'>",
 | |
|               'output': 'Set-Cookie: keebler=E=mc2',
 | |
|             }
 | |
|         ]
 | |
| 
 | |
|         for case in cases:
 | |
|             C = cookies.SimpleCookie()
 | |
|             C.load(case['data'])
 | |
|             self.assertEqual(repr(C), case['repr'])
 | |
|             self.assertEqual(C.output(sep='\n'), case['output'])
 | |
|             for k, v in sorted(case['dict'].items()):
 | |
|                 self.assertEqual(C[k].value, v)
 | |
| 
 | |
|     def test_load(self):
 | |
|         C = cookies.SimpleCookie()
 | |
|         C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
 | |
| 
 | |
|         self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
 | |
|         self.assertEqual(C['Customer']['version'], '1')
 | |
|         self.assertEqual(C['Customer']['path'], '/acme')
 | |
| 
 | |
|         self.assertEqual(C.output(['path']),
 | |
|             'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
 | |
|         self.assertEqual(C.js_output(), r"""
 | |
|         <script type="text/javascript">
 | |
|         <!-- begin hiding
 | |
|         document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1";
 | |
|         // end hiding -->
 | |
|         </script>
 | |
|         """)
 | |
|         self.assertEqual(C.js_output(['path']), r"""
 | |
|         <script type="text/javascript">
 | |
|         <!-- begin hiding
 | |
|         document.cookie = "Customer=\"WILE_E_COYOTE\"; Path=/acme";
 | |
|         // end hiding -->
 | |
|         </script>
 | |
|         """)
 | |
| 
 | |
|     def test_special_attrs(self):
 | |
|         # 'expires'
 | |
|         C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
 | |
|         C['Customer']['expires'] = 0
 | |
|         # can't test exact output, it always depends on current date/time
 | |
|         self.assertTrue(C.output().endswith('GMT'))
 | |
| 
 | |
|         # loading 'expires'
 | |
|         C = cookies.SimpleCookie()
 | |
|         C.load('Customer="W"; expires=Wed, 01-Jan-2010 00:00:00 GMT')
 | |
|         self.assertEqual(C['Customer']['expires'],
 | |
|                          'Wed, 01-Jan-2010 00:00:00 GMT')
 | |
|         C = cookies.SimpleCookie()
 | |
|         C.load('Customer="W"; expires=Wed, 01-Jan-98 00:00:00 GMT')
 | |
|         self.assertEqual(C['Customer']['expires'],
 | |
|                          'Wed, 01-Jan-98 00:00:00 GMT')
 | |
| 
 | |
|         # 'max-age'
 | |
|         C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
 | |
|         C['Customer']['max-age'] = 10
 | |
|         self.assertEqual(C.output(),
 | |
|                          'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10')
 | |
| 
 | |
|         # others
 | |
|         C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
 | |
|         C['Customer']['secure'] = True
 | |
|         C['Customer']['httponly'] = True
 | |
|         self.assertEqual(C.output(),
 | |
|             'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
 | |
| 
 | |
|     def test_quoted_meta(self):
 | |
|         # Try cookie with quoted meta-data
 | |
|         C = cookies.SimpleCookie()
 | |
|         C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
 | |
|         self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
 | |
|         self.assertEqual(C['Customer']['version'], '1')
 | |
|         self.assertEqual(C['Customer']['path'], '/acme')
 | |
| 
 | |
| def test_main():
 | |
|     run_unittest(CookieTests)
 | |
|     run_doctest(cookies)
 | |
| 
 | |
| if __name__ == '__main__':
 | |
|     test_main()
 |