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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
from __future__ import annotations
import re
from inspect import currentframe, getframeinfo
from operator import itemgetter
from socket import AF_INET, AF_INET6
from unittest.mock import Mock
import pytest
import urllib3
from requests.adapters import HTTPAdapter
from streamlink.exceptions import StreamlinkDeprecationWarning
from streamlink.session import Streamlink
from streamlink.session.http import TLSNoDHAdapter
from streamlink.session.options import StreamlinkOptions
_original_allowed_gai_family = urllib3.util.connection.allowed_gai_family # type: ignore[attr-defined]
class TestOptionsDocumentation:
@pytest.fixture()
def docstring(self, session: Streamlink):
docstring = session.options.__doc__
assert docstring is not None
return docstring
def test_default_option_is_documented(self, session: Streamlink, docstring: str):
assert session.options.keys()
for option in session.options:
assert f"* - {option}" in docstring, f"Option '{option}' is documented"
def test_documented_option_exists(self, session: Streamlink, docstring: str):
options = session.options
setters = options._MAP_SETTERS.keys()
documented = re.compile(r"\* - (\S+)").findall(docstring)[1:]
assert documented
for option in documented:
assert option in options or option in setters, f"Documented option '{option}' exists"
def test_default_objects():
one = Streamlink(plugins_builtin=False)
two = Streamlink(plugins_builtin=False)
ids_one = {key: id(value) for key, value in one.options.defaults.items()}
ids_two = {key: id(value) for key, value in two.options.defaults.items()}
assert ids_one != ids_two
def test_session_wrapper_methods(session: Streamlink):
session.set_option("test_option", "option")
assert session.get_option("test_option") == "option"
assert session.get_option("non_existing") is None
def test_session_option_set_deprecated(recwarn: pytest.WarningsRecorder, session: Streamlink):
def get_lineno():
frame = currentframe()
assert frame
assert frame.f_back
return getframeinfo(frame.f_back).lineno
class FakeStreamlinkOptions(StreamlinkOptions):
_MAP_SETTERS = {
"deprecated": StreamlinkOptions._factory_set_deprecated("new", int),
}
session.options = FakeStreamlinkOptions(session)
assert session.get_option("new") is None
assert recwarn.list == []
session.set_option("deprecated", 123)
lineno = get_lineno() - 1
assert session.get_option("new") == 123
assert [(item.filename, item.lineno, item.category, str(item.message)) for item in recwarn.list] == [
(__file__, lineno, StreamlinkDeprecationWarning, "`deprecated` has been deprecated in favor of the `new` option"),
]
def test_options_locale(monkeypatch: pytest.MonkeyPatch, session: Streamlink):
monkeypatch.setattr("locale.getlocale", lambda: ("C", None))
assert session.get_option("locale") is None
localization = session.localization
assert localization.explicit is False
assert localization.language_code == "en_US"
assert localization.country.alpha2 == "US"
assert localization.country.name == "United States"
assert localization.language.alpha2 == "en"
assert localization.language.name == "English"
session.set_option("locale", "de_DE")
assert session.get_option("locale") == "de_DE"
localization = session.localization
assert localization.explicit is True
assert localization.language_code == "de_DE"
assert localization.country.alpha2 == "DE"
assert localization.country.name == "Germany"
assert localization.language.alpha2 == "de"
assert localization.language.name == "German"
class TestOptionsInterface:
def test_options_interface(self, session: Streamlink):
session.http.mount("custom://", TLSNoDHAdapter())
a_http, a_https, a_custom, a_file = itemgetter("http://", "https://", "custom://", "file://")(session.http.adapters)
assert isinstance(a_http, HTTPAdapter)
assert isinstance(a_https, HTTPAdapter)
assert isinstance(a_custom, HTTPAdapter)
assert not isinstance(a_file, HTTPAdapter)
assert session.get_option("interface") is None
assert a_http.poolmanager.connection_pool_kw.get("source_address") is None
assert a_https.poolmanager.connection_pool_kw.get("source_address") is None
assert a_custom.poolmanager.connection_pool_kw.get("source_address") is None
session.set_option("interface", "my-interface")
assert session.get_option("interface") == "my-interface"
assert a_http.poolmanager.connection_pool_kw.get("source_address") == ("my-interface", 0)
assert a_https.poolmanager.connection_pool_kw.get("source_address") == ("my-interface", 0)
assert a_custom.poolmanager.connection_pool_kw.get("source_address") == ("my-interface", 0)
session.set_option("interface", None)
assert session.get_option("interface") is None
assert a_http.poolmanager.connection_pool_kw.get("source_address") is None
assert a_https.poolmanager.connection_pool_kw.get("source_address") is None
assert a_custom.poolmanager.connection_pool_kw.get("source_address") is None
# doesn't raise
session.set_option("interface", None)
def test_options_ipv4_ipv6(monkeypatch: pytest.MonkeyPatch, session: Streamlink):
mock_urllib3_util_connection = Mock(allowed_gai_family=_original_allowed_gai_family)
monkeypatch.setattr("streamlink.session.options.urllib3_util_connection", mock_urllib3_util_connection)
assert session.get_option("ipv4") is False
assert session.get_option("ipv6") is False
assert mock_urllib3_util_connection.allowed_gai_family is _original_allowed_gai_family
session.set_option("ipv4", True)
assert session.get_option("ipv4") is True
assert session.get_option("ipv6") is False
assert mock_urllib3_util_connection.allowed_gai_family is not _original_allowed_gai_family
assert mock_urllib3_util_connection.allowed_gai_family() is AF_INET
session.set_option("ipv4", False)
assert session.get_option("ipv4") is False
assert session.get_option("ipv6") is False
assert mock_urllib3_util_connection.allowed_gai_family is _original_allowed_gai_family
session.set_option("ipv6", True)
assert session.get_option("ipv4") is False
assert session.get_option("ipv6") is True
assert mock_urllib3_util_connection.allowed_gai_family is not _original_allowed_gai_family
assert mock_urllib3_util_connection.allowed_gai_family() is AF_INET6
session.set_option("ipv6", False)
assert session.get_option("ipv4") is False
assert session.get_option("ipv6") is False
assert mock_urllib3_util_connection.allowed_gai_family is _original_allowed_gai_family
session.set_option("ipv4", True)
session.set_option("ipv6", False)
assert session.get_option("ipv4") is True
assert session.get_option("ipv6") is False
assert mock_urllib3_util_connection.allowed_gai_family is _original_allowed_gai_family
def test_options_http_disable_dh(session: Streamlink):
assert isinstance(session.http.adapters["https://"], HTTPAdapter)
assert not isinstance(session.http.adapters["https://"], TLSNoDHAdapter)
session.set_option("http-disable-dh", True)
assert isinstance(session.http.adapters["https://"], TLSNoDHAdapter)
session.set_option("http-disable-dh", False)
assert isinstance(session.http.adapters["https://"], HTTPAdapter)
assert not isinstance(session.http.adapters["https://"], TLSNoDHAdapter)
class TestOptionsHttpProxy:
@pytest.fixture()
def _no_deprecation(self, recwarn: pytest.WarningsRecorder):
yield
assert recwarn.list == []
@pytest.fixture()
def _logs_deprecation(self, recwarn: pytest.WarningsRecorder):
yield
assert [(record.category, str(record.message), record.filename) for record in recwarn.list] == [
(
StreamlinkDeprecationWarning,
"The `https-proxy` option has been deprecated in favor of a single `http-proxy` option",
__file__,
),
]
@pytest.mark.usefixtures("_no_deprecation")
def test_https_proxy_default(self, session: Streamlink):
session.set_option("http-proxy", "http://testproxy.com")
assert session.http.proxies["http"] == "http://testproxy.com"
assert session.http.proxies["https"] == "http://testproxy.com"
@pytest.mark.usefixtures("_logs_deprecation")
def test_https_proxy_set_first(self, session: Streamlink):
session.set_option("https-proxy", "https://testhttpsproxy.com")
session.set_option("http-proxy", "http://testproxy.com")
assert session.http.proxies["http"] == "http://testproxy.com"
assert session.http.proxies["https"] == "http://testproxy.com"
@pytest.mark.usefixtures("_logs_deprecation")
def test_https_proxy_default_override(self, session: Streamlink):
session.set_option("http-proxy", "http://testproxy.com")
session.set_option("https-proxy", "https://testhttpsproxy.com")
assert session.http.proxies["http"] == "https://testhttpsproxy.com"
assert session.http.proxies["https"] == "https://testhttpsproxy.com"
@pytest.mark.usefixtures("_logs_deprecation")
def test_https_proxy_set_only(self, session: Streamlink):
session.set_option("https-proxy", "https://testhttpsproxy.com")
assert session.http.proxies["http"] == "https://testhttpsproxy.com"
assert session.http.proxies["https"] == "https://testhttpsproxy.com"
@pytest.mark.usefixtures("_no_deprecation")
def test_http_proxy_socks(self, session: Streamlink):
session.set_option("http-proxy", "socks5://localhost:1234")
assert session.http.proxies["http"] == "socks5://localhost:1234"
assert session.http.proxies["https"] == "socks5://localhost:1234"
@pytest.mark.usefixtures("_logs_deprecation")
def test_https_proxy_socks(self, session: Streamlink):
session.set_option("https-proxy", "socks5://localhost:1234")
assert session.http.proxies["http"] == "socks5://localhost:1234"
assert session.http.proxies["https"] == "socks5://localhost:1234"
@pytest.mark.usefixtures("_no_deprecation")
def test_get_http_proxy(self, session: Streamlink):
session.http.proxies["http"] = "http://testproxy1.com"
session.http.proxies["https"] = "http://testproxy2.com"
assert session.get_option("http-proxy") == "http://testproxy1.com"
@pytest.mark.usefixtures("_logs_deprecation")
def test_get_https_proxy(self, session: Streamlink):
session.http.proxies["http"] = "http://testproxy1.com"
session.http.proxies["https"] = "http://testproxy2.com"
assert session.get_option("https-proxy") == "http://testproxy2.com"
@pytest.mark.usefixtures("_logs_deprecation")
def test_https_proxy_get_directly(self, session: Streamlink):
# The DeprecationWarning's origin must point to this call, even without the set_option() wrapper
session.options.get("https-proxy")
@pytest.mark.usefixtures("_logs_deprecation")
def test_https_proxy_set_directly(self, session: Streamlink):
# The DeprecationWarning's origin must point to this call, even without the set_option() wrapper
session.options.set("https-proxy", "https://foo")
class TestOptionsKeyEqualsValue:
@pytest.fixture()
def option(self, request, session: Streamlink):
key, attr = request.param
httpsessionattr = getattr(session.http, attr)
assert session.get_option(key) is httpsessionattr
assert "foo" not in httpsessionattr
assert "bar" not in httpsessionattr
yield key
assert httpsessionattr.get("foo") == "foo=bar"
assert httpsessionattr.get("bar") == "123"
@pytest.mark.parametrize(
"option",
[
pytest.param(("http-cookies", "cookies"), id="http-cookies"),
pytest.param(("http-headers", "headers"), id="http-headers"),
pytest.param(("http-query-params", "params"), id="http-query-params"),
],
indirect=["option"],
)
def test_dict(self, session: Streamlink, option: str):
session.set_option(option, {"foo": "foo=bar", "bar": "123"})
@pytest.mark.parametrize(
("option", "value"),
[
pytest.param(("http-cookies", "cookies"), "foo=foo=bar;bar=123;baz", id="http-cookies"),
pytest.param(("http-headers", "headers"), "foo=foo=bar;bar=123;baz", id="http-headers"),
pytest.param(("http-query-params", "params"), "foo=foo=bar&bar=123&baz", id="http-query-params"),
],
indirect=["option"],
)
def test_string(self, session: Streamlink, option: str, value: str):
session.set_option(option, value)
@pytest.mark.parametrize(
("option", "attr", "default", "value"),
[
("http-ssl-cert", "cert", None, "foo"),
("http-ssl-verify", "verify", True, False),
("http-trust-env", "trust_env", True, False),
("http-timeout", "timeout", 20.0, 30.0),
],
)
def test_options_http_other(session: Streamlink, option: str, attr: str, default, value):
httpsessionattr = getattr(session.http, attr)
assert httpsessionattr == default
assert session.get_option(option) == httpsessionattr
session.set_option(option, value)
assert session.get_option(option) == value
|