◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
70 changes: 67 additions & 3 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@

class UrlParseTestCase(unittest.TestCase):

def checkRoundtrips(self, url, parsed, split):
result = urllib.parse.urlparse(url)
self.assertSequenceEqual(result, parsed)
t = (result.scheme, result.netloc, result.path,
result.params, result.query, result.fragment)
self.assertSequenceEqual(t, parsed)
# put it back together and it should be the same
result2 = urllib.parse.urlunparse(result)
self.assertSequenceEqual(result2, url)
self.assertSequenceEqual(result2, result.geturl())

# the result of geturl() is a fixpoint; we can always parse it
Expand All @@ -137,7 +139,7 @@ def checkRoundtrips(self, url, parsed, split):
result.query, result.fragment)
self.assertSequenceEqual(t, split)
result2 = urllib.parse.urlunsplit(result)
self.assertSequenceEqual(result2, url)
self.assertSequenceEqual(result2, result.geturl())

# check the fixpoint property of re-parsing the result of geturl()
Expand Down Expand Up @@ -175,9 +177,39 @@ def test_qs(self):

def test_roundtrips(self):
str_cases = [
('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', '', '')),
Expand Down Expand Up @@ -213,6 +245,38 @@ def _encode(t):
for url, parsed, split in str_cases + bytes_cases:
self.checkRoundtrips(url, parsed, split)

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.
Expand Down
2 changes: 1 addition & 1 deletion Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def urlunsplit(components):
empty query; the RFC states that these are equivalent)."""
scheme, netloc, url, query, fragment, _coerce_result = (
_coerce_args(*components))
if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
if url and url[:1] != '/': url = '/' + url
url = '//' + (netloc or '') + url
if scheme:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Toggle all file notes Toggle all file annotations