1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
import re
import pytest
from mopidy import httpclient
@pytest.mark.parametrize(
"config,expected",
[
({}, None),
({"hostname": ""}, None),
({"hostname": "proxy.lan"}, "http://proxy.lan:80"),
({"scheme": None, "hostname": "proxy.lan"}, "http://proxy.lan:80"),
({"scheme": "https", "hostname": "proxy.lan"}, "https://proxy.lan:80"),
({"username": "user", "hostname": "proxy.lan"}, "http://proxy.lan:80"),
({"password": "pass", "hostname": "proxy.lan"}, "http://proxy.lan:80"),
({"hostname": "proxy.lan", "port": 8080}, "http://proxy.lan:8080"),
({"hostname": "proxy.lan", "port": -1}, "http://proxy.lan:80"),
({"hostname": "proxy.lan", "port": None}, "http://proxy.lan:80"),
({"hostname": "proxy.lan", "port": ""}, "http://proxy.lan:80"),
(
{"username": "user", "password": "pass", "hostname": "proxy.lan"},
"http://user:pass@proxy.lan:80",
),
],
)
def test_format_proxy(config, expected):
assert httpclient.format_proxy(config) == expected
def test_format_proxy_without_auth():
config = {"username": "user", "password": "pass", "hostname": "proxy.lan"}
formated_proxy = httpclient.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|/[^ ]+$"),
("Foo/1.2.3", r"^Foo/1.2.3 Mopidy/[^ ]+ CPython|/[^ ]+$"),
],
)
def test_format_user_agent(name, expected):
assert re.match(expected, httpclient.format_user_agent(name))
|