Added a test to show the interface on Django's request.POST changes f… · D3X/django-rest-framework@aa6a4c4 (original) (raw)
1
1
`from django.conf.urls import url
`
2
2
`from django.contrib.auth.models import User
`
``
3
`+
from django.http import HttpResponse
`
3
4
`from django.test import override_settings
`
4
5
``
5
6
`from rest_framework.authentication import TokenAuthentication
`
6
7
`from rest_framework.authtoken.models import Token
`
7
8
`from rest_framework.response import Response
`
8
9
`from rest_framework.test import APITestCase
`
``
10
`+
from rest_framework.utils import json
`
9
11
`from rest_framework.views import APIView
`
10
12
``
11
13
``
`@@ -14,9 +16,14 @@ def post(self, request):
`
14
16
`return Response(data=request.data, status=200)
`
15
17
``
16
18
``
``
19
`+
def regular_view(request):
`
``
20
`+
return HttpResponse()
`
``
21
+
``
22
+
17
23
`urlpatterns = [
`
18
24
`url(r'^auth$', APIView.as_view(authentication_classes=(TokenAuthentication,))),
`
19
25
`url(r'^post$', PostView.as_view()),
`
``
26
`+
url(r'^regular$', regular_view),
`
20
27
`]
`
21
28
``
22
29
``
`@@ -44,6 +51,17 @@ def call(self, request):
`
44
51
`return response
`
45
52
``
46
53
``
``
54
`+
class RequestJSONMiddleware(object):
`
``
55
`+
def init(self, get_response):
`
``
56
`+
self.get_response = get_response
`
``
57
+
``
58
`+
def call(self, request):
`
``
59
`+
response = self.get_response(request)
`
``
60
`+
assert list(request.POST.keys()) == []
`
``
61
+
``
62
`+
return response
`
``
63
+
``
64
+
47
65
`@override_settings(ROOT_URLCONF='tests.test_middleware')
`
48
66
`class TestMiddleware(APITestCase):
`
49
67
``
`@@ -59,3 +77,13 @@ def test_middleware_can_access_user_when_processing_response(self):
`
59
77
`def test_middleware_can_access_request_post_when_processing_response(self):
`
60
78
`response = self.client.post('/post', {'foo': 'bar'})
`
61
79
`assert response.status_code == 200
`
``
80
+
``
81
`+
@override_settings(MIDDLEWARE=('tests.test_middleware.RequestJSONMiddleware',))
`
``
82
`+
def test_original_request_post_is_not_populated_on_json_requests(self):
`
``
83
`+
regular view
`
``
84
`+
response = self.client.post('/regular', json.dumps({'foo': 'bar'}), content_type='application/json')
`
``
85
`+
assert response.status_code == 200
`
``
86
+
``
87
`+
drf
`
``
88
`+
with self.assertRaises(AssertionError):
`
``
89
`+
response = self.client.post('/post', json.dumps({'foo': 'bar'}), content_type='application/json')
`