[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/publication/ Make request factories configurable.

Gary Poster gary at zope.com
Fri Aug 5 18:11:41 EDT 2005


Log message for revision 37751:
  Make request factories configurable.
  
  Add simple test for configurable soap request factory.
  
  

Changed:
  U   Zope3/trunk/src/zope/app/publication/httpfactory.py
  U   Zope3/trunk/src/zope/app/publication/interfaces.py
  U   Zope3/trunk/src/zope/app/publication/tests/test_httpfactory.py

-=-
Modified: Zope3/trunk/src/zope/app/publication/httpfactory.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/httpfactory.py	2005-08-05 21:01:45 UTC (rev 37750)
+++ Zope3/trunk/src/zope/app/publication/httpfactory.py	2005-08-05 22:11:41 UTC (rev 37751)
@@ -17,24 +17,22 @@
 """
 __docformat__ = 'restructuredtext'
 
-from zope.interface import implements
+from zope import component, interface
 
 from zope.publisher.http import HTTPRequest
 from zope.publisher.browser import BrowserRequest
 from zope.publisher.xmlrpc import XMLRPCRequest
 
-from zope.app import zapi
-from zope.app.publication.interfaces import IPublicationRequestFactory
+from zope.app.publication import interfaces
 from zope.app.publication.http import HTTPPublication
 from zope.app.publication.browser import BrowserPublication, setDefaultSkin
 from zope.app.publication.xmlrpc import XMLRPCPublication
 from zope.app.publication.soap import SOAPPublication
-from zope.app.publication.interfaces import ISOAPRequestFactory
 
 _browser_methods = 'GET', 'POST', 'HEAD'
 
 class HTTPPublicationRequestFactory(object):
-    implements(IPublicationRequestFactory)
+    interface.implements(interfaces.IPublicationRequestFactory)
 
     def __init__(self, db):
         """See `zope.app.publication.interfaces.IPublicationRequestFactory`"""
@@ -42,7 +40,13 @@
         self._brower = BrowserPublication(db)
         self._xmlrpc = XMLRPCPublication(db)
         self._soappub = SOAPPublication(db)
-        self._soapreq = zapi.queryUtility(ISOAPRequestFactory)
+        self._soapreq = component.queryUtility(interfaces.ISOAPRequestFactory)
+        self._httpreq = component.queryUtility(
+            interfaces.IHTTPRequestFactory, default=HTTPRequest)
+        self._xmlrpcreq = component.queryUtility(
+            interfaces.IXMLRPCRequestFactory, default=XMLRPCRequest)
+        self._browserrequest = component.queryUtility(
+            interfaces.IBrowserRequestFactory, default=BrowserRequest)
 
     def __call__(self, input_stream, output_steam, env):
         """See `zope.app.publication.interfaces.IPublicationRequestFactory`"""
@@ -58,14 +62,14 @@
                 request = self._soapreq(input_stream, output_steam, env)
                 request.setPublication(self._soappub)
             elif (method == 'POST' and is_xml):
-                request = XMLRPCRequest(input_stream, output_steam, env)
+                request = self._xmlrpcreq(input_stream, output_steam, env)
                 request.setPublication(self._xmlrpc)
             else:
-                request = BrowserRequest(input_stream, output_steam, env)
+                request = self._browserrequest(input_stream, output_steam, env)
                 request.setPublication(self._brower)
                 setDefaultSkin(request)
         else:
-            request = HTTPRequest(input_stream, output_steam, env)
+            request = self._httpreq(input_stream, output_steam, env)
             request.setPublication(self._http)
 
         return request

Modified: Zope3/trunk/src/zope/app/publication/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/interfaces.py	2005-08-05 21:01:45 UTC (rev 37750)
+++ Zope3/trunk/src/zope/app/publication/interfaces.py	2005-08-05 22:11:41 UTC (rev 37751)
@@ -57,13 +57,24 @@
         self.object = ob
         self.request = request
 
+class IRequestFactory(interface.Interface):
 
-class ISOAPRequestFactory(interface.Interface):
+    def __call__(input_stream, output_steam, env):
+        """Create a request object to handle input."""
+
+class ISOAPRequestFactory(IRequestFactory):
     """SOAP request factory"""
 
-    def __call__(input_stream, output_steam, env):
-        """Create a request object to handle SOAP input."""
+class IHTTPRequestFactory(IRequestFactory):
+    # TODO: should SOAP, XMLRPC, and Browser extend this?
+    """generic HTTP request factory"""
 
+class IXMLRPCRequestFactory(IRequestFactory):
+    """XMLRPC request factory"""
+
+class IBrowserRequestFactory(IRequestFactory):
+    """browser request factory"""
+
 class IFileContent(interface.Interface):
     """Marker interface for content that can be managed as files.
 

Modified: Zope3/trunk/src/zope/app/publication/tests/test_httpfactory.py
===================================================================
--- Zope3/trunk/src/zope/app/publication/tests/test_httpfactory.py	2005-08-05 21:01:45 UTC (rev 37750)
+++ Zope3/trunk/src/zope/app/publication/tests/test_httpfactory.py	2005-08-05 22:11:41 UTC (rev 37751)
@@ -19,6 +19,7 @@
 
 from StringIO import StringIO
 
+from zope import component, interface
 from zope.publisher.browser import BrowserRequest
 from zope.publisher.http import HTTPRequest
 from zope.publisher.xmlrpc import XMLRPCRequest
@@ -29,7 +30,18 @@
 from zope.app.publication.http import HTTPPublication
 from zope.app.publication.xmlrpc import XMLRPCPublication
 from zope.app.testing import ztapi
+from zope.app.publication import interfaces
 
+class DummyRequestFactory(object):
+    def __call__(self, input_stream, output_steam, env):
+        self.input_stream = input_stream
+        self.output_steam = output_steam
+        self.env = env
+        return self
+
+    def setPublication(self, pub):
+        self.pub = pub
+
 class Test(PlacelessSetup, TestCase):
 
     def setUp(self):
@@ -42,6 +54,42 @@
             'GATEWAY_INTERFACE':  'TestFooInterface/1.0',
             }
 
+    def test_override(self):
+        # TODO: making a SOAP request without configuring a SOAP request
+        # currently generates an XMLRPC request.  Not sure what the right thing
+        # is, but that doesn't seem to be the right thing.
+        soaprequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            soaprequestfactory, interfaces.ISOAPRequestFactory)
+        component.provideUtility(soaprequestfactory)
+        xmlrpcrequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            xmlrpcrequestfactory, interfaces.IXMLRPCRequestFactory)
+        component.provideUtility(xmlrpcrequestfactory)
+        httprequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            httprequestfactory, interfaces.IHTTPRequestFactory)
+        component.provideUtility(httprequestfactory)
+        browserrequestfactory = DummyRequestFactory()
+        interface.directlyProvides(
+            browserrequestfactory, interfaces.IBrowserRequestFactory)
+        component.provideUtility(browserrequestfactory)
+        httpfactory = HTTPPublicationRequestFactory(None)
+        env = self.__env
+        env['REQUEST_METHOD'] = 'POST'
+        env['CONTENT_TYPE'] = 'text/xml'
+        input = StringIO('')
+        output = StringIO()
+        env['HTTP_SOAPACTION'] = 'foo'
+        self.assertEqual(httpfactory(input, output, env), soaprequestfactory)
+        del env['HTTP_SOAPACTION']
+        self.assertEqual(httpfactory(input, output, env), xmlrpcrequestfactory)
+        env['CONTENT_TYPE'] = 'text/foo'
+        self.assertEqual(
+            httpfactory(input, output, env), browserrequestfactory)
+        env['REQUEST_METHOD'] = 'FLOO'
+        self.assertEqual(httpfactory(input, output, env), httprequestfactory)
+
     def test_browser(self):
         r = self.__factory(StringIO(''), StringIO(), self.__env)
         self.assertEqual(r.__class__, BrowserRequest)



More information about the Zope3-Checkins mailing list