diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py
index df79ad2029..6002d54443 100644
--- a/django/core/handlers/base.py
+++ b/django/core/handlers/base.py
@@ -78,39 +78,43 @@ class BaseHandler(object):
urlconf = settings.ROOT_URLCONF
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
+ response = None
# Apply request middleware
for middleware_method in self._request_middleware:
response = middleware_method(request)
if response:
- return response
+ break
- if hasattr(request, "urlconf"):
- # Reset url resolver with a custom urlconf.
- urlconf = request.urlconf
- urlresolvers.set_urlconf(urlconf)
- resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
+ if response is None:
+ if hasattr(request, "urlconf"):
+ # Reset url resolver with a custom urlconf.
+ urlconf = request.urlconf
+ urlresolvers.set_urlconf(urlconf)
+ resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
- callback, callback_args, callback_kwargs = resolver.resolve(
- request.path_info)
+ callback, callback_args, callback_kwargs = resolver.resolve(
+ request.path_info)
- # Apply view middleware
- for middleware_method in self._view_middleware:
- response = middleware_method(request, callback, callback_args, callback_kwargs)
- if response:
- return response
-
- try:
- response = callback(request, *callback_args, **callback_kwargs)
- except Exception, e:
- # If the view raised an exception, run it through exception
- # middleware, and if the exception middleware returns a
- # response, use that. Otherwise, reraise the exception.
- for middleware_method in self._exception_middleware:
- response = middleware_method(request, e)
+ # Apply view middleware
+ for middleware_method in self._view_middleware:
+ response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
- return response
- raise
+ break
+
+ if response is None:
+ try:
+ response = callback(request, *callback_args, **callback_kwargs)
+ except Exception, e:
+ # If the view raised an exception, run it through exception
+ # middleware, and if the exception middleware returns a
+ # response, use that. Otherwise, reraise the exception.
+ for middleware_method in self._exception_middleware:
+ response = middleware_method(request, e)
+ if response:
+ break
+ if response is None:
+ raise
# Complain if the view returned None (a common error).
if response is None:
@@ -120,12 +124,6 @@ class BaseHandler(object):
view_name = callback.__class__.__name__ + '.__call__' # If it's a class
raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))
- # Apply response middleware
- for middleware_method in self._response_middleware:
- response = middleware_method(request, response)
- response = self.apply_response_fixes(request, response)
-
- return response
except http.Http404, e:
logger.warning('Not Found: %s' % request.path,
extra={
@@ -134,14 +132,14 @@ class BaseHandler(object):
})
if settings.DEBUG:
from django.views import debug
- return debug.technical_404_response(request, e)
+ response = debug.technical_404_response(request, e)
else:
try:
callback, param_dict = resolver.resolve404()
- return callback(request, **param_dict)
+ response = callback(request, **param_dict)
except:
try:
- return self.handle_uncaught_exception(request, resolver, sys.exc_info())
+ response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
finally:
receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
except exceptions.PermissionDenied:
@@ -150,19 +148,30 @@ class BaseHandler(object):
'status_code': 403,
'request': request
})
- return http.HttpResponseForbidden('
Permission denied
')
+ response = http.HttpResponseForbidden('Permission denied
')
except SystemExit:
# Allow sys.exit() to actually exit. See tickets #1023 and #4701
raise
except: # Handle everything else, including SuspiciousOperation, etc.
# Get the exception info now, in case another exception is thrown later.
receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
- return self.handle_uncaught_exception(request, resolver, sys.exc_info())
+ response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
finally:
# Reset URLconf for this thread on the way out for complete
# isolation of request.urlconf
urlresolvers.set_urlconf(None)
+ try:
+ # Apply response middleware, regardless of the response
+ for middleware_method in self._response_middleware:
+ response = middleware_method(request, response)
+ response = self.apply_response_fixes(request, response)
+ except: # Any exception should be gathered and handled
+ receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
+ response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
+
+ return response
+
def handle_uncaught_exception(self, request, resolver, exc_info):
"""
Processing for any otherwise uncaught exceptions (those that will
diff --git a/django/test/client.py b/django/test/client.py
index 1baf6176ed..4f09726900 100644
--- a/django/test/client.py
+++ b/django/test/client.py
@@ -81,11 +81,6 @@ class ClientHandler(BaseHandler):
# admin views.
request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
response = self.get_response(request)
-
- # Apply response middleware.
- for middleware_method in self._response_middleware:
- response = middleware_method(request, response)
- response = self.apply_response_fixes(request, response)
finally:
signals.request_finished.disconnect(close_connection)
signals.request_finished.send(sender=self.__class__)
diff --git a/tests/regressiontests/middleware_exceptions/tests.py b/tests/regressiontests/middleware_exceptions/tests.py
index 93c2a89ab3..7c456bafb4 100644
--- a/tests/regressiontests/middleware_exceptions/tests.py
+++ b/tests/regressiontests/middleware_exceptions/tests.py
@@ -2,19 +2,81 @@ import sys
from django.test import TestCase
from django.core.signals import got_request_exception
+from django.http import HttpResponse
+
class TestException(Exception):
pass
-class TestRequestMiddleware(object):
+# A middleware base class that tracks which methods have been called
+
+class TestMiddleware(object):
+ def __init__(self):
+ self.process_request_called = False
+ self.process_view_called = False
+ self.process_response_called = False
+ self.process_exception_called = False
+
def process_request(self, request):
- raise TestException('Test Exception')
+ self.process_request_called = True
+
+ def process_view(self, request, view_func, view_args, view_kwargs):
+ self.process_view_called = True
-class TestResponseMiddleware(object):
def process_response(self, request, response):
- raise TestException('Test Exception')
+ self.process_response_called = True
+ return response
-class MiddlewareExceptionTest(TestCase):
+ def process_exception(self, request, exception):
+ self.process_exception_called = True
+
+# Middleware examples that do the right thing
+
+class RequestMiddleware(TestMiddleware):
+ def process_request(self, request):
+ super(RequestMiddleware, self).process_request(request)
+ return HttpResponse('Request Middleware')
+
+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')
+
+class ResponseMiddleware(TestMiddleware):
+ def process_response(self, request, response):
+ super(ResponseMiddleware, self).process_response(request, response)
+ return HttpResponse('Response Middleware')
+
+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')
+
+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')
+
+class BadResponseMiddleware(TestMiddleware):
+ def process_response(self, request, response):
+ super(BadResponseMiddleware, self).process_response(request, response)
+ raise TestException('Test Response Exception')
+
+class BadExceptionMiddleware(TestMiddleware):
+ def process_exception(self, request, exception):
+ super(BadExceptionMiddleware, self).process_exception(request, exception)
+ raise TestException('Test Exception Exception')
+
+
+class BaseMiddlewareExceptionTest(TestCase):
def setUp(self):
self.exceptions = []
got_request_exception.connect(self._on_request_exception)
@@ -27,9 +89,15 @@ class MiddlewareExceptionTest(TestCase):
def _on_request_exception(self, sender, request, **kwargs):
self.exceptions.append(sys.exc_info())
- def _assert_exception_handled(self):
+ 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)
+ 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):
try:
- response = self.client.get('/middleware_exceptions/')
+ response = self.client.get(url)
except TestException, e:
# Test client intentionally re-raises any exceptions being raised
# during request handling. Hence actual testing that exception was
@@ -37,15 +105,592 @@ class MiddlewareExceptionTest(TestCase):
# signal being sent.
pass
except Exception, e:
- self.fail("Unexpected exception: %s" % e)
- self.assertEquals(len(self.exceptions), 1)
- exception, value, tb = self.exceptions[0]
- self.assertEquals(value.args, ('Test Exception', ))
+ if type(extra_error) != type(e):
+ self.fail("Unexpected exception: %s" % e)
+ self.assertEquals(len(self.exceptions), len(errors))
+ for i, error in enumerate(errors):
+ exception, value, tb = self.exceptions[i]
+ self.assertEquals(value.args, (error, ))
- def test_process_request(self):
- self.client.handler._request_middleware.insert(0, TestRequestMiddleware().process_request)
- self._assert_exception_handled()
+ def assert_middleware_usage(self, middleware, request, view, response, exception):
+ self.assertEqual(middleware.process_request_called, request)
+ self.assertEqual(middleware.process_view_called, view)
+ self.assertEqual(middleware.process_response_called, response)
+ self.assertEqual(middleware.process_exception_called, exception)
- def test_process_response(self):
- self.client.handler._response_middleware.insert(0, TestResponseMiddleware().process_response)
- self._assert_exception_handled()
+
+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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, True)
+ self.assert_middleware_usage(middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, True)
+ self.assert_middleware_usage(middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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)
+ self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
+ "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object.",
+ ],
+ ValueError())
+
+ # Check that the right middleware methods have been invoked
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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)
+ self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
+ "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object."
+ ],
+ ValueError())
+
+ # Check that the right middleware methods have been invoked
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, True)
+ self.assert_middleware_usage(middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(bad_middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, False, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(bad_middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, False, True)
+ self.assert_middleware_usage(bad_middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(bad_middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, False, True)
+ self.assert_middleware_usage(bad_middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(bad_middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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)
+ self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
+ "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object.",
+ 'Test Response Exception'
+ ])
+
+ # Check that the right middleware methods have been invoked
+ self.assert_middleware_usage(pre_middleware, True, True, False, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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)
+ self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
+ "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object."
+ ],
+ ValueError())
+
+ # Check that the right middleware methods have been invoked
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, True, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, False, True, False)
+ self.assert_middleware_usage(bad_middleware, True, False, True, False)
+ self.assert_middleware_usage(post_middleware, False, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, False)
+ self.assert_middleware_usage(post_middleware, True, False, True, False)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, False, True)
+ self.assert_middleware_usage(bad_middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
+
+ 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
+ self.assert_middleware_usage(pre_middleware, True, True, True, False)
+ self.assert_middleware_usage(bad_middleware, True, True, True, True)
+ self.assert_middleware_usage(post_middleware, True, True, True, True)
diff --git a/tests/regressiontests/middleware_exceptions/urls.py b/tests/regressiontests/middleware_exceptions/urls.py
index 63f3ba1496..5b477a4ede 100644
--- a/tests/regressiontests/middleware_exceptions/urls.py
+++ b/tests/regressiontests/middleware_exceptions/urls.py
@@ -4,5 +4,9 @@ from django.conf.urls.defaults import *
import views
urlpatterns = patterns('',
- (r'^$', views.index),
+ (r'^view/$', views.normal_view),
+ (r'^not_found/$', views.not_found),
+ (r'^error/$', views.server_error),
+ (r'^null_view/$', views.null_view),
+ (r'^permission_denied/$', views.permission_denied),
)
diff --git a/tests/regressiontests/middleware_exceptions/views.py b/tests/regressiontests/middleware_exceptions/views.py
index e2d597d9b1..96dbd05095 100644
--- a/tests/regressiontests/middleware_exceptions/views.py
+++ b/tests/regressiontests/middleware_exceptions/views.py
@@ -1,4 +1,17 @@
from django import http
+from django.core.exceptions import PermissionDenied
-def index(request):
- return http.HttpResponse('')
+def normal_view(request):
+ return http.HttpResponse('OK')
+
+def not_found(request):
+ raise http.Http404()
+
+def server_error(request):
+ raise Exception('Error in view')
+
+def null_view(request):
+ return None
+
+def permission_denied(request):
+ raise PermissionDenied()