summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKevin Michel <kevin.michel@sereema.com>2020-08-24 22:25:33 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-08-28 12:35:53 +0200
commit3a42c0447b6a3187bd2aaae0bba566d835c47d22 (patch)
tree1352fdfce2e4761177be5a34523d29ff394733fb /tests
parent14a19700d8b90b5949381153c854613a45923ef4 (diff)
[3.1.x] Fixed #31928 -- Fixed detecting an async get_response in various middlewares.
SecurityMiddleware and the three cache middlewares were not calling super().__init__() during their initialization or calling the required MiddlewareMixin._async_check() method. This made the middlewares not properly present as coroutine and confused the middleware chain when used in a fully async context. Thanks Kordian Kowalski for the report. Backport of 825ce75faec63ce81601e31152c757a9c28fed13 from master
Diffstat (limited to 'tests')
-rw-r--r--tests/deprecation/test_middleware_mixin.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/deprecation/test_middleware_mixin.py b/tests/deprecation/test_middleware_mixin.py
index c90aeb8360..02987ff961 100644
--- a/tests/deprecation/test_middleware_mixin.py
+++ b/tests/deprecation/test_middleware_mixin.py
@@ -1,3 +1,4 @@
+import asyncio
import threading
from asgiref.sync import async_to_sync
@@ -45,6 +46,29 @@ class MiddlewareMixinTests(SimpleTestCase):
with self.assertRaisesMessage(RemovedInDjango40Warning, self.msg):
middleware()
+ def test_coroutine(self):
+ async def async_get_response(request):
+ return HttpResponse()
+
+ def sync_get_response(request):
+ return HttpResponse()
+
+ for middleware in [
+ CacheMiddleware,
+ FetchFromCacheMiddleware,
+ UpdateCacheMiddleware,
+ SecurityMiddleware,
+ ]:
+ with self.subTest(middleware=middleware.__qualname__):
+ # Middleware appears as coroutine if get_function is
+ # a coroutine.
+ middleware_instance = middleware(async_get_response)
+ self.assertIs(asyncio.iscoroutinefunction(middleware_instance), True)
+ # Middleware doesn't appear as coroutine if get_function is not
+ # a coroutine.
+ middleware_instance = middleware(sync_get_response)
+ self.assertIs(asyncio.iscoroutinefunction(middleware_instance), False)
+
def test_sync_to_async_uses_base_thread_and_connection(self):
"""
The process_request() and process_response() hooks must be called with