diff --git a/tests/middleware_exceptions/middleware.py b/tests/middleware_exceptions/middleware.py
index b87da84b9d..a9746998ec 100644
a
|
b
|
|
1 | 1 | import asyncio |
| 2 | import contextvars |
2 | 3 | |
3 | 4 | from django.http import Http404, HttpResponse |
4 | 5 | from django.template import engines |
… |
… |
from django.utils.decorators import (
|
7 | 8 | async_only_middleware, sync_and_async_middleware, sync_only_middleware, |
8 | 9 | ) |
9 | 10 | |
| 11 | current_context = contextvars.ContextVar('current_context') |
10 | 12 | log = [] |
11 | 13 | |
12 | 14 | |
… |
… |
class SyncAndAsyncMiddleware(BaseMiddleware):
|
123 | 125 | pass |
124 | 126 | |
125 | 127 | |
| 128 | @sync_and_async_middleware |
| 129 | class SyncAndAsyncTemplateResponseMiddleware(BaseMiddleware): |
| 130 | def process_view(self, request, view_func, view_args, view_kwargs): |
| 131 | request.META['_CONTEXT_RESET_TOKEN'] = current_context.set(id(request)) |
| 132 | return None |
| 133 | |
| 134 | def process_template_response(self, request, response): |
| 135 | current_context.reset(request.META['_CONTEXT_RESET_TOKEN']) |
| 136 | response.context_data['mw'].append(self.__class__.__name__) |
| 137 | return response |
| 138 | |
| 139 | |
126 | 140 | @sync_only_middleware |
127 | 141 | class DecoratedPaymentMiddleware(PaymentMiddleware): |
128 | 142 | pass |
diff --git a/tests/middleware_exceptions/tests.py b/tests/middleware_exceptions/tests.py
index a68775d173..e8bdbc4700 100644
a
|
b
|
class MiddlewareSyncAsyncTests(SimpleTestCase):
|
300 | 300 | self.assertEqual(response.content, b'OK') |
301 | 301 | self.assertEqual(response.status_code, 200) |
302 | 302 | |
| 303 | @override_settings(MIDDLEWARE=[ |
| 304 | 'middleware_exceptions.middleware.SyncAndAsyncTemplateResponseMiddleware', |
| 305 | ]) |
| 306 | def test_sync_process_template_response(self): |
| 307 | response = self.client.get( |
| 308 | '/middleware_exceptions/template_response/' |
| 309 | ) |
| 310 | self.assertEqual( |
| 311 | response.content, |
| 312 | b'template_response OK\nSyncAndAsyncTemplateResponseMiddleware', |
| 313 | ) |
| 314 | |
| 315 | @override_settings(MIDDLEWARE=[ |
| 316 | 'middleware_exceptions.middleware.SyncAndAsyncTemplateResponseMiddleware', |
| 317 | ]) |
| 318 | async def test_async_process_template_response(self): |
| 319 | response = await self.async_client.get( |
| 320 | '/middleware_exceptions/template_response/' |
| 321 | ) |
| 322 | self.assertEqual( |
| 323 | response.content, |
| 324 | b'template_response OK\nSyncAndAsyncTemplateResponseMiddleware', |
| 325 | ) |
| 326 | |
303 | 327 | |
304 | 328 | @override_settings(ROOT_URLCONF='middleware_exceptions.urls') |
305 | 329 | class AsyncMiddlewareTests(SimpleTestCase): |