|
| 1 | +import logging |
1 | 2 | import time |
2 | 3 |
|
| 4 | +from logging_tests.tests import LoggingAssertionMixin |
| 5 | + |
3 | 6 | from django.core.exceptions import ImproperlyConfigured |
4 | 7 | from django.http import HttpResponse |
5 | 8 | from django.test import RequestFactory, SimpleTestCase, override_settings |
@@ -63,7 +66,7 @@ def get(self, request): |
63 | 66 | return self |
64 | 67 |
|
65 | 68 |
|
66 | | -class ViewTest(SimpleTestCase): |
| 69 | +class ViewTest(LoggingAssertionMixin, SimpleTestCase): |
67 | 70 | rf = RequestFactory() |
68 | 71 |
|
69 | 72 | def _assert_simple(self, response): |
@@ -297,6 +300,25 @@ def test_direct_instantiation(self): |
297 | 300 | response = view.dispatch(self.rf.head("/")) |
298 | 301 | self.assertEqual(response.status_code, 405) |
299 | 302 |
|
| 303 | + def test_method_not_allowed_response_logged(self): |
| 304 | + for path, escaped in [ |
| 305 | + ("/foo/", "/foo/"), |
| 306 | + (r"/%1B[1;31mNOW IN RED!!!1B[0m/", r"/\x1b[1;31mNOW IN RED!!!1B[0m/"), |
| 307 | + ]: |
| 308 | + with self.subTest(path=path): |
| 309 | + request = self.rf.get(path, REQUEST_METHOD="BOGUS") |
| 310 | + with self.assertLogs("django.request", "WARNING") as handler: |
| 311 | + response = SimpleView.as_view()(request) |
| 312 | + |
| 313 | + self.assertLogRecord( |
| 314 | + handler, |
| 315 | + f"Method Not Allowed (BOGUS): {escaped}", |
| 316 | + logging.WARNING, |
| 317 | + 405, |
| 318 | + request, |
| 319 | + ) |
| 320 | + self.assertEqual(response.status_code, 405) |
| 321 | + |
300 | 322 |
|
301 | 323 | @override_settings(ROOT_URLCONF="generic_views.urls") |
302 | 324 | class TemplateViewTest(SimpleTestCase): |
@@ -425,7 +447,7 @@ def test_extra_context(self): |
425 | 447 |
|
426 | 448 |
|
427 | 449 | @override_settings(ROOT_URLCONF="generic_views.urls") |
428 | | -class RedirectViewTest(SimpleTestCase): |
| 450 | +class RedirectViewTest(LoggingAssertionMixin, SimpleTestCase): |
429 | 451 | rf = RequestFactory() |
430 | 452 |
|
431 | 453 | def test_no_url(self): |
@@ -549,6 +571,20 @@ def test_direct_instantiation(self): |
549 | 571 | response = view.dispatch(self.rf.head("/foo/")) |
550 | 572 | self.assertEqual(response.status_code, 410) |
551 | 573 |
|
| 574 | + def test_gone_response_logged(self): |
| 575 | + for path, escaped in [ |
| 576 | + ("/foo/", "/foo/"), |
| 577 | + (r"/%1B[1;31mNOW IN RED!!!1B[0m/", r"/\x1b[1;31mNOW IN RED!!!1B[0m/"), |
| 578 | + ]: |
| 579 | + with self.subTest(path=path): |
| 580 | + request = self.rf.get(path) |
| 581 | + with self.assertLogs("django.request", "WARNING") as handler: |
| 582 | + RedirectView().dispatch(request) |
| 583 | + |
| 584 | + self.assertLogRecord( |
| 585 | + handler, f"Gone: {escaped}", logging.WARNING, 410, request |
| 586 | + ) |
| 587 | + |
552 | 588 |
|
553 | 589 | class GetContextDataTest(SimpleTestCase): |
554 | 590 | def test_get_context_data_super(self): |
|
0 commit comments