Ticket #32815: django-issue-32815.diff

File django-issue-32815.diff, 2.6 KB (added by Michael Manganiello, 3 years ago)

Test scenario (diff over main commit f10c52afab)

  • tests/middleware_exceptions/middleware.py

    diff --git a/tests/middleware_exceptions/middleware.py b/tests/middleware_exceptions/middleware.py
    index b87da84b9d..a9746998ec 100644
    a b  
    11import asyncio
     2import contextvars
    23
    34from django.http import Http404, HttpResponse
    45from django.template import engines
    from django.utils.decorators import (  
    78    async_only_middleware, sync_and_async_middleware, sync_only_middleware,
    89)
    910
     11current_context = contextvars.ContextVar('current_context')
    1012log = []
    1113
    1214
    class SyncAndAsyncMiddleware(BaseMiddleware):  
    123125    pass
    124126
    125127
     128@sync_and_async_middleware
     129class 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
    126140@sync_only_middleware
    127141class DecoratedPaymentMiddleware(PaymentMiddleware):
    128142    pass
  • tests/middleware_exceptions/tests.py

    diff --git a/tests/middleware_exceptions/tests.py b/tests/middleware_exceptions/tests.py
    index a68775d173..e8bdbc4700 100644
    a b class MiddlewareSyncAsyncTests(SimpleTestCase):  
    300300        self.assertEqual(response.content, b'OK')
    301301        self.assertEqual(response.status_code, 200)
    302302
     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
    303327
    304328@override_settings(ROOT_URLCONF='middleware_exceptions.urls')
    305329class AsyncMiddlewareTests(SimpleTestCase):
Back to Top