Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14588 -- Corrected r14393 to ensure that response middlewares …

…are are always applied. Includes a battery of tests to validate the expected behavior of the middleware cycle. Thanks to Petr Marhoun for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14398 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 52e0bcbc62c4df5d488b18f6a8e2cba798ec0807 1 parent 20e09ca
Russell Keith-Magee authored October 30, 2010
83  django/core/handlers/base.py
@@ -78,39 +78,43 @@ def get_response(self, request):
78 78
                 urlconf = settings.ROOT_URLCONF
79 79
                 urlresolvers.set_urlconf(urlconf)
80 80
                 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
  81
+                response = None
81 82
 
82 83
                 # Apply request middleware
83 84
                 for middleware_method in self._request_middleware:
84 85
                     response = middleware_method(request)
85 86
                     if response:
86  
-                        return response
  87
+                        break
87 88
 
88  
-                if hasattr(request, "urlconf"):
89  
-                    # Reset url resolver with a custom urlconf.
90  
-                    urlconf = request.urlconf
91  
-                    urlresolvers.set_urlconf(urlconf)
92  
-                    resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
93  
-
94  
-                callback, callback_args, callback_kwargs = resolver.resolve(
95  
-                        request.path_info)
96  
-
97  
-                # Apply view middleware
98  
-                for middleware_method in self._view_middleware:
99  
-                    response = middleware_method(request, callback, callback_args, callback_kwargs)
100  
-                    if response:
101  
-                        return response
102  
-
103  
-                try:
104  
-                    response = callback(request, *callback_args, **callback_kwargs)
105  
-                except Exception, e:
106  
-                    # If the view raised an exception, run it through exception
107  
-                    # middleware, and if the exception middleware returns a
108  
-                    # response, use that. Otherwise, reraise the exception.
109  
-                    for middleware_method in self._exception_middleware:
110  
-                        response = middleware_method(request, e)
  89
+                if response is None:
  90
+                    if hasattr(request, "urlconf"):
  91
+                        # Reset url resolver with a custom urlconf.
  92
+                        urlconf = request.urlconf
  93
+                        urlresolvers.set_urlconf(urlconf)
  94
+                        resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
  95
+
  96
+                    callback, callback_args, callback_kwargs = resolver.resolve(
  97
+                            request.path_info)
  98
+
  99
+                    # Apply view middleware
  100
+                    for middleware_method in self._view_middleware:
  101
+                        response = middleware_method(request, callback, callback_args, callback_kwargs)
111 102
                         if response:
112  
-                            return response
113  
-                    raise
  103
+                            break
  104
+
  105
+                if response is None:
  106
+                    try:
  107
+                        response = callback(request, *callback_args, **callback_kwargs)
  108
+                    except Exception, e:
  109
+                        # If the view raised an exception, run it through exception
  110
+                        # middleware, and if the exception middleware returns a
  111
+                        # response, use that. Otherwise, reraise the exception.
  112
+                        for middleware_method in self._exception_middleware:
  113
+                            response = middleware_method(request, e)
  114
+                            if response:
  115
+                                break
  116
+                        if response is None:
  117
+                            raise
114 118
 
115 119
                 # Complain if the view returned None (a common error).
116 120
                 if response is None:
@@ -120,12 +124,6 @@ def get_response(self, request):
120 124
                         view_name = callback.__class__.__name__ + '.__call__' # If it's a class
121 125
                     raise ValueError("The view %s.%s didn't return an HttpResponse object." % (callback.__module__, view_name))
122 126
 
123  
-                # Apply response middleware
124  
-                for middleware_method in self._response_middleware:
125  
-                    response = middleware_method(request, response)
126  
-                response = self.apply_response_fixes(request, response)
127  
-
128  
-                return response
129 127
             except http.Http404, e:
130 128
                 logger.warning('Not Found: %s' % request.path,
131 129
                             extra={
@@ -134,14 +132,14 @@ def get_response(self, request):
134 132
                             })
135 133
                 if settings.DEBUG:
136 134
                     from django.views import debug
137  
-                    return debug.technical_404_response(request, e)
  135
+                    response = debug.technical_404_response(request, e)
138 136
                 else:
139 137
                     try:
140 138
                         callback, param_dict = resolver.resolve404()
141  
-                        return callback(request, **param_dict)
  139
+                        response = callback(request, **param_dict)
142 140
                     except:
143 141
                         try:
144  
-                            return self.handle_uncaught_exception(request, resolver, sys.exc_info())
  142
+                            response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
145 143
                         finally:
146 144
                             receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
147 145
             except exceptions.PermissionDenied:
@@ -150,19 +148,30 @@ def get_response(self, request):
150 148
                                 'status_code': 403,
151 149
                                 'request': request
152 150
                             })
153  
-                return http.HttpResponseForbidden('<h1>Permission denied</h1>')
  151
+                response = http.HttpResponseForbidden('<h1>Permission denied</h1>')
154 152
             except SystemExit:
155 153
                 # Allow sys.exit() to actually exit. See tickets #1023 and #4701
156 154
                 raise
157 155
             except: # Handle everything else, including SuspiciousOperation, etc.
158 156
                 # Get the exception info now, in case another exception is thrown later.
159 157
                 receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
160  
-                return self.handle_uncaught_exception(request, resolver, sys.exc_info())
  158
+                response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
161 159
         finally:
162 160
             # Reset URLconf for this thread on the way out for complete
163 161
             # isolation of request.urlconf
164 162
             urlresolvers.set_urlconf(None)
165 163
 
  164
+        try:
  165
+            # Apply response middleware, regardless of the response
  166
+            for middleware_method in self._response_middleware:
  167
+                response = middleware_method(request, response)
  168
+            response = self.apply_response_fixes(request, response)
  169
+        except: # Any exception should be gathered and handled
  170
+            receivers = signals.got_request_exception.send(sender=self.__class__, request=request)
  171
+            response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  172
+
  173
+        return response
  174
+
166 175
     def handle_uncaught_exception(self, request, resolver, exc_info):
167 176
         """
168 177
         Processing for any otherwise uncaught exceptions (those that will
5  django/test/client.py
@@ -81,11 +81,6 @@ def __call__(self, environ):
81 81
             # admin views.
82 82
             request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
83 83
             response = self.get_response(request)
84  
-
85  
-            # Apply response middleware.
86  
-            for middleware_method in self._response_middleware:
87  
-                response = middleware_method(request, response)
88  
-            response = self.apply_response_fixes(request, response)
89 84
         finally:
90 85
             signals.request_finished.disconnect(close_connection)
91 86
             signals.request_finished.send(sender=self.__class__)
683  tests/regressiontests/middleware_exceptions/tests.py
@@ -2,19 +2,81 @@
2 2
 
3 3
 from django.test import TestCase
4 4
 from django.core.signals import got_request_exception
  5
+from django.http import HttpResponse
  6
+
5 7
 
6 8
 class TestException(Exception):
7 9
     pass
8 10
 
9  
-class TestRequestMiddleware(object):
  11
+# A middleware base class that tracks which methods have been called
  12
+
  13
+class TestMiddleware(object):
  14
+    def __init__(self):
  15
+        self.process_request_called = False
  16
+        self.process_view_called = False
  17
+        self.process_response_called = False
  18
+        self.process_exception_called = False
  19
+
  20
+    def process_request(self, request):
  21
+        self.process_request_called = True
  22
+
  23
+    def process_view(self, request, view_func, view_args, view_kwargs):
  24
+        self.process_view_called = True
  25
+
  26
+    def process_response(self, request, response):
  27
+        self.process_response_called = True
  28
+        return response
  29
+
  30
+    def process_exception(self, request, exception):
  31
+        self.process_exception_called = True
  32
+
  33
+# Middleware examples that do the right thing
  34
+
  35
+class RequestMiddleware(TestMiddleware):
10 36
     def process_request(self, request):
11  
-        raise TestException('Test Exception')
  37
+        super(RequestMiddleware, self).process_request(request)
  38
+        return HttpResponse('Request Middleware')
  39
+
  40
+class ViewMiddleware(TestMiddleware):
  41
+    def process_view(self, request, view_func, view_args, view_kwargs):
  42
+        super(ViewMiddleware, self).process_view(request, view_func, view_args, view_kwargs)
  43
+        return HttpResponse('View Middleware')
12 44
 
13  
-class TestResponseMiddleware(object):
  45
+class ResponseMiddleware(TestMiddleware):
14 46
     def process_response(self, request, response):
15  
-        raise TestException('Test Exception')
  47
+        super(ResponseMiddleware, self).process_response(request, response)
  48
+        return HttpResponse('Response Middleware')
  49
+
  50
+class ExceptionMiddleware(TestMiddleware):
  51
+    def process_exception(self, request, exception):
  52
+        super(ExceptionMiddleware, self).process_exception(request, exception)
  53
+        return HttpResponse('Exception Middleware')
  54
+
  55
+
  56
+# Sample middlewares that raise exceptions
16 57
 
17  
-class MiddlewareExceptionTest(TestCase):
  58
+class BadRequestMiddleware(TestMiddleware):
  59
+    def process_request(self, request):
  60
+        super(BadRequestMiddleware, self).process_request(request)
  61
+        raise TestException('Test Request Exception')
  62
+
  63
+class BadViewMiddleware(TestMiddleware):
  64
+    def process_view(self, request, view_func, view_args, view_kwargs):
  65
+        super(BadViewMiddleware, self).process_view(request, view_func, view_args, view_kwargs)
  66
+        raise TestException('Test View Exception')
  67
+
  68
+class BadResponseMiddleware(TestMiddleware):
  69
+    def process_response(self, request, response):
  70
+        super(BadResponseMiddleware, self).process_response(request, response)
  71
+        raise TestException('Test Response Exception')
  72
+
  73
+class BadExceptionMiddleware(TestMiddleware):
  74
+    def process_exception(self, request, exception):
  75
+        super(BadExceptionMiddleware, self).process_exception(request, exception)
  76
+        raise TestException('Test Exception Exception')
  77
+
  78
+
  79
+class BaseMiddlewareExceptionTest(TestCase):
18 80
     def setUp(self):
19 81
         self.exceptions = []
20 82
         got_request_exception.connect(self._on_request_exception)
@@ -27,9 +89,15 @@ def tearDown(self):
27 89
     def _on_request_exception(self, sender, request, **kwargs):
28 90
         self.exceptions.append(sys.exc_info())
29 91
 
30  
-    def _assert_exception_handled(self):
  92
+    def _add_middleware(self, middleware):
  93
+        self.client.handler._request_middleware.insert(0, middleware.process_request)
  94
+        self.client.handler._view_middleware.insert(0, middleware.process_view)
  95
+        self.client.handler._response_middleware.append(middleware.process_response)
  96
+        self.client.handler._exception_middleware.append(middleware.process_exception)
  97
+
  98
+    def assert_exceptions_handled(self, url, errors, extra_error=None):
31 99
         try:
32  
-            response = self.client.get('/middleware_exceptions/')
  100
+            response = self.client.get(url)
33 101
         except TestException, e:
34 102
             # Test client intentionally re-raises any exceptions being raised
35 103
             # during request handling. Hence actual testing that exception was
@@ -37,15 +105,592 @@ def _assert_exception_handled(self):
37 105
             # signal being sent.
38 106
             pass
39 107
         except Exception, e:
40  
-            self.fail("Unexpected exception: %s" % e)
41  
-        self.assertEquals(len(self.exceptions), 1)
42  
-        exception, value, tb = self.exceptions[0]
43  
-        self.assertEquals(value.args, ('Test Exception', ))
44  
-
45  
-    def test_process_request(self):
46  
-        self.client.handler._request_middleware.insert(0, TestRequestMiddleware().process_request)
47  
-        self._assert_exception_handled()
48  
-
49  
-    def test_process_response(self):
50  
-        self.client.handler._response_middleware.insert(0, TestResponseMiddleware().process_response)
51  
-        self._assert_exception_handled()
  108
+            if type(extra_error) != type(e):
  109
+                self.fail("Unexpected exception: %s" % e)
  110
+        self.assertEquals(len(self.exceptions), len(errors))
  111
+        for i, error in enumerate(errors):
  112
+            exception, value, tb = self.exceptions[i]
  113
+            self.assertEquals(value.args, (error, ))
  114
+
  115
+    def assert_middleware_usage(self, middleware, request, view, response, exception):
  116
+        self.assertEqual(middleware.process_request_called, request)
  117
+        self.assertEqual(middleware.process_view_called, view)
  118
+        self.assertEqual(middleware.process_response_called, response)
  119
+        self.assertEqual(middleware.process_exception_called, exception)
  120
+
  121
+
  122
+class MiddlewareTests(BaseMiddlewareExceptionTest):
  123
+
  124
+    def test_process_request_middleware(self):
  125
+        pre_middleware = TestMiddleware()
  126
+        middleware = RequestMiddleware()
  127
+        post_middleware = TestMiddleware()
  128
+        self._add_middleware(post_middleware)
  129
+        self._add_middleware(middleware)
  130
+        self._add_middleware(pre_middleware)
  131
+        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
  132
+
  133
+        # Check that the right middleware methods have been invoked
  134
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  135
+        self.assert_middleware_usage(middleware,      True,  False, True,  False)
  136
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  137
+
  138
+    def test_process_view_middleware(self):
  139
+        pre_middleware = TestMiddleware()
  140
+        middleware = ViewMiddleware()
  141
+        post_middleware = TestMiddleware()
  142
+        self._add_middleware(post_middleware)
  143
+        self._add_middleware(middleware)
  144
+        self._add_middleware(pre_middleware)
  145
+        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
  146
+
  147
+        # Check that the right middleware methods have been invoked
  148
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  149
+        self.assert_middleware_usage(middleware,      True,  True,  True, False)
  150
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  151
+
  152
+    def test_process_response_middleware(self):
  153
+        pre_middleware = TestMiddleware()
  154
+        middleware = ResponseMiddleware()
  155
+        post_middleware = TestMiddleware()
  156
+        self._add_middleware(post_middleware)
  157
+        self._add_middleware(middleware)
  158
+        self._add_middleware(pre_middleware)
  159
+        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
  160
+
  161
+        # Check that the right middleware methods have been invoked
  162
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  False)
  163
+        self.assert_middleware_usage(middleware,      True,  True,  True,  False)
  164
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
  165
+
  166
+    def test_process_exception_middleware(self):
  167
+        pre_middleware = TestMiddleware()
  168
+        middleware = ExceptionMiddleware()
  169
+        post_middleware = TestMiddleware()
  170
+        self._add_middleware(post_middleware)
  171
+        self._add_middleware(middleware)
  172
+        self._add_middleware(pre_middleware)
  173
+        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
  174
+
  175
+        # Check that the right middleware methods have been invoked
  176
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  177
+        self.assert_middleware_usage(middleware,      True,  True,  True, False)
  178
+        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
  179
+
  180
+    def test_process_request_middleware_not_found(self):
  181
+        pre_middleware = TestMiddleware()
  182
+        middleware = RequestMiddleware()
  183
+        post_middleware = TestMiddleware()
  184
+        self._add_middleware(post_middleware)
  185
+        self._add_middleware(middleware)
  186
+        self._add_middleware(pre_middleware)
  187
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
  188
+
  189
+        # Check that the right middleware methods have been invoked
  190
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  191
+        self.assert_middleware_usage(middleware,      True,  False, True,  False)
  192
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  193
+
  194
+    def test_process_view_middleware_not_found(self):
  195
+        pre_middleware = TestMiddleware()
  196
+        middleware = ViewMiddleware()
  197
+        post_middleware = TestMiddleware()
  198
+        self._add_middleware(post_middleware)
  199
+        self._add_middleware(middleware)
  200
+        self._add_middleware(pre_middleware)
  201
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
  202
+
  203
+        # Check that the right middleware methods have been invoked
  204
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  205
+        self.assert_middleware_usage(middleware,      True,  True,  True, False)
  206
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  207
+
  208
+    def test_process_response_middleware_not_found(self):
  209
+        pre_middleware = TestMiddleware()
  210
+        middleware = ResponseMiddleware()
  211
+        post_middleware = TestMiddleware()
  212
+        self._add_middleware(post_middleware)
  213
+        self._add_middleware(middleware)
  214
+        self._add_middleware(pre_middleware)
  215
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
  216
+
  217
+        # Check that the right middleware methods have been invoked
  218
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  True)
  219
+        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
  220
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  221
+
  222
+    def test_process_exception_middleware_not_found(self):
  223
+        pre_middleware = TestMiddleware()
  224
+        middleware = ExceptionMiddleware()
  225
+        post_middleware = TestMiddleware()
  226
+        self._add_middleware(post_middleware)
  227
+        self._add_middleware(middleware)
  228
+        self._add_middleware(pre_middleware)
  229
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', [])
  230
+
  231
+        # Check that the right middleware methods have been invoked
  232
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  233
+        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
  234
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  235
+
  236
+    def test_process_request_middleware_exception(self):
  237
+        pre_middleware = TestMiddleware()
  238
+        middleware = RequestMiddleware()
  239
+        post_middleware = TestMiddleware()
  240
+        self._add_middleware(post_middleware)
  241
+        self._add_middleware(middleware)
  242
+        self._add_middleware(pre_middleware)
  243
+        self.assert_exceptions_handled('/middleware_exceptions/error/', [])
  244
+
  245
+        # Check that the right middleware methods have been invoked
  246
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  247
+        self.assert_middleware_usage(middleware,  True,  False, True,  False)
  248
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  249
+
  250
+    def test_process_view_middleware_exception(self):
  251
+        pre_middleware = TestMiddleware()
  252
+        middleware = ViewMiddleware()
  253
+        post_middleware = TestMiddleware()
  254
+        self._add_middleware(post_middleware)
  255
+        self._add_middleware(middleware)
  256
+        self._add_middleware(pre_middleware)
  257
+        self.assert_exceptions_handled('/middleware_exceptions/error/', [])
  258
+
  259
+        # Check that the right middleware methods have been invoked
  260
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  261
+        self.assert_middleware_usage(middleware,      True,  True,  True, False)
  262
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  263
+
  264
+    def test_process_response_middleware_exception(self):
  265
+        pre_middleware = TestMiddleware()
  266
+        middleware = ResponseMiddleware()
  267
+        post_middleware = TestMiddleware()
  268
+        self._add_middleware(post_middleware)
  269
+        self._add_middleware(middleware)
  270
+        self._add_middleware(pre_middleware)
  271
+        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view'], Exception())
  272
+
  273
+        # Check that the right middleware methods have been invoked
  274
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  True)
  275
+        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
  276
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  277
+
  278
+    def test_process_exception_middleware_exception(self):
  279
+        pre_middleware = TestMiddleware()
  280
+        middleware = ExceptionMiddleware()
  281
+        post_middleware = TestMiddleware()
  282
+        self._add_middleware(post_middleware)
  283
+        self._add_middleware(middleware)
  284
+        self._add_middleware(pre_middleware)
  285
+        self.assert_exceptions_handled('/middleware_exceptions/error/', [])
  286
+
  287
+        # Check that the right middleware methods have been invoked
  288
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  289
+        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
  290
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  291
+
  292
+    def test_process_request_middleware_null_view(self):
  293
+        pre_middleware = TestMiddleware()
  294
+        middleware = RequestMiddleware()
  295
+        post_middleware = TestMiddleware()
  296
+        self._add_middleware(post_middleware)
  297
+        self._add_middleware(middleware)
  298
+        self._add_middleware(pre_middleware)
  299
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
  300
+
  301
+        # Check that the right middleware methods have been invoked
  302
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  303
+        self.assert_middleware_usage(middleware,      True,  False, True,  False)
  304
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  305
+
  306
+    def test_process_view_middleware_null_view(self):
  307
+        pre_middleware = TestMiddleware()
  308
+        middleware = ViewMiddleware()
  309
+        post_middleware = TestMiddleware()
  310
+        self._add_middleware(post_middleware)
  311
+        self._add_middleware(middleware)
  312
+        self._add_middleware(pre_middleware)
  313
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [])
  314
+
  315
+        # Check that the right middleware methods have been invoked
  316
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  317
+        self.assert_middleware_usage(middleware,      True,  True,  True, False)
  318
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  319
+
  320
+    def test_process_response_middleware_null_view(self):
  321
+        pre_middleware = TestMiddleware()
  322
+        middleware = ResponseMiddleware()
  323
+        post_middleware = TestMiddleware()
  324
+        self._add_middleware(post_middleware)
  325
+        self._add_middleware(middleware)
  326
+        self._add_middleware(pre_middleware)
  327
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
  328
+                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object.",
  329
+            ],
  330
+            ValueError())
  331
+
  332
+        # Check that the right middleware methods have been invoked
  333
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  False)
  334
+        self.assert_middleware_usage(middleware,      True,  True,  True,  False)
  335
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
  336
+
  337
+    def test_process_exception_middleware_null_view(self):
  338
+        pre_middleware = TestMiddleware()
  339
+        middleware = ExceptionMiddleware()
  340
+        post_middleware = TestMiddleware()
  341
+        self._add_middleware(post_middleware)
  342
+        self._add_middleware(middleware)
  343
+        self._add_middleware(pre_middleware)
  344
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
  345
+                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object."
  346
+            ],
  347
+            ValueError())
  348
+
  349
+        # Check that the right middleware methods have been invoked
  350
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  351
+        self.assert_middleware_usage(middleware,      True,  True,  True, False)
  352
+        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
  353
+
  354
+    def test_process_request_middleware_permission_denied(self):
  355
+        pre_middleware = TestMiddleware()
  356
+        middleware = RequestMiddleware()
  357
+        post_middleware = TestMiddleware()
  358
+        self._add_middleware(post_middleware)
  359
+        self._add_middleware(middleware)
  360
+        self._add_middleware(pre_middleware)
  361
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
  362
+
  363
+        # Check that the right middleware methods have been invoked
  364
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  365
+        self.assert_middleware_usage(middleware,      True,  False, True,  False)
  366
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  367
+
  368
+    def test_process_view_middleware_permission_denied(self):
  369
+        pre_middleware = TestMiddleware()
  370
+        middleware = ViewMiddleware()
  371
+        post_middleware = TestMiddleware()
  372
+        self._add_middleware(post_middleware)
  373
+        self._add_middleware(middleware)
  374
+        self._add_middleware(pre_middleware)
  375
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
  376
+
  377
+        # Check that the right middleware methods have been invoked
  378
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  379
+        self.assert_middleware_usage(middleware,      True,  True,  True, False)
  380
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  381
+
  382
+    def test_process_response_middleware_permission_denied(self):
  383
+        pre_middleware = TestMiddleware()
  384
+        middleware = ResponseMiddleware()
  385
+        post_middleware = TestMiddleware()
  386
+        self._add_middleware(post_middleware)
  387
+        self._add_middleware(middleware)
  388
+        self._add_middleware(pre_middleware)
  389
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
  390
+
  391
+        # Check that the right middleware methods have been invoked
  392
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True,  True)
  393
+        self.assert_middleware_usage(middleware,      True,  True,  True,  True)
  394
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  395
+
  396
+    def test_process_exception_middleware_permission_denied(self):
  397
+        pre_middleware = TestMiddleware()
  398
+        middleware = ExceptionMiddleware()
  399
+        post_middleware = TestMiddleware()
  400
+        self._add_middleware(post_middleware)
  401
+        self._add_middleware(middleware)
  402
+        self._add_middleware(pre_middleware)
  403
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', [])
  404
+
  405
+        # Check that the right middleware methods have been invoked
  406
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  407
+        self.assert_middleware_usage(middleware,  True,  True,  True,  True)
  408
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  409
+
  410
+class BadMiddlewareTests(BaseMiddlewareExceptionTest):
  411
+
  412
+    def test_process_request_bad_middleware(self):
  413
+        pre_middleware = TestMiddleware()
  414
+        bad_middleware = BadRequestMiddleware()
  415
+        post_middleware = TestMiddleware()
  416
+        self._add_middleware(post_middleware)
  417
+        self._add_middleware(bad_middleware)
  418
+        self._add_middleware(pre_middleware)
  419
+        self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Request Exception'])
  420
+
  421
+        # Check that the right middleware methods have been invoked
  422
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  423
+        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
  424
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  425
+
  426
+    def test_process_view_bad_middleware(self):
  427
+        pre_middleware = TestMiddleware()
  428
+        bad_middleware = BadViewMiddleware()
  429
+        post_middleware = TestMiddleware()
  430
+        self._add_middleware(post_middleware)
  431
+        self._add_middleware(bad_middleware)
  432
+        self._add_middleware(pre_middleware)
  433
+        self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test View Exception'])
  434
+
  435
+        # Check that the right middleware methods have been invoked
  436
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  437
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
  438
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  439
+
  440
+    def test_process_response_bad_middleware(self):
  441
+        pre_middleware = TestMiddleware()
  442
+        bad_middleware = BadResponseMiddleware()
  443
+        post_middleware = TestMiddleware()
  444
+        self._add_middleware(post_middleware)
  445
+        self._add_middleware(bad_middleware)
  446
+        self._add_middleware(pre_middleware)
  447
+        self.assert_exceptions_handled('/middleware_exceptions/view/', ['Test Response Exception'])
  448
+
  449
+        # Check that the right middleware methods have been invoked
  450
+        self.assert_middleware_usage(pre_middleware,  True,  True,  False, False)
  451
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  False)
  452
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
  453
+
  454
+    def test_process_exception_bad_middleware(self):
  455
+        pre_middleware = TestMiddleware()
  456
+        bad_middleware = BadExceptionMiddleware()
  457
+        post_middleware = TestMiddleware()
  458
+        self._add_middleware(post_middleware)
  459
+        self._add_middleware(bad_middleware)
  460
+        self._add_middleware(pre_middleware)
  461
+        self.assert_exceptions_handled('/middleware_exceptions/view/', [])
  462
+
  463
+        # Check that the right middleware methods have been invoked
  464
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  465
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
  466
+        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
  467
+
  468
+    def test_process_request_bad_middleware_not_found(self):
  469
+        pre_middleware = TestMiddleware()
  470
+        bad_middleware = BadRequestMiddleware()
  471
+        post_middleware = TestMiddleware()
  472
+        self._add_middleware(post_middleware)
  473
+        self._add_middleware(bad_middleware)
  474
+        self._add_middleware(pre_middleware)
  475
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Request Exception'])
  476
+
  477
+        # Check that the right middleware methods have been invoked
  478
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  479
+        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
  480
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  481
+
  482
+    def test_process_view_bad_middleware_not_found(self):
  483
+        pre_middleware = TestMiddleware()
  484
+        bad_middleware = BadViewMiddleware()
  485
+        post_middleware = TestMiddleware()
  486
+        self._add_middleware(post_middleware)
  487
+        self._add_middleware(bad_middleware)
  488
+        self._add_middleware(pre_middleware)
  489
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test View Exception'])
  490
+
  491
+        # Check that the right middleware methods have been invoked
  492
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  493
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
  494
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  495
+
  496
+    def test_process_response_bad_middleware_not_found(self):
  497
+        pre_middleware = TestMiddleware()
  498
+        bad_middleware = BadResponseMiddleware()
  499
+        post_middleware = TestMiddleware()
  500
+        self._add_middleware(post_middleware)
  501
+        self._add_middleware(bad_middleware)
  502
+        self._add_middleware(pre_middleware)
  503
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Response Exception'])
  504
+
  505
+        # Check that the right middleware methods have been invoked
  506
+        self.assert_middleware_usage(pre_middleware,  True,  True,  False, True)
  507
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
  508
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  509
+
  510
+    def test_process_exception_bad_middleware_not_found(self):
  511
+        pre_middleware = TestMiddleware()
  512
+        bad_middleware = BadExceptionMiddleware()
  513
+        post_middleware = TestMiddleware()
  514
+        self._add_middleware(post_middleware)
  515
+        self._add_middleware(bad_middleware)
  516
+        self._add_middleware(pre_middleware)
  517
+        self.assert_exceptions_handled('/middleware_exceptions/not_found/', ['Test Exception Exception'])
  518
+
  519
+        # Check that the right middleware methods have been invoked
  520
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  521
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
  522
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  523
+
  524
+    def test_process_request_bad_middleware_exception(self):
  525
+        pre_middleware = TestMiddleware()
  526
+        bad_middleware = BadRequestMiddleware()
  527
+        post_middleware = TestMiddleware()
  528
+        self._add_middleware(post_middleware)
  529
+        self._add_middleware(bad_middleware)
  530
+        self._add_middleware(pre_middleware)
  531
+        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Request Exception'])
  532
+
  533
+        # Check that the right middleware methods have been invoked
  534
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  535
+        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
  536
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  537
+
  538
+    def test_process_view_bad_middleware_exception(self):
  539
+        pre_middleware = TestMiddleware()
  540
+        bad_middleware = BadViewMiddleware()
  541
+        post_middleware = TestMiddleware()
  542
+        self._add_middleware(post_middleware)
  543
+        self._add_middleware(bad_middleware)
  544
+        self._add_middleware(pre_middleware)
  545
+        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test View Exception'])
  546
+
  547
+        # Check that the right middleware methods have been invoked
  548
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  549
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
  550
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  551
+
  552
+    def test_process_response_bad_middleware_exception(self):
  553
+        pre_middleware = TestMiddleware()
  554
+        bad_middleware = BadResponseMiddleware()
  555
+        post_middleware = TestMiddleware()
  556
+        self._add_middleware(post_middleware)
  557
+        self._add_middleware(bad_middleware)
  558
+        self._add_middleware(pre_middleware)
  559
+        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Error in view', 'Test Response Exception'])
  560
+
  561
+        # Check that the right middleware methods have been invoked
  562
+        self.assert_middleware_usage(pre_middleware,  True,  True,  False, True)
  563
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
  564
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  565
+
  566
+    def test_process_exception_bad_middleware_exception(self):
  567
+        pre_middleware = TestMiddleware()
  568
+        bad_middleware = BadExceptionMiddleware()
  569
+        post_middleware = TestMiddleware()
  570
+        self._add_middleware(post_middleware)
  571
+        self._add_middleware(bad_middleware)
  572
+        self._add_middleware(pre_middleware)
  573
+        self.assert_exceptions_handled('/middleware_exceptions/error/', ['Test Exception Exception'])
  574
+
  575
+        # Check that the right middleware methods have been invoked
  576
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  577
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
  578
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  579
+
  580
+    def test_process_request_bad_middleware_null_view(self):
  581
+        pre_middleware = TestMiddleware()
  582
+        bad_middleware = BadRequestMiddleware()
  583
+        post_middleware = TestMiddleware()
  584
+        self._add_middleware(post_middleware)
  585
+        self._add_middleware(bad_middleware)
  586
+        self._add_middleware(pre_middleware)
  587
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test Request Exception'])
  588
+
  589
+        # Check that the right middleware methods have been invoked
  590
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  591
+        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
  592
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  593
+
  594
+    def test_process_view_bad_middleware_null_view(self):
  595
+        pre_middleware = TestMiddleware()
  596
+        bad_middleware = BadViewMiddleware()
  597
+        post_middleware = TestMiddleware()
  598
+        self._add_middleware(post_middleware)
  599
+        self._add_middleware(bad_middleware)
  600
+        self._add_middleware(pre_middleware)
  601
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', ['Test View Exception'])
  602
+
  603
+        # Check that the right middleware methods have been invoked
  604
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  605
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
  606
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  607
+
  608
+    def test_process_response_bad_middleware_null_view(self):
  609
+        pre_middleware = TestMiddleware()
  610
+        bad_middleware = BadResponseMiddleware()
  611
+        post_middleware = TestMiddleware()
  612
+        self._add_middleware(post_middleware)
  613
+        self._add_middleware(bad_middleware)
  614
+        self._add_middleware(pre_middleware)
  615
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
  616
+                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object.",
  617
+                'Test Response Exception'
  618
+            ])
  619
+
  620
+        # Check that the right middleware methods have been invoked
  621
+        self.assert_middleware_usage(pre_middleware,  True,  True,  False, False)
  622
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  False)
  623
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  False)
  624
+
  625
+    def test_process_exception_bad_middleware_null_view(self):
  626
+        pre_middleware = TestMiddleware()
  627
+        bad_middleware = BadExceptionMiddleware()
  628
+        post_middleware = TestMiddleware()
  629
+        self._add_middleware(post_middleware)
  630
+        self._add_middleware(bad_middleware)
  631
+        self._add_middleware(pre_middleware)
  632
+        self.assert_exceptions_handled('/middleware_exceptions/null_view/', [
  633
+                "The view regressiontests.middleware_exceptions.views.null_view didn't return an HttpResponse object."
  634
+            ],
  635
+            ValueError())
  636
+
  637
+        # Check that the right middleware methods have been invoked
  638
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  639
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
  640
+        self.assert_middleware_usage(post_middleware, True,  True,  True, False)
  641
+
  642
+    def test_process_request_bad_middleware_permission_denied(self):
  643
+        pre_middleware = TestMiddleware()
  644
+        bad_middleware = BadRequestMiddleware()
  645
+        post_middleware = TestMiddleware()
  646
+        self._add_middleware(post_middleware)
  647
+        self._add_middleware(bad_middleware)
  648
+        self._add_middleware(pre_middleware)
  649
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Request Exception'])
  650
+
  651
+        # Check that the right middleware methods have been invoked
  652
+        self.assert_middleware_usage(pre_middleware,  True,  False, True,  False)
  653
+        self.assert_middleware_usage(bad_middleware,  True,  False, True,  False)
  654
+        self.assert_middleware_usage(post_middleware, False, False, True,  False)
  655
+
  656
+    def test_process_view_bad_middleware_permission_denied(self):
  657
+        pre_middleware = TestMiddleware()
  658
+        bad_middleware = BadViewMiddleware()
  659
+        post_middleware = TestMiddleware()
  660
+        self._add_middleware(post_middleware)
  661
+        self._add_middleware(bad_middleware)
  662
+        self._add_middleware(pre_middleware)
  663
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test View Exception'])
  664
+
  665
+        # Check that the right middleware methods have been invoked
  666
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  667
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True, False)
  668
+        self.assert_middleware_usage(post_middleware, True,  False, True, False)
  669
+
  670
+    def test_process_response_bad_middleware_permission_denied(self):
  671
+        pre_middleware = TestMiddleware()
  672
+        bad_middleware = BadResponseMiddleware()
  673
+        post_middleware = TestMiddleware()
  674
+        self._add_middleware(post_middleware)
  675
+        self._add_middleware(bad_middleware)
  676
+        self._add_middleware(pre_middleware)
  677
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Response Exception'])
  678
+
  679
+        # Check that the right middleware methods have been invoked
  680
+        self.assert_middleware_usage(pre_middleware,  True,  True,  False, True)
  681
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
  682
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
  683
+
  684
+    def test_process_exception_bad_middleware_permission_denied(self):
  685
+        pre_middleware = TestMiddleware()
  686
+        bad_middleware = BadExceptionMiddleware()
  687
+        post_middleware = TestMiddleware()
  688
+        self._add_middleware(post_middleware)
  689
+        self._add_middleware(bad_middleware)
  690
+        self._add_middleware(pre_middleware)
  691
+        self.assert_exceptions_handled('/middleware_exceptions/permission_denied/', ['Test Exception Exception'])
  692
+
  693
+        # Check that the right middleware methods have been invoked
  694
+        self.assert_middleware_usage(pre_middleware,  True,  True,  True, False)
  695
+        self.assert_middleware_usage(bad_middleware,  True,  True,  True,  True)
  696
+        self.assert_middleware_usage(post_middleware, True,  True,  True,  True)
6  tests/regressiontests/middleware_exceptions/urls.py
@@ -4,5 +4,9 @@
4 4
 import views
5 5
 
6 6
 urlpatterns = patterns('',
7  
-    (r'^$', views.index),
  7
+    (r'^view/$', views.normal_view),
  8
+    (r'^not_found/$', views.not_found),
  9
+    (r'^error/$', views.server_error),
  10
+    (r'^null_view/$', views.null_view),
  11
+    (r'^permission_denied/$', views.permission_denied),
8 12
 )
17  tests/regressiontests/middleware_exceptions/views.py
... ...
@@ -1,4 +1,17 @@
1 1
 from django import http
  2
+from django.core.exceptions import PermissionDenied
2 3
 
3  
-def index(request):
4  
-    return http.HttpResponse('')
  4
+def normal_view(request):
  5
+    return http.HttpResponse('OK')
  6
+
  7
+def not_found(request):
  8
+    raise http.Http404()
  9
+
  10
+def server_error(request):
  11
+    raise Exception('Error in view')
  12
+
  13
+def null_view(request):
  14
+    return None
  15
+
  16
+def permission_denied(request):
  17
+    raise PermissionDenied()

0 notes on commit 52e0bcb

Please sign in to comment.
Something went wrong with that request. Please try again.