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/test_utils | |
| parent | 42490768441701bc02255b22df8e6894cbe487c7 (diff) | |
Fixed #27398 -- Added an assertion to compare URLs, ignoring the order of their query strings.
Diffstat (limited to 'tests/test_utils')
| -rw-r--r-- | tests/test_utils/tests.py | 48 |
1 files changed, 48 insertions, 0 deletions
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')] |
