mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
[3.11] gh-67693: Fix urlunparse() and urlunsplit() for URIs with path starting with multiple slashes and no authority (GH-113563) (#119025)
(cherry picked from commit e237b25a4f
)
This commit is contained in:
parent
f7c0f09e69
commit
d0e8c100e4
3 changed files with 70 additions and 4 deletions
|
@ -103,7 +103,9 @@ parse_qs_test_cases = [
|
|||
|
||||
class UrlParseTestCase(unittest.TestCase):
|
||||
|
||||
def checkRoundtrips(self, url, parsed, split):
|
||||
def checkRoundtrips(self, url, parsed, split, url2=None):
|
||||
if url2 is None:
|
||||
url2 = url
|
||||
result = urllib.parse.urlparse(url)
|
||||
self.assertEqual(result, parsed)
|
||||
t = (result.scheme, result.netloc, result.path,
|
||||
|
@ -111,7 +113,7 @@ class UrlParseTestCase(unittest.TestCase):
|
|||
self.assertEqual(t, parsed)
|
||||
# put it back together and it should be the same
|
||||
result2 = urllib.parse.urlunparse(result)
|
||||
self.assertEqual(result2, url)
|
||||
self.assertEqual(result2, url2)
|
||||
self.assertEqual(result2, result.geturl())
|
||||
|
||||
# the result of geturl() is a fixpoint; we can always parse it
|
||||
|
@ -137,7 +139,7 @@ class UrlParseTestCase(unittest.TestCase):
|
|||
result.query, result.fragment)
|
||||
self.assertEqual(t, split)
|
||||
result2 = urllib.parse.urlunsplit(result)
|
||||
self.assertEqual(result2, url)
|
||||
self.assertEqual(result2, url2)
|
||||
self.assertEqual(result2, result.geturl())
|
||||
|
||||
# check the fixpoint property of re-parsing the result of geturl()
|
||||
|
@ -175,9 +177,39 @@ class UrlParseTestCase(unittest.TestCase):
|
|||
|
||||
def test_roundtrips(self):
|
||||
str_cases = [
|
||||
('path/to/file',
|
||||
('', '', 'path/to/file', '', '', ''),
|
||||
('', '', 'path/to/file', '', '')),
|
||||
('/path/to/file',
|
||||
('', '', '/path/to/file', '', '', ''),
|
||||
('', '', '/path/to/file', '', '')),
|
||||
('//path/to/file',
|
||||
('', 'path', '/to/file', '', '', ''),
|
||||
('', 'path', '/to/file', '', '')),
|
||||
('////path/to/file',
|
||||
('', '', '//path/to/file', '', '', ''),
|
||||
('', '', '//path/to/file', '', '')),
|
||||
('scheme:path/to/file',
|
||||
('scheme', '', 'path/to/file', '', '', ''),
|
||||
('scheme', '', 'path/to/file', '', '')),
|
||||
('scheme:/path/to/file',
|
||||
('scheme', '', '/path/to/file', '', '', ''),
|
||||
('scheme', '', '/path/to/file', '', '')),
|
||||
('scheme://path/to/file',
|
||||
('scheme', 'path', '/to/file', '', '', ''),
|
||||
('scheme', 'path', '/to/file', '', '')),
|
||||
('scheme:////path/to/file',
|
||||
('scheme', '', '//path/to/file', '', '', ''),
|
||||
('scheme', '', '//path/to/file', '', '')),
|
||||
('file:///tmp/junk.txt',
|
||||
('file', '', '/tmp/junk.txt', '', '', ''),
|
||||
('file', '', '/tmp/junk.txt', '', '')),
|
||||
('file:////tmp/junk.txt',
|
||||
('file', '', '//tmp/junk.txt', '', '', ''),
|
||||
('file', '', '//tmp/junk.txt', '', '')),
|
||||
('file://///tmp/junk.txt',
|
||||
('file', '', '///tmp/junk.txt', '', '', ''),
|
||||
('file', '', '///tmp/junk.txt', '', '')),
|
||||
('imap://mail.python.org/mbox1',
|
||||
('imap', 'mail.python.org', '/mbox1', '', '', ''),
|
||||
('imap', 'mail.python.org', '/mbox1', '', '')),
|
||||
|
@ -208,6 +240,38 @@ class UrlParseTestCase(unittest.TestCase):
|
|||
for url, parsed, split in str_cases + bytes_cases:
|
||||
self.checkRoundtrips(url, parsed, split)
|
||||
|
||||
def test_roundtrips_normalization(self):
|
||||
str_cases = [
|
||||
('///path/to/file',
|
||||
'/path/to/file',
|
||||
('', '', '/path/to/file', '', '', ''),
|
||||
('', '', '/path/to/file', '', '')),
|
||||
('scheme:///path/to/file',
|
||||
'scheme:/path/to/file',
|
||||
('scheme', '', '/path/to/file', '', '', ''),
|
||||
('scheme', '', '/path/to/file', '', '')),
|
||||
('file:/tmp/junk.txt',
|
||||
'file:///tmp/junk.txt',
|
||||
('file', '', '/tmp/junk.txt', '', '', ''),
|
||||
('file', '', '/tmp/junk.txt', '', '')),
|
||||
('http:/tmp/junk.txt',
|
||||
'http:///tmp/junk.txt',
|
||||
('http', '', '/tmp/junk.txt', '', '', ''),
|
||||
('http', '', '/tmp/junk.txt', '', '')),
|
||||
('https:/tmp/junk.txt',
|
||||
'https:///tmp/junk.txt',
|
||||
('https', '', '/tmp/junk.txt', '', '', ''),
|
||||
('https', '', '/tmp/junk.txt', '', '')),
|
||||
]
|
||||
def _encode(t):
|
||||
return (t[0].encode('ascii'),
|
||||
t[1].encode('ascii'),
|
||||
tuple(x.encode('ascii') for x in t[2]),
|
||||
tuple(x.encode('ascii') for x in t[3]))
|
||||
bytes_cases = [_encode(x) for x in str_cases]
|
||||
for url, url2, parsed, split in str_cases + bytes_cases:
|
||||
self.checkRoundtrips(url, parsed, split, url2)
|
||||
|
||||
def test_http_roundtrips(self):
|
||||
# urllib.parse.urlsplit treats 'http:' as an optimized special case,
|
||||
# so we test both 'http:' and 'https:' in all the following.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue