Raise helpful error when erronously including nested data in multipart post requests with test client. Closes #2919.

This commit is contained in:
Tom Christie 2015-07-14 14:49:44 +01:00
parent c14ad7add7
commit 132eab7bbd
2 changed files with 18 additions and 0 deletions

View File

@ -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)

View File

@ -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):