|
8 | 8 | from django.shortcuts import redirect
|
9 | 9 | from django.test import TestCase, override_settings
|
10 | 10 |
|
| 11 | +from rest_framework import fields, serializers |
11 | 12 | from rest_framework.decorators import api_view
|
12 | 13 | from rest_framework.response import Response
|
13 | 14 | from rest_framework.test import (
|
@@ -37,10 +38,22 @@ def redirect_view(request):
|
37 | 38 | return redirect('/view/')
|
38 | 39 |
|
39 | 40 |
|
| 41 | +class BasicSerializer(serializers.Serializer): |
| 42 | + flag = fields.BooleanField(default=lambda: True) |
| 43 | + |
| 44 | + |
| 45 | +@api_view(['POST']) |
| 46 | +def post_view(request): |
| 47 | + serializer = BasicSerializer(data=request.data) |
| 48 | + serializer.is_valid(raise_exception=True) |
| 49 | + return Response(serializer.validated_data) |
| 50 | + |
| 51 | + |
40 | 52 | urlpatterns = [
|
41 | 53 | url(r'^view/$', view),
|
42 | 54 | url(r'^session-view/$', session_view),
|
43 | 55 | url(r'^redirect-view/$', redirect_view),
|
| 56 | + url(r'^post-view/$', post_view) |
44 | 57 | ]
|
45 | 58 |
|
46 | 59 |
|
@@ -181,6 +194,15 @@ def test_invalid_multipart_data(self):
|
181 | 194 | path='/view/', data={'valid': 123, 'invalid': {'a': 123}}
|
182 | 195 | )
|
183 | 196 |
|
| 197 | + def test_empty_post_uses_default_boolean_value(self): |
| 198 | + response = self.client.post( |
| 199 | + '/post-view/', |
| 200 | + data=None, |
| 201 | + content_type='application/json' |
| 202 | + ) |
| 203 | + self.assertEqual(response.status_code, 200, response.content) |
| 204 | + self.assertEqual(response.data, {"flag": True}) |
| 205 | + |
184 | 206 |
|
185 | 207 | class TestAPIRequestFactory(TestCase):
|
186 | 208 | def test_csrf_exempt_by_default(self):
|
|
0 commit comments