Fixed #30677 -- Improved error message for urlencode() and Client when None is passed as data.

This commit is contained in:
swatantra 2019-08-09 13:49:18 +05:30 committed by Mariusz Felisiak
parent c1b26c77a9
commit 73ac9e3f04
4 changed files with 13 additions and 12 deletions

View File

@ -193,8 +193,8 @@ def encode_multipart(boundary, data):
for (key, value) in data.items(): for (key, value) in data.items():
if value is None: if value is None:
raise TypeError( raise TypeError(
'Cannot encode None as POST data. Did you mean to pass an ' "Cannot encode None for key '%s' as POST data. Did you mean "
'empty string or omit the value?' "to pass an empty string or omit the value?" % key
) )
elif is_file(value): elif is_file(value):
lines.extend(encode_file(boundary, key, value)) lines.extend(encode_file(boundary, key, value))

View File

@ -113,8 +113,8 @@ def urlencode(query, doseq=False):
for key, value in query: for key, value in query:
if value is None: if value is None:
raise TypeError( raise TypeError(
'Cannot encode None in a query string. Did you mean to pass ' "Cannot encode None for key '%s' in a query string. Did you "
'an empty string or omit the value?' "mean to pass an empty string or omit the value?" % key
) )
elif not doseq or isinstance(value, (str, bytes)): elif not doseq or isinstance(value, (str, bytes)):
query_val = value query_val = value
@ -130,8 +130,9 @@ def urlencode(query, doseq=False):
for item in itr: for item in itr:
if item is None: if item is None:
raise TypeError( raise TypeError(
'Cannot encode None in a query string. Did you ' "Cannot encode None for key '%s' in a query "
'mean to pass an empty string or omit the value?' "string. Did you mean to pass an empty string or "
"omit the value?" % key
) )
elif not isinstance(item, bytes): elif not isinstance(item, bytes):
item = str(item) item = str(item)

View File

@ -61,8 +61,8 @@ class ClientTest(TestCase):
def test_get_data_none(self): def test_get_data_none(self):
msg = ( msg = (
'Cannot encode None in a query string. Did you mean to pass an ' "Cannot encode None for key 'value' in a query string. Did you "
'empty string or omit the value?' "mean to pass an empty string or omit the value?"
) )
with self.assertRaisesMessage(TypeError, msg): with self.assertRaisesMessage(TypeError, msg):
self.client.get('/get_view/', {'value': None}) self.client.get('/get_view/', {'value': None})
@ -102,8 +102,8 @@ class ClientTest(TestCase):
def test_post_data_none(self): def test_post_data_none(self):
msg = ( msg = (
'Cannot encode None as POST data. Did you mean to pass an empty ' "Cannot encode None for key 'value' as POST data. Did you mean "
'string or omit the value?' "to pass an empty string or omit the value?"
) )
with self.assertRaisesMessage(TypeError, msg): with self.assertRaisesMessage(TypeError, msg):
self.client.post('/post_view/', {'value': None}) self.client.post('/post_view/', {'value': None})

View File

@ -14,8 +14,8 @@ from django.utils.http import (
class URLEncodeTests(SimpleTestCase): class URLEncodeTests(SimpleTestCase):
cannot_encode_none_msg = ( cannot_encode_none_msg = (
'Cannot encode None in a query string. Did you mean to pass an ' "Cannot encode None for key 'a' in a query string. Did you mean to "
'empty string or omit the value?' "pass an empty string or omit the value?"
) )
def test_tuples(self): def test_tuples(self):