utils: Support opting out of adding auth to proxy

This commit is contained in:
Thomas Adamcik 2015-04-29 00:38:06 +02:00
parent 8cf9da3d55
commit 9182a7870e
2 changed files with 15 additions and 6 deletions

View File

@ -7,24 +7,27 @@ import mopidy
"Helpers for configuring HTTP clients used in Mopidy extensions."
def format_proxy(proxy_config):
def format_proxy(proxy_config, auth=True):
"""Convert a Mopidy proxy config to the commonly used proxy string format.
Outputs ``scheme://host:port``, ``scheme://user:pass@host:port`` or
:class:`None` depending on the proxy config provided.
You can also opt out of getting the basic auth by setting ``auth`` to
:type:`False`.
"""
if not proxy_config.get('hostname'):
return None
if proxy_config.get('username') and proxy_config.get('password'):
template = '{scheme}://{username}:{password}@{hostname}:{port}'
else:
template = '{scheme}://{hostname}:{port}'
port = proxy_config.get('port', 80)
if port < 0:
port = 80
if proxy_config.get('username') and proxy_config.get('password') and auth:
template = '{scheme}://{username}:{password}@{hostname}:{port}'
else:
template = '{scheme}://{hostname}:{port}'
return template.format(scheme=proxy_config.get('scheme') or 'http',
username=proxy_config.get('username'),
password=proxy_config.get('password'),

View File

@ -23,6 +23,12 @@ def test_format_proxy(config, expected):
assert http.format_proxy(config) == expected
def test_format_proxy_without_auth():
config = {'username': 'user', 'password': 'pass', 'hostname': 'proxy.lan'}
formated_proxy = http.format_proxy(config, auth=False)
assert formated_proxy == 'http://proxy.lan:80'
@pytest.mark.parametrize("name,expected", [
(None, r'^Mopidy/[^ ]+ CPython|/[^ ]+$'),
('Foo', r'^Foo Mopidy/[^ ]+ CPython|/[^ ]+$'),