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

Backport of 542b4b3ab4 from master
This commit is contained in:
Patrick Arminio 2020-11-02 17:42:13 +00:00 committed by Mariusz Felisiak
parent 31946faf4f
commit bb74d2db98
3 changed files with 18 additions and 2 deletions

View File

@ -537,7 +537,7 @@ class AsyncRequestFactory(RequestFactory):
}
if data:
s['headers'].extend([
(b'content-length', bytes(len(data))),
(b'content-length', str(len(data)).encode('ascii')),
(b'content-type', content_type.encode('ascii')),
])
s['_body_file'] = FakePayload(data)

View File

@ -9,4 +9,5 @@ Django 3.1.4 fixes several bugs in 3.1.3.
Bugfixes
========
* ...
* Fixed setting the ``Content-Length`` HTTP header in ``AsyncRequestFactory``
(:ticket:`32162`).

View File

@ -973,3 +973,18 @@ class AsyncRequestFactoryTest(SimpleTestCase):
request = method('/somewhere/')
response = await async_generic_view(request)
self.assertEqual(response.status_code, 200)
async def test_request_factory_data(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',
)
self.assertEqual(request.headers['content-length'], '19')
self.assertEqual(request.headers['content-type'], 'application/json')
response = await async_generic_view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'{"example": "data"}')