2010-01-11 01:35:01 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.core.signals import got_request_exception
|
|
|
|
|
2010-03-13 00:45:29 +08:00
|
|
|
class TestException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestMiddleware(object):
|
2010-01-11 01:35:01 +08:00
|
|
|
def process_request(self, request):
|
2010-03-13 00:45:29 +08:00
|
|
|
raise TestException('Test Exception')
|
2010-01-11 01:35:01 +08:00
|
|
|
|
|
|
|
class MiddlewareExceptionTest(TestCase):
|
2010-01-11 02:48:08 +08:00
|
|
|
def setUp(self):
|
2010-01-11 01:35:01 +08:00
|
|
|
self.exceptions = []
|
|
|
|
got_request_exception.connect(self._on_request_exception)
|
|
|
|
self.client.handler.load_middleware()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2010-01-11 02:48:08 +08:00
|
|
|
got_request_exception.disconnect(self._on_request_exception)
|
2010-01-11 01:35:01 +08:00
|
|
|
self.exceptions = []
|
|
|
|
|
|
|
|
def _on_request_exception(self, sender, request, **kwargs):
|
|
|
|
self.exceptions.append(sys.exc_info())
|
|
|
|
|
|
|
|
def test_process_request(self):
|
2010-03-13 00:45:29 +08:00
|
|
|
self.client.handler._request_middleware.insert(0, TestMiddleware().process_request)
|
2010-01-11 01:35:01 +08:00
|
|
|
try:
|
|
|
|
response = self.client.get('/')
|
2010-03-13 00:45:29 +08:00
|
|
|
except TestException, e:
|
2010-01-11 01:35:01 +08:00
|
|
|
# Test client indefinitely re-raises any exceptions being raised
|
|
|
|
# during request handling. Hence actual testing that exception was
|
|
|
|
# properly handled is done by relying on got_request_exception
|
|
|
|
# signal being sent.
|
|
|
|
pass
|
2010-03-13 00:45:29 +08:00
|
|
|
except Exception, e:
|
|
|
|
self.fail("Unexpected exception: %s" % e)
|
2010-01-11 01:35:01 +08:00
|
|
|
self.assertEquals(len(self.exceptions), 1)
|
|
|
|
exception, value, tb = self.exceptions[0]
|
2010-03-13 00:45:29 +08:00
|
|
|
self.assertEquals(value.args, ('Test Exception', ))
|