-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support an async middleware for the toolbar.
extract redudant code in _postprocess disable non async capable panels add middleware sync and async compatible test case rename file add panel async compatibility tests/ added panel async compatibility tests/ marked erreneous panels as non async refactor panel test Add function docstrings update async panel compatibility tests revert middleware back to __call__ and __acall__ approach update architecture.rst documentation fix typo in docs remove ASGI keyword from docs
- Loading branch information
1 parent
46efd5d
commit 25656ee
Showing
11 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.http import HttpResponse | ||
from django.test import AsyncRequestFactory, RequestFactory, TestCase | ||
|
||
from debug_toolbar.panels import Panel | ||
from debug_toolbar.toolbar import DebugToolbar | ||
|
||
|
||
class MockAsyncPanel(Panel): | ||
is_async = True | ||
|
||
|
||
class MockSyncPanel(Panel): | ||
is_async = False | ||
|
||
|
||
class PanelAsyncCompatibilityTestCase(TestCase): | ||
def setUp(self): | ||
self.async_factory = AsyncRequestFactory() | ||
self.wsgi_factory = RequestFactory() | ||
|
||
def test_panels_with_asgi(self): | ||
async_request = self.async_factory.get("/") | ||
toolbar = DebugToolbar(async_request, lambda request: HttpResponse()) | ||
|
||
async_panel = MockAsyncPanel(toolbar, async_request) | ||
sync_panel = MockSyncPanel(toolbar, async_request) | ||
|
||
self.assertTrue(async_panel.enabled) | ||
self.assertFalse(sync_panel.enabled) | ||
|
||
def test_panels_with_wsgi(self): | ||
wsgi_request = self.wsgi_factory.get("/") | ||
toolbar = DebugToolbar(wsgi_request, lambda request: HttpResponse()) | ||
|
||
async_panel = MockAsyncPanel(toolbar, wsgi_request) | ||
sync_panel = MockSyncPanel(toolbar, wsgi_request) | ||
|
||
self.assertTrue(async_panel.enabled) | ||
self.assertTrue(sync_panel.enabled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import asyncio | ||
|
||
from django.http import HttpResponse | ||
from django.test import AsyncRequestFactory, RequestFactory, TestCase | ||
|
||
from debug_toolbar.middleware import DebugToolbarMiddleware | ||
|
||
|
||
class MiddlewareSyncAsyncCompatibilityTestCase(TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
self.async_factory = AsyncRequestFactory() | ||
|
||
def test_sync_mode(self): | ||
""" | ||
test middlware switches to sync (__call__) based on get_response type | ||
""" | ||
|
||
request = self.factory.get("/") | ||
middleware = DebugToolbarMiddleware( | ||
lambda x: HttpResponse("<html><body>Django debug toolbar</body></html>") | ||
) | ||
|
||
self.assertFalse(asyncio.iscoroutinefunction(middleware)) | ||
|
||
response = middleware(request) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
async def test_async_mode(self): | ||
""" | ||
test middlware switches to async (__acall__) based on get_response type | ||
and returns a coroutine | ||
""" | ||
|
||
async def get_response(request): | ||
return HttpResponse("<html><body>Django debug toolbar</body></html>") | ||
|
||
middleware = DebugToolbarMiddleware(get_response) | ||
request = self.async_factory.get("/") | ||
|
||
self.assertTrue(asyncio.iscoroutinefunction(middleware)) | ||
|
||
response = await middleware(request) | ||
self.assertEqual(response.status_code, 200) |