[Repoze-checkins] r1233 - in repoze.bfg/trunk: . repoze/bfg repoze/bfg/tests

Chris McDonough chrism at agendaless.com
Fri Jul 4 21:33:25 EDT 2008


Author: Chris McDonough <chrism at agendaless.com>
Date: Fri Jul  4 21:33:25 2008
New Revision: 1233

Log:
IWSGIApplication -> IWSGIApplicationFactory

The router is not middleware.




Added:
   repoze.bfg/trunk/repoze/bfg/tests/test_router.py   (contents, props changed)
Modified:
   repoze.bfg/trunk/   (props changed)
   repoze.bfg/trunk/repoze/bfg/interfaces.py
   repoze.bfg/trunk/repoze/bfg/router.py
   repoze.bfg/trunk/setup.py

Modified: repoze.bfg/trunk/repoze/bfg/interfaces.py
==============================================================================
--- repoze.bfg/trunk/repoze/bfg/interfaces.py	(original)
+++ repoze.bfg/trunk/repoze/bfg/interfaces.py	Fri Jul  4 21:33:25 2008
@@ -1,8 +1,12 @@
 from zope.interface import Interface
 
-class IWSGIApplication(Interface):
-    def __call__(environ, start_response):
-        """ Represent a WSGI (PEP 333) application """
+class IWSGIApplicationFactory(Interface):
+    def __call__(context):
+        """ Return a WSGI (PEP333) application """
+
+class IRootPolicy(Interface):
+    def __call__(environ):
+        """ Return a root object """
 
 class ITraversalPolicy(Interface):
     def __call__(environ, root):

Modified: repoze.bfg/trunk/repoze/bfg/router.py
==============================================================================
--- repoze.bfg/trunk/repoze/bfg/router.py	(original)
+++ repoze.bfg/trunk/repoze/bfg/router.py	Fri Jul  4 21:33:25 2008
@@ -1,16 +1,14 @@
 from zope.component import getAdapter
-from repoze.bfg.interfaces import IWSGIApplication
+from repoze.bfg.interfaces import IWSGIApplicationFactory
 
 class Router:
-    def __init__(self, app, root_policy, traversal_policy):
-        self.app = app
+    def __init__(self, root_policy, traversal_policy):
         self.root_policy = root_policy
         self.traversal_policy = traversal_policy
 
     def __call__(self, environ, start_response):
         root = self.root_policy(environ)
         context, name, subpath = self.traversal_policy(root, environ)
-        app = getAdapter(context, IWSGIApplication, name)
-        environ['repoze.bfg.context'] = context
         environ['repoze.bfg.subpath'] = subpath
+        app = getAdapter(context, IWSGIApplicationFactory, name=name)
         return app(environ, start_response)

Added: repoze.bfg/trunk/repoze/bfg/tests/test_router.py
==============================================================================
--- (empty file)
+++ repoze.bfg/trunk/repoze/bfg/tests/test_router.py	Fri Jul  4 21:33:25 2008
@@ -0,0 +1,85 @@
+import unittest
+
+from zope.component.testing import PlacelessSetup
+
+class RouterTests(unittest.TestCase, PlacelessSetup):
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+
+    def tearDown(self):
+        PlacelessSetup.tearDown(self)
+
+    def _registerFactory(self, app, for_, name):
+        import zope.component
+        gsm = zope.component.getGlobalSiteManager()
+        from repoze.bfg.interfaces import IWSGIApplicationFactory
+        gsm.registerAdapter(app, (for_,), IWSGIApplicationFactory, name)
+        
+    def _getTargetClass(self):
+        from repoze.bfg.router import Router
+        return Router
+
+    def _makeOne(self, *arg, **kw):
+        klass = self._getTargetClass()
+        return klass(*arg, **kw)
+
+    def test_call_no_app_registered(self):
+        def rootpolicy(environ):
+            return None
+        def traversalpolicy(root, environ):
+            return DummyContext(), 'foo', []
+        def start_response(status, headers):
+            pass
+        environ = {}
+        router = self._makeOne(rootpolicy, traversalpolicy)
+        from zope.component import ComponentLookupError
+        self.assertRaises(ComponentLookupError, router, environ, start_response)
+
+    def test_call_app_registered_default_path(self):
+        def rootpolicy(environ):
+            return None
+        context = DummyContext()
+        _marker = []
+        def traversalpolicy(root, environ):
+            return context, '', []
+        def start_response(status, headers):
+            pass
+        class DummyWSGIApplicationFactory:
+            def __init__(self, context):
+                self.context = context
+
+            def __call__(self, environ, start_response):
+                return _marker
+        environ = {}
+        self._registerFactory(DummyWSGIApplicationFactory, None, '')
+        router = self._makeOne(rootpolicy, traversalpolicy)
+        result = router(environ, start_response)
+        self.failUnless(result is _marker)
+        self.assertEqual(environ['repoze.bfg.subpath'], [])
+
+    def test_call_app_registered_nondefault_path_and_subpath(self):
+        def rootpolicy(environ):
+            return None
+        context = DummyContext()
+        _marker = []
+        def traversalpolicy(root, environ):
+            return context, 'foo', ['bar', 'baz']
+        def start_response(status, headers):
+            pass
+        class DummyWSGIApplicationFactory:
+            def __init__(self, context):
+                self.context = context
+
+            def __call__(self, environ, start_response):
+                return _marker
+        environ = {}
+        self._registerFactory(DummyWSGIApplicationFactory, None, 'foo')
+        router = self._makeOne(rootpolicy, traversalpolicy)
+        result = router(environ, start_response)
+        self.failUnless(result is _marker)
+        self.assertEqual(environ['repoze.bfg.subpath'], ['bar', 'baz'])
+        
+class DummyContext:
+    pass
+
+    

Modified: repoze.bfg/trunk/setup.py
==============================================================================
--- repoze.bfg/trunk/setup.py	(original)
+++ repoze.bfg/trunk/setup.py	Fri Jul  4 21:33:25 2008
@@ -36,7 +36,6 @@
         "Topic :: Internet :: WWW/HTTP",
         "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
         "Topic :: Internet :: WWW/HTTP :: WSGI",
-        "Topic :: Internet :: WWW/HTTP :: WSGI :: Middleware",
         "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
         ],
       keywords='web wsgi bfg zope',


More information about the Repoze-checkins mailing list