Ticket #12250: django-base-handlers-view-middleware-with-tests.diff

File django-base-handlers-view-middleware-with-tests.diff, 7.0 KB (added by Michael Manfre, 14 years ago)

Patch against trunk rev 13959

  • django/core/handlers/base.py

     
    9090                callback, callback_args, callback_kwargs = resolver.resolve(
    9191                        request.path_info)
    9292
    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
    9899
    99                 try:
    100100                    response = callback(request, *callback_args, **callback_kwargs)
    101101                except Exception, e:
    102102                    # If the view raised an exception, run it through exception
  • tests/regressiontests/base_handler/middleware.py

     
     1"""
     2Middleware to help test django.core.handlers.base.BaseHandler
     3"""
     4from django.http import HttpResponse
     5
     6class ProcessExceptionException(Exception):
     7        pass
     8
     9class ProcessRequestException(Exception):
     10        pass
     11
     12class ProcessResponseException(Exception):
     13        pass
     14
     15class ProcessViewException(Exception):
     16        pass
     17
     18class ProcessExceptionRaise:
     19        def process_exception(self, request, exception):
     20                raise ProcessExceptionException()
     21
     22class ProcessExceptionResponse:
     23        def process_exception(self, request, exception):
     24                return HttpResponse('OK')
     25
     26class ProcessResponseRaise:
     27        def process_response(self, request, response):
     28                raise ProcessResponseException()
     29
     30class ProcessResponseResponse:
     31        def process_response(self, request, response):
     32                return HttpResponse('OK')
     33
     34class ProcessRequestRaise:
     35        def process_request(self, request):
     36                raise ProcessRequestException()
     37
     38class ProcessRequestResponse:
     39        def process_request(self, request):
     40                return HttpResponse('OK')
     41
     42class ProcessViewRaise:
     43        def process_view(self, request, view_func, *view_args, **view_kwargs):
     44                raise ProcessViewException()
     45
     46class ProcessViewResponse:
     47        def process_view(self, request, view_func, *view_args, **view_kwargs):
     48                return HttpResponse('OK')
  • tests/regressiontests/base_handler/tests.py

     
     1"""
     2Tests for django.core.handlers.base.BaseHandler
     3"""
     4from django.test import TestCase
     5from django.conf import settings
     6from django.core.handlers.base import BaseHandler
     7from django.http import HttpRequest
     8from middleware import *
     9
     10
     11class 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

     
     1from django.conf.urls.defaults import patterns
     2import views
     3
     4urlpatterns = patterns('',
     5    (r'^path/$', views.view),
     6)
  • tests/regressiontests/base_handler/views.py

     
     1from django.http import HttpResponse
     2
     3def view(request):
     4        return HttpResponse('OK')
  • tests/urls.py

     
    4141
    4242    # special headers views
    4343    (r'special_headers/', include('regressiontests.special_headers.urls')),
     44   
     45    # base_handler views
     46    (r'base_handler/', include('regressiontests.base_handler.urls')),
    4447)
Back to Top