Ticket #6094: 6094-minimal.diff

File 6094-minimal.diff, 4.8 KB (added by isagalaev, 5 years ago)

Minimal patch and test

  • django/core/handlers/base.py

    === modified file 'django/core/handlers/base.py'
     
    6868        from django.core import exceptions, urlresolvers
    6969        from django.conf import settings
    7070
    71         # Reset the urlconf for this thread.
    72         urlresolvers.set_urlconf(None)
    73 
    74         # Apply request middleware
    75         for middleware_method in self._request_middleware:
    76             response = middleware_method(request)
    77             if response:
    78                 return response
    79 
    80         # Get urlconf from request object, if available.  Otherwise use default.
    81         urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
    82 
    83         # Set the urlconf for this thread to the one specified above.
    84         urlresolvers.set_urlconf(urlconf)
    85 
    86         resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
    8771        try:
    8872            try:
     73                # Reset the urlconf for this thread.
     74                urlresolvers.set_urlconf(None)
     75
     76                # Get urlconf from request object, if available.  Otherwise use default.
     77                urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
     78
     79                # Set the urlconf for this thread to the one specified above.
     80                urlresolvers.set_urlconf(urlconf)
     81                resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
     82
     83                # Apply request middleware
     84                for middleware_method in self._request_middleware:
     85                    response = middleware_method(request)
     86                    if response:
     87                        return response
     88
    8989                callback, callback_args, callback_kwargs = resolver.resolve(
    9090                        request.path_info)
    9191
  • tests/regressiontests/middleware_exceptions/models.py

    === added directory 'tests/regressiontests/middleware_exceptions'
    === added file 'tests/regressiontests/middleware_exceptions/__init__.py'
    === added file 'tests/regressiontests/middleware_exceptions/models.py'
     
     1from django.db import models
  • tests/regressiontests/middleware_exceptions/tests.py

    === added file 'tests/regressiontests/middleware_exceptions/tests.py'
     
     1import sys
     2
     3from django.test import TestCase
     4from django.core.signals import got_request_exception
     5
     6class RequestMiddleware(object):
     7    def process_request(self, request):
     8        raise Exception('Exception')
     9
     10class MiddlewareExceptionTest(TestCase):
     11    def __init__(self, *args, **kwargs):
     12        super(MiddlewareExceptionTest, self).__init__(*args, **kwargs)
     13        self.exceptions = []
     14        got_request_exception.connect(self._on_request_exception)
     15
     16    def setUp(self):
     17        self.client.handler.load_middleware()
     18
     19    def tearDown(self):
     20        self.exceptions = []
     21
     22    def _on_request_exception(self, sender, request, **kwargs):
     23        self.exceptions.append(sys.exc_info())
     24
     25    def test_process_request(self):
     26        self.client.handler._request_middleware.insert(0, RequestMiddleware().process_request)
     27        try:
     28            response = self.client.get('/')
     29        except:
     30            # Test client indefinitely re-raises any exceptions being raised
     31            # during request handling. Hence actual testing that exception was
     32            # properly handled is done by relying on got_request_exception
     33            # signal being sent.
     34            pass
     35        self.assertEquals(len(self.exceptions), 1)
     36        exception, value, tb = self.exceptions[0]
     37        self.assertEquals(value.args, ('Exception', ))
  • tests/regressiontests/middleware_exceptions/urls.py

    === added file 'tests/regressiontests/middleware_exceptions/urls.py'
     
     1# coding: utf-8
     2from django.conf.urls.defaults import *
     3
     4import views
     5
     6urlpatterns = patterns('',
     7    (r'^$', views.index),
     8)
  • tests/regressiontests/middleware_exceptions/views.py

    === added file 'tests/regressiontests/middleware_exceptions/views.py'
     
     1from django import http
     2
     3def index(request):
     4    return http.HttpResponse('')
  • tests/urls.py

    === modified file 'tests/urls.py'
     
    3535
    3636    # conditional get views
    3737    (r'condition/', include('regressiontests.conditional_processing.urls')),
     38
     39    # middleware exceptions tests
     40    (r'middleware_exceptions/', include('regressiontests.middleware_exceptions.urls')),
    3841)
Back to Top