#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:  invalid
     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 Mariusz,

 Thanks for the documentation link. So the async client intended behaviour
 is inconsistent with the sync counterpart.

 But I should've mentioned in my original post that the headers field does
 not work neither.
 Because converting the above example test using headers argument still
 fails.

 {{{
 class EXAMPLE_TESTS(TestCase):
     async def test_should_return_ok(     # FAILS
         self,
     ) -> None:
         async_client = AsyncClient(headers={"HTTP_AUTHORIZATION": f"Bearer
 I_AM_JWT_TOKEN"})

         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)
 }}}


 The reason is still due the the original post.


 {{{
     def _base_scope(self, **request):
         """The base scope for a request."""
         # This is a minimal valid ASGI scope, plus:
         # - headers['cookie'] for cookie support,
         # - 'client' often useful, see #8551.
         scope = {
             "asgi": {"version": "3.0"},
             "type": "http",
             "http_version": "1.1",
             "client": ["127.0.0.1", 0],
             "server": ("testserver", "80"),
             "scheme": "http",
             "method": "GET",
             "headers": [], # <- scope ignores default header
             **self.defaults,
             **request,
         }
         scope["headers"].append(
             (
                 b"cookie",
                 b"; ".join(
                     sorted(
                         ("%s=%s" % (morsel.key,
 morsel.coded_value)).encode("ascii")
                         for morsel in self.cookies.values()
                     )
                 ),
             )
         )
         return scope
 }}}

 the scope only takes in default argument but ignores default header.

 Using the example test, this is the constructed META header after the
 initialization.
 The default headers are still 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': ''}
 }}}

 Thanks.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35414#comment:9>
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/0107018f468dded2-194cd961-0ee4-4a2f-aa4e-ec09c91c3c14-000000%40eu-central-1.amazonses.com.

Reply via email to