Tested a middleware's process_template_response() returning None.

This commit is contained in:
Tim Graham 2018-11-20 10:38:42 -05:00
parent bff5ccff75
commit ced0bdd923
2 changed files with 14 additions and 0 deletions

View File

@ -58,6 +58,11 @@ class LogMiddleware(BaseMiddleware):
return response return response
class NoTemplateResponseMiddleware(BaseMiddleware):
def process_template_response(self, request, response):
return None
class NotFoundMiddleware(BaseMiddleware): class NotFoundMiddleware(BaseMiddleware):
def __call__(self, request): def __call__(self, request):
raise Http404('not found') raise Http404('not found')

View File

@ -56,6 +56,15 @@ class MiddlewareTests(SimpleTestCase):
response = self.client.get('/middleware_exceptions/template_response/') response = self.client.get('/middleware_exceptions/template_response/')
self.assertEqual(response.content, b'template_response OK\nTemplateResponseMiddleware') self.assertEqual(response.content, b'template_response OK\nTemplateResponseMiddleware')
@override_settings(MIDDLEWARE=['middleware_exceptions.middleware.NoTemplateResponseMiddleware'])
def test_process_template_response_returns_none(self):
msg = (
"NoTemplateResponseMiddleware.process_template_response didn't "
"return an HttpResponse object. It returned None instead."
)
with self.assertRaisesMessage(ValueError, msg):
self.client.get('/middleware_exceptions/template_response/')
@override_settings(MIDDLEWARE=['middleware_exceptions.middleware.LogMiddleware']) @override_settings(MIDDLEWARE=['middleware_exceptions.middleware.LogMiddleware'])
def test_view_exception_converted_before_middleware(self): def test_view_exception_converted_before_middleware(self):
response = self.client.get('/middleware_exceptions/permission_denied/') response = self.client.get('/middleware_exceptions/permission_denied/')