mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-26 03:53:42 +03:00
Merge pull request #5351 from rpkilby/requestfactory-contenttype
Unexpected result when passing empty body to RequestFactory
This commit is contained in:
commit
27c382c98d
|
@ -303,7 +303,7 @@ class Request(object):
|
||||||
stream = None
|
stream = None
|
||||||
|
|
||||||
if stream is None or media_type is 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)
|
empty_data = QueryDict('', encoding=self._request._encoding)
|
||||||
else:
|
else:
|
||||||
empty_data = {}
|
empty_data = {}
|
||||||
|
|
|
@ -227,6 +227,15 @@ class APIRequestFactory(DjangoRequestFactory):
|
||||||
data, content_type = self._encode_data(data, format, content_type)
|
data, content_type = self._encode_data(data, format, content_type)
|
||||||
return self.generic('OPTIONS', path, data, content_type, **extra)
|
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):
|
def request(self, **kwargs):
|
||||||
request = super(APIRequestFactory, self).request(**kwargs)
|
request = super(APIRequestFactory, self).request(**kwargs)
|
||||||
request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
|
request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
|
||||||
|
|
|
@ -274,3 +274,12 @@ class TestAPIRequestFactory(TestCase):
|
||||||
assert dict(request.GET) == {'demo': ['testé']}
|
assert dict(request.GET) == {'demo': ['testé']}
|
||||||
request = factory.get('/view/', {'demo': 'testé'})
|
request = factory.get('/view/', {'demo': 'testé'})
|
||||||
assert dict(request.GET) == {'demo': ['testé']}
|
assert dict(request.GET) == {'demo': ['testé']}
|
||||||
|
|
||||||
|
def test_empty_request_content_type(self):
|
||||||
|
factory = APIRequestFactory()
|
||||||
|
request = factory.post(
|
||||||
|
'/post-view/',
|
||||||
|
data=None,
|
||||||
|
content_type='application/json',
|
||||||
|
)
|
||||||
|
assert request.META['CONTENT_TYPE'] == 'application/json'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user