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 /tests | |
| parent | 42490768441701bc02255b22df8e6894cbe487c7 (diff) | |
Fixed #27398 -- Added an assertion to compare URLs, ignoring the order of their query strings.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/auth_tests/test_views.py | 27 | ||||
| -rw-r--r-- | tests/test_client/tests.py | 6 | ||||
| -rw-r--r-- | tests/test_utils/tests.py | 48 |
3 files changed, 59 insertions, 22 deletions
diff --git a/tests/auth_tests/test_views.py b/tests/auth_tests/test_views.py index c7d64d5d48..6482f92c5d 100644 --- a/tests/auth_tests/test_views.py +++ b/tests/auth_tests/test_views.py @@ -3,7 +3,7 @@ import itertools import os import re from importlib import import_module -from urllib.parse import ParseResult, quote, urlparse +from urllib.parse import quote from django.apps import apps from django.conf import settings @@ -23,7 +23,7 @@ from django.contrib.sessions.middleware import SessionMiddleware from django.contrib.sites.requests import RequestSite from django.core import mail from django.db import connection -from django.http import HttpRequest, QueryDict +from django.http import HttpRequest from django.middleware.csrf import CsrfViewMiddleware, get_token from django.test import Client, TestCase, override_settings from django.test.client import RedirectCycleError @@ -70,23 +70,6 @@ class AuthViewsTestCase(TestCase): form_errors = list(itertools.chain(*response.context['form'].errors.values())) self.assertIn(str(error), form_errors) - def assertURLEqual(self, url, expected, parse_qs=False): - """ - Given two URLs, make sure all their components (the ones given by - urlparse) are equal, only comparing components that are present in both - URLs. - If `parse_qs` is True, then the querystrings are parsed with QueryDict. - This is useful if you don't want the order of parameters to matter. - Otherwise, the query strings are compared as-is. - """ - fields = ParseResult._fields - - for attr, x, y in zip(fields, urlparse(url), urlparse(expected)): - if parse_qs and attr == 'query': - x, y = QueryDict(x), QueryDict(y) - if x and y and x != y: - self.fail("%r != %r (%s doesn't match)" % (url, expected, attr)) - @override_settings(ROOT_URLCONF='django.contrib.auth.urls') class AuthViewNamedURLTests(AuthViewsTestCase): @@ -724,10 +707,10 @@ class LoginTest(AuthViewsTestCase): class LoginURLSettings(AuthViewsTestCase): """Tests for settings.LOGIN_URL.""" - def assertLoginURLEquals(self, url, parse_qs=False): + def assertLoginURLEquals(self, url): response = self.client.get('/login_required/') self.assertEqual(response.status_code, 302) - self.assertURLEqual(response.url, url, parse_qs=parse_qs) + self.assertURLEqual(response.url, url) @override_settings(LOGIN_URL='/login/') def test_standard_login_url(self): @@ -751,7 +734,7 @@ class LoginURLSettings(AuthViewsTestCase): @override_settings(LOGIN_URL='/login/?pretty=1') def test_login_url_with_querystring(self): - self.assertLoginURLEquals('/login/?pretty=1&next=/login_required/', parse_qs=True) + self.assertLoginURLEquals('/login/?pretty=1&next=/login_required/') @override_settings(LOGIN_URL='http://remote.example.com/login/?next=/default/') def test_remote_login_url_with_next_querystring(self): diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py index fb506e7ca9..e45a743f22 100644 --- a/tests/test_client/tests.py +++ b/tests/test_client/tests.py @@ -205,6 +205,12 @@ class ClientTest(TestCase): response = self.client.get('/redirect_view/', {'var': 'value'}) self.assertRedirects(response, '/get_view/?var=value') + def test_redirect_with_query_ordering(self): + """assertRedirects() ignores the order of query string parameters.""" + response = self.client.get('/redirect_view/', {'var': 'value', 'foo': 'bar'}) + self.assertRedirects(response, '/get_view/?var=value&foo=bar') + self.assertRedirects(response, '/get_view/?foo=bar&var=value') + def test_permanent_redirect(self): "GET a URL that redirects permanently elsewhere" response = self.client.get('/permanent_redirect_view/') diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 439ca0b7c7..d908b52a8a 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -911,6 +911,54 @@ class AssertFieldOutputTests(SimpleTestCase): self.assertFieldOutput(MyCustomField, {}, {}, empty_value=None) +class AssertURLEqualTests(SimpleTestCase): + def test_equal(self): + valid_tests = ( + ('http://example.com/?', 'http://example.com/'), + ('http://example.com/?x=1&', 'http://example.com/?x=1'), + ('http://example.com/?x=1&y=2', 'http://example.com/?y=2&x=1'), + ('http://example.com/?x=1&y=2', 'http://example.com/?y=2&x=1'), + ('http://example.com/?x=1&y=2&a=1&a=2', 'http://example.com/?a=1&a=2&y=2&x=1'), + ('/path/to/?x=1&y=2&z=3', '/path/to/?z=3&y=2&x=1'), + ('?x=1&y=2&z=3', '?z=3&y=2&x=1'), + ) + for url1, url2 in valid_tests: + with self.subTest(url=url1): + self.assertURLEqual(url1, url2) + + def test_not_equal(self): + invalid_tests = ( + # Protocol must be the same. + ('http://example.com/', 'https://example.com/'), + ('http://example.com/?x=1&x=2', 'https://example.com/?x=2&x=1'), + ('http://example.com/?x=1&y=bar&x=2', 'https://example.com/?y=bar&x=2&x=1'), + # Parameters of the same name must be in the same order. + ('/path/to?a=1&a=2', '/path/to/?a=2&a=1') + ) + for url1, url2 in invalid_tests: + with self.subTest(url=url1), self.assertRaises(AssertionError): + self.assertURLEqual(url1, url2) + + def test_message(self): + msg = ( + "Expected 'http://example.com/?x=1&x=2' to equal " + "'https://example.com/?x=2&x=1'" + ) + with self.assertRaisesMessage(AssertionError, msg): + self.assertURLEqual('http://example.com/?x=1&x=2', 'https://example.com/?x=2&x=1') + + def test_msg_prefix(self): + msg = ( + "Prefix: Expected 'http://example.com/?x=1&x=2' to equal " + "'https://example.com/?x=2&x=1'" + ) + with self.assertRaisesMessage(AssertionError, msg): + self.assertURLEqual( + 'http://example.com/?x=1&x=2', 'https://example.com/?x=2&x=1', + msg_prefix='Prefix: ', + ) + + class FirstUrls: urlpatterns = [url(r'first/$', empty_response, name='first')] |
