Ticket #13090: 13090.1.diff

File 13090.1.diff, 3.7 KB (added by Ivan Sagalaev, 9 years ago)

Patch with tests

  • django/core/handlers/base.py

     
    7070
    7171        try:
    7272            try:
    73                 # Reset the urlconf for this thread.
    74                 urlresolvers.set_urlconf(None)
    75                 # Obtain a default resolver. It's needed early for handling 404's.
    76                 resolver = urlresolvers.RegexURLResolver(r'^/', None)
     73                # Setup default url resolver for this thread.
     74                urlconf = settings.ROOT_URLCONF
     75                urlresolvers.set_urlconf(urlconf)
     76                resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
    7777
    7878                # Apply request middleware
    7979                for middleware_method in self._request_middleware:
     
    8181                    if response:
    8282                        return response
    8383
    84                 # Get urlconf from request object, if available.  Otherwise use default.
    85                 urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
    86                 # Set the urlconf for this thread to the one specified above.
    87                 urlresolvers.set_urlconf(urlconf)
    88                 # Reset the resolver with a possibly new urlconf
    89                 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
     84                if hasattr(request, "urlconf"):
     85                    # Reset url resolver with a custom urlconf.
     86                    urlconf = request.urlconf
     87                    urlresolvers.set_urlconf(urlconf)
     88                    resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
    9089
    9190                callback, callback_args, callback_kwargs = resolver.resolve(
    9291                        request.path_info)
  • tests/regressiontests/middleware_exceptions/tests.py

     
    33from django.test import TestCase
    44from django.core.signals import got_request_exception
    55
    6 class RequestMiddleware(object):
    7     def process_request(self, request):
    8         raise Exception('Exception')
    96
    10 class MiddlewareExceptionTest(TestCase):
    11     def setUp(self):
    12         self.exceptions = []
    13         got_request_exception.connect(self._on_request_exception)
    14         self.client.handler.load_middleware()
     7class RequestException(Exception):
     8    pass
    159
    16     def tearDown(self):
    17         got_request_exception.disconnect(self._on_request_exception)
    18         self.exceptions = []
    1910
    20     def _on_request_exception(self, sender, request, **kwargs):
    21         self.exceptions.append(sys.exc_info())
     11class RequestMiddleware(object):
     12    def process_request(self, request):
     13        raise RequestException('Exception')
    2214
     15
     16class MiddlewareExceptionTest(TestCase):
    2317    def test_process_request(self):
     18        self.client.handler.load_middleware()
    2419        self.client.handler._request_middleware.insert(0, RequestMiddleware().process_request)
    25         try:
    26             response = self.client.get('/')
    27         except:
    28             # Test client indefinitely re-raises any exceptions being raised
    29             # during request handling. Hence actual testing that exception was
    30             # properly handled is done by relying on got_request_exception
    31             # signal being sent.
    32             pass
    33         self.assertEquals(len(self.exceptions), 1)
    34         exception, value, tb = self.exceptions[0]
    35         self.assertEquals(value.args, ('Exception', ))
     20        self.assertRaises(RequestException, self.client.get, '/')
Back to Top