[Zope-Checkins] SVN: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/ Add tests for the old latin1_alias \213 / \233 fixup, and then fix it.

Tres Seaver tseaver at palladion.com
Mon Dec 21 21:42:09 EST 2009


Log message for revision 106841:
  Add tests for the old latin1_alias \213 / \233 fixup, and then fix it.

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	2009-12-21 22:50:41 UTC (rev 106840)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/HTTPResponse.py	2009-12-22 02:42:09 UTC (rev 106841)
@@ -490,27 +490,30 @@
                 self.body = body
 
 
-        ct = self.headers.get('content-type')
-        if ct is None:
-            if self.isHTML(self.body):
-                ct = 'text/html; charset=%s' % default_encoding
-            else:
-                ct = 'text/plain; charset=%s' % default_encoding
-            self.setHeader('content-type', ct)
-        else:
-            if ct.startswith('text/') and not 'charset=' in  ct:
-                ct = '%s; charset=%s' % (ct, default_encoding)                
-                self.setHeader('content-type', ct)
+        content_type = self.headers.get('content-type')
 
         # Some browsers interpret certain characters in Latin 1 as html
         # special characters. These cannot be removed by html_quote,
         # because this is not the case for all encodings.
-        content_type = self.headers['content-type']
-        if content_type == 'text/html' or latin1_alias_match(
-            content_type) is not None:
+        if (content_type == 'text/html' or
+            content_type and latin1_alias_match(content_type) is not None):
             body = '<'.join(body.split('\213'))
             body = '>'.join(body.split('\233'))
+            self.body = body
 
+        if content_type is None:
+            if self.isHTML(self.body):
+                content_type = 'text/html; charset=%s' % default_encoding
+            else:
+                content_type = 'text/plain; charset=%s' % default_encoding
+            self.setHeader('content-type', content_type)
+        else:
+            if (content_type.startswith('text/') and
+                'charset=' not in content_type):
+                content_type = '%s; charset=%s' % (content_type,
+                                                   default_encoding)
+                self.setHeader('content-type', content_type)
+
         self.setHeader('content-length', len(self.body))
         self.insertBase()
         if self.use_HTTP_content_compression and \

Modified: Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py
===================================================================
--- Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py	2009-12-21 22:50:41 UTC (rev 106840)
+++ Zope/branches/tseaver-fix_wsgi/src/ZPublisher/tests/testHTTPResponse.py	2009-12-22 02:42:09 UTC (rev 106841)
@@ -577,7 +577,7 @@
         self.assertEqual(response.getHeader('Content-Length'), str(len(HTML)))
 
     def test_setBody_object_with_unicode(self):
-        HTML = u'<html><head></head><body><h1>Tr\u0039</body></html>'
+        HTML = u'<html><head></head><body><h1>Tr\u0039s Bien</h1></body></html>'
         ENCODED = HTML.encode('iso-8859-15')
         response = self._makeOne()
         result = response.setBody(HTML)
@@ -598,8 +598,32 @@
         response = self._makeOne()
         self.assertRaises(NotFound, response.setBody, BOGUS)
 
+    def test_setBody_html_no_charset_escapes_latin1_gt_lt(self):
+        response = self._makeOne()
+        BEFORE = ('<html><head></head><body><p>LT: \213</p>'
+                  '<p>GT: \233</p></body></html>')
+        AFTER = ('<html><head></head><body><p>LT: &lt;</p>'
+                  '<p>GT: &gt;</p></body></html>')
+        response.setHeader('Content-Type', 'text/html')
+        result = response.setBody(BEFORE)
+        self.failUnless(result)
+        self.assertEqual(response.body, AFTER)
+        self.assertEqual(response.getHeader('Content-Length'), str(len(AFTER)))
+
+    def test_setBody_latin_alias_escapes_latin1_gt_lt(self):
+        response = self._makeOne()
+        BEFORE = ('<html><head></head><body><p>LT: \213</p>'
+                  '<p>GT: \233</p></body></html>')
+        AFTER = ('<html><head></head><body><p>LT: &lt;</p>'
+                  '<p>GT: &gt;</p></body></html>')
+        response.setHeader('Content-Type', 'text/html; charset=latin1')
+        result = response.setBody(BEFORE)
+        self.failUnless(result)
+        self.assertEqual(response.body, AFTER)
+        self.assertEqual(response.getHeader('Content-Length'), str(len(AFTER)))
+
+
     #TODO
-    #def test_setBody_escapes_latin1_gt_lt(self):
     #def test_setBody_w_base(self):
     #def test_setBody_w_HTTP_content_compression(self):
 



More information about the Zope-Checkins mailing list