From c77fbda7ceaf00d09c322b6e0d0b0b82b4f32e98 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Thu, 27 Jul 2023 16:09:09 +0100 Subject: [PATCH] Added more tests for django.http.request.split_domain_port(). --- tests/requests_tests/tests.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/tests/requests_tests/tests.py b/tests/requests_tests/tests.py index 79f82741db..099f600867 100644 --- a/tests/requests_tests/tests.py +++ b/tests/requests_tests/tests.py @@ -1013,10 +1013,37 @@ class HostValidationTests(SimpleTestCase): ): request.get_host() - def test_split_domain_port_removes_trailing_dot(self): - domain, port = split_domain_port("example.com.:8080") - self.assertEqual(domain, "example.com") - self.assertEqual(port, "8080") + def test_split_domain_port(self): + for host, expected in [ + ("", ("", "")), + (":8080", ("", "")), + ("example.com 8080", ("", "")), + ("example.com:invalid", ("", "")), + ("[::1]", ("[::1]", "")), + ("[::1]:8080", ("[::1]", "8080")), + ("[::ffff:127.0.0.1]", ("[::ffff:127.0.0.1]", "")), + ("[::ffff:127.0.0.1]:8080", ("[::ffff:127.0.0.1]", "8080")), + ( + "[1851:0000:3238:DEF1:0177:0000:0000:0125]", + ("[1851:0000:3238:def1:0177:0000:0000:0125]", ""), + ), + ( + "[1851:0000:3238:DEF1:0177:0000:0000:0125]:8080", + ("[1851:0000:3238:def1:0177:0000:0000:0125]", "8080"), + ), + ("127.0.0.1", ("127.0.0.1", "")), + ("127.0.0.1:8080", ("127.0.0.1", "8080")), + ("example.com", ("example.com", "")), + ("example.com:8080", ("example.com", "8080")), + ("example.com.", ("example.com", "")), + ("example.com.:8080", ("example.com", "8080")), + ("xn--n28h.test", ("xn--n28h.test", "")), + ("xn--n28h.test:8080", ("xn--n28h.test", "8080")), + ("subdomain.example.com", ("subdomain.example.com", "")), + ("subdomain.example.com:8080", ("subdomain.example.com", "8080")), + ]: + with self.subTest(host=host): + self.assertEqual(split_domain_port(host), expected) class BuildAbsoluteURITests(SimpleTestCase):