Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32162 closed Bug (fixed)

AsyncTestClient crashes on requests with JSON data.

Reported by: Tom Miller Owned by: patrick
Component: Testing framework Version: 3.1
Severity: Release blocker Keywords: AsyncTestClient, AsyncRequestFactory
Cc: Andrew Godwin, Carlton Gibson Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

POST requests that are created using the AsyncTestClient do not seem to have a valid 'Content-Length' header. In addition, attempting to print the body of a POST request raises an exception (seemingly related to the invalid 'Content-Length').

These issues can be demonstrated using AsyncRequestFactory:

>>> from django.test import AsyncRequestFactory
>>> rf = AsyncRequestFactory()
>>> post_request = rf.post('/submit/', {'foo': 'bar'})
>>> print(post_request.headers)
{'Host': 'testserver', 'Content-Length': '\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\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\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\x00\x00', 'Content-Type': 'multipart/form-data; boundary=BoUnDaRyStRiNg', 'Cookie': ''}
>>> print(post_request.body)
ValueError                                Traceback (most recent call last)
/opt/my_project/venv/lib64/python3.6/site-packages/django/http/request.py in body(self)
    318             # Limit the maximum request data size that will be handled in-memory.
    319             if (settings.DATA_UPLOAD_MAX_MEMORY_SIZE is not None and
--> 320                     int(self.META.get('CONTENT_LENGTH') or 0) > settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
    321                 raise RequestDataTooBig('Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.')
    322 
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\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0

Change History (5)

comment:1 by Tom Miller, 3 years ago

Component: UncategorizedTesting framework

comment:2 by Mariusz Felisiak, 3 years ago

Cc: Andrew Godwin Carlton Gibson added
Has patch: set
Owner: changed from nobody to patrick
Severity: NormalRelease blocker
Status: newassigned
Summary: Invalid 'Content-Length' header, and printing request body raises exception in POST requests using AsyncTestClientAsyncTestClient crashes on requests with JSON data.
Triage Stage: UnreviewedAccepted

Thanks for the report!

PR

#32164 was closes as a duplicate.

comment:3 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In bb74d2db:

[3.1.x] Fixed #32162 -- Fixed setting Content-Length header in AsyncRequestFactory.

Backport of 542b4b3ab44d33dfd9b00c22f624ee4aed6f7534 from master

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 542b4b3a:

Fixed #32162 -- Fixed setting Content-Length header in AsyncRequestFactory.

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