diff options
| author | Jan Pieter Waagmeester <jieter@jieter.nl> | 2017-12-19 20:05:10 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-06-20 13:26:12 -0400 |
| commit | 24959e48d949a20be969f649ece3576dbc7ce422 (patch) | |
| tree | 0d2e751a91e3af2fdc890e912eaf1a1ee93acb9d /django | |
| parent | 42490768441701bc02255b22df8e6894cbe487c7 (diff) | |
Fixed #27398 -- Added an assertion to compare URLs, ignoring the order of their query strings.
Diffstat (limited to 'django')
| -rw-r--r-- | django/test/testcases.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 1fac9ef931..488b8e2cd7 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -9,7 +9,9 @@ from contextlib import contextmanager from copy import copy from functools import wraps from unittest.util import safe_repr -from urllib.parse import unquote, urljoin, urlparse, urlsplit +from urllib.parse import ( + parse_qsl, unquote, urlencode, urljoin, urlparse, urlsplit, urlunparse, +) from urllib.request import url2pathname from django.apps import apps @@ -313,11 +315,30 @@ class SimpleTestCase(unittest.TestCase): % (path, redirect_response.status_code, target_status_code) ) - self.assertEqual( + self.assertURLEqual( url, expected_url, msg_prefix + "Response redirected to '%s', expected '%s'" % (url, expected_url) ) + def assertURLEqual(self, url1, url2, msg_prefix=''): + """ + Assert that two URLs are the same, ignoring the order of query string + parameters except for parameters with the same name. + + For example, /path/?x=1&y=2 is equal to /path/?y=2&x=1, but + /path/?a=1&a=2 isn't equal to /path/?a=2&a=1. + """ + def normalize(url): + """Sort the URL's query string parameters.""" + scheme, netloc, path, params, query, fragment = urlparse(url) + query_parts = sorted(parse_qsl(query)) + return urlunparse((scheme, netloc, path, params, urlencode(query_parts), fragment)) + + self.assertEqual( + normalize(url1), normalize(url2), + msg_prefix + "Expected '%s' to equal '%s'." % (url1, url2) + ) + def _assert_contains(self, response, text, status_code, msg_prefix, html): # If the response supports deferred rendering and hasn't been rendered # yet, then ensure that it does get rendered before proceeding further. |
