Skip to content

Commit

Permalink
Force content_type inclusion in APIRequestFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Aug 31, 2017
1 parent eb88687 commit 0ec915e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rest_framework/request.py
Expand Up @@ -303,7 +303,7 @@ def _parse(self):
stream = None

if stream is None or media_type is None:
if media_type and not is_form_media_type(media_type):
if media_type and is_form_media_type(media_type):
empty_data = QueryDict('', encoding=self._request._encoding)
else:
empty_data = {}
Expand Down
9 changes: 9 additions & 0 deletions rest_framework/test.py
Expand Up @@ -227,6 +227,15 @@ def options(self, path, data=None, format=None, content_type=None, **extra):
data, content_type = self._encode_data(data, format, content_type)
return self.generic('OPTIONS', path, data, content_type, **extra)

def generic(self, method, path, data='',
content_type='application/octet-stream', secure=False, **extra):
# Include the CONTENT_TYPE, regardless of whether or not data is empty.
if content_type is not None:
extra['CONTENT_TYPE'] = str(content_type)

return super(APIRequestFactory, self).generic(
method, path, data, content_type, secure, **extra)

def request(self, **kwargs):
request = super(APIRequestFactory, self).request(**kwargs)
request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
Expand Down
2 changes: 1 addition & 1 deletion tests/test_testing.py
Expand Up @@ -282,4 +282,4 @@ def test_empty_request_content_type(self):
data=None,
content_type='application/json',
)
assert request.content_type == 'application/json'
assert request.META['CONTENT_TYPE'] == 'application/json'

1 comment on commit 0ec915e

@alexarsh
Copy link

@alexarsh alexarsh commented on 0ec915e Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rpkilby ,
Hi. This commit breaks tests in my application. Can you take a look at my question, please?

Please sign in to comment.