Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9089 -- Correctly handle list values in MultiValueDict instanc…

…es when passed to django.utils.http.urlencode. Thanks, kratorius, guettli and obeattie.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16064 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f4be8bd53d13c9ecb7f31998f89a2def11111efb 1 parent 7d11c30
Jannis Leidel authored April 22, 2011
5  django/utils/http.py
@@ -6,6 +6,7 @@
6 6
 import urlparse
7 7
 from email.Utils import formatdate
8 8
 
  9
+from django.utils.datastructures import MultiValueDict
9 10
 from django.utils.encoding import smart_str, force_unicode
10 11
 from django.utils.functional import allow_lazy
11 12
 
@@ -49,7 +50,9 @@ def urlencode(query, doseq=0):
49 50
     unicode strings. The parameters are first case to UTF-8 encoded strings and
50 51
     then encoded as per normal.
51 52
     """
52  
-    if hasattr(query, 'items'):
  53
+    if isinstance(query, MultiValueDict):
  54
+        query = query.lists()
  55
+    elif hasattr(query, 'items'):
53 56
         query = query.items()
54 57
     return urllib.urlencode(
55 58
         [(smart_str(k),
30  tests/regressiontests/utils/http.py
... ...
@@ -1,5 +1,6 @@
1 1
 from django.utils import http
2 2
 from django.utils import unittest
  3
+from django.utils.datastructures import MultiValueDict
3 4
 
4 5
 class TestUtilsHttp(unittest.TestCase):
5 6
 
@@ -21,3 +22,32 @@ def test_same_origin_false(self):
21 22
         self.assertFalse(http.same_origin('http://foo.com', 'http://foo.com.evil.com'))
22 23
         # Different port
23 24
         self.assertFalse(http.same_origin('http://foo.com:8000', 'http://foo.com:8001'))
  25
+
  26
+    def test_urlencode(self):
  27
+        # 2-tuples (the norm)
  28
+        result = http.urlencode((('a', 1), ('b', 2), ('c', 3)))
  29
+        self.assertEqual(result, 'a=1&b=2&c=3')
  30
+        # A dictionary
  31
+        result = http.urlencode({ 'a': 1, 'b': 2, 'c': 3})
  32
+        acceptable_results = [
  33
+            # Need to allow all of these as dictionaries have to be treated as
  34
+            # unordered
  35
+            'a=1&b=2&c=3',
  36
+            'a=1&c=3&b=2',
  37
+            'b=2&a=1&c=3',
  38
+            'b=2&c=3&a=1',
  39
+            'c=3&a=1&b=2',
  40
+            'c=3&b=2&a=1'
  41
+        ]
  42
+        self.assertTrue(result in acceptable_results)
  43
+        # A MultiValueDict
  44
+        result = http.urlencode(MultiValueDict({
  45
+            'name': ['Adrian', 'Simon'],
  46
+            'position': ['Developer']
  47
+        }), doseq=True)
  48
+        acceptable_results = [
  49
+            # MultiValueDicts are similarly unordered
  50
+            'name=Adrian&name=Simon&position=Developer',
  51
+            'position=Developer&name=Adrian&name=Simon'
  52
+        ]
  53
+        self.assertTrue(result in acceptable_results)

0 notes on commit f4be8bd

Please sign in to comment.
Something went wrong with that request. Please try again.