Ticket #3162: text_client_exception_raise_v2.patch
File text_client_exception_raise_v2.patch, 5.4 KB (added by , 18 years ago) |
---|
-
django/test/client.py
1 1 from cStringIO import StringIO 2 import sys 2 3 from django.core.handlers.base import BaseHandler 3 4 from django.core.handlers.wsgi import WSGIRequest 5 from django.core.signals import got_request_exception 4 6 from django.dispatch import dispatcher 5 7 from django.http import urlencode, SimpleCookie 6 8 from django.test import signals … … 98 100 self.handler = ClientHandler() 99 101 self.defaults = defaults 100 102 self.cookie = SimpleCookie() 103 self.exc_info = None 101 104 102 105 def request(self, **request): 103 106 """ … … 106 109 Assumes defaults for the query environment, which can be overridden 107 110 using the arguments to the request. 108 111 """ 112 self.exc_info = None 109 113 110 114 environ = { 111 115 'HTTP_COOKIE': self.cookie, … … 126 130 on_template_render = curry(store_rendered_templates, data) 127 131 dispatcher.connect(on_template_render, signal=signals.template_rendered) 128 132 133 # Capture exceptions created by the handler 134 dispatcher.connect(self.store_exc_info, signal=got_request_exception) 135 129 136 response = self.handler(environ) 130 137 131 138 # Add any rendered template detail to the response … … 143 150 if response.cookies: 144 151 self.cookie.update(response.cookies) 145 152 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 146 157 return response 147 158 148 159 def get(self, path, data={}, **extra): … … 214 241 215 242 # Since we are logged in, request the actual page again 216 243 return self.get(path) 244 245 def store_exc_info(self, *args, **kwargs): 246 self.exc_info = sys.exc_info() -
django/core/handlers/base.py
103 103 except SystemExit: 104 104 pass # See http://code.djangoproject.com/ticket/1023 105 105 except: # Handle everything else, including SuspiciousOperation, etc. 106 receivers = dispatcher.send(signal=signals.got_request_exception) 106 107 if settings.DEBUG: 107 108 from django.views import debug 108 109 return debug.technical_500_response(request, *sys.exc_info()) 109 110 else: 110 111 # Get the exception info now, in case another exception is thrown later. 111 112 exc_info = sys.exc_info() 112 receivers = dispatcher.send(signal=signals.got_request_exception)113 113 # When DEBUG is False, send an error message to the admins. 114 114 subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path) 115 115 try: -
tests/modeltests/test_client/views.py
33 49 c = Context({'user': request.user}) 34 50 35 51 return HttpResponse(t.render(c)) 52 53 def broken_view(request): 54 """A view which just raises an exception, simulating a broken view.""" 55 raise KeyError("Oops! Looks like you wrote some bad code.") -
tests/modeltests/test_client/models.py
99 107 100 108 response = self.client.login('/test_client/login_protected_view/', 'otheruser', 'nopassword') 101 109 self.assertFalse(response) 110 111 def test_view_with_exception(self): 112 self.assertRaises(KeyError, self.client.get, "/test_client/broken_view/") -
tests/modeltests/test_client/urls.py
6 6 (r'^post_view/$', views.post_view), 7 7 (r'^redirect_view/$', views.redirect_view), 8 8 (r'^login_protected_view/$', views.login_protected_view), 9 (r'^broken_view/$', views.broken_view), 9 10 ) -
docs/testing.txt
314 324 # Check that the rendered context contains 5 customers 315 325 self.failUnlessEqual(len(response.context['customers']), 5) 316 326 327 Exceptions 328 ---------- 329 330 Exceptions raised by views or other code while handling a request made by 331 ``Client`` will bubble up to your test case. The default test runner, 332 ``django.test.simple.run_tests`` will print a stack trace for an unhandled 333 exception. ``unittest.TestCase``'s ``assertRaises`` method can be used to test 334 that exceptions are raised by views, which can be useful to test error 335 handling. You may also catch exceptions in the normal way and inspect them in 336 your test code. 337 338 ``Http404``, ``PermissionDenied`` and ``SystemExit`` are caught by Django's 339 base handler. These exceptions will not make it back to your test if raised 340 during a request. 341 317 342 Fixtures 318 343 -------- 319 344