Opened 4 years ago

Closed 4 years ago

#32164 closed Bug (duplicate)

Broken content_length header in AsyncTestClient when doing post requests with json data

Reported by: patrick Owned by: nobody
Component: Testing framework Version: 3.1
Severity: Normal Keywords: aysnc
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by patrick)

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 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/", 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/", 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/", 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/", line 145, in dispatch
    operation_context = self.get_execution_context(request)
  File "/Users/patrick/Documents/github/strawberry-graphql/strawberry/strawberry/django/", line 51, in get_execution_context
    data = self.parse_body(request)
  File "/Users/patrick/Documents/github/strawberry-graphql/strawberry/strawberry/django/", 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/", 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:


Code is here:

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 ='/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:

Change History (2)

comment:1 by patrick, 4 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 4 years ago

Has patch: unset
Resolution: duplicate
Status: newclosed

Duplicate of #32162.

Note: See TracTickets for help on using tickets.
Back to Top