#32164: Broken content_length header in AsyncTestClient when doing post requests
with json data
-----------------------------------+--------------------------------------
Reporter: patrick | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 3.1
Severity: Normal | Resolution:
Keywords: aysnc | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Description changed by patrick:
Old description:
> Hello there, this is my first formal bug report, so I hope I'm doing this
> correctly. Anyway I was working on a async view and adding a test for it
> but I got this error:
>
> {{{
> ERROR django.request:log.py:224 Internal Server Error: /graphql_async
> Traceback (most recent call last):
> File "/Users/patrick/Documents/github/strawberry-
> graphql/strawberry/.venv/lib/python3.9/site-packages/asgiref/sync.py",
> line 339, in thread_handler
> raise exc_info[1]
> File "/Users/patrick/Documents/github/strawberry-
> graphql/strawberry/.venv/lib/python3.9/site-
> packages/django/core/handlers/exception.py", line 38, in inner
> response = await get_response(request)
> File "/Users/patrick/Documents/github/strawberry-
> graphql/strawberry/.venv/lib/python3.9/site-
> packages/django/core/handlers/base.py", line 231, in _get_response_async
> response = await wrapped_callback(request, *callback_args,
> **callback_kwargs)
> File "/Users/patrick/Documents/github/strawberry-
> graphql/strawberry/strawberry/django/views.py", line 145, in dispatch
> operation_context = self.get_execution_context(request)
> File "/Users/patrick/Documents/github/strawberry-
> graphql/strawberry/strawberry/django/views.py", line 51, in
> get_execution_context
> data = self.parse_body(request)
> File "/Users/patrick/Documents/github/strawberry-
> graphql/strawberry/strawberry/django/views.py", line 42, in parse_body
> return json.loads(request.body)
> File "/Users/patrick/Documents/github/strawberry-
> graphql/strawberry/.venv/lib/python3.9/site-
> packages/django/http/request.py", line 320, in body
> int(self.META.get('CONTENT_LENGTH') or 0) >
> settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
> ValueError: invalid literal for int() with base 10:
> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
> }}}
>
> after digging a bit it looks like we set the content_length to
> `bytes(len(data))`, which seems to be wrong, since it produces this
> string:
>
> {{{
> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
> }}}
>
> Code is here:
> https://github.com/django/django/blob/d1791539a7d86739cd44c909fa8239cae7f85874/django/test/client.py#L543
>
> Also I did a patch for this which seems to work:
> https://github.com/django/django/pull/13632
New description:
Hello there, this is my first formal bug report, so I hope I'm doing this
correctly. Anyway I was working on a async view and adding a test for it
but I got this error:
{{{
ERROR django.request:log.py:224 Internal Server Error: /graphql_async
Traceback (most recent call last):
File "/Users/patrick/Documents/github/strawberry-
graphql/strawberry/.venv/lib/python3.9/site-packages/asgiref/sync.py",
line 339, in thread_handler
raise exc_info[1]
File "/Users/patrick/Documents/github/strawberry-
graphql/strawberry/.venv/lib/python3.9/site-
packages/django/core/handlers/exception.py", line 38, in inner
response = await get_response(request)
File "/Users/patrick/Documents/github/strawberry-
graphql/strawberry/.venv/lib/python3.9/site-
packages/django/core/handlers/base.py", line 231, in _get_response_async
response = await wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/Users/patrick/Documents/github/strawberry-
graphql/strawberry/strawberry/django/views.py", line 145, in dispatch
operation_context = self.get_execution_context(request)
File "/Users/patrick/Documents/github/strawberry-
graphql/strawberry/strawberry/django/views.py", line 51, in
get_execution_context
data = self.parse_body(request)
File "/Users/patrick/Documents/github/strawberry-
graphql/strawberry/strawberry/django/views.py", line 42, in parse_body
return json.loads(request.body)
File "/Users/patrick/Documents/github/strawberry-
graphql/strawberry/.venv/lib/python3.9/site-
packages/django/http/request.py", line 320, in body
int(self.META.get('CONTENT_LENGTH') or 0) >
settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
ValueError: invalid literal for int() with base 10:
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
}}}
after digging a bit it looks like we set the content_length to
`bytes(len(data))`, which seems to be wrong, since it produces this
string:
{{{
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
}}}
Code is here:
https://github.com/django/django/blob/d1791539a7d86739cd44c909fa8239cae7f85874/django/test/client.py#L543
Code the reproduce is this:
{{{
class AsyncRequestFactoryTestPassingData(SimpleTestCase):
request_factory = AsyncRequestFactory()
async def test_request_factory(self):
async def async_generic_view(request):
return HttpResponse(status=200, content=request.body)
request = self.request_factory.post('/somewhere/',
data={'example': 'data'}, content_type="application/json")
response = await async_generic_view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'{"example": "data"}')
}}}
Also I did a patch for this which seems to work:
https://github.com/django/django/pull/13632
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32164#comment:1>
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.39ace576a94531978b8be178e8cbf7e0%40djangoproject.com.