diff --git a/django/utils/http.py b/django/utils/http.py
index c93a338..4b43e58 100644
a
|
b
|
import urllib
|
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 | |
… |
… |
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), |
diff --git a/tests/regressiontests/utils/http.py b/tests/regressiontests/utils/http.py
index 83a4a7f..3f3a36c 100644
a
|
b
|
|
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 | |
… |
… |
class TestUtilsHttp(unittest.TestCase):
|
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) |