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():
if value is None:
raise TypeError(
'Cannot encode None as POST data. Did you mean to pass an '
'empty string or omit the value?'
"Cannot encode None for key '%s' as POST data. Did you mean "
"to pass an empty string or omit the value?" % key
)
elif is_file(value):
lines.extend(encode_file(boundary, key, value))

View File

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

View File

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

View File

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