Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not treat empty non-form input as HTML. #4566

Merged
merged 6 commits into from
Oct 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rest_framework/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ def _parse(self):
stream = None

if stream is None or media_type is None:
empty_data = QueryDict('', encoding=self._request._encoding)
if media_type and not is_form_media_type(media_type):
empty_data = QueryDict('', encoding=self._request._encoding)
else:
empty_data = {}
empty_files = MultiValueDict()
return (empty_data, empty_files)

Expand Down
22 changes: 22 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.shortcuts import redirect
from django.test import TestCase, override_settings

from rest_framework import fields, serializers
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.test import (
Expand Down Expand Up @@ -37,10 +38,22 @@ def redirect_view(request):
return redirect('/view/')


class BasicSerializer(serializers.Serializer):
flag = fields.BooleanField(default=lambda: True)


@api_view(['POST'])
def post_view(request):
serializer = BasicSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(serializer.validated_data)


urlpatterns = [
url(r'^view/$', view),
url(r'^session-view/$', session_view),
url(r'^redirect-view/$', redirect_view),
url(r'^post-view/$', post_view)
]


Expand Down Expand Up @@ -181,6 +194,15 @@ def test_invalid_multipart_data(self):
path='/view/', data={'valid': 123, 'invalid': {'a': 123}}
)

def test_empty_post_uses_default_boolean_value(self):
response = self.client.post(
'/post-view/',
data=None,
content_type='application/json'
)
self.assertEqual(response.status_code, 200, response.content)
self.assertEqual(response.data, {"flag": True})


class TestAPIRequestFactory(TestCase):
def test_csrf_exempt_by_default(self):
Expand Down