Ticket #12250: django-base-handlers-view-middleware-with-tests.diff
File django-base-handlers-view-middleware-with-tests.diff, 7.0 KB (added by , 14 years ago) |
---|
-
django/core/handlers/base.py
90 90 callback, callback_args, callback_kwargs = resolver.resolve( 91 91 request.path_info) 92 92 93 # Apply view middleware 94 for middleware_method in self._view_middleware: 95 response = middleware_method(request, callback, callback_args, callback_kwargs) 96 if response: 97 return response 93 try: 94 # Apply view middleware 95 for middleware_method in self._view_middleware: 96 response = middleware_method(request, callback, callback_args, callback_kwargs) 97 if response: 98 return response 98 99 99 try:100 100 response = callback(request, *callback_args, **callback_kwargs) 101 101 except Exception, e: 102 102 # If the view raised an exception, run it through exception -
tests/regressiontests/base_handler/middleware.py
1 """ 2 Middleware to help test django.core.handlers.base.BaseHandler 3 """ 4 from django.http import HttpResponse 5 6 class ProcessExceptionException(Exception): 7 pass 8 9 class ProcessRequestException(Exception): 10 pass 11 12 class ProcessResponseException(Exception): 13 pass 14 15 class ProcessViewException(Exception): 16 pass 17 18 class ProcessExceptionRaise: 19 def process_exception(self, request, exception): 20 raise ProcessExceptionException() 21 22 class ProcessExceptionResponse: 23 def process_exception(self, request, exception): 24 return HttpResponse('OK') 25 26 class ProcessResponseRaise: 27 def process_response(self, request, response): 28 raise ProcessResponseException() 29 30 class ProcessResponseResponse: 31 def process_response(self, request, response): 32 return HttpResponse('OK') 33 34 class ProcessRequestRaise: 35 def process_request(self, request): 36 raise ProcessRequestException() 37 38 class ProcessRequestResponse: 39 def process_request(self, request): 40 return HttpResponse('OK') 41 42 class ProcessViewRaise: 43 def process_view(self, request, view_func, *view_args, **view_kwargs): 44 raise ProcessViewException() 45 46 class ProcessViewResponse: 47 def process_view(self, request, view_func, *view_args, **view_kwargs): 48 return HttpResponse('OK') -
tests/regressiontests/base_handler/tests.py
1 """ 2 Tests for django.core.handlers.base.BaseHandler 3 """ 4 from django.test import TestCase 5 from django.conf import settings 6 from django.core.handlers.base import BaseHandler 7 from django.http import HttpRequest 8 from middleware import * 9 10 11 class BaseHandlerTestCase(TestCase): 12 13 def setUp(self): 14 self.middleware_classes = settings.MIDDLEWARE_CLASSES 15 16 # Tests want uncaught exceptions to fall through 17 self.debug_propagate_exceptions = settings.DEBUG_PROPAGATE_EXCEPTIONS 18 settings.DEBUG_PROPAGATE_EXCEPTIONS = True 19 20 self.handler = BaseHandler() 21 22 def tearDown(self): 23 # reset settings changes 24 settings.MIDDLEWARE_CLASSES = self.middleware_classes 25 settings.DEBUG_PROPAGATE_EXCEPTIONS = self.debug_propagate_exceptions 26 27 def _get_request(self): 28 request = HttpRequest() 29 request.META = { 30 'SERVER_NAME': 'testserver', 31 'SERVER_PORT': 80, 32 } 33 request.path = request.path_info = "/base_handler/path/" 34 return request 35 36 def test_request_raises_uncaught(self): 37 """ 38 Middleware.process_request exceptions are uncaught. 39 """ 40 settings.MIDDLEWARE_CLASSES = ( 41 'regressiontests.base_handler.middleware.ProcessRequestRaise', 42 ) 43 44 self.handler.load_middleware() 45 46 self.assertRaises( 47 ProcessRequestException, 48 self.handler.get_response, 49 self._get_request(), 50 ) 51 52 def test_view_raises_uncaught(self): 53 """ 54 Middleware.process_view generated exception without any 55 process_exception handlers. 56 """ 57 settings.MIDDLEWARE_CLASSES = ( 58 'regressiontests.base_handler.middleware.ProcessViewRaise', 59 ) 60 61 self.handler.load_middleware() 62 63 self.assertRaises( 64 ProcessViewException, 65 self.handler.get_response, 66 self._get_request(), 67 ) 68 69 def test_view_raises_caught(self): 70 """ 71 Middleware.process_view generated exception with a process_exception 72 handler providing a response. 73 """ 74 settings.MIDDLEWARE_CLASSES = ( 75 'regressiontests.base_handler.middleware.ProcessViewRaise', 76 'regressiontests.base_handler.middleware.ProcessExceptionResponse', 77 ) 78 79 self.handler.load_middleware() 80 response = self.handler.get_response(self._get_request()) 81 82 # Handled exception yields a valid response 83 self.assertEquals(response.status_code, 200) 84 85 -
tests/regressiontests/base_handler/urls.py
1 from django.conf.urls.defaults import patterns 2 import views 3 4 urlpatterns = patterns('', 5 (r'^path/$', views.view), 6 ) -
tests/regressiontests/base_handler/views.py
1 from django.http import HttpResponse 2 3 def view(request): 4 return HttpResponse('OK') -
tests/urls.py
41 41 42 42 # special headers views 43 43 (r'special_headers/', include('regressiontests.special_headers.urls')), 44 45 # base_handler views 46 (r'base_handler/', include('regressiontests.base_handler.urls')), 44 47 )