diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-12-19 18:40:19 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-12-19 18:40:19 +0000 |
| commit | 6225a24982dfaeffbc53f85d214159c08a0dbfc2 (patch) | |
| tree | f6939169df6a212c1a95972e55007b285930c8e1 /repoze/bfg/tests/test_configuration.py | |
| parent | 3809abb4c55ef98541158e4f58593c6c6011034f (diff) | |
| download | pyramid-6225a24982dfaeffbc53f85d214159c08a0dbfc2.tar.gz pyramid-6225a24982dfaeffbc53f85d214159c08a0dbfc2.tar.bz2 pyramid-6225a24982dfaeffbc53f85d214159c08a0dbfc2.zip | |
- Add a ``custom_predicates`` argument to the ``Configurator``
``add_view`` method, the ``bfg_view`` decorator and the attribute
list of the ZCML ``view`` directive. If ``custom_predicates`` is
specified, it must be a sequence of predicate callables (a predicate
callable accepts two arguments: ``context`` and ``request`` and
returns ``True`` or ``False``). The associated view callable will
only be invoked if all custom predicates return ``True``. Use one
or more custom predicates when no existing predefined predicate is
useful. Predefined and custom predicates can be mixed freely.
- Add a ``custom_predicates`` argument to the ``Configurator``
``add_route`` and the attribute list of the ZCML ``route``
directive. If ``custom_predicates`` is specified, it must be a
sequence of predicate callables (a predicate callable accepts two
arguments: ``context`` and ``request`` and returns ``True`` or
``False``). The associated route will match will only be invoked if
all custom predicates return ``True``, else route matching
continues. Use one or more custom predicates when no existing
predefined predicate is useful. Predefined and custom predicates
can be mixed freely.
Diffstat (limited to 'repoze/bfg/tests/test_configuration.py')
| -rw-r--r-- | repoze/bfg/tests/test_configuration.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/repoze/bfg/tests/test_configuration.py b/repoze/bfg/tests/test_configuration.py index a42e230f1..7d714f42a 100644 --- a/repoze/bfg/tests/test_configuration.py +++ b/repoze/bfg/tests/test_configuration.py @@ -926,6 +926,32 @@ class ConfiguratorTests(unittest.TestCase): request.path_info = '/' self._assertNotFound(wrapper, None, request) + def test_add_view_with_custom_predicates_match(self): + view = lambda *arg: 'OK' + config = self._makeOne() + def pred1(context, request): + return True + def pred2(context, request): + return True + predicates = (pred1, pred2) + config.add_view(view=view, custom_predicates=predicates) + wrapper = self._getViewCallable(config) + request = self._makeRequest(config) + self.assertEqual(wrapper(None, request), 'OK') + + def test_add_view_with_custom_predicates_nomatch(self): + view = lambda *arg: 'OK' + config = self._makeOne() + def pred1(context, request): + return True + def pred2(context, request): + return False + predicates = (pred1, pred2) + config.add_view(view=view, custom_predicates=predicates) + wrapper = self._getViewCallable(config) + request = self._makeRequest(config) + self._assertNotFound(wrapper, None, request) + def _assertRoute(self, config, name, path, num_predicates=0): from repoze.bfg.interfaces import IRoutesMapper mapper = config.registry.getUtility(IRoutesMapper) @@ -990,6 +1016,14 @@ class ConfiguratorTests(unittest.TestCase): request.params = {} self.assertEqual(predicate(None, request), False) + def test_add_route_with_custom_predicates(self): + config = self._makeOne() + def pred1(context, request): pass + def pred2(context, request): pass + config.add_route('name', 'path', custom_predicates=(pred1, pred2)) + route = self._assertRoute(config, 'name', 'path', 2) + self.assertEqual(route.predicates, [pred1, pred2]) + def test_add_route_with_header(self): config = self._makeOne() config.add_route('name', 'path', header='Host') |
