Fixed #12140 -- Fixed http.urlencode result for empty lists

Thanks aneil for the report and the initial patch.
This commit is contained in:
Claude Paroz 2012-06-14 11:32:40 +02:00
parent a2022dae7f
commit fe873e2765
2 changed files with 9 additions and 1 deletions

View File

@ -71,7 +71,7 @@ def urlencode(query, doseq=0):
query = query.items() query = query.items()
return urllib.urlencode( return urllib.urlencode(
[(smart_str(k), [(smart_str(k),
isinstance(v, (list,tuple)) and [smart_str(i) for i in v] or smart_str(v)) [smart_str(i) for i in v] if isinstance(v, (list,tuple)) else smart_str(v))
for k, v in query], for k, v in query],
doseq) doseq)

View File

@ -31,6 +31,7 @@ class TestUtilsHttp(unittest.TestCase):
# 2-tuples (the norm) # 2-tuples (the norm)
result = http.urlencode((('a', 1), ('b', 2), ('c', 3))) result = http.urlencode((('a', 1), ('b', 2), ('c', 3)))
self.assertEqual(result, 'a=1&b=2&c=3') self.assertEqual(result, 'a=1&b=2&c=3')
# A dictionary # A dictionary
result = http.urlencode({ 'a': 1, 'b': 2, 'c': 3}) result = http.urlencode({ 'a': 1, 'b': 2, 'c': 3})
acceptable_results = [ acceptable_results = [
@ -44,6 +45,13 @@ class TestUtilsHttp(unittest.TestCase):
'c=3&b=2&a=1' 'c=3&b=2&a=1'
] ]
self.assertTrue(result in acceptable_results) self.assertTrue(result in acceptable_results)
result = http.urlencode({'a': [1, 2]}, doseq=False)
self.assertEqual(result, 'a=%5B%271%27%2C+%272%27%5D')
result = http.urlencode({'a': [1, 2]}, doseq=True)
self.assertEqual(result, 'a=1&a=2')
result = http.urlencode({'a': []}, doseq=True)
self.assertEqual(result, '')
# A MultiValueDict # A MultiValueDict
result = http.urlencode(MultiValueDict({ result = http.urlencode(MultiValueDict({
'name': ['Adrian', 'Simon'], 'name': ['Adrian', 'Simon'],