Convert test_cookie to unittest.

This commit is contained in:
Georg Brandl 2006-10-29 19:51:16 +00:00
parent bd0fb14e51
commit a962eb32d9
2 changed files with 65 additions and 65 deletions

View File

@ -1,32 +0,0 @@
test_cookie
<SimpleCookie: chips='ahoy' vienna='finger'>
Set-Cookie: chips=ahoy
Set-Cookie: vienna=finger
chips 'ahoy' 'ahoy'
Set-Cookie: chips=ahoy
vienna 'finger' 'finger'
Set-Cookie: vienna=finger
<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\n;'>
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;"
<SimpleCookie: keebler='E=mc2'>
Set-Cookie: keebler=E=mc2
keebler 'E=mc2' 'E=mc2'
Set-Cookie: keebler=E=mc2
Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme
<script type="text/javascript">
<!-- begin hiding
document.cookie = "Customer="WILE_E_COYOTE"; Path=/acme; Version=1";
// end hiding -->
</script>
<script type="text/javascript">
<!-- begin hiding
document.cookie = "Customer="WILE_E_COYOTE"; Path=/acme";
// end hiding -->
</script>
If anything blows up after this line, it's from Cookie's doctest.

View File

@ -1,6 +1,7 @@
# Simple test suite for Cookie.py # 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 Cookie
import warnings import warnings
@ -8,43 +9,74 @@ warnings.filterwarnings("ignore",
".* class is insecure.*", ".* class is insecure.*",
DeprecationWarning) DeprecationWarning)
class CookieTests(unittest.TestCase):
# Currently this only tests SimpleCookie # Currently this only tests SimpleCookie
def test_basic(self):
cases = [ cases = [
('chips=ahoy; vienna=finger', {'chips':'ahoy', 'vienna':'finger'}), { 'data': 'chips=ahoy; vienna=finger',
('keebler="E=mc2; L=\\"Loves\\"; fudge=\\012;"', 'dict': {'chips':'ahoy', 'vienna':'finger'},
{'keebler' : 'E=mc2; L="Loves"; fudge=\012;'}), '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 # Check illegal cookies that have an '=' char in an unquoted value
('keebler=E=mc2', {'keebler' : 'E=mc2'}) { 'data': 'keebler=E=mc2',
'dict': {'keebler' : 'E=mc2'},
'repr': "<SimpleCookie: keebler='E=mc2'>",
'output': 'Set-Cookie: keebler=E=mc2',
}
] ]
for data, dict in cases: for case in cases:
C = Cookie.SimpleCookie() ; C.load(data) C = Cookie.SimpleCookie()
print repr(C) C.load(case['data'])
print C.output(sep='\n') self.assertEqual(repr(C), case['repr'])
for k, v in sorted(dict.iteritems()): self.assertEqual(C.output(sep='\n'), case['output'])
print ' ', k, repr( C[k].value ), repr(v) for k, v in sorted(case['dict'].iteritems()):
verify(C[k].value == v) self.assertEqual(C[k].value, v)
print C[k]
def test_load(self):
C = Cookie.SimpleCookie() C = Cookie.SimpleCookie()
C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme')
verify(C['Customer'].value == 'WILE_E_COYOTE') self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
verify(C['Customer']['version'] == '1') self.assertEqual(C['Customer']['version'], '1')
verify(C['Customer']['path'] == '/acme') self.assertEqual(C['Customer']['path'], '/acme')
print C.output(['path']) self.assertEqual(C.output(['path']),
print C.js_output() 'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
print C.js_output(['path']) self.assertEqual(C.js_output(), """
<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']), """
<script type="text/javascript">
<!-- begin hiding
document.cookie = "Customer="WILE_E_COYOTE"; Path=/acme";
// end hiding -->
</script>
""")
def test_quoted_meta(self):
# Try cookie with quoted meta-data # Try cookie with quoted meta-data
C = Cookie.SimpleCookie() C = Cookie.SimpleCookie()
C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') C.load('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
verify(C['Customer'].value == 'WILE_E_COYOTE') self.assertEqual(C['Customer'].value, 'WILE_E_COYOTE')
verify(C['Customer']['version'] == '1') self.assertEqual(C['Customer']['version'], '1')
verify(C['Customer']['path'] == '/acme') self.assertEqual(C['Customer']['path'], '/acme')
print "If anything blows up after this line, it's from Cookie's doctest." def test_main():
run_unittest(CookieTests)
run_doctest(Cookie) run_doctest(Cookie)
if __name__ == '__main__':
test_main()