diff options
| author | Chris McDonough <chrism@agendaless.com> | 2008-07-05 01:33:25 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2008-07-05 01:33:25 +0000 |
| commit | 5ed24b859b1e8deed12f73d1dc1808142218f61e (patch) | |
| tree | 36ca317a5d2803d11d6c287b5be41f75791b3566 | |
| parent | 1da614e3dab88f4a7ccf97630e808c1c119a0f90 (diff) | |
| download | pyramid-5ed24b859b1e8deed12f73d1dc1808142218f61e.tar.gz pyramid-5ed24b859b1e8deed12f73d1dc1808142218f61e.tar.bz2 pyramid-5ed24b859b1e8deed12f73d1dc1808142218f61e.zip | |
IWSGIApplication -> IWSGIApplicationFactory
The router is not middleware.
| -rw-r--r-- | repoze/bfg/interfaces.py | 10 | ||||
| -rw-r--r-- | repoze/bfg/router.py | 8 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_router.py | 85 | ||||
| -rw-r--r-- | setup.py | 1 |
4 files changed, 95 insertions, 9 deletions
diff --git a/repoze/bfg/interfaces.py b/repoze/bfg/interfaces.py index b6d7ff623..e915519e0 100644 --- a/repoze/bfg/interfaces.py +++ b/repoze/bfg/interfaces.py @@ -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): diff --git a/repoze/bfg/router.py b/repoze/bfg/router.py index 68e85d50c..f37ca0f7c 100644 --- a/repoze/bfg/router.py +++ b/repoze/bfg/router.py @@ -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) diff --git a/repoze/bfg/tests/test_router.py b/repoze/bfg/tests/test_router.py new file mode 100644 index 000000000..46fdecbbc --- /dev/null +++ b/repoze/bfg/tests/test_router.py @@ -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 + + @@ -36,7 +36,6 @@ setup(name='repoze.bfg', "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', |
