bpo-30500: urllib: Simplify splithost by calling into urlparse. (#184… · python/cpython@b0fba88
@@ -755,28 +755,35 @@ def test_default_scheme(self):
755755def test_parse_fragments(self):
756756# Exercise the allow_fragments parameter of urlparse() and urlsplit()
757757tests = (
758- ("http:#frag", "path"),
759- ("//example.net#frag", "path"),
760- ("index.html#frag", "path"),
761- (";a=b#frag", "params"),
762- ("?a=b#frag", "query"),
763- ("#frag", "path"),
758+ ("http:#frag", "path", "frag"),
759+ ("//example.net#frag", "path", "frag"),
760+ ("index.html#frag", "path", "frag"),
761+ (";a=b#frag", "params", "frag"),
762+ ("?a=b#frag", "query", "frag"),
763+ ("#frag", "path", "frag"),
764+ ("abc#@frag", "path", "@frag"),
765+ ("//abc#@frag", "path", "@frag"),
766+ ("//abc:80#@frag", "path", "@frag"),
767+ ("//abc#@frag:80", "path", "@frag:80"),
764768 )
765-for url, attr in tests:
769+for url, attr, expected_frag in tests:
766770for func in (urllib.parse.urlparse, urllib.parse.urlsplit):
767771if attr == "params" and func is urllib.parse.urlsplit:
768772attr = "path"
769773with self.subTest(url=url, function=func):
770774result = func(url, allow_fragments=False)
771775self.assertEqual(result.fragment, "")
772-self.assertTrue(getattr(result, attr).endswith("#frag"))
776+self.assertTrue(
777+getattr(result, attr).endswith("#" + expected_frag))
773778self.assertEqual(func(url, "", False).fragment, "")
774779775780result = func(url, allow_fragments=True)
776-self.assertEqual(result.fragment, "frag")
777-self.assertFalse(getattr(result, attr).endswith("frag"))
778-self.assertEqual(func(url, "", True).fragment, "frag")
779-self.assertEqual(func(url).fragment, "frag")
781+self.assertEqual(result.fragment, expected_frag)
782+self.assertFalse(
783+getattr(result, attr).endswith(expected_frag))
784+self.assertEqual(func(url, "", True).fragment,
785+expected_frag)
786+self.assertEqual(func(url).fragment, expected_frag)
780787781788def test_mixed_types_rejected(self):
782789# Several functions that process either strings or ASCII encoded bytes
@@ -983,6 +990,26 @@ def test_splithost(self):
983990self.assertEqual(splithost('/foo/bar/baz.html'),
984991 (None, '/foo/bar/baz.html'))
985992993+# bpo-30500: # starts a fragment.
994+self.assertEqual(splithost('//127.0.0.1#@host.com'),
995+ ('127.0.0.1', '/#@host.com'))
996+self.assertEqual(splithost('//127.0.0.1#@host.com:80'),
997+ ('127.0.0.1', '/#@host.com:80'))
998+self.assertEqual(splithost('//127.0.0.1:80#@host.com'),
999+ ('127.0.0.1:80', '/#@host.com'))
1000+1001+# Empty host is returned as empty string.
1002+self.assertEqual(splithost("///file"),
1003+ ('', '/file'))
1004+1005+# Trailing semicolon, question mark and hash symbol are kept.
1006+self.assertEqual(splithost("//example.net/file;"),
1007+ ('example.net', '/file;'))
1008+self.assertEqual(splithost("//example.net/file?"),
1009+ ('example.net', '/file?'))
1010+self.assertEqual(splithost("//example.net/file#"),
1011+ ('example.net', '/file#'))
1012+9861013def test_splituser(self):
9871014splituser = urllib.parse.splituser
9881015self.assertEqual(splituser('User:Pass@www.python.org:080'),