|
| 1 | +import io |
1 | 2 | import os
|
2 | 3 | import re
|
| 4 | +import sys |
3 | 5 | import time
|
4 | 6 | import unittest
|
5 | 7 | from unittest.mock import patch
|
|
33 | 35 | webdriver = None
|
34 | 36 |
|
35 | 37 |
|
| 38 | +try: |
| 39 | + from django.test import AsyncRequestFactory |
| 40 | +except ImportError: |
| 41 | + AsyncRequestFactory = None |
| 42 | + |
| 43 | + |
36 | 44 | rf = RequestFactory()
|
37 | 45 |
|
38 | 46 |
|
@@ -843,3 +851,46 @@ def test_theme_toggle(self):
|
843 | 851 | self.get("/regular/basic/")
|
844 | 852 | toolbar = self.selenium.find_element(By.ID, "djDebug")
|
845 | 853 | self.assertEqual(toolbar.get_attribute("data-theme"), "light")
|
| 854 | + |
| 855 | + |
| 856 | +@unittest.skipUnless( |
| 857 | + AsyncRequestFactory is not None, "Test valid only for django with async requests" |
| 858 | +) |
| 859 | +@override_settings(DEBUG=True) |
| 860 | +class DebugToolbarAsyncTestCase(BaseTestCase): |
| 861 | + @classmethod |
| 862 | + def setUpClass(cls): |
| 863 | + super().setUpClass() |
| 864 | + cls.async_rf = AsyncRequestFactory() |
| 865 | + cls.simple_get_response = lambda *args, **kwargs: HttpResponse( |
| 866 | + "<html><body></body></html>" |
| 867 | + ) |
| 868 | + cls._default_stdout = sys.stdout |
| 869 | + |
| 870 | + def setUp(self): |
| 871 | + super().setUp() |
| 872 | + self.captured_output = io.StringIO() |
| 873 | + sys.stdout = self.captured_output |
| 874 | + |
| 875 | + @classmethod |
| 876 | + def tearDownClass(cls): |
| 877 | + super().tearDownClass() |
| 878 | + sys.stdout = cls._default_stdout |
| 879 | + |
| 880 | + def test_do_not_render_toolbar_if_it_was_async_request(self): |
| 881 | + captured_output = io.StringIO() |
| 882 | + sys.stdout = captured_output |
| 883 | + |
| 884 | + request = self.async_rf.get("/") |
| 885 | + response = DebugToolbarMiddleware(self.simple_get_response)(request) |
| 886 | + |
| 887 | + self.assertEqual(response.content, b"<html><body></body></html>") |
| 888 | + |
| 889 | + def test_prints_warning_async_is_not_supported(self): |
| 890 | + request = self.async_rf.get("/") |
| 891 | + DebugToolbarMiddleware(self.simple_get_response)(request) |
| 892 | + |
| 893 | + assert ( |
| 894 | + self.captured_output.getvalue() |
| 895 | + == "----------\nBe caution, django-debug-toolbar does not support async requests!\n----------\n" |
| 896 | + ) |
0 commit comments