#35414: Issue with AsyncClient ignoring default headers compared to synchronous
Client
-------------------------------------+-------------------------------------
     Reporter:  설원준(Wonjoon       |                    Owner:  nobody
  Seol)/Dispatch squad               |
         Type:  Bug                  |                   Status:  closed
    Component:  HTTP handling        |                  Version:  5.0
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  AsyncClient,         |             Triage Stage:
  ASGIRequest                        |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by 설원준(Wonjoon Seol)/Dispatch squad):

 Hi Sarah, the pytest fixture is only there to reduce boilerplates.
 Here is your requested test case without dependencies.

 **polls/views.py**

 {{{
 from django.http import JsonResponse
 def index(request):
     data = {"message": "This is an example API response"}
     return JsonResponse(data)
 }}}


 **middleware.py (Not required, can just print self.META inside
 ASGIRequest.**
 {{{
 class JWTMiddleware:
     def __init__(self, get_response):
         self.get_response = get_response

     def __call__(self, request):
         if 'HTTP_AUTHORIZATION' not in request.META:
             return JsonResponse({'error': 'Authorization header is
 missing'}, status=401)
         return self.get_response(request)
 }}}

 **polls/tests.py**
 {{{

 from http import HTTPStatus

 from django.test import TestCase, AsyncClient
 from django.urls import reverse


 class EXAMPLE_TESTS(TestCase):
     async def test_should_return_ok( # Fails
         self,
     ) -> None:
         async_client = AsyncClient(HTTP_AUTHORIZATION=f"Bearer
 I_AM_JWT_TOKEN") # AUTHORIZATION, HTTP_AUTHORIZATION both fails due to the
 reason in the original post.

         response = await async_client.get(
             reverse("index"),
         )

         self.assertEqual(response.status_code, HTTPStatus.OK)

     async def test_should_return_ok2( # Passes
             self,
     ) -> None:
         async_client = AsyncClient()

         response = await async_client.get(
             reverse("index"),
             AUTHORIZATION=f"Bearer I_AM_JWT_TOKEN"
         )

         self.assertEqual(response.status_code, HTTPStatus.OK)
 }}}



 {{{
 **printing META: (Customer header missing)**
 {'REQUEST_METHOD': 'GET', 'QUERY_STRING': '', 'SCRIPT_NAME': '',
 'PATH_INFO': '/polls/', 'wsgi.multithread': True, 'wsgi.multiprocess':
 True, 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_HOST': '127.0.0.1',
 'REMOTE_PORT': 0, 'SERVER_NAME': '127.0.0.1', 'SERVER_PORT': '80',
 'HTTP_HOST': 'testserver', 'HTTP_COOKIE': ''}

 F
 ======================================================================
 FAIL: test_should_return_ok
 (polls.tests.EXAMPLE_TESTS.test_should_return_ok)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
 ...
   File "/Users/.../workspace/django-mvp/mysite/polls/tests.py", line 17,
 in test_should_return_ok
     self.assertEqual(response.status_code, HTTPStatus.OK)
 AssertionError: 401 != <HTTPStatus.OK: 200>

 Ran 2 tests in 0.012s

 FAILED (failures=1)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35414#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018f2e81f32f-9edb90e9-b9e4-467d-901c-afe3a3347e15-000000%40eu-central-1.amazonses.com.

Reply via email to