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_exceptions.py | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 repoze/bfg/tests/test_exceptions.py (limited to 'repoze/bfg/tests/test_exceptions.py') 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') -- cgit v1.2.3