diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 65b5282c5..8cdffcfc2 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -13,6 +13,11 @@ from debug_toolbar.toolbar import DebugToolbar from debug_toolbar.utils import clear_stack_trace_caches +try: + from django.core.handlers.asgi import ASGIRequest +except ImportError: + ASGIRequest = None + _HTML_TYPES = ("text/html", "application/xhtml+xml") @@ -73,6 +78,10 @@ def __call__(self, request): if not show_toolbar(request) or DebugToolbar.is_toolbar_request(request): return self.get_response(request) + if self._check_async_request(request): + self._show_async_request_is_not_supported() + return self.get_response(request) + toolbar = DebugToolbar(request, self.get_response) # Activate instrumentation ie. monkey-patch. @@ -133,3 +142,12 @@ def get_headers(request, panels): else: headers[header] = value return headers + + def _check_async_request(self, request) -> bool: + return type(request) == ASGIRequest + + @staticmethod + def _show_async_request_is_not_supported(): + print("-" * 10) + print("Be caution, django-debug-toolbar does not support async requests!") + print("-" * 10) diff --git a/docs/changes.rst b/docs/changes.rst index d47f69784..428842372 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -6,6 +6,7 @@ Pending * Changed ordering (and grammatical number) of panels and their titles in documentation to match actual panel ordering and titles. +* Added warning "async is not supported" 4.4.5 (2024-07-05) ------------------ diff --git a/tests/test_integration.py b/tests/test_integration.py index 95207c21b..9a0e826b0 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,5 +1,7 @@ +import io import os import re +import sys import time import unittest from unittest.mock import patch @@ -11,7 +13,7 @@ from django.db import connection from django.http import HttpResponse from django.template.loader import get_template -from django.test import RequestFactory +from django.test import AsyncRequestFactory, RequestFactory from django.test.utils import override_settings from debug_toolbar.forms import SignedDataForm @@ -843,3 +845,43 @@ def test_theme_toggle(self): self.get("/regular/basic/") toolbar = self.selenium.find_element(By.ID, "djDebug") self.assertEqual(toolbar.get_attribute("data-theme"), "light") + + +@override_settings(DEBUG=True) +class DebugToolbarAsyncTestCase(BaseTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.async_rf = AsyncRequestFactory() + cls.simple_get_response = lambda *args, **kwargs: HttpResponse( + "" + ) + cls._default_stdout = sys.stdout + + def setUp(self): + super().setUp() + self.captured_output = io.StringIO() + sys.stdout = self.captured_output + + @classmethod + def tearDownClass(cls): + super().tearDownClass() + sys.stdout = cls._default_stdout + + def test_do_not_render_toolbar_if_it_was_async_request(self): + captured_output = io.StringIO() + sys.stdout = captured_output + + request = self.async_rf.get("/") + response = DebugToolbarMiddleware(self.simple_get_response)(request) + + self.assertEqual(response.content, b"") + + def test_prints_warning_async_is_not_supported(self): + request = self.async_rf.get("/") + DebugToolbarMiddleware(self.simple_get_response)(request) + + assert ( + self.captured_output.getvalue() + == "----------\nBe caution, django-debug-toolbar does not support async requests!\n----------\n" + )