Commits

Sergey Schetinin  committed 77d4423

more cookie tests

  • Participants
  • Parent commits 376707d

Comments (0)

Files changed (2)

File tests/test_cookies.py

 
 def test_cookie():
     """
-        Testing several missing features of cookies.Cookie.
-            * repr version
+        Test cookie parsing, serialization and `repr`
     """
     c = cookies.Cookie() # empty cookie
     eq_(repr(c), '<Cookie: []>')
     c = cookies.Cookie('dismiss-top=6;')
     eq_(repr(c), "<Cookie: [<Morsel: dismiss-top='6'>]>")
     # more complex cookie, (also mixing commas and semicolons)
-    c = cookies.Cookie("dismiss-top=6; CP=null*, "\
-                       "PHPSESSID=0a539d42abc001cdc762809248d4beed, a=42")
+    c = cookies.Cookie('dismiss-top=6; CP=null*, '\
+                       'PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42,"')
     c_dict = dict((k,v.value) for k,v in c.items())
-    eq_(c_dict, {'a': '42',
+    eq_(c_dict, {'a': '42,',
         'CP': 'null*',
         'PHPSESSID': '0a539d42abc001cdc762809248d4beed',
         'dismiss-top': '6'
     })
     eq_(c.serialize(),
-        'CP=null*, PHPSESSID=0a539d42abc001cdc762809248d4beed, a=42, '
+        'CP=null*, PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42,", '
         'dismiss-top=6')
     # reserved keys ($xx)
     c = cookies.Cookie('dismiss-top=6; CP=null*; $version=42; a=42')
             * input value is an int, should be converted to timedelta and we should
               continue the rest of the process
     """
+    assert '"' not in cookies._quote(cookies.serialize_cookie_date(0))
     ok_(cookies.serialize_cookie_date('Tue, 04-Jan-2011 13:43:50 GMT')==\
         'Tue, 04-Jan-2011 13:43:50 GMT', 'We passed a string, should get the '
         'same one')
     )
 
 def test_ch_unquote():
-    """Inner method _ch_unquote in cookies._unquote is not tested"""
-    str_ = u'"'+u'hello world'+u'"'
-    v = cookies._unquote(str_)
-    ok_(v==u'hello world', 'Wrong output from _unquote. Expected: %r, '
-        'Got: %r' % (u'hello world', v))
-    str_ = u'hello world'
-    v = cookies._unquote(str_)
-    ok_(v==u'hello world', 'Wrong output from _unquote. Expected: %r, '
-        'Got: %r' % (u'hello world', v))
-    str_ = u'"'+u'hello world'
-    v = cookies._unquote(str_)
-    ok_(v==u'\"hello world', 'Wrong output from _unquote. Expected: %r, '
-        'Got: %r' % (u'\"hello world', v))
-    # example extracted from webob.cookies
-    ok_(cookies._unquote(r'"a\"\377"')=='a"\xff')
-
+    eq_(cookies._unquote(u'"hello world'), u'"hello world')
+    eq_(cookies._unquote(u'hello world'), u'hello world')
+    for unq, q in [
+        ('hello world', '"hello world"'),
+        # quotation mark is escaped w/ backslash
+        ('"', r'"\""'),
+        # misc byte escaped as octal
+        ('\xff', r'"\377"'),
+        # combination
+        ('a"\xff', r'"a\"\377"'),
+    ]:
+        eq_(cookies._unquote(q), unq)
+        eq_(cookies._quote(unq), q)

File webob/cookies.py

 
 _trans_noop = ''.join(chr(x) for x in xrange(256))
 
+# these chars can be in cookie value w/o causing it to be quoted
 _no_escape_special_chars = "!#$%&'*+-.^_`|~/"
 _no_escape_chars = string.ascii_letters + string.digits + _no_escape_special_chars
-#_no_escape_chars = string.ascii_letters + string.digits + _legal_special_chars
+# these chars never need to be quoted
 _escape_noop_chars = _no_escape_chars+':, '
+# this is a map used to escape the values
 _escape_map = dict((chr(i), '\\%03o' % i) for i in xrange(256))
 _escape_map.update(zip(_escape_noop_chars, _escape_noop_chars))
 _escape_map['"'] = '\\"'
     if needs_quoting(v):
         return '"' + ''.join(map(_escape_char, v)) + '"'
     return v
-
-
-
-#print _quote(serialize_cookie_date(0))
-
-#assert _quote('a"\xff') == r'"a\"\377"'
-#assert _unquote(r'"a\"\377"') == 'a"\xff'
-
-#print repr(Cookie('foo=bar'))
-# c = Cookie('bad_cookie=; expires="... GMT"; Max-Age=0; Path=/')
-# print c
-#print c['bad_cookie'].items()