mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-13 10:00:53 +03:00
Raise helpful error when erronously including nested data in multipart post requests with test client. Closes #2919.
This commit is contained in:
parent
c14ad7add7
commit
132eab7bbd
|
@ -679,4 +679,12 @@ class MultiPartRenderer(BaseRenderer):
|
||||||
BOUNDARY = 'BoUnDaRyStRiNg' if django.VERSION >= (1, 5) else b'BoUnDaRyStRiNg'
|
BOUNDARY = 'BoUnDaRyStRiNg' if django.VERSION >= (1, 5) else b'BoUnDaRyStRiNg'
|
||||||
|
|
||||||
def render(self, data, accepted_media_type=None, renderer_context=None):
|
def render(self, data, accepted_media_type=None, renderer_context=None):
|
||||||
|
if hasattr(data, 'items'):
|
||||||
|
for key, value in data.items():
|
||||||
|
assert not isinstance(value, dict), (
|
||||||
|
"Test data contained a dictionary value for key '%s', "
|
||||||
|
"but multipart uploads do not support nested data. "
|
||||||
|
"You may want to consider using format='JSON' in this "
|
||||||
|
"test case." % key
|
||||||
|
)
|
||||||
return encode_multipart(self.BOUNDARY, data)
|
return encode_multipart(self.BOUNDARY, data)
|
||||||
|
|
|
@ -172,6 +172,16 @@ class TestAPITestClient(TestCase):
|
||||||
self.assertIsNotNone(response.redirect_chain)
|
self.assertIsNotNone(response.redirect_chain)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
def test_invalid_multipart_data(self):
|
||||||
|
"""
|
||||||
|
MultiPart encoding cannot support nested data, so raise a helpful
|
||||||
|
error if the user attempts to do so.
|
||||||
|
"""
|
||||||
|
self.assertRaises(
|
||||||
|
AssertionError, self.client.post,
|
||||||
|
path='/view/', data={'valid': 123, 'invalid': {'a': 123}}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestAPIRequestFactory(TestCase):
|
class TestAPIRequestFactory(TestCase):
|
||||||
def test_csrf_exempt_by_default(self):
|
def test_csrf_exempt_by_default(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user