|
7 | 7 | from django.core.files.uploadhandler import (
|
8 | 8 | MemoryFileUploadHandler, TemporaryFileUploadHandler
|
9 | 9 | )
|
| 10 | +from django.http.request import RawPostDataException |
10 | 11 | from django.test import TestCase
|
11 | 12 | from django.utils.six.moves import StringIO
|
12 | 13 |
|
13 | 14 | from rest_framework.exceptions import ParseError
|
14 |
| -from rest_framework.parsers import FileUploadParser, FormParser |
| 15 | +from rest_framework.parsers import ( |
| 16 | + FileUploadParser, FormParser, JSONParser, MultiPartParser |
| 17 | +) |
| 18 | +from rest_framework.request import Request |
| 19 | +from rest_framework.test import APIRequestFactory |
15 | 20 |
|
16 | 21 |
|
17 | 22 | class Form(forms.Form):
|
@@ -122,3 +127,39 @@ def test_get_encoded_filename(self):
|
122 | 127 |
|
123 | 128 | def __replace_content_disposition(self, disposition):
|
124 | 129 | self.parser_context['request'].META['HTTP_CONTENT_DISPOSITION'] = disposition
|
| 130 | + |
| 131 | + |
| 132 | +class TestPOSTAccessed(TestCase): |
| 133 | + def setUp(self): |
| 134 | + self.factory = APIRequestFactory() |
| 135 | + |
| 136 | + def test_post_accessed_in_post_method(self): |
| 137 | + django_request = self.factory.post('/', {'foo': 'bar'}) |
| 138 | + request = Request(django_request, parsers=[FormParser(), MultiPartParser()]) |
| 139 | + django_request.POST |
| 140 | + assert request.POST == {'foo': ['bar']} |
| 141 | + assert request.data == {'foo': ['bar']} |
| 142 | + |
| 143 | + def test_post_accessed_in_post_method_with_json_parser(self): |
| 144 | + django_request = self.factory.post('/', {'foo': 'bar'}) |
| 145 | + request = Request(django_request, parsers=[JSONParser()]) |
| 146 | + django_request.POST |
| 147 | + assert request.POST == {} |
| 148 | + assert request.data == {} |
| 149 | + |
| 150 | + def test_post_accessed_in_put_method(self): |
| 151 | + django_request = self.factory.put('/', {'foo': 'bar'}) |
| 152 | + request = Request(django_request, parsers=[FormParser(), MultiPartParser()]) |
| 153 | + django_request.POST |
| 154 | + assert request.POST == {'foo': ['bar']} |
| 155 | + assert request.data == {'foo': ['bar']} |
| 156 | + |
| 157 | + def test_request_read_before_parsing(self): |
| 158 | + django_request = self.factory.put('/', {'foo': 'bar'}) |
| 159 | + request = Request(django_request, parsers=[FormParser(), MultiPartParser()]) |
| 160 | + django_request.read() |
| 161 | + with pytest.raises(RawPostDataException): |
| 162 | + request.POST |
| 163 | + with pytest.raises(RawPostDataException): |
| 164 | + request.POST |
| 165 | + request.data |
0 commit comments