From a962eb32d98a1dae1f0c31623832a48725f3d6e0 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 29 Oct 2006 19:51:16 +0000 Subject: [PATCH] Convert test_cookie to unittest. --- Lib/test/output/test_cookie | 32 ------------ Lib/test/test_cookie.py | 98 ++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 65 deletions(-) delete mode 100644 Lib/test/output/test_cookie diff --git a/Lib/test/output/test_cookie b/Lib/test/output/test_cookie deleted file mode 100644 index 95c73283bc2..00000000000 --- a/Lib/test/output/test_cookie +++ /dev/null @@ -1,32 +0,0 @@ -test_cookie - -Set-Cookie: chips=ahoy -Set-Cookie: vienna=finger - chips 'ahoy' 'ahoy' -Set-Cookie: chips=ahoy - vienna 'finger' 'finger' -Set-Cookie: vienna=finger - -Set-Cookie: keebler="E=mc2; L=\"Loves\"; fudge=\012;" - keebler 'E=mc2; L="Loves"; fudge=\n;' 'E=mc2; L="Loves"; fudge=\n;' -Set-Cookie: keebler="E=mc2; L=\"Loves\"; fudge=\012;" - -Set-Cookie: keebler=E=mc2 - keebler 'E=mc2' 'E=mc2' -Set-Cookie: keebler=E=mc2 -Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme - - - - - - -If anything blows up after this line, it's from Cookie's doctest. diff --git a/Lib/test/test_cookie.py b/Lib/test/test_cookie.py index c20beeeb1e5..e7c0cf1cccb 100644 --- a/Lib/test/test_cookie.py +++ b/Lib/test/test_cookie.py @@ -1,6 +1,7 @@ # Simple test suite for Cookie.py -from test.test_support import verify, verbose, run_doctest +from test.test_support import run_unittest, run_doctest +import unittest import Cookie import warnings @@ -8,43 +9,74 @@ warnings.filterwarnings("ignore", ".* class is insecure.*", DeprecationWarning) -# Currently this only tests SimpleCookie +class CookieTests(unittest.TestCase): + # Currently this only tests SimpleCookie + def test_basic(self): + cases = [ + { 'data': 'chips=ahoy; vienna=finger', + 'dict': {'chips':'ahoy', 'vienna':'finger'}, + 'repr': "", + 'output': 'Set-Cookie: chips=ahoy\nSet-Cookie: vienna=finger', + }, -cases = [ - ('chips=ahoy; vienna=finger', {'chips':'ahoy', 'vienna':'finger'}), - ('keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', - {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}), + { 'data': 'keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', + 'dict': {'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}, + 'repr': '''''', + 'output': 'Set-Cookie: keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', + }, - # Check illegal cookies that have an '=' char in an unquoted value - ('keebler=E=mc2', {'keebler' : 'E=mc2'}) - ] + # Check illegal cookies that have an '=' char in an unquoted value + { 'data': 'keebler=E=mc2', + 'dict': {'keebler' : 'E=mc2'}, + 'repr': "", + 'output': 'Set-Cookie: keebler=E=mc2', + } + ] -for data, dict in cases: - C = Cookie.SimpleCookie() ; C.load(data) - print repr(C) - print C.output(sep='\n') - for k, v in sorted(dict.iteritems()): - print ' ', k, repr( C[k].value ), repr(v) - verify(C[k].value == v) - print C[k] + for case in cases: + C = Cookie.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'].iteritems()): + self.assertEqual(C[k].value, v) -C = Cookie.SimpleCookie() -C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') + def test_load(self): + C = Cookie.SimpleCookie() + C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') -verify(C['Customer'].value == 'WILE_E_COYOTE') -verify(C['Customer']['version'] == '1') -verify(C['Customer']['path'] == '/acme') + self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE') + self.assertEqual(C['Customer']['version'], '1') + self.assertEqual(C['Customer']['path'], '/acme') -print C.output(['path']) -print C.js_output() -print C.js_output(['path']) + self.assertEqual(C.output(['path']), + 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme') + self.assertEqual(C.js_output(), """ + + """) + self.assertEqual(C.js_output(['path']), """ + + """) -# Try cookie with quoted meta-data -C = Cookie.SimpleCookie() -C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') -verify(C['Customer'].value == 'WILE_E_COYOTE') -verify(C['Customer']['version'] == '1') -verify(C['Customer']['path'] == '/acme') + def test_quoted_meta(self): + # Try cookie with quoted meta-data + C = Cookie.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') -print "If anything blows up after this line, it's from Cookie's doctest." -run_doctest(Cookie) +def test_main(): + run_unittest(CookieTests) + run_doctest(Cookie) + +if __name__ == '__main__': + test_main()