[Zope-Checkins] SVN: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/ Move finalization logic out of HTTPResponse.listHeaders.

Tres Seaver tseaver at palladion.com
Sat May 29 00:24:49 EDT 2010


Log message for revision 112833:
  Move finalization logic out of HTTPResponse.listHeaders.

Changed:
  U   Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py
  U   Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py

-=-
Modified: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py
===================================================================
--- Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py	2010-05-29 04:24:46 UTC (rev 112832)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py	2010-05-29 04:24:48 UTC (rev 112833)
@@ -914,16 +914,21 @@
 
         return cookie_list
 
-    def listHeaders(self):
-        """ Return a list of (key, value) pairs for our headers.
-
-        o Do appropriate case normalization.
+    def finalize(self):
+        """ Set headers required by various parts of protocol.
         """
         body = self.body
         if (not 'content-length' in self.headers and 
             not 'transfer-encoding' in self.headers):
             self.setHeader('content-length', len(body))
+        return "%d %s" % (self.status, self.errmsg), self.listHeaders()
 
+    def listHeaders(self):
+        """ Return a list of (key, value) pairs for our headers.
+
+        o Do appropriate case normalization.
+        """
+
         result = [
           ('X-Powered-By', 'Zope (www.zope.org), Python (www.python.org)')
         ]
@@ -950,15 +955,15 @@
         if self._wrote:
             return ''       # Streaming output was used.
 
-        headers = self.headers
+        status, headers = self.finalize()
         body = self.body
 
         chunks = []
 
         # status header must come first.
-        chunks.append("Status: %d %s" % (self.status, self.errmsg))
+        chunks.append("Status: %s" % status)
 
-        for key, value in self.listHeaders():
+        for key, value in headers:
             chunks.append("%s: %s" % (key, value))
         # RFC 2616 mandates empty line between headers and payload
         chunks.append('')

Modified: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py
===================================================================
--- Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py	2010-05-29 04:24:46 UTC (rev 112832)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py	2010-05-29 04:24:48 UTC (rev 112833)
@@ -929,13 +929,67 @@
         else:
             self.fail("Didn't raise Unauthorized")
 
+    def test_finalize_empty(self):
+        response = self._makeOne()
+        status, headers = response.finalize()
+        self.assertEqual(status, '200 OK')
+        self.assertEqual(headers,
+                         [('X-Powered-By', 'Zope (www.zope.org), '
+                                           'Python (www.python.org)'),
+                          ('Content-Length', '0'),
+                         ])
+
+    def test_finalize_w_body(self):
+        response = self._makeOne()
+        response.body = 'TEST'
+        status, headers = response.finalize()
+        self.assertEqual(status, '200 OK')
+        self.assertEqual(headers,
+                         [('X-Powered-By', 'Zope (www.zope.org), '
+                                           'Python (www.python.org)'),
+                          ('Content-Length', '4'),
+                         ])
+
+    def test_finalize_w_existing_content_length(self):
+        response = self._makeOne()
+        response.setHeader('Content-Length', '42')
+        status, headers = response.finalize()
+        self.assertEqual(status, '200 OK')
+        self.assertEqual(headers,
+                         [('X-Powered-By', 'Zope (www.zope.org), '
+                                           'Python (www.python.org)'),
+                          ('Content-Length', '42'),
+                         ])
+
+    def test_finalize_w_transfer_encoding(self):
+        response = self._makeOne()
+        response.setHeader('Transfer-Encoding', 'slurry')
+        status, headers = response.finalize()
+        self.assertEqual(status, '200 OK')
+        self.assertEqual(headers,
+                         [('X-Powered-By', 'Zope (www.zope.org), '
+                                           'Python (www.python.org)'),
+                          ('Transfer-Encoding', 'slurry'),
+                         ])
+
+    def test_finalize_after_redirect(self):
+        response = self._makeOne()
+        response.redirect('http://example.com/')
+        status, headers = response.finalize()
+        self.assertEqual(status, '302 Moved Temporarily')
+        self.assertEqual(headers,
+                         [('X-Powered-By', 'Zope (www.zope.org), '
+                                           'Python (www.python.org)'),
+                          ('Content-Length', '0'),
+                          ('Location', 'http://example.com/'),
+                         ])
+
     def test_listHeaders_empty(self):
         response = self._makeOne()
         headers = response.listHeaders()
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                          ])
 
     def test_listHeaders_already_wrote(self):
@@ -946,7 +1000,6 @@
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                          ])
 
     def test_listHeaders_existing_content_length(self):
@@ -977,7 +1030,6 @@
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                           ('X-Consistency', 'Foolish'),
                          ])
 
@@ -988,7 +1040,6 @@
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                           ('X-consistency', 'Foolish'),
                          ])
 
@@ -999,7 +1050,6 @@
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                           ('Location', 'http://example.com/'),
                          ])
 
@@ -1011,7 +1061,6 @@
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                           ('Set-Cookie', 'foo="bar%3Abaz"; Path=/'),
                          ])
 
@@ -1022,7 +1071,6 @@
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                           ('Set-Cookie', 'qux="deleted"; '
                                          'Path=/; '
                                          'Expires=Wed, 31-Dec-97 23:59:59 GMT; '
@@ -1037,7 +1085,6 @@
         self.assertEqual(headers,
                          [('X-Powered-By', 'Zope (www.zope.org), '
                                            'Python (www.python.org)'),
-                          ('Content-Length', '0'),
                           ('X-Consistency', 'Foolish'),
                           ('X-Consistency', 'Oatmeal'),
                          ])



More information about the Zope-Checkins mailing list