[Zope3-checkins] CVS: Zope3/src/zope/testing - functional.py:1.7.10.1

Grégoire Weber zope@i-con.ch
Sun, 22 Jun 2003 10:27:34 -0400


Update of /cvs-repository/Zope3/src/zope/testing
In directory cvs.zope.org:/tmp/cvs-serv28993/src/zope/testing

Modified Files:
      Tag: cw-mail-branch
	functional.py 
Log Message:
Synced up with HEAD

=== Zope3/src/zope/testing/functional.py 1.7 => 1.7.10.1 ===
--- Zope3/src/zope/testing/functional.py:1.7	Thu May  1 15:35:51 2003
+++ Zope3/src/zope/testing/functional.py	Sun Jun 22 10:27:02 2003
@@ -23,7 +23,7 @@
 import traceback
 import unittest
 
-from cStringIO import StringIO
+from StringIO import StringIO
 
 from transaction import get_transaction
 from zodb.db import DB
@@ -31,10 +31,20 @@
 from zodb.storage.demo import DemoStorage
 from zope.app import Application
 from zope.app.publication.zopepublication import ZopePublication
+from zope.app.publication.http import HTTPPublication
 from zope.publisher.browser import BrowserRequest
+from zope.publisher.http import HTTPRequest
 from zope.publisher.publish import publish
 from zope.exceptions import Forbidden, Unauthorized
 
+__metaclass__ = type
+
+
+class HTTPTaskStub(StringIO):
+
+    def setAuthUserName(self, user):
+        pass
+
 
 class ResponseWrapper:
     """A wrapper that adds several introspective methods to a response."""
@@ -159,7 +169,7 @@
           outstream -- a stream where the HTTP response will be written
         """
         if outstream is None:
-            outstream = StringIO()
+            outstream = HTTPTaskStub()
         environment = {"HTTP_HOST": 'localhost',
                        "HTTP_REFERER": 'localhost'}
         environment.update(env)
@@ -170,7 +180,8 @@
                                request=BrowserRequest)
         return request
 
-    def publish(self, path, basic=None, form=None, env={}, handle_errors=False):
+    def publish(self, path, basic=None, form=None, env={},
+                handle_errors=False):
         """Renders an object at a given location.
 
         Arguments are the same as in makeRequest with the following exception:
@@ -183,7 +194,7 @@
           getBody()      -- returns the full response body as a string
           getPath()      -- returns the path used in the request
         """
-        outstream = StringIO()
+        outstream = HTTPTaskStub()
         request = self.makeRequest(path, basic=basic, form=form, env=env,
                                    outstream=outstream)
         response = ResponseWrapper(request.response, outstream, path)
@@ -247,6 +258,61 @@
         if errors:
             self.fail("%s contains broken links:\n" % path
                       + "\n".join(["  %s:\t%s" % (a, e) for a, e in errors]))
+
+
+class HTTPTestCase(FunctionalTestCase):
+    """Functional test case for HTTP requests."""
+
+    def makeRequest(self, path='', basic=None, form=None, env={},
+                    instream=None, outstream=None):
+        """Creates a new request object.
+
+        Arguments:
+          path   -- the path to be traversed (e.g. "/folder1/index.html")
+          basic  -- basic HTTP authentication credentials ("user:password")
+          form   -- a dictionary emulating a form submission
+                    (Note that field values should be Unicode strings)
+          env    -- a dictionary of additional environment variables
+                    (You can emulate HTTP request header
+                       X-Header: foo
+                     by adding 'HTTP_X_HEADER': 'foo' to env)
+          instream  -- a stream from where the HTTP request will be read
+          outstream -- a stream where the HTTP response will be written
+        """
+        if outstream is None:
+            outstream = HTTPTaskStub()
+        if instream is None:
+            instream = ''
+        environment = {"HTTP_HOST": 'localhost',
+                       "HTTP_REFERER": 'localhost'}
+        environment.update(env)
+        app = FunctionalTestSetup().getApplication()
+        request = app._request(path, instream, outstream,
+                               environment=environment,
+                               basic=basic, form=form,
+                               request=HTTPRequest, publication=HTTPPublication)
+        return request
+
+    def publish(self, path, basic=None, form=None, env={},
+                handle_errors=False, request_body=''):
+        """Renders an object at a given location.
+
+        Arguments are the same as in makeRequest with the following exception:
+          handle_errors  -- if False (default), exceptions will not be caught
+                            if True, exceptions will return a formatted error
+                            page.
+
+        Returns the response object enhanced with the following methods:
+          getOutput()    -- returns the full HTTP output as a string
+          getBody()      -- returns the full response body as a string
+          getPath()      -- returns the path used in the request
+        """
+        outstream = HTTPTaskStub()
+        request = self.makeRequest(path, basic=basic, form=form, env=env,
+                                   instream=request_body, outstream=outstream)
+        response = ResponseWrapper(request.response, outstream, path)
+        publish(request, handle_errors=handle_errors)
+        return response
 
 #
 # Sample functional test case