Ticket #12250: 12250.1.diff

File 12250.1.diff, 5.8 KB (added by ramiro, 4 years ago)

Patch modified and simplfied

  • new file tests/regressiontests/base_handler/middleware.py

    diff --git a/tests/regressiontests/base_handler/__init__.py b/tests/regressiontests/base_handler/__init__.py
    new file mode 100644
    diff --git a/tests/regressiontests/base_handler/middleware.py b/tests/regressiontests/base_handler/middleware.py
    new file mode 100644
    - +  
     1"""
     2Middleware to help test django.core.handlers.base.BaseHandler
     3"""
     4from django.http import HttpResponse
     5
     6class ProcessRequestException(Exception):
     7    pass
     8
     9class ProcessViewException(Exception):
     10    pass
     11
     12
     13class ProcessExceptionResponse:
     14    def process_exception(self, request, exception):
     15        return HttpResponse('OK')
     16
     17
     18class ProcessRequestRaise:
     19    def process_request(self, request):
     20        raise ProcessRequestException()
     21
     22
     23class ProcessViewRaise:
     24    def process_view(self, request, view_func, *view_args, **view_kwargs):
     25        raise ProcessViewException()
  • new file tests/regressiontests/base_handler/tests.py

    diff --git a/tests/regressiontests/base_handler/models.py b/tests/regressiontests/base_handler/models.py
    new file mode 100644
    diff --git a/tests/regressiontests/base_handler/tests.py b/tests/regressiontests/base_handler/tests.py
    new file mode 100644
    - +  
     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 (ProcessRequestException, ProcessViewException,
     9    ProcessExceptionResponse, ProcessRequestRaise, ProcessViewRaise)
     10
     11
     12class BaseHandlerTestCase(TestCase):
     13
     14    def setUp(self):
     15        self.middleware_classes = settings.MIDDLEWARE_CLASSES
     16
     17        # Tests want uncaught exceptions to fall through
     18        self.debug_propagate_exceptions = settings.DEBUG_PROPAGATE_EXCEPTIONS
     19        settings.DEBUG_PROPAGATE_EXCEPTIONS = True
     20
     21        self.handler = BaseHandler()
     22
     23    def tearDown(self):
     24        # reset settings changes
     25        settings.MIDDLEWARE_CLASSES = self.middleware_classes
     26        settings.DEBUG_PROPAGATE_EXCEPTIONS = self.debug_propagate_exceptions
     27
     28    def _get_request(self, path):
     29        request = HttpRequest()
     30        request.META = {
     31            'SERVER_NAME': 'testserver',
     32            'SERVER_PORT': 80,
     33        }
     34        request.path = request.path_info = "/base_handler/%s" % path
     35        return request
     36
     37    def test_process_request_raises_uncaught(self):
     38        """
     39        Exceptions generated in <Middleware>.process_request() should propagate uncaught.
     40        """
     41        settings.MIDDLEWARE_CLASSES = (
     42            'regressiontests.base_handler.middleware.ProcessRequestRaise',
     43        )
     44
     45        self.handler.load_middleware()
     46
     47        self.assertRaises(
     48            ProcessRequestException,
     49            self.handler.get_response,
     50            self._get_request('path/')
     51        )
     52
     53    def test_process_view_raises_uncaught(self):
     54        """
     55        Exceptions generated in <Middleware>.process_view() without any
     56        process_exception handlers should propagate uncaught.
     57        """
     58        settings.MIDDLEWARE_CLASSES = (
     59            'regressiontests.base_handler.middleware.ProcessViewRaise',
     60        )
     61
     62        self.handler.load_middleware()
     63
     64        self.assertRaises(
     65            ProcessViewException,
     66            self.handler.get_response,
     67            self._get_request('path/'),
     68        )
     69
     70    def test_view_raises_caught(self):
     71        """
     72        Ticket #12250. A view that generates an exception and a middleware
     73        whose process_exception() provides a response. Result should be that
     74        response.
     75        """
     76        settings.MIDDLEWARE_CLASSES = (
     77            'regressiontests.base_handler.middleware.ProcessExceptionResponse',
     78        )
     79
     80        self.handler.load_middleware()
     81        response = self.handler.get_response(self._get_request('view_that_raises_exc/'))
     82
     83        # Handled exception yields a valid response
     84        self.assertEqual(response.status_code, 200)
     85
     86    def test_process_view_raises_caught(self):
     87        """
     88        A <Middleware>.process_view() that generates an exception and another
     89        middleware whose process_exception() provides a response. Result should
     90        be that response.
     91        """
     92        settings.MIDDLEWARE_CLASSES = (
     93            'regressiontests.base_handler.middleware.ProcessViewRaise',
     94            'regressiontests.base_handler.middleware.ProcessExceptionResponse',
     95        )
     96
     97        self.handler.load_middleware()
     98        response = self.handler.get_response(self._get_request('path/'))
     99
     100        # Handled exception yields a valid response
     101        self.assertEqual(response.status_code, 200)
  • new file tests/regressiontests/base_handler/urls.py

    diff --git a/tests/regressiontests/base_handler/urls.py b/tests/regressiontests/base_handler/urls.py
    new file mode 100644
    - +  
     1from django.conf.urls.defaults import patterns
     2import views
     3
     4urlpatterns = patterns('',
     5    (r'^path/$', views.view),
     6    (r'^view_that_raises_exc/$', views.view_that_raises_exc),
     7)
  • new file tests/regressiontests/base_handler/views.py

    diff --git a/tests/regressiontests/base_handler/views.py b/tests/regressiontests/base_handler/views.py
    new file mode 100644
    - +  
     1from django.http import HttpResponse
     2
     3class ViewException(Exception):
     4    pass
     5
     6def view(request):
     7    return HttpResponse('OK')
     8
     9def view_that_raises_exc(request):
     10    raise ViewException()
  • tests/urls.py

    diff --git a/tests/urls.py b/tests/urls.py
    a b  
    4545
    4646    # test util views
    4747    (r'test_utils/', include('regressiontests.test_utils.urls')),
     48
     49    # base_handler views
     50    (r'base_handler/', include('regressiontests.base_handler.urls')),
    4851)
Back to Top