Commit 132eab7b by Tom Christie

Raise helpful error when erronously including nested data in multipart post…

Raise helpful error when erronously including nested data in multipart post requests with test client. Closes #2919.
parent c14ad7ad
......@@ -679,4 +679,12 @@ class MultiPartRenderer(BaseRenderer):
BOUNDARY = 'BoUnDaRyStRiNg' if django.VERSION >= (1, 5) else b'BoUnDaRyStRiNg'
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)
......@@ -172,6 +172,16 @@ class TestAPITestClient(TestCase):
self.assertIsNotNone(response.redirect_chain)
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):
def test_csrf_exempt_by_default(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment