| 1 | """ |
| 2 | Tests for django.core.handlers.base.BaseHandler |
| 3 | """ |
| 4 | from django.test import TestCase |
| 5 | from django.conf import settings |
| 6 | from django.core.handlers.base import BaseHandler |
| 7 | from django.http import HttpRequest |
| 8 | from middleware import (ProcessRequestException, ProcessViewException, |
| 9 | ProcessExceptionResponse, ProcessRequestRaise, ProcessViewRaise) |
| 10 | |
| 11 | |
| 12 | class BaseHandlerTestCase(TestCase): |
| 13 | |
| 14 | def setUp(self): |
| 15 | self.middleware_classes = settings.MIDDLEWARE_CLASSES |
| 16 | |
| 17 | # Tests want uncaught exceptions to fall through |
| 18 | self.debug_propagate_exceptions = settings.DEBUG_PROPAGATE_EXCEPTIONS |
| 19 | settings.DEBUG_PROPAGATE_EXCEPTIONS = True |
| 20 | |
| 21 | self.handler = BaseHandler() |
| 22 | |
| 23 | def tearDown(self): |
| 24 | # reset settings changes |
| 25 | settings.MIDDLEWARE_CLASSES = self.middleware_classes |
| 26 | settings.DEBUG_PROPAGATE_EXCEPTIONS = self.debug_propagate_exceptions |
| 27 | |
| 28 | def _get_request(self, path): |
| 29 | request = HttpRequest() |
| 30 | request.META = { |
| 31 | 'SERVER_NAME': 'testserver', |
| 32 | 'SERVER_PORT': 80, |
| 33 | } |
| 34 | request.path = request.path_info = "/base_handler/%s" % path |
| 35 | return request |
| 36 | |
| 37 | def test_process_request_raises_uncaught(self): |
| 38 | """ |
| 39 | Exceptions generated in <Middleware>.process_request() should propagate uncaught. |
| 40 | """ |
| 41 | settings.MIDDLEWARE_CLASSES = ( |
| 42 | 'regressiontests.base_handler.middleware.ProcessRequestRaise', |
| 43 | ) |
| 44 | |
| 45 | self.handler.load_middleware() |
| 46 | |
| 47 | self.assertRaises( |
| 48 | ProcessRequestException, |
| 49 | self.handler.get_response, |
| 50 | self._get_request('path/') |
| 51 | ) |
| 52 | |
| 53 | def test_process_view_raises_uncaught(self): |
| 54 | """ |
| 55 | Exceptions generated in <Middleware>.process_view() without any |
| 56 | process_exception handlers should propagate uncaught. |
| 57 | """ |
| 58 | settings.MIDDLEWARE_CLASSES = ( |
| 59 | 'regressiontests.base_handler.middleware.ProcessViewRaise', |
| 60 | ) |
| 61 | |
| 62 | self.handler.load_middleware() |
| 63 | |
| 64 | self.assertRaises( |
| 65 | ProcessViewException, |
| 66 | self.handler.get_response, |
| 67 | self._get_request('path/'), |
| 68 | ) |
| 69 | |
| 70 | def test_view_raises_caught(self): |
| 71 | """ |
| 72 | Ticket #12250. A view that generates an exception and a middleware |
| 73 | whose process_exception() provides a response. Result should be that |
| 74 | response. |
| 75 | """ |
| 76 | settings.MIDDLEWARE_CLASSES = ( |
| 77 | 'regressiontests.base_handler.middleware.ProcessExceptionResponse', |
| 78 | ) |
| 79 | |
| 80 | self.handler.load_middleware() |
| 81 | response = self.handler.get_response(self._get_request('view_that_raises_exc/')) |
| 82 | |
| 83 | # Handled exception yields a valid response |
| 84 | self.assertEqual(response.status_code, 200) |
| 85 | |
| 86 | def test_process_view_raises_caught(self): |
| 87 | """ |
| 88 | A <Middleware>.process_view() that generates an exception and another |
| 89 | middleware whose process_exception() provides a response. Result should |
| 90 | be that response. |
| 91 | """ |
| 92 | settings.MIDDLEWARE_CLASSES = ( |
| 93 | 'regressiontests.base_handler.middleware.ProcessViewRaise', |
| 94 | 'regressiontests.base_handler.middleware.ProcessExceptionResponse', |
| 95 | ) |
| 96 | |
| 97 | self.handler.load_middleware() |
| 98 | response = self.handler.get_response(self._get_request('path/')) |
| 99 | |
| 100 | # Handled exception yields a valid response |
| 101 | self.assertEqual(response.status_code, 200) |