Commit 8fe88081 authored by Jens Vagelpohl's avatar Jens Vagelpohl

- LP #142226: Added an extra keyword argument to the HTTPResponse

  setCookie method to suppress enclosing the cookie value field
  in double quotes.
parent daa96f11
...@@ -11,7 +11,12 @@ http://docs.zope.org/zope2/releases/. ...@@ -11,7 +11,12 @@ http://docs.zope.org/zope2/releases/.
Bugs Fixed Bugs Fixed
++++++++++ ++++++++++
Features Added
++++++++++++++
- LP #142226: Added an extra keyword argument to the HTTPResponse
setCookie method to suppress enclosing the cookie value field
in double quotes.
2.12.6 (2010-05-17) 2.12.6 (2010-05-17)
------------------- -------------------
......
...@@ -110,7 +110,7 @@ class Response: ...@@ -110,7 +110,7 @@ class Response:
''' '''
def setCookie(name,value,**kw): def setCookie(name, value, quoted=True, **kw):
''' '''
Set an HTTP cookie on the browser Set an HTTP cookie on the browser
...@@ -120,6 +120,10 @@ class Response: ...@@ -120,6 +120,10 @@ class Response:
"value". This overwrites any previously set value for the "value". This overwrites any previously set value for the
cookie in the Response object. cookie in the Response object.
By default, the cookie value will be enclosed in double quotes.
To suppress the double quotes you can pass the "quoted" argument
with a False value such as False or 0.
Permission -- Always available Permission -- Always available
''' '''
......
...@@ -564,7 +564,7 @@ class HTTPResponse(BaseResponse): ...@@ -564,7 +564,7 @@ class HTTPResponse(BaseResponse):
d['expires'] = 'Wed, 31-Dec-97 23:59:59 GMT' d['expires'] = 'Wed, 31-Dec-97 23:59:59 GMT'
apply(HTTPResponse.setCookie, (self, name, 'deleted'), d) apply(HTTPResponse.setCookie, (self, name, 'deleted'), d)
def setCookie(self,name,value,**kw): def setCookie(self, name, value, quoted=True, **kw):
'''\ '''\
Set an HTTP cookie on the browser Set an HTTP cookie on the browser
...@@ -584,6 +584,7 @@ class HTTPResponse(BaseResponse): ...@@ -584,6 +584,7 @@ class HTTPResponse(BaseResponse):
for k, v in kw.items(): for k, v in kw.items():
cookie[k] = v cookie[k] = v
cookie['value'] = value cookie['value'] = value
cookie['quoted'] = quoted
def appendHeader(self, name, value, delimiter=","): def appendHeader(self, name, value, delimiter=","):
'''\ '''\
...@@ -848,7 +849,10 @@ class HTTPResponse(BaseResponse): ...@@ -848,7 +849,10 @@ class HTTPResponse(BaseResponse):
# quoted cookie attr values, so only the value part # quoted cookie attr values, so only the value part
# of name=value pairs may be quoted. # of name=value pairs may be quoted.
if attrs.get('quoted', True):
cookie = 'Set-Cookie: %s="%s"' % (name, quote(attrs['value'])) cookie = 'Set-Cookie: %s="%s"' % (name, quote(attrs['value']))
else:
cookie = 'Set-Cookie: %s=%s' % (name, quote(attrs['value']))
for name, v in attrs.items(): for name, v in attrs.items():
name = name.lower() name = name.lower()
if name == 'expires': if name == 'expires':
......
...@@ -34,8 +34,9 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -34,8 +34,9 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar') response.setCookie('foo', 'bar')
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 1) self.assertEqual(len(cookie), 2)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list() cookies = response._cookie_list()
self.assertEqual(len(cookies), 1) self.assertEqual(len(cookies), 1)
...@@ -49,6 +50,7 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -49,6 +50,7 @@ class HTTPResponseTests(unittest.TestCase):
self.failUnless(cookie) self.failUnless(cookie)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('expires'), EXPIRES) self.assertEqual(cookie.get('expires'), EXPIRES)
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list() cookies = response._cookie_list()
self.assertEqual(len(cookies), 1) self.assertEqual(len(cookies), 1)
...@@ -59,9 +61,10 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -59,9 +61,10 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar', domain='example.com') response.setCookie('foo', 'bar', domain='example.com')
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2) self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('domain'), 'example.com') self.assertEqual(cookie.get('domain'), 'example.com')
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list() cookies = response._cookie_list()
self.assertEqual(len(cookies), 1) self.assertEqual(len(cookies), 1)
...@@ -72,9 +75,10 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -72,9 +75,10 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar', path='/') response.setCookie('foo', 'bar', path='/')
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2) self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('path'), '/') self.assertEqual(cookie.get('path'), '/')
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list() cookies = response._cookie_list()
self.assertEqual(len(cookies), 1) self.assertEqual(len(cookies), 1)
...@@ -84,9 +88,10 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -84,9 +88,10 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar', comment='COMMENT') response.setCookie('foo', 'bar', comment='COMMENT')
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2) self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('comment'), 'COMMENT') self.assertEqual(cookie.get('comment'), 'COMMENT')
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list() cookies = response._cookie_list()
self.assertEqual(len(cookies), 1) self.assertEqual(len(cookies), 1)
...@@ -96,9 +101,10 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -96,9 +101,10 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar', secure='SECURE') response.setCookie('foo', 'bar', secure='SECURE')
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2) self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('secure'), 'SECURE') self.assertEqual(cookie.get('secure'), 'SECURE')
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list() cookies = response._cookie_list()
self.assertEqual(len(cookies), 1) self.assertEqual(len(cookies), 1)
...@@ -108,9 +114,10 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -108,9 +114,10 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar', secure='') response.setCookie('foo', 'bar', secure='')
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2) self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('secure'), '') self.assertEqual(cookie.get('secure'), '')
self.assertEqual(cookie.get('quoted'), True)
cookies = response._cookie_list() cookies = response._cookie_list()
self.assertEqual(len(cookies), 1) self.assertEqual(len(cookies), 1)
...@@ -129,9 +136,10 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -129,9 +136,10 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar', http_only=True) response.setCookie('foo', 'bar', http_only=True)
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2) self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('http_only'), True) self.assertEqual(cookie.get('http_only'), True)
self.assertEqual(cookie.get('quoted'), True)
cookie_list = response._cookie_list() cookie_list = response._cookie_list()
self.assertEqual(len(cookie_list), 1) self.assertEqual(len(cookie_list), 1)
...@@ -141,14 +149,27 @@ class HTTPResponseTests(unittest.TestCase): ...@@ -141,14 +149,27 @@ class HTTPResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setCookie('foo', 'bar', http_only=False) response.setCookie('foo', 'bar', http_only=False)
cookie = response.cookies.get('foo', None) cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2) self.assertEqual(len(cookie), 3)
self.assertEqual(cookie.get('value'), 'bar') self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('http_only'), False) self.assertEqual(cookie.get('http_only'), False)
self.assertEqual(cookie.get('quoted'), True)
cookie_list = response._cookie_list() cookie_list = response._cookie_list()
self.assertEqual(len(cookie_list), 1) self.assertEqual(len(cookie_list), 1)
self.assertEqual(cookie_list[0], 'Set-Cookie: foo="bar"') self.assertEqual(cookie_list[0], 'Set-Cookie: foo="bar"')
def test_setCookie_unquoted(self):
response = self._makeOne()
response.setCookie('foo', 'bar', quoted=False)
cookie = response.cookies.get('foo', None)
self.assertEqual(len(cookie), 2)
self.assertEqual(cookie.get('value'), 'bar')
self.assertEqual(cookie.get('quoted'), False)
cookie_list = response._cookie_list()
self.assertEqual(len(cookie_list), 1)
self.assertEqual(cookie_list[0], 'Set-Cookie: foo=bar')
def test_expireCookie1160(self): def test_expireCookie1160(self):
# Verify that the cookie is expired even if an expires kw arg is passed # Verify that the cookie is expired even if an expires kw arg is passed
# http://zope.org/Collectors/Zope/1160 # http://zope.org/Collectors/Zope/1160
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment