|
6 | 6 | from django.test import SimpleTestCase
|
7 | 7 | from django.utils.datastructures import MultiValueDict
|
8 | 8 | from django.utils.http import (
|
| 9 | + _urlsplit, |
9 | 10 | base36_to_int,
|
10 | 11 | content_disposition_header,
|
11 | 12 | escape_leading_slashes,
|
@@ -291,6 +292,46 @@ def test_secure_param_non_https_urls(self):
|
291 | 292 | False,
|
292 | 293 | )
|
293 | 294 |
|
| 295 | + # TODO: Remove when dropping support for PY38. |
| 296 | + def test_invalid_bracketed_hosts(self): |
| 297 | + # Port of urllib.parse.urlsplit() tests from Python. |
| 298 | + tests = [ |
| 299 | + "Scheme://user@[192.0.2.146]/Path?Query", |
| 300 | + "Scheme://user@[important.com:8000]/Path?Query", |
| 301 | + "Scheme://user@[v123r.IP]/Path?Query", |
| 302 | + "Scheme://user@[v12ae]/Path?Query", |
| 303 | + "Scheme://user@[v.IP]/Path?Query", |
| 304 | + "Scheme://user@[v123.]/Path?Query", |
| 305 | + "Scheme://user@[v]/Path?Query", |
| 306 | + "Scheme://user@[0439:23af::2309::fae7:1234]/Path?Query", |
| 307 | + "Scheme://user@[0439:23af:2309::fae7:1234:2342:438e:192.0.2.146]/" |
| 308 | + "Path?Query", |
| 309 | + "Scheme://user@]v6a.ip[/Path", |
| 310 | + ] |
| 311 | + for invalid_url in tests: |
| 312 | + with self.subTest(invalid_url=invalid_url): |
| 313 | + self.assertRaises(ValueError, _urlsplit, invalid_url) |
| 314 | + |
| 315 | + # TODO: Remove when dropping support for PY38. |
| 316 | + def test_splitting_bracketed_hosts(self): |
| 317 | + # Port of urllib.parse.urlsplit() tests from Python. |
| 318 | + p1 = _urlsplit("scheme://user@[v6a.ip]/path?query") |
| 319 | + self.assertEqual(p1.hostname, "v6a.ip") |
| 320 | + self.assertEqual(p1.username, "user") |
| 321 | + self.assertEqual(p1.path, "/path") |
| 322 | + # Removed the '%test' suffix from ported tests as %scope_id suffixes were |
| 323 | + # added in Python 3.9: https://docs.python.org/3/whatsnew/3.9.html#ipaddress |
| 324 | + p2 = _urlsplit("scheme://user@[0439:23af:2309::fae7]/path?query") |
| 325 | + self.assertEqual(p2.hostname, "0439:23af:2309::fae7") |
| 326 | + self.assertEqual(p2.username, "user") |
| 327 | + self.assertEqual(p2.path, "/path") |
| 328 | + p3 = _urlsplit( |
| 329 | + "scheme://user@[0439:23af:2309::fae7:1234:192.0.2.146]/path?query" |
| 330 | + ) |
| 331 | + self.assertEqual(p3.hostname, "0439:23af:2309::fae7:1234:192.0.2.146") |
| 332 | + self.assertEqual(p3.username, "user") |
| 333 | + self.assertEqual(p3.path, "/path") |
| 334 | + |
294 | 335 |
|
295 | 336 | class URLSafeBase64Tests(unittest.TestCase):
|
296 | 337 | def test_roundtrip(self):
|
|
0 commit comments