[Checkins] SVN: zope.publisher/branches/py3-attempt2/src/zope/publisher/ test_browserresponse passes

Andrey Lebedev cvs-admin at zope.org
Wed Feb 20 15:17:09 UTC 2013


Log message for revision 129539:
  test_browserresponse passes
  
  

Changed:
  U   zope.publisher/branches/py3-attempt2/src/zope/publisher/browser.py
  U   zope.publisher/branches/py3-attempt2/src/zope/publisher/http.py
  U   zope.publisher/branches/py3-attempt2/src/zope/publisher/tests/test_browserresponse.py

-=-
Modified: zope.publisher/branches/py3-attempt2/src/zope/publisher/browser.py
===================================================================
--- zope.publisher/branches/py3-attempt2/src/zope/publisher/browser.py	2013-02-20 14:17:14 UTC (rev 129538)
+++ zope.publisher/branches/py3-attempt2/src/zope/publisher/browser.py	2013-02-20 15:17:09 UTC (rev 129539)
@@ -41,7 +41,7 @@
 from zope.publisher.interfaces.browser import IBrowserPage
 from zope.publisher.interfaces.browser import IBrowserSkinType
 from zope.publisher.interfaces.http import IHTTPRequest
-from zope.publisher.http import HTTPRequest, HTTPResponse
+from zope.publisher.http import HTTPRequest, HTTPResponse, getCharsetUsingRequest
 
 # BBB imports, this compoennts get moved from this module
 from zope.publisher.interfaces import ISkinType #BBB import
@@ -56,8 +56,8 @@
 
 __ArrayTypes = (list, tuple)
 
-start_of_header_search=re.compile('(<head[^>]*>)', re.I).search
-base_re_search=re.compile('(<base.*?>)',re.I).search
+start_of_header_search=re.compile(b'(<head[^>]*>)', re.I).search
+base_re_search=re.compile(b'(<base.*?>)',re.I).search
 isRelative = re.compile("[-_.!~*a-zA-z0-9'()@&=+$,]+(/|$)").match
 newlines = re.compile('\r\n|\n\r|\r')
 
@@ -737,9 +737,17 @@
                     ibase = base_re_search(body)
                     if ibase is None:
                         # Make sure the base URL is not a unicode string.
-                        base = str(self.getBase())
-                        body = ('%s\n<base href="%s" />\n%s' %
-                                (body[:index], base, body[index:]))
+                        base = self.getBase()
+                        if not isinstance(base, bytes):
+                            encoding = getCharsetUsingRequest(self._request) or 'utf-8'
+                            base = self.getBase().encode(encoding)
+                        #body = (b'%s\n<base href="%s" />\n%s' %
+                        #        (body[:index], base, body[index:]))
+                        body = b''.join([body[:index],
+                                         b'\n<base href="',
+                                         base,
+                                         b'" />\n',
+                                         body[index:]])
         return body
 
     def getBase(self):

Modified: zope.publisher/branches/py3-attempt2/src/zope/publisher/http.py
===================================================================
--- zope.publisher/branches/py3-attempt2/src/zope/publisher/http.py	2013-02-20 14:17:14 UTC (rev 129538)
+++ zope.publisher/branches/py3-attempt2/src/zope/publisher/http.py	2013-02-20 15:17:09 UTC (rev 129539)
@@ -51,7 +51,8 @@
 else:
     import http.cookies as cookies
     from urllib.parse import splitport, quote, urlsplit
-    basestring = unicode = str
+    unicode = str
+    basestring = (str, bytes)
 
 # Default Encoding
 ENCODING = 'UTF-8'

Modified: zope.publisher/branches/py3-attempt2/src/zope/publisher/tests/test_browserresponse.py
===================================================================
--- zope.publisher/branches/py3-attempt2/src/zope/publisher/tests/test_browserresponse.py	2013-02-20 14:17:14 UTC (rev 129538)
+++ zope.publisher/branches/py3-attempt2/src/zope/publisher/tests/test_browserresponse.py	2013-02-20 15:17:09 UTC (rev 129539)
@@ -95,7 +95,7 @@
         #       the result :(
         response = BrowserResponse()
         response.setStatus(304)
-        response.setResult('')
+        response.setResult(b'')
         self.assertEqual(response.getHeader('content-type'), None)
 
     def testInsertBase(self):
@@ -107,21 +107,21 @@
         # Make sure that bases are inserted
         response.setBase('http://localhost/folder/')
         self.assert_(
-            '<base href="http://localhost/folder/" />' in
-            insertBase('<html><head></head><body>Page</body></html>'))
+            b'<base href="http://localhost/folder/" />' in
+            insertBase(b'<html><head></head><body>Page</body></html>'))
 
         # Ensure that unicode bases work as well
         response.setBase(u'http://localhost/folder/')
-        body = insertBase('<html><head></head><body>Page</body></html>')
-        self.assert_(isinstance(body, str))
-        self.assert_('<base href="http://localhost/folder/" />' in body)
+        body = insertBase(b'<html><head></head><body>Page</body></html>')
+        self.assert_(isinstance(body, bytes))
+        self.assert_(b'<base href="http://localhost/folder/" />' in body)
 
         # Ensure that encoded bodies work, when a base is inserted.
         response.setBase('http://localhost/folder')
         result = insertBase(
-            '<html><head></head><body>\xc3\x9bung</body></html>')
-        self.assert_(isinstance(body, str))
-        self.assert_('<base href="http://localhost/folder" />' in result)
+            b'<html><head></head><body>\xc3\x9bung</body></html>')
+        self.assert_(isinstance(body, bytes))
+        self.assert_(b'<base href="http://localhost/folder" />' in result)
 
     def testInsertBaseInSetResultUpdatesContentLength(self):
         # Make sure that the Content-Length header is updated to account
@@ -131,7 +131,7 @@
         base = 'http://localhost/folder/'
         response.setBase(base)
         inserted_text = '\n<base href="%s" />\n' % base
-        html_page = """<html>
+        html_page = b"""<html>
             <head></head>
             <blah>
             </html>
@@ -159,15 +159,15 @@
             "text/html;charset=utf-8")
         self.assertEquals(response.getStatus(), 500)
         self.assert_(response.consumeBody() in
-            ["<html><head><title>&lt;type 'exceptions.ValueError'&gt;</title></head>\n"
-            "<body><h2>&lt;type 'exceptions.ValueError'&gt;</h2>\n"
-            "A server error occurred.\n"
-            "</body></html>\n",
-            "<html><head><title>ValueError</title></head>\n"
-            "<body><h2>ValueError</h2>\n"
-            "A server error occurred.\n"
-            "</body></html>\n"]
-            )
+            [b"<html><head><title>&lt;type 'exceptions.ValueError'&gt;</title></head>\n"
+             b"<body><h2>&lt;type 'exceptions.ValueError'&gt;</h2>\n"
+             b"A server error occurred.\n"
+             b"</body></html>\n",
+             b"<html><head><title>ValueError</title></head>\n"
+             b"<body><h2>ValueError</h2>\n"
+             b"A server error occurred.\n"
+             b"</body></html>\n"]
+                     )
 
 
 def test_suite():



More information about the checkins mailing list