Code

Ticket #3162: test_client_exception_catch.patch

File test_client_exception_catch.patch, 1.9 KB (added by afternoon@…, 7 years ago)

[patch] Reraise exceptions created during request handling so test cases may inspect them

  • django/test/client.py

     
    11from cStringIO import StringIO 
     2import sys 
    23from django.core.handlers.base import BaseHandler 
    34from django.core.handlers.wsgi import WSGIRequest 
     5from django.core.signals import got_request_exception 
    46from django.dispatch import dispatcher 
    57from django.http import urlencode, SimpleCookie 
    68from django.test import signals 
     
    98100        self.handler = ClientHandler() 
    99101        self.defaults = defaults 
    100102        self.cookie = SimpleCookie() 
     103        self.exc_info = None 
    101104 
    102105    def request(self, **request): 
    103106        """ 
     
    106109        Assumes defaults for the query environment, which can be overridden 
    107110        using the arguments to the request. 
    108111        """ 
     112        self.exc_info = None 
    109113 
    110114        environ = { 
    111115            'HTTP_COOKIE':      self.cookie, 
     
    126130        on_template_render = curry(store_rendered_templates, data) 
    127131        dispatcher.connect(on_template_render, signal=signals.template_rendered) 
    128132 
     133        # Capture exceptions created by the handler 
     134        dispatcher.connect(self.store_exc_info, signal=got_request_exception) 
     135 
    129136        response = self.handler(environ) 
    130137 
    131138        # Add any rendered template detail to the response 
     
    143150        if response.cookies: 
    144151            self.cookie.update(response.cookies) 
    145152 
     153        # Look for a signalled exception and reraise it 
     154        if self.exc_info: 
     155            raise self.exc_info[1], None, self.exc_info[2] 
     156 
    146157        return response 
    147158 
    148159    def get(self, path, data={}, **extra): 
     
    214242 
    215243        # Since we are logged in, request the actual page again 
    216244        return self.get(path) 
     245 
     246    def store_exc_info(self, *args, **kwargs): 
     247        self.exc_info = sys.exc_info()