[Zope3-checkins] SVN: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_ Updated for api changes

Jim Fulton jim at zope.com
Fri Sep 2 21:14:06 EDT 2005


Log message for revision 38283:
  Updated for api changes
  

Changed:
  U   Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_baserequest.py
  U   Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserrequest.py
  U   Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserresponse.py
  U   Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_http.py

-=-
Modified: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_baserequest.py
===================================================================
--- Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_baserequest.py	2005-09-03 01:14:03 UTC (rev 38282)
+++ Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_baserequest.py	2005-09-03 01:14:05 UTC (rev 38283)
@@ -35,7 +35,7 @@
 
     def _Test__new(self, **kw):
         from zope.publisher.base import BaseRequest
-        return BaseRequest(StringIO(''), StringIO(), kw)
+        return BaseRequest(StringIO(''), kw)
 
     def _Test__expectedViewType(self):
         return None # we don't expect

Modified: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserrequest.py
===================================================================
--- Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserrequest.py	2005-09-03 01:14:03 UTC (rev 38282)
+++ Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserrequest.py	2005-09-03 01:14:05 UTC (rev 38283)
@@ -81,12 +81,12 @@
         class Item(object):
             """Required docstring for the publisher."""
             def __call__(self, a, b):
-                return "%s, %s" % (`a`, `b`)
+                return u"%s, %s" % (`a`, `b`)
 
         class Item3(object):
             """Required docstring for the publisher."""
             def __call__(self, *args):
-                return "..."
+                return u"..."
 
         class View(object):
             """Required docstring for the publisher."""
@@ -95,7 +95,7 @@
 
             def index(self, a, b):
                 """Required docstring for the publisher."""
-                return "%s, %s" % (`a`, `b`)
+                return u"%s, %s" % (`a`, `b`)
 
         class Item2(object):
             """Required docstring for the publisher."""

Modified: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserresponse.py
===================================================================
--- Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserresponse.py	2005-09-03 01:14:03 UTC (rev 38282)
+++ Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_browserresponse.py	2005-09-03 01:14:05 UTC (rev 38283)
@@ -78,42 +78,10 @@
         response.setBody(
             """<p>Hello world
             """)
-        self.assert_(response.getHeader('content-type').startswith(
-            "text/plain")
-                     )
+        self.assert_(
+            response.getHeader('content-type').startswith("text/plain")
+            )
 
-    def test_writeDataDirectlyToResponse(self):
-        # In this test we are going to simulate the behavior of a view that
-        # writes its data directly to the output pipe, instead of going
-        # through the entire machinery. This is particularly interesting for
-        # views returning large amount of binary data. 
-        output = StringIO()
-        response = BrowserResponse(output)
-        data = 'My special data.'
-
-        # If you write the data yourself directly, then you are responsible
-        # for setting the status and any other HTTP header yourself as well.
-        response.setHeader('content-type', 'text/plain')
-        response.setHeader('content-length', str(len(data)))
-        response.setStatus(200)
-        
-        # Write the data directly to the output stream from the view
-        response.write(data)
-
-        # Then the view returns `None` and the publisher calls
-        response.setBody(None)
-
-        # Now, if we got here already everything should be fine. The `None`
-        # value for the body should have been ignored and our putput value
-        # should just be our data:
-        self.assertEqual(
-            output.getvalue(),
-            'Status: 200 Ok\r\nContent-Length: 16\r\n'
-            'Content-Type: text/plain;charset=utf-8\r\n'
-            'X-Powered-By: Zope (www.zope.org), Python (www.python.org)\r\n'
-            '\r\n'
-            'My special data.')
-
     def test_interface(self):
         from zope.publisher.interfaces.http import IHTTPResponse
         from zope.publisher.interfaces.http import IHTTPApplicationResponse

Modified: Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_http.py
===================================================================
--- Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_http.py	2005-09-03 01:14:03 UTC (rev 38282)
+++ Zope3/branches/stephan_and_jim-response-refactor/src/zope/publisher/tests/test_http.py	2005-09-03 01:14:05 UTC (rev 38283)
@@ -32,7 +32,7 @@
 from zope.interface.verify import verifyObject
 
 from StringIO import StringIO
-from Cookie import SimpleCookie, CookieError
+from Cookie import CookieError
 
 
 class UserStub(object):
@@ -95,12 +95,19 @@
         return request
 
     def _publisherResults(self, extra_env={}, body=""):
-        outstream = StringIO()
-        request = self._createRequest(extra_env, body, outstream=outstream)
-        publish(request, handle_errors=0)
-        return outstream.getvalue()
+        request = self._createRequest(extra_env, body)
+        response = request.response
+        publish(request, handle_errors=False)
+        headers = response.getHeaders()
+        headers.sort()
+        return (
+            "Status: %s\r\n" % response.getStatusString()
+            +
+            "\r\n".join([("%s: %s" % h) for h in headers]) + "\r\n\r\n"
+            +
+            ''.join(response.result.body)
+            )
 
-
     def test_repr(self):
         request = self._createRequest()
         expect = '<%s.%s instance URL=http://foobar.com>' % (
@@ -125,7 +132,7 @@
         location = request.response.redirect('http://foobar.com/redirected')
         self.assertEquals(location, 'http://foobar.com/redirected')
         self.assertEquals(request.response.getStatus(), 302)
-        self.assertEquals(request.response._headers['location'], location)
+        self.assertEquals(request.response.getHeader('location'), location)
 
         # test HTTP/1.1
         env = {'SERVER_PROTOCOL':'HTTP/1.1'}
@@ -286,11 +293,7 @@
         task = HTTPTaskStub()
         req = self._createRequest(outstream=task)
         req.setPrincipal(UserStub("jim"))
-        self.assert_(not req.response._outstream.auth_user_name)
-        req = self._createRequest(outstream=task)
-        req.response.setHTTPTransaction(task)
-        req.setPrincipal(UserStub("jim"))
-        self.assertEquals(req.response.http_transaction.auth_user_name, "jim")
+        self.assertEquals(req.response.authUser, 'jim')
 
     def test_method(self):
         r = self._createRequest(extra_env={'REQUEST_METHOD':'SPAM'})
@@ -426,39 +429,23 @@
         verifyObject(IResponse, rp)
 
     def _createResponse(self):
-        stream = StringIO()
-        response = HTTPResponse(stream)
-        return response, stream
+        response = HTTPResponse()
+        return response
 
-    def _parseResult(self, result):
-        hdrs_text, body = result.split("\r\n\r\n", 1)
-        headers = {}
-        for line in hdrs_text.splitlines():
-            key, val = line.split(":", 1)
-            key = key.strip()
-            val = val.strip()
-            if headers.has_key(key):
-                if type(headers[key]) == type([]):
-                    headers[key].append(val)
-                else:
-                    headers[key] = [headers[key], val]
-            else:
-                headers[key] = val
-        return headers, body
+    def _parseResult(self, response):
+        return dict(response.getHeaders()), ''.join(response.result.body)
 
-    def _getResultFromResponse(self, body, charset=None, headers=None):
-        response, stream = self._createResponse()
-        if charset is not None:
-            response.setCharset(charset)
+    def _getResultFromResponse(self, body, charset='utf-8', headers=None):
+        response = self._createResponse()
+        assert(charset == 'utf-8')
         if headers is not None:
             for hdr, val in headers.iteritems():
                 response.setHeader(hdr, val)
         response.setBody(body)
-        response.outputBody()
-        return self._parseResult(stream.getvalue())
+        return self._parseResult(response)
 
     def testWrite(self):
-        response, stream = self._createResponse()
+        response = self._createResponse()
         data = 'a'*10
         # We have to set all the headers ourself
         response.setHeader('Content-Type', 'text/plain;charset=us-ascii')
@@ -468,7 +455,7 @@
         for ch in data:
             response.write(ch)
 
-        headers, body = self._parseResult(stream.getvalue())
+        headers, body = self._parseResult(response)
         # Check that the data have been written, and that the header
         # has been preserved   
         self.assertEqual(headers['Content-Type'], 'text/plain;charset=us-ascii')
@@ -476,7 +463,7 @@
         self.assertEqual(body, data)
 
     def testWrite_noContentLength(self):
-        response, stream = self._createResponse()
+        response = self._createResponse()
         data = 'a'*10
         # We have to set all the headers ourself, we choose not to provide a
         # content-length header
@@ -486,7 +473,7 @@
         for ch in data:
             response.write(ch)
 
-        headers, body = self._parseResult(stream.getvalue())
+        headers, body = self._parseResult(response)
         # Check that the data have been written, and that the header
         # has been preserved   
         self.assertEqual(headers['Content-Type'], 'text/plain;charset=us-ascii')
@@ -516,17 +503,17 @@
         eq("", headers.get("Content-Type", ""))
         eq("test", body)
 
-        headers, body = self._getResultFromResponse("test",
+        headers, body = self._getResultFromResponse(u"test",
             headers={"content-type": "text/plain"})
         eq("text/plain;charset=utf-8", headers["Content-Type"])
         eq("test", body)
 
-        headers, body = self._getResultFromResponse("test", "utf-8",
+        headers, body = self._getResultFromResponse(u"test", "utf-8",
             {"content-type": "text/html"})
         eq("text/html;charset=utf-8", headers["Content-Type"])
         eq("test", body)
 
-        headers, body = self._getResultFromResponse("test", "utf-8",
+        headers, body = self._getResultFromResponse(u"test", "utf-8",
             {"content-type": "text/plain;charset=cp1251"})
         eq("text/plain;charset=cp1251", headers["Content-Type"])
         eq("test", body)
@@ -539,13 +526,11 @@
     def _getCookieFromResponse(self, cookies):
         # Shove the cookies through request, parse the Set-Cookie header
         # and spit out a list of headers for examination
-        response, stream = self._createResponse()
+        response = self._createResponse()
         for name, value, kw in cookies:
             response.setCookie(name, value, **kw)
         response.setBody('test')
-        response.outputBody()
-        headers, body = self._parseResult(stream.getvalue())
-        c = SimpleCookie()
+        headers, body = self._parseResult(response)
         cookie_headers = headers["Set-Cookie"]
         if type(cookie_headers) != type([]):
             cookie_headers = [cookie_headers]
@@ -555,7 +540,7 @@
         c = self._getCookieFromResponse([
                 ('foo', 'bar', {}),
                 ])
-        self.failUnless('foo=bar;' in c, 'foo=bar not in %r' % c)
+        self.failUnless('foo=bar;' in c, 'foo=bar; not in %r' % c)
 
         c = self._getCookieFromResponse([
                 ('foo', 'bar', {}),



More information about the Zope3-Checkins mailing list