From d96ff9144f98bb44254f77f56e55967c46b09774 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 8 Aug 2010 07:25:28 +0000 Subject: - New public interface: ``repoze.bfg.exceptions.IExceptionResponse``. This interface is provided by all internal exception classes (such as ``repoze.bfg.exceptions.NotFound`` and ``repoze.bfg.exceptions.Forbidden``), instances of which are both exception objects and can behave as WSGI response objects. This interface is made public so that exception classes which are also valid WSGI response factories can be configured to implement them or exception instances which are also or response instances can be configured to provide them. - New API class: ``repoze.bfg.view.AppendSlashNotFoundViewFactory`` (undoes previous custom_notfound_view on request passsed to append_slash_notfound_view). - Previously, two default view functions were registered at Configurator setup (one for ``repoze.bfg.exceptions.NotFound`` named ``default_notfound_view`` and one for ``repoze.bfg.exceptions.Forbidden`` named ``default_forbidden_view``) to render internal exception responses. Those default view functions have been removed, replaced with a generic default view function which is registered at Configurator setup for the ``repoze.bfg.interfaces.IExceptionResponse`` interface that simply returns the exception instance; the ``NotFound` and ``Forbidden`` classes are now still exception factories but they are also response factories which generate instances that implement the new ``repoze.bfg.interfaces.IExceptionResponse`` interface. --- repoze/bfg/tests/test_configuration.py | 57 +++++++++++++--- repoze/bfg/tests/test_exceptions.py | 45 +++++++++++++ repoze/bfg/tests/test_view.py | 119 ++++++++++++++------------------- 3 files changed, 144 insertions(+), 77 deletions(-) create mode 100644 repoze/bfg/tests/test_exceptions.py (limited to 'repoze/bfg/tests') diff --git a/repoze/bfg/tests/test_configuration.py b/repoze/bfg/tests/test_configuration.py index 2f001ba8d..1eae66094 100644 --- a/repoze/bfg/tests/test_configuration.py +++ b/repoze/bfg/tests/test_configuration.py @@ -194,11 +194,9 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(reg.notify(1), None) self.assertEqual(reg.events, (1,)) - def test_setup_registry_registers_default_exception_views(self): - from repoze.bfg.exceptions import NotFound - from repoze.bfg.exceptions import Forbidden - from repoze.bfg.view import default_notfound_view - from repoze.bfg.view import default_forbidden_view + def test_setup_registry_registers_default_exceptionresponse_view(self): + from repoze.bfg.interfaces import IExceptionResponse + from repoze.bfg.view import default_exceptionresponse_view class DummyRegistry(object): def registerUtility(self, *arg, **kw): pass @@ -207,10 +205,25 @@ class ConfiguratorTests(unittest.TestCase): views = [] config.add_view = lambda *arg, **kw: views.append((arg, kw)) config.setup_registry() - self.assertEqual(views[0], ((default_notfound_view,), - {'context':NotFound})) - self.assertEqual(views[1], ((default_forbidden_view,), - {'context':Forbidden})) + self.assertEqual(views[0], ((default_exceptionresponse_view,), + {'context':IExceptionResponse})) + + def test_setup_registry_explicit_notfound_trumps_iexceptionresponse(self): + from zope.interface import implementedBy + from repoze.bfg.interfaces import IRequest + from repoze.bfg.exceptions import NotFound + from repoze.bfg.registry import Registry + reg = Registry() + config = self._makeOne(reg) + config.setup_registry() # registers IExceptionResponse default view + def myview(context, request): + return 'OK' + config.add_view(myview, context=NotFound) + request = self._makeRequest(config) + view = self._getViewCallable(config, ctx_iface=implementedBy(NotFound), + request_iface=IRequest) + result = view(None, request) + self.assertEqual(result, 'OK') def test_setup_registry_custom_settings(self): from repoze.bfg.registry import Registry @@ -3702,6 +3715,32 @@ class TestDottedNameResolver(unittest.TestCase): self.assertEqual(e.args[0], "The dotted name 'cant.be.found' cannot be imported") +class Test_isexception(unittest.TestCase): + def _callFUT(self, ob): + from repoze.bfg.configuration import isexception + return isexception(ob) + + def test_is_exception_instance(self): + class E(Exception): + pass + e = E() + self.assertEqual(self._callFUT(e), True) + + def test_is_exception_class(self): + class E(Exception): + pass + self.assertEqual(self._callFUT(E), True) + + def test_is_IException(self): + from repoze.bfg.interfaces import IException + self.assertEqual(self._callFUT(IException), True) + + def test_is_IException_subinterface(self): + from repoze.bfg.interfaces import IException + class ISubException(IException): + pass + self.assertEqual(self._callFUT(ISubException), True) + class DummyRequest: subpath = () def __init__(self): diff --git a/repoze/bfg/tests/test_exceptions.py b/repoze/bfg/tests/test_exceptions.py new file mode 100644 index 000000000..4091eb941 --- /dev/null +++ b/repoze/bfg/tests/test_exceptions.py @@ -0,0 +1,45 @@ +import unittest + +class TestExceptionResponse(unittest.TestCase): + def _makeOne(self, message): + from repoze.bfg.exceptions import ExceptionResponse + return ExceptionResponse(message) + + def test_app_iter(self): + exc = self._makeOne('') + self.failUnless('' in exc.app_iter[0]) + + def test_headerlist(self): + exc = self._makeOne('') + headerlist = exc.headerlist + headerlist.sort() + app_iter = exc.app_iter + clen = str(len(app_iter[0])) + self.assertEqual(headerlist[0], ('Content-Length', clen)) + self.assertEqual(headerlist[1], ('Content-Type', 'text/html')) + + def test_withmessage(self): + exc = self._makeOne('abc&123') + self.failUnless('abc&123' in exc.app_iter[0]) + +class TestNotFound(unittest.TestCase): + def _makeOne(self, message): + from repoze.bfg.exceptions import NotFound + return NotFound(message) + + def test_it(self): + from repoze.bfg.exceptions import ExceptionResponse + e = self._makeOne('notfound') + self.failUnless(isinstance(e, ExceptionResponse)) + self.assertEqual(e.status, '404 Not Found') + +class TestForbidden(unittest.TestCase): + def _makeOne(self, message): + from repoze.bfg.exceptions import Forbidden + return Forbidden(message) + + def test_it(self): + from repoze.bfg.exceptions import ExceptionResponse + e = self._makeOne('unauthorized') + self.failUnless(isinstance(e, ExceptionResponse)) + self.assertEqual(e.status, '401 Unauthorized') diff --git a/repoze/bfg/tests/test_view.py b/repoze/bfg/tests/test_view.py index 27f468c74..18a46a205 100644 --- a/repoze/bfg/tests/test_view.py +++ b/repoze/bfg/tests/test_view.py @@ -342,59 +342,7 @@ class TestBFGViewDecorator(unittest.TestCase): self.assertEqual(settings[0]['renderer'], 'repoze.bfg.tests:fixtures/minimal.pt') -class TestDefaultView(BaseTest): - def test_no_registry_on_request(self): - request = None - context = Exception() - response = self._callFUT(context, request) - self.assertEqual(response.status, self.status) - self.failUnless('' in response.body) - - def test_nomessage(self): - request = self._makeRequest() - context = Exception() - response = self._callFUT(context, request) - self.assertEqual(response.status, self.status) - self.failUnless('' in response.body) - - def test_withmessage(self): - request = self._makeRequest() - context = Exception('abc&123') - response = self._callFUT(context, request) - self.assertEqual(response.status, self.status) - self.failUnless('abc&123' in response.body) - - def test_context_not_exception(self): - request = self._makeRequest() - request.exception = Exception('woo') - context = None - response = self._callFUT(context, request) - self.assertEqual(response.status, self.status) - self.failUnless('woo' in response.body) - - def test_msg_exception_raised(self): - request = self._makeRequest() - context = None - response = self._callFUT(context, request) - self.assertEqual(response.status, self.status) - self.failUnless('' in response.body) - -class TestDefaultForbiddenView(TestDefaultView, unittest.TestCase): - status = '401 Unauthorized' - - def _callFUT(self, context, request): - from repoze.bfg.view import default_forbidden_view - return default_forbidden_view(context, request) - - -class TestDefaultNotFoundView(TestDefaultView, unittest.TestCase): - status = '404 Not Found' - - def _callFUT(self, context, request): - from repoze.bfg.view import default_notfound_view - return default_notfound_view(context, request) - -class AppendSlashNotFoundView(BaseTest, unittest.TestCase): +class Test_append_slash_notfound_view(BaseTest, unittest.TestCase): def _callFUT(self, context, request): from repoze.bfg.view import append_slash_notfound_view return append_slash_notfound_view(context, request) @@ -417,49 +365,81 @@ class AppendSlashNotFoundView(BaseTest, unittest.TestCase): def test_context_is_not_exception(self): request = self._makeRequest(PATH_INFO='/abc') - request.exception = Exception('halloo') + request.exception = ExceptionResponse() context = DummyContext() response = self._callFUT(context, request) self.assertEqual(response.status, '404 Not Found') - self.failUnless('halloo' in response.body) + self.assertEqual(response.app_iter, ['Not Found']) def test_no_mapper(self): request = self._makeRequest(PATH_INFO='/abc') - context = Exception() + context = ExceptionResponse() response = self._callFUT(context, request) self.assertEqual(response.status, '404 Not Found') - def test_custom_notfound_view(self): - request = self._makeRequest(PATH_INFO='/abc') - def notfound(exc, request): - return 'abc' - request.custom_notfound_view = notfound - context = Exception() - response = self._callFUT(context, request) - self.assertEqual(response, 'abc') - def test_no_path(self): request = self._makeRequest() - context = Exception() + context = ExceptionResponse() self._registerMapper(request.registry, True) response = self._callFUT(context, request) self.assertEqual(response.status, '404 Not Found') def test_mapper_path_already_slash_ending(self): request = self._makeRequest(PATH_INFO='/abc/') - context = Exception() + context = ExceptionResponse() self._registerMapper(request.registry, True) response = self._callFUT(context, request) self.assertEqual(response.status, '404 Not Found') def test_matches(self): request = self._makeRequest(PATH_INFO='/abc') - context = Exception() + context = ExceptionResponse() self._registerMapper(request.registry, True) response = self._callFUT(context, request) self.assertEqual(response.status, '302 Found') self.assertEqual(response.location, '/abc/') +class TestAppendSlashNotFoundViewFactory(BaseTest, unittest.TestCase): + def _makeOne(self, notfound_view): + from repoze.bfg.view import AppendSlashNotFoundViewFactory + return AppendSlashNotFoundViewFactory(notfound_view) + + def test_custom_notfound_view(self): + request = self._makeRequest(PATH_INFO='/abc') + context = ExceptionResponse() + def custom_notfound(context, request): + return 'OK' + view = self._makeOne(custom_notfound) + response = view(context, request) + self.assertEqual(response, 'OK') + +class Test_default_exceptionresponse_view(unittest.TestCase): + def _callFUT(self, context, request): + from repoze.bfg.view import default_exceptionresponse_view + return default_exceptionresponse_view(context, request) + + def test_is_exception(self): + context = Exception() + result = self._callFUT(context, None) + self.failUnless(result is context) + + def test_is_not_exception_no_request_exception(self): + context = object() + request = DummyRequest() + result = self._callFUT(context, request) + self.failUnless(result is context) + + def test_is_not_exception_request_exception(self): + context = object() + request = DummyRequest() + request.exception = 'abc' + result = self._callFUT(context, request) + self.assertEqual(result, 'abc') + +class ExceptionResponse(Exception): + status = '404 Not Found' + app_iter = ['Not Found'] + headerlist = [] class DummyContext: pass @@ -469,6 +449,9 @@ def make_view(response): return response return view +class DummyRequest: + pass + class DummyResponse: status = '200 OK' headerlist = () -- cgit v1.2.3