[Zope-Checkins] SVN: Zope/trunk/ - LP #142226: Added an extra keyword argument to the HTTPResponse

Jens Vagelpohl jens at dataflake.org
Wed May 19 05:17:40 EDT 2010


Log message for revision 112511:
  - LP #142226: Added an extra keyword argument to the HTTPResponse
    setCookie method to suppress enclosing the cookie value field
    in double quotes.
  

Changed:
  U   Zope/trunk/doc/CHANGES.rst
  U   Zope/trunk/src/Products/OFSP/help/Response.py
  U   Zope/trunk/src/ZPublisher/HTTPResponse.py
  U   Zope/trunk/src/ZPublisher/tests/testHTTPResponse.py

-=-
Modified: Zope/trunk/doc/CHANGES.rst
===================================================================
--- Zope/trunk/doc/CHANGES.rst	2010-05-19 07:32:33 UTC (rev 112510)
+++ Zope/trunk/doc/CHANGES.rst	2010-05-19 09:17:40 UTC (rev 112511)
@@ -99,6 +99,10 @@
 Features Added
 ++++++++++++++
 
+- LP #142226: Added an extra keyword argument to the HTTPResponse 
+  setCookie method to suppress enclosing the cookie value field 
+  in double quotes.
+
 - LP #142464:  Make undo log easier to read.  Thanks to Toby Dickinson
   for the patch.
 

Modified: Zope/trunk/src/Products/OFSP/help/Response.py
===================================================================
--- Zope/trunk/src/Products/OFSP/help/Response.py	2010-05-19 07:32:33 UTC (rev 112510)
+++ Zope/trunk/src/Products/OFSP/help/Response.py	2010-05-19 09:17:40 UTC (rev 112511)
@@ -110,7 +110,7 @@
 
         '''
 
-    def setCookie(name,value,**kw):
+    def setCookie(name, value, quoted=True, **kw):
         '''
 
         Set an HTTP cookie on the browser
@@ -120,6 +120,10 @@
         "value". This overwrites any previously set value for the
         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
 
         '''

Modified: Zope/trunk/src/ZPublisher/HTTPResponse.py
===================================================================
--- Zope/trunk/src/ZPublisher/HTTPResponse.py	2010-05-19 07:32:33 UTC (rev 112510)
+++ Zope/trunk/src/ZPublisher/HTTPResponse.py	2010-05-19 09:17:40 UTC (rev 112511)
@@ -563,7 +563,7 @@
         d['expires'] = 'Wed, 31-Dec-97 23:59:59 GMT'
         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
 
@@ -583,6 +583,7 @@
         for k, v in kw.items():
             cookie[k] = v
         cookie['value'] = value
+        cookie['quoted'] = quoted
 
     def appendHeader(self, name, value, delimiter=","):
         '''\
@@ -843,7 +844,10 @@
             # quoted cookie attr values, so only the value part
             # of name=value pairs may be quoted.
 
-            cookie = 'Set-Cookie: %s="%s"' % (name, quote(attrs['value']))
+            if attrs.get('quoted', True):
+                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():
                 name = name.lower()
                 if name == 'expires':

Modified: Zope/trunk/src/ZPublisher/tests/testHTTPResponse.py
===================================================================
--- Zope/trunk/src/ZPublisher/tests/testHTTPResponse.py	2010-05-19 07:32:33 UTC (rev 112510)
+++ Zope/trunk/src/ZPublisher/tests/testHTTPResponse.py	2010-05-19 09:17:40 UTC (rev 112511)
@@ -34,8 +34,9 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar')
         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('quoted'), True)
 
         cookies = response._cookie_list()
         self.assertEqual(len(cookies), 1)
@@ -49,6 +50,7 @@
         self.failUnless(cookie)
         self.assertEqual(cookie.get('value'), 'bar')
         self.assertEqual(cookie.get('expires'), EXPIRES)
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookies = response._cookie_list()
         self.assertEqual(len(cookies), 1)
@@ -59,9 +61,10 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar', domain='example.com')
         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('domain'), 'example.com')
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookies = response._cookie_list()
         self.assertEqual(len(cookies), 1)
@@ -72,9 +75,10 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar', path='/')
         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('path'), '/')
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookies = response._cookie_list()
         self.assertEqual(len(cookies), 1)
@@ -84,9 +88,10 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar', comment='COMMENT')
         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('comment'), 'COMMENT')
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookies = response._cookie_list()
         self.assertEqual(len(cookies), 1)
@@ -96,9 +101,10 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar', secure='SECURE')
         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('secure'), 'SECURE')
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookies = response._cookie_list()
         self.assertEqual(len(cookies), 1)
@@ -108,9 +114,10 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar', secure='')
         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('secure'), '')
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookies = response._cookie_list()
         self.assertEqual(len(cookies), 1)
@@ -129,9 +136,10 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar', http_only=True)
         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('http_only'), True)
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookie_list = response._cookie_list()
         self.assertEqual(len(cookie_list), 1)
@@ -141,14 +149,27 @@
         response = self._makeOne()
         response.setCookie('foo', 'bar', http_only=False)
         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('http_only'), False)
+        self.assertEqual(cookie.get('quoted'), True)
 
         cookie_list = response._cookie_list()
         self.assertEqual(len(cookie_list), 1)
         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):
         # Verify that the cookie is expired even if an expires kw arg is passed
         # http://zope.org/Collectors/Zope/1160



More information about the Zope-Checkins mailing list