Author: jezdez
Date: 2011-04-22 05:01:41 -0700 (Fri, 22 Apr 2011)
New Revision: 16064

Modified:
   django/trunk/django/utils/http.py
   django/trunk/tests/regressiontests/utils/http.py
Log:
Fixed #9089 -- Correctly handle list values in MultiValueDict instances when 
passed to django.utils.http.urlencode. Thanks, kratorius, guettli and obeattie.

Modified: django/trunk/django/utils/http.py
===================================================================
--- django/trunk/django/utils/http.py   2011-04-22 12:01:33 UTC (rev 16063)
+++ django/trunk/django/utils/http.py   2011-04-22 12:01:41 UTC (rev 16064)
@@ -6,6 +6,7 @@
 import urlparse
 from email.Utils import formatdate
 
+from django.utils.datastructures import MultiValueDict
 from django.utils.encoding import smart_str, force_unicode
 from django.utils.functional import allow_lazy
 
@@ -49,7 +50,9 @@
     unicode strings. The parameters are first case to UTF-8 encoded strings and
     then encoded as per normal.
     """
-    if hasattr(query, 'items'):
+    if isinstance(query, MultiValueDict):
+        query = query.lists()
+    elif hasattr(query, 'items'):
         query = query.items()
     return urllib.urlencode(
         [(smart_str(k),

Modified: django/trunk/tests/regressiontests/utils/http.py
===================================================================
--- django/trunk/tests/regressiontests/utils/http.py    2011-04-22 12:01:33 UTC 
(rev 16063)
+++ django/trunk/tests/regressiontests/utils/http.py    2011-04-22 12:01:41 UTC 
(rev 16064)
@@ -1,5 +1,6 @@
 from django.utils import http
 from django.utils import unittest
+from django.utils.datastructures import MultiValueDict
 
 class TestUtilsHttp(unittest.TestCase):
 
@@ -21,3 +22,32 @@
         self.assertFalse(http.same_origin('http://foo.com', 
'http://foo.com.evil.com'))
         # Different port
         self.assertFalse(http.same_origin('http://foo.com:8000', 
'http://foo.com:8001'))
+
+    def test_urlencode(self):
+        # 2-tuples (the norm)
+        result = http.urlencode((('a', 1), ('b', 2), ('c', 3)))
+        self.assertEqual(result, 'a=1&b=2&c=3')
+        # A dictionary
+        result = http.urlencode({ 'a': 1, 'b': 2, 'c': 3})
+        acceptable_results = [
+            # Need to allow all of these as dictionaries have to be treated as
+            # unordered
+            'a=1&b=2&c=3',
+            'a=1&c=3&b=2',
+            'b=2&a=1&c=3',
+            'b=2&c=3&a=1',
+            'c=3&a=1&b=2',
+            'c=3&b=2&a=1'
+        ]
+        self.assertTrue(result in acceptable_results)
+        # A MultiValueDict
+        result = http.urlencode(MultiValueDict({
+            'name': ['Adrian', 'Simon'],
+            'position': ['Developer']
+        }), doseq=True)
+        acceptable_results = [
+            # MultiValueDicts are similarly unordered
+            'name=Adrian&name=Simon&position=Developer',
+            'position=Developer&name=Adrian&name=Simon'
+        ]
+        self.assertTrue(result in acceptable_results)

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to