Files
live-hub-py/tests/test_proxy_runtime.py
T

59 lines
1.7 KiB
Python

"""全局代理配置到运行时来源的唯一映射测试。"""
from web.backend.models import ProxyConfig
from web.backend.services.proxy_service import build_proxy_runtime
def _config(**overrides) -> ProxyConfig:
values = {
"enabled": True,
"api_url": "",
"http": "",
"https": "",
"whitelist_enabled": False,
"whitelist_platform": "xiequ",
"whitelist_credentials": None,
"whitelist_uid": "",
"whitelist_ukey": "",
}
values.update(overrides)
config = ProxyConfig()
for key, value in values.items():
setattr(config, key, value)
return config
def test_proxy_runtime_preserves_static_http_and_https_addresses():
runtime = build_proxy_runtime(
_config(
http="http://10.0.0.1:8080",
https="http://10.0.0.2:8080",
)
)
assert runtime.static_proxies == {
"http": "http://10.0.0.1:8080",
"https": "http://10.0.0.2:8080",
}
assert runtime.api_fetcher is None
def test_proxy_runtime_builds_api_fetcher_with_legacy_whitelist_fields():
runtime = build_proxy_runtime(
_config(
api_url="https://proxy.example/fetch",
whitelist_enabled=True,
whitelist_uid="legacy-user",
whitelist_ukey="legacy-key",
)
)
assert runtime.static_proxies is None
assert runtime.api_fetcher is not None
assert runtime.api_fetcher.api_url == "https://proxy.example/fetch"
def test_proxy_runtime_is_empty_when_caller_or_global_switch_is_off():
assert not build_proxy_runtime(_config(), enabled=False).enabled
assert not build_proxy_runtime(_config(enabled=False)).enabled