Skip to content

Commit c9b3fd6

Browse files
author
Ryan P Kilby
committed
Force content_type inclusion in APIRequestFactory
1 parent eb88687 commit c9b3fd6

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

rest_framework/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def _parse(self):
303303
stream = None
304304

305305
if stream is None or media_type is None:
306-
if media_type and not is_form_media_type(media_type):
306+
if media_type and is_form_media_type(media_type):
307307
empty_data = QueryDict('', encoding=self._request._encoding)
308308
else:
309309
empty_data = {}

rest_framework/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ def options(self, path, data=None, format=None, content_type=None, **extra):
227227
data, content_type = self._encode_data(data, format, content_type)
228228
return self.generic('OPTIONS', path, data, content_type, **extra)
229229

230+
def generic(self, method, path, data='',
231+
content_type='application/octet-stream', secure=False, **extra):
232+
# Include the CONTENT_TYPE, regardless of whether or not data is empty.
233+
if content_type is not None:
234+
extra['CONTENT_TYPE'] = content_type
235+
236+
return super(APIRequestFactory, self).generic(
237+
method, path, data, content_type, secure, **extra)
238+
230239
def request(self, **kwargs):
231240
request = super(APIRequestFactory, self).request(**kwargs)
232241
request._dont_enforce_csrf_checks = not self.enforce_csrf_checks

tests/test_testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,4 @@ def test_empty_request_content_type(self):
282282
data=None,
283283
content_type='application/json',
284284
)
285-
assert request.content_type == 'application/json'
285+
assert request.META['CONTENT_TYPE'] == 'application/json'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy