mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2026-09-03 20:52:55 -05:00
intersphinx: style nits
This commit is contained in:
+11
-12
@@ -43,14 +43,14 @@ from sphinx.locale import _
|
||||
from sphinx.builders.html import INVENTORY_FILENAME
|
||||
|
||||
|
||||
handlers = [request.ProxyHandler(), request.HTTPRedirectHandler(),
|
||||
request.HTTPHandler()]
|
||||
default_handlers = [request.ProxyHandler(), request.HTTPRedirectHandler(),
|
||||
request.HTTPHandler()]
|
||||
try:
|
||||
handlers.append(request.HTTPSHandler)
|
||||
default_handlers.append(request.HTTPSHandler)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
request.install_opener(request.build_opener(*handlers))
|
||||
default_opener = request.build_opener(*default_handlers)
|
||||
|
||||
UTF8StreamReader = codecs.lookup('utf-8')[2]
|
||||
|
||||
@@ -172,15 +172,14 @@ def _read_from_url(url):
|
||||
:rtype: ``file``-like object
|
||||
"""
|
||||
url, username, password = _strip_basic_auth(url)
|
||||
handler = request.BaseHandler()
|
||||
|
||||
if username is not None and password is not None:
|
||||
# case: url contains basic auth creds
|
||||
password_mgr = request.HTTPPasswordMgrWithDefaultRealm()
|
||||
password_mgr.add_password(None, url, username, password)
|
||||
handler = request.HTTPBasicAuthHandler(password_mgr)
|
||||
|
||||
opener = request.build_opener(handler)
|
||||
opener = request.build_opener(default_handlers + [handler])
|
||||
else:
|
||||
opener = default_opener
|
||||
|
||||
return opener.open(url)
|
||||
|
||||
@@ -207,7 +206,7 @@ def _get_safe_url(url):
|
||||
if username is not None:
|
||||
# case: url contained basic auth creds; obscure password
|
||||
url_parts = parse.urlsplit(url)
|
||||
safe_netloc = '{0}:********@{1}'.format(username, url_parts.hostname)
|
||||
safe_netloc = '{0}@{1}'.format(username, url_parts.hostname)
|
||||
# replace original netloc w/ obscured version
|
||||
frags = list(url_parts)
|
||||
frags[1] = safe_netloc
|
||||
@@ -220,13 +219,13 @@ def fetch_inventory(app, uri, inv):
|
||||
"""Fetch, parse and return an intersphinx inventory file."""
|
||||
# both *uri* (base URI of the links to generate) and *inv* (actual
|
||||
# location of the inventory file) can be local or remote URIs
|
||||
localuri = uri.find('://') == -1
|
||||
if localuri is False:
|
||||
localuri = '://' not in uri
|
||||
if not localuri:
|
||||
# case: inv URI points to remote resource; strip any existing auth
|
||||
uri, _, _ = _strip_basic_auth(uri)
|
||||
join = localuri and path.join or posixpath.join
|
||||
try:
|
||||
if inv.find('://') != -1:
|
||||
if '://' in inv:
|
||||
f = _read_from_url(inv)
|
||||
else:
|
||||
f = open(path.join(app.srcdir, inv), 'rb')
|
||||
|
||||
@@ -200,54 +200,52 @@ class TestStripBasicAuth(unittest.TestCase):
|
||||
self.assertEqual(None, actual_password)
|
||||
|
||||
|
||||
class TestReadFromUrl(unittest.TestCase):
|
||||
"""Tests for sphinx.ext.intersphinx._read_from_url()"""
|
||||
@mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler')
|
||||
@mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm')
|
||||
@mock.patch('six.moves.urllib.request.build_opener')
|
||||
def test_authed(self, m_build_opener, m_HTTPPasswordMgrWithDefaultRealm,
|
||||
m_HTTPBasicAuthHandler):
|
||||
"""read from URL containing basic auth creds"""
|
||||
password_mgr = mock.Mock()
|
||||
m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr
|
||||
@mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler')
|
||||
@mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm')
|
||||
@mock.patch('six.moves.urllib.request.build_opener')
|
||||
def test_readfromurl_authed(m_build_opener, m_HTTPPasswordMgrWithDefaultRealm,
|
||||
m_HTTPBasicAuthHandler):
|
||||
# read from URL containing basic auth creds
|
||||
password_mgr = mock.Mock()
|
||||
m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr
|
||||
|
||||
url = 'https://user:12345@domain.com/project/objects.inv'
|
||||
_read_from_url(url)
|
||||
url = 'https://user:12345@domain.com/project/objects.inv'
|
||||
_read_from_url(url)
|
||||
|
||||
m_HTTPPasswordMgrWithDefaultRealm.assert_called_once_with()
|
||||
password_mgr.add_password.assert_called_with(
|
||||
None, 'https://domain.com/project/objects.inv', 'user', '12345')
|
||||
|
||||
@mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler')
|
||||
@mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm')
|
||||
@mock.patch('six.moves.urllib.request.build_opener')
|
||||
def test_unauthed(self, m_build_opener, m_HTTPPasswordMgrWithDefaultRealm,
|
||||
m_HTTPBasicAuthHandler):
|
||||
"""read from URL without auth creds"""
|
||||
password_mgr = mock.Mock()
|
||||
m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr
|
||||
|
||||
url = 'https://domain.com/project/objects.inv'
|
||||
_read_from_url(url)
|
||||
|
||||
# assert password manager not created
|
||||
self.assertEqual(None, m_HTTPPasswordMgrWithDefaultRealm.call_args)
|
||||
# assert no password added to the password manager
|
||||
self.assertEqual(None, password_mgr.add_password.call_args)
|
||||
m_HTTPPasswordMgrWithDefaultRealm.assert_called_once_with()
|
||||
password_mgr.add_password.assert_called_with(
|
||||
None, 'https://domain.com/project/objects.inv', 'user', '12345')
|
||||
|
||||
|
||||
class TestGetSafeUrl(unittest.TestCase):
|
||||
"""Tests for sphinx.ext.intersphinx._get_safe_url()"""
|
||||
def test_authed(self):
|
||||
"""_get_safe_url() with a url with basic auth"""
|
||||
url = 'https://user:12345@domain.com/project/objects.inv'
|
||||
expected = 'https://user:********@domain.com/project/objects.inv'
|
||||
actual = _get_safe_url(url)
|
||||
self.assertEqual(expected, actual)
|
||||
@mock.patch('six.moves.urllib.request.HTTPBasicAuthHandler')
|
||||
@mock.patch('six.moves.urllib.request.HTTPPasswordMgrWithDefaultRealm')
|
||||
@mock.patch('sphinx.ext.intersphinx.default_opener')
|
||||
def test_readfromurl_unauthed(m_default_opener, m_HTTPPasswordMgrWithDefaultRealm,
|
||||
m_HTTPBasicAuthHandler):
|
||||
# read from URL without auth creds
|
||||
password_mgr = mock.Mock()
|
||||
m_HTTPPasswordMgrWithDefaultRealm.return_value = password_mgr
|
||||
|
||||
def test_unauthed(self):
|
||||
"""_get_safe_url() with a url without basic auth"""
|
||||
url = 'https://domain.com/project/objects.inv'
|
||||
expected = 'https://domain.com/project/objects.inv'
|
||||
actual = _get_safe_url(url)
|
||||
self.assertEqual(expected, actual)
|
||||
url = 'https://domain.com/project/objects.inv'
|
||||
_read_from_url(url)
|
||||
|
||||
# assert password manager not created
|
||||
assert m_HTTPPasswordMgrWithDefaultRealm.call_args is None
|
||||
# assert no password added to the password manager
|
||||
assert password_mgr.add_password.call_args is None
|
||||
|
||||
|
||||
def test_getsafeurl_authed():
|
||||
"""_get_safe_url() with a url with basic auth"""
|
||||
url = 'https://user:12345@domain.com/project/objects.inv'
|
||||
expected = 'https://user@domain.com/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'
|
||||
expected = 'https://domain.com/project/objects.inv'
|
||||
actual = _get_safe_url(url)
|
||||
assert expected == actual
|
||||
|
||||
Reference in New Issue
Block a user