intersphinx: Refactor _get_safe_url()

This commit is contained in:
Takeshi KOMIYA
2016-08-18 01:25:29 +09:00
parent 847efaf541
commit 609ae03947
2 changed files with 18 additions and 11 deletions

View File

@@ -208,18 +208,17 @@ def _get_safe_url(url):
:return: *url* with password removed
:rtype: ``str``
"""
safe_url = url
url, username, _ = _strip_basic_auth(url)
if username is not None:
# case: url contained basic auth creds; obscure password
url_parts = urlsplit(url)
safe_netloc = '{0}@{1}'.format(username, url_parts.hostname)
# replace original netloc w/ obscured version
frags = list(url_parts)
frags[1] = safe_netloc
safe_url = urlunsplit(frags)
parts = urlsplit(url)
if parts.username is None:
return url
else:
frags = list(parts)
if parts.port:
frags[1] = '{0}@{1}:{2}'.format(parts.username, parts.hostname, parts.port)
else:
frags[1] = '{0}@{1}'.format(parts.username, parts.hostname)
return safe_url
return urlunsplit(frags)
def fetch_inventory(app, uri, inv):

View File

@@ -298,6 +298,14 @@ def test_getsafeurl_authed():
assert expected == actual
def test_getsafeurl_authed_having_port():
"""_get_safe_url() with a url with basic auth having port"""
url = 'https://user:12345@domain.com:8080/project/objects.inv'
expected = 'https://user@domain.com:8080/project/objects.inv'
actual = _get_safe_url(url)
assert expected == actual
def test_getsafeurl_unauthed():
"""_get_safe_url() with a url without basic auth"""
url = 'https://domain.com/project/objects.inv'