#35414: Issue with AsyncClient ignoring default headers compared to synchronous
Client
-------------------------------------+-------------------------------------
     Reporter:  설원준(Wonjoon       |                    Owner:  nobody
  Seol)/Dispatch squad               |
         Type:  Bug                  |                   Status:  new
    Component:  HTTP handling        |                  Version:  5.0
     Severity:  Normal               |               Resolution:
     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
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

 * cc: Andrew Godwin, Carlton Gibson (added)

Comment:

 I've been thinking about this report for a while, and after making some
 experiments, I think this is a valid issue. Originally I thought this was
 a dupe of #32159 but I don't think that's the case.

 What I can't put my finger on is whether there is an issue with the docs,
 or if the headers should really be added to the ASGI scope `headers` key
 (as the reporter says). The docs shows:

 > AsyncClient has the same methods and signatures as the synchronous
 (normal) test client, with the following exceptions:
 > In the initialization, arbitrary keyword arguments in defaults are added
 directly into the ASGI scope.
 > Headers passed as extra keyword arguments should not have the HTTP_
 prefix required by the synchronous client (see Client.get()). For example,
 here is how to set an HTTP Accept header:

 To me this implies that the `headers` parameter at class instantiation
 time could be use to set parameters to be used in every client operation
 (just like with `Client`). But when using the test case provided by the
 reporter this is not the case (using a simple Django app with the provided
 middleware and a simple view):

 {{{#!python
 from django.test import TestCase, AsyncClient


 class Ticket35414Tests(TestCase):

     async def test_should_return_ok(self):
         async_client = AsyncClient(headers={"AUTHORIZATION": "A Token"})
         response = await async_client.get("/ticket_35414/")
         self.assertEqual(response.status_code, 200)
 }}}

 Failure:
 {{{
 ======================================================================
 FAIL: test_should_return_ok
 (ticket_35414.tests.Ticket35414Tests.test_should_return_ok)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/home/nessita/.virtualenvs/djangodev/lib/python3.11/site-
 packages/asgiref/sync.py", line 254, in __call__
     return call_result.result()
            ^^^^^^^^^^^^^^^^^^^^
   File "/usr/lib/python3.11/concurrent/futures/_base.py", line 449, in
 result
     return self.__get_result()
            ^^^^^^^^^^^^^^^^^^^
   File "/usr/lib/python3.11/concurrent/futures/_base.py", line 401, in
 __get_result
     raise self._exception
   File "/home/nessita/.virtualenvs/djangodev/lib/python3.11/site-
 packages/asgiref/sync.py", line 331, in main_wrap
     result = await self.awaitable(*args, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/home/nessita/fellowship/projectfromrepo/ticket_35414/tests.py",
 line 9, in test_should_return_ok
     self.assertEqual(response.status_code, 200)
 AssertionError: 401 != 200
 }}}

 Carlton, Andrew, would you have an opinion?
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35414#comment:11>
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/0107018f5f35c96a-7a3d0435-e4ac-40e6-97d5-cfa254ad43b3-000000%40eu-central-1.amazonses.com.

Reply via email to