2010-01-11 01:35:01 +08:00
|
|
|
import sys
|
|
|
|
|
2010-11-08 00:02:22 +08:00
|
|
|
from django.conf import settings
|
2014-11-08 03:57:04 +08:00
|
|
|
from django.core.exceptions import MiddlewareNotUsed
|
2010-01-11 01:35:01 +08:00
|
|
|
from django.core.signals import got_request_exception
|
2010-10-30 15:19:04 +08:00
|
|
|
from django.http import HttpResponse
|
2015-01-10 05:59:00 +08:00
|
|
|
from django.template import engines
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import RequestFactory, SimpleTestCase, override_settings
|
2016-05-10 01:13:55 +08:00
|
|
|
from django.test.utils import ignore_warnings, patch_logger
|
|
|
|
from django.utils.deprecation import MiddlewareMixin, RemovedInDjango20Warning
|
2010-10-30 15:19:04 +08:00
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-03-13 00:45:29 +08:00
|
|
|
class TestException(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
# A middleware base class that tracks which methods have been called
|
2015-11-07 23:12:37 +08:00
|
|
|
class TestMiddleware(MiddlewareMixin):
|
|
|
|
def __init__(self, get_response=None):
|
2010-10-30 15:19:04 +08:00
|
|
|
self.process_request_called = False
|
|
|
|
self.process_view_called = False
|
|
|
|
self.process_response_called = False
|
2010-12-07 21:57:01 +08:00
|
|
|
self.process_template_response_called = False
|
2010-10-30 15:19:04 +08:00
|
|
|
self.process_exception_called = False
|
2015-11-07 23:12:37 +08:00
|
|
|
self.get_response = get_response
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def process_request(self, request):
|
|
|
|
self.process_request_called = True
|
|
|
|
|
|
|
|
def process_view(self, request, view_func, view_args, view_kwargs):
|
|
|
|
self.process_view_called = True
|
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
def process_template_response(self, request, response):
|
|
|
|
self.process_template_response_called = True
|
|
|
|
return response
|
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
def process_response(self, request, response):
|
|
|
|
self.process_response_called = True
|
|
|
|
return response
|
|
|
|
|
|
|
|
def process_exception(self, request, exception):
|
|
|
|
self.process_exception_called = True
|
|
|
|
|
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
# Middleware examples that do the right thing
|
2010-10-30 15:19:04 +08:00
|
|
|
class RequestMiddleware(TestMiddleware):
|
2010-01-11 01:35:01 +08:00
|
|
|
def process_request(self, request):
|
2010-10-30 15:19:04 +08:00
|
|
|
super(RequestMiddleware, self).process_request(request)
|
|
|
|
return HttpResponse('Request Middleware')
|
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
class ViewMiddleware(TestMiddleware):
|
|
|
|
def process_view(self, request, view_func, view_args, view_kwargs):
|
|
|
|
super(ViewMiddleware, self).process_view(request, view_func, view_args, view_kwargs)
|
|
|
|
return HttpResponse('View Middleware')
|
2010-01-11 01:35:01 +08:00
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
class ResponseMiddleware(TestMiddleware):
|
2010-10-29 15:43:56 +08:00
|
|
|
def process_response(self, request, response):
|
2010-10-30 15:19:04 +08:00
|
|
|
super(ResponseMiddleware, self).process_response(request, response)
|
|
|
|
return HttpResponse('Response Middleware')
|
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
class TemplateResponseMiddleware(TestMiddleware):
|
|
|
|
def process_template_response(self, request, response):
|
|
|
|
super(TemplateResponseMiddleware, self).process_template_response(request, response)
|
2015-01-10 05:59:00 +08:00
|
|
|
template = engines['django'].from_string('Template Response Middleware')
|
|
|
|
return TemplateResponse(request, template)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
class ExceptionMiddleware(TestMiddleware):
|
|
|
|
def process_exception(self, request, exception):
|
|
|
|
super(ExceptionMiddleware, self).process_exception(request, exception)
|
|
|
|
return HttpResponse('Exception Middleware')
|
|
|
|
|
|
|
|
|
|
|
|
# Sample middlewares that raise exceptions
|
|
|
|
class BadRequestMiddleware(TestMiddleware):
|
|
|
|
def process_request(self, request):
|
|
|
|
super(BadRequestMiddleware, self).process_request(request)
|
|
|
|
raise TestException('Test Request Exception')
|
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
class BadViewMiddleware(TestMiddleware):
|
|
|
|
def process_view(self, request, view_func, view_args, view_kwargs):
|
|
|
|
super(BadViewMiddleware, self).process_view(request, view_func, view_args, view_kwargs)
|
|
|
|
raise TestException('Test View Exception')
|
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
class BadTemplateResponseMiddleware(TestMiddleware):
|
|
|
|
def process_template_response(self, request, response):
|
|
|
|
super(BadTemplateResponseMiddleware, self).process_template_response(request, response)
|
|
|
|
raise TestException('Test Template Response Exception')
|
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
class BadResponseMiddleware(TestMiddleware):
|
|
|
|
def process_response(self, request, response):
|
|
|
|
super(BadResponseMiddleware, self).process_response(request, response)
|
|
|
|
raise TestException('Test Response Exception')
|
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
class BadExceptionMiddleware(TestMiddleware):
|
|
|
|
def process_exception(self, request, exception):
|
|
|
|
super(BadExceptionMiddleware, self).process_exception(request, exception)
|
|
|
|
raise TestException('Test Exception Exception')
|
|
|
|
|
|
|
|
|
2014-06-08 01:48:45 +08:00
|
|
|
# Sample middlewares that omit to return an HttpResonse
|
|
|
|
class NoTemplateResponseMiddleware(TestMiddleware):
|
|
|
|
def process_template_response(self, request, response):
|
|
|
|
super(NoTemplateResponseMiddleware, self).process_template_response(request, response)
|
|
|
|
|
|
|
|
|
|
|
|
class NoResponseMiddleware(TestMiddleware):
|
|
|
|
def process_response(self, request, response):
|
|
|
|
super(NoResponseMiddleware, self).process_response(request, response)
|
|
|
|
|
|
|
|
|
2016-05-10 01:13:55 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2015-11-07 23:12:37 +08:00
|
|
|
@override_settings(
|
|
|
|
ROOT_URLCONF='middleware_exceptions.urls',
|
|
|
|
MIDDLEWARE_CLASSES=['django.middleware.common.CommonMiddleware'],
|
|
|
|
MIDDLEWARE=None,
|
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
class BaseMiddlewareExceptionTest(SimpleTestCase):
|
2011-04-02 21:27:17 +08:00
|
|
|
|
2010-01-11 02:48:08 +08:00
|
|
|
def setUp(self):
|
2010-01-11 01:35:01 +08:00
|
|
|
self.exceptions = []
|
|
|
|
got_request_exception.connect(self._on_request_exception)
|
|
|
|
self.client.handler.load_middleware()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2010-01-11 02:48:08 +08:00
|
|
|
got_request_exception.disconnect(self._on_request_exception)
|
2010-01-11 01:35:01 +08:00
|
|
|
self.exceptions = []
|
|
|
|
|
|
|
|
def _on_request_exception(self, sender, request, **kwargs):
|
|
|
|
self.exceptions.append(sys.exc_info())
|
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
def _add_middleware(self, middleware):
|
|
|
|
self.client.handler._request_middleware.insert(0, middleware.process_request)
|
|
|
|
self.client.handler._view_middleware.insert(0, middleware.process_view)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.client.handler._template_response_middleware.append(middleware.process_template_response)
|
2010-10-30 15:19:04 +08:00
|
|
|
self.client.handler._response_middleware.append(middleware.process_response)
|
|
|
|
self.client.handler._exception_middleware.append(middleware.process_exception)
|
|
|
|
|
|
|
|
def assert_exceptions_handled(self, url, errors, extra_error=None):
|
2010-01-11 01:35:01 +08:00
|
|
|
try:
|
2013-10-19 20:31:38 +08:00
|
|
|
self.client.get(url)
|
2012-04-29 00:09:37 +08:00
|
|
|
except TestException:
|
2010-10-29 15:43:56 +08:00
|
|
|
# Test client intentionally re-raises any exceptions being raised
|
2010-01-11 01:35:01 +08:00
|
|
|
# during request handling. Hence actual testing that exception was
|
|
|
|
# properly handled is done by relying on got_request_exception
|
|
|
|
# signal being sent.
|
|
|
|
pass
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2010-10-30 15:19:04 +08:00
|
|
|
if type(extra_error) != type(e):
|
|
|
|
self.fail("Unexpected exception: %s" % e)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(len(self.exceptions), len(errors))
|
2010-10-30 15:19:04 +08:00
|
|
|
for i, error in enumerate(errors):
|
|
|
|
exception, value, tb = self.exceptions[i]
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(value.args, (error, ))
|
2010-10-30 15:19:04 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
def assert_middleware_usage(self, middleware, request, view, template_response, response, exception):
|
2010-10-30 15:19:04 +08:00
|
|
|
self.assertEqual(middleware.process_request_called, request)
|
|
|
|
self.assertEqual(middleware.process_view_called, view)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assertEqual(middleware.process_template_response_called, template_response)
|
2010-10-30 15:19:04 +08:00
|
|
|
self.assertEqual(middleware.process_response_called, response)
|
|
|
|
self.assertEqual(middleware.process_exception_called, exception)
|
|
|
|
|
|
|
|
|
|
|
|
class MiddlewareTests(BaseMiddlewareExceptionTest):
|
|
|
|
|
|
|
|
def test_process_request_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = RequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
|
|
|
|
|
|
|
def test_process_template_response_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = TemplateResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/template_response/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, True, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, True, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, True, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = RequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
|
|
|
|
|
|
|
def test_process_template_response_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = TemplateResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, True)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, True)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = RequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view'], Exception())
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, True)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = RequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assert_exceptions_handled(
|
|
|
|
'/middleware_exceptions/null_view/', [
|
|
|
|
"The view middleware_exceptions.views.null_view didn't return "
|
|
|
|
"an HttpResponse object. It returned None instead."
|
|
|
|
],
|
|
|
|
ValueError()
|
|
|
|
)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assert_exceptions_handled(
|
|
|
|
'/middleware_exceptions/null_view/', [
|
|
|
|
"The view middleware_exceptions.views.null_view didn't return "
|
|
|
|
"an HttpResponse object. It returned None instead."
|
|
|
|
],
|
|
|
|
ValueError()
|
|
|
|
)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = RequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, True)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = ExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
|
|
|
|
|
|
|
def test_process_template_response_error(self):
|
|
|
|
middleware = TestMiddleware()
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/template_response_error/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
|
|
|
self.assert_middleware_usage(middleware, True, True, True, True, False)
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
@override_settings(MIDDLEWARE=['middleware_exceptions.middleware.ProcessExceptionMiddleware'])
|
2015-06-05 21:30:03 +08:00
|
|
|
def test_exception_in_render_passed_to_process_exception(self):
|
|
|
|
# Repopulate the list of middlewares since it's already been populated
|
2015-11-07 23:12:37 +08:00
|
|
|
# by setUp() before the MIDDLEWARE setting got overridden.
|
2015-06-05 21:30:03 +08:00
|
|
|
self.client.handler.load_middleware()
|
|
|
|
response = self.client.get('/middleware_exceptions/exception_in_render/')
|
|
|
|
self.assertEqual(response.content, b'Exception caught')
|
|
|
|
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
class BadMiddlewareTests(BaseMiddlewareExceptionTest):
|
|
|
|
|
|
|
|
def test_process_request_bad_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadRequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Request Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_bad_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test View Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
|
|
|
|
|
|
|
def test_process_template_response_bad_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadTemplateResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assert_exceptions_handled(
|
|
|
|
'/middleware_exceptions/template_response/',
|
|
|
|
['Test Template Response Exception']
|
|
|
|
)
|
2010-12-07 21:57:01 +08:00
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, True, True, False)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, True, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_bad_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Response Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, False, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_bad_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', [])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_bad_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadRequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Request Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_bad_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test View Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_bad_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Response Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, False, True)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, True)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_bad_middleware_not_found(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Exception Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_bad_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadRequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Request Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_bad_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test View Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_bad_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view', 'Test Response Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, False, True)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, True)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_bad_middleware_exception(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Exception Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_bad_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadRequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test Request Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_bad_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test View Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_bad_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assert_exceptions_handled(
|
|
|
|
'/middleware_exceptions/null_view/', [
|
|
|
|
"The view middleware_exceptions.views.null_view didn't return "
|
|
|
|
"an HttpResponse object. It returned None instead.",
|
|
|
|
'Test Response Exception'
|
|
|
|
]
|
|
|
|
)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, False, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_bad_middleware_null_view(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assert_exceptions_handled(
|
|
|
|
'/middleware_exceptions/null_view/', [
|
|
|
|
"The view middleware_exceptions.views.null_view didn't return "
|
|
|
|
"an HttpResponse object. It returned None instead."
|
|
|
|
],
|
|
|
|
ValueError()
|
|
|
|
)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_request_bad_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadRequestMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Request Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, False, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, False, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, False, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_view_bad_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadViewMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test View Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, False)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, False, False, True, False)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_response_bad_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Response Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, False, True)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, True)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-10-30 15:19:04 +08:00
|
|
|
|
|
|
|
def test_process_exception_bad_middleware_permission_denied(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
bad_middleware = BadExceptionMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(bad_middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Exception Exception'])
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
2013-11-03 03:37:48 +08:00
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(bad_middleware, True, True, False, True, True)
|
2010-12-07 21:57:01 +08:00
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, True)
|
2010-11-08 00:02:22 +08:00
|
|
|
|
2014-06-08 01:48:45 +08:00
|
|
|
def test_process_response_no_response_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = NoResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
|
|
|
self.assert_exceptions_handled('/middleware_exceptions/view/', [
|
|
|
|
"NoResponseMiddleware.process_response didn't return an HttpResponse object. It returned None instead."
|
|
|
|
],
|
|
|
|
ValueError())
|
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, False, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, False, True, False)
|
|
|
|
|
|
|
|
def test_process_template_response_no_response_middleware(self):
|
|
|
|
pre_middleware = TestMiddleware()
|
|
|
|
middleware = NoTemplateResponseMiddleware()
|
|
|
|
post_middleware = TestMiddleware()
|
|
|
|
self._add_middleware(post_middleware)
|
|
|
|
self._add_middleware(middleware)
|
|
|
|
self._add_middleware(pre_middleware)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assert_exceptions_handled(
|
|
|
|
'/middleware_exceptions/template_response/', [
|
|
|
|
"NoTemplateResponseMiddleware.process_template_response didn't "
|
|
|
|
"return an HttpResponse object. It returned None instead."
|
|
|
|
],
|
|
|
|
ValueError()
|
|
|
|
)
|
2014-06-08 01:48:45 +08:00
|
|
|
|
|
|
|
# Check that the right middleware methods have been invoked
|
|
|
|
self.assert_middleware_usage(pre_middleware, True, True, False, True, False)
|
|
|
|
self.assert_middleware_usage(middleware, True, True, True, True, False)
|
|
|
|
self.assert_middleware_usage(post_middleware, True, True, True, True, False)
|
2010-11-08 00:02:22 +08:00
|
|
|
|
|
|
|
_missing = object()
|
2013-11-03 12:36:09 +08:00
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='middleware_exceptions.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class RootUrlconfTests(SimpleTestCase):
|
2011-04-02 21:27:17 +08:00
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(ROOT_URLCONF=None)
|
2010-11-08 00:02:22 +08:00
|
|
|
def test_missing_root_urlconf(self):
|
2013-10-14 21:14:17 +08:00
|
|
|
# Removing ROOT_URLCONF is safe, as override_settings will restore
|
|
|
|
# the previously defined settings.
|
|
|
|
del settings.ROOT_URLCONF
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
self.client.get("/middleware_exceptions/view/")
|
2014-11-08 03:57:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MyMiddleware(object):
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
def __init__(self, get_response=None):
|
2014-11-08 03:57:04 +08:00
|
|
|
raise MiddlewareNotUsed
|
|
|
|
|
|
|
|
def process_request(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class MyMiddlewareWithExceptionMessage(object):
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
def __init__(self, get_response=None):
|
2014-11-08 03:57:04 +08:00
|
|
|
raise MiddlewareNotUsed('spam eggs')
|
|
|
|
|
|
|
|
def process_request(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
DEBUG=True,
|
|
|
|
ROOT_URLCONF='middleware_exceptions.urls',
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=['django.middleware.common.CommonMiddleware'],
|
2014-11-08 03:57:04 +08:00
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
class MiddlewareNotUsedTests(SimpleTestCase):
|
2014-11-08 03:57:04 +08:00
|
|
|
|
|
|
|
rf = RequestFactory()
|
|
|
|
|
|
|
|
def test_raise_exception(self):
|
|
|
|
request = self.rf.get('middleware_exceptions/view/')
|
|
|
|
with self.assertRaises(MiddlewareNotUsed):
|
|
|
|
MyMiddleware().process_request(request)
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
@override_settings(MIDDLEWARE=['middleware_exceptions.tests.MyMiddleware'])
|
2014-11-08 03:57:04 +08:00
|
|
|
def test_log(self):
|
|
|
|
with patch_logger('django.request', 'debug') as calls:
|
|
|
|
self.client.get('/middleware_exceptions/view/')
|
|
|
|
self.assertEqual(len(calls), 1)
|
|
|
|
self.assertEqual(
|
|
|
|
calls[0],
|
|
|
|
"MiddlewareNotUsed: 'middleware_exceptions.tests.MyMiddleware'"
|
|
|
|
)
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
@override_settings(MIDDLEWARE=['middleware_exceptions.tests.MyMiddlewareWithExceptionMessage'])
|
2014-11-08 03:57:04 +08:00
|
|
|
def test_log_custom_message(self):
|
|
|
|
with patch_logger('django.request', 'debug') as calls:
|
|
|
|
self.client.get('/middleware_exceptions/view/')
|
|
|
|
self.assertEqual(len(calls), 1)
|
|
|
|
self.assertEqual(
|
|
|
|
calls[0],
|
|
|
|
"MiddlewareNotUsed('middleware_exceptions.tests.MyMiddlewareWithExceptionMessage'): spam eggs"
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(DEBUG=False)
|
|
|
|
def test_do_not_log_when_debug_is_false(self):
|
|
|
|
with patch_logger('django.request', 'debug') as calls:
|
|
|
|
self.client.get('/middleware_exceptions/view/')
|
|
|
|
self.assertEqual(len(calls), 0)
|
2015-11-07 23:12:37 +08:00
|
|
|
|
|
|
|
|
2016-05-10 01:13:55 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2015-11-07 23:12:37 +08:00
|
|
|
@override_settings(
|
|
|
|
MIDDLEWARE_CLASSES=['django.middleware.common.CommonMiddleware'],
|
|
|
|
MIDDLEWARE=None,
|
|
|
|
)
|
|
|
|
class MiddlewareNotUsedMiddlewareClassesTests(MiddlewareNotUsedTests):
|
|
|
|
pass
|