mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
* fix: 修复了国内配置一些模型不可用问题 1. 常见的openai和anthropic协议,如 智谱的codingpan https://open.bigmodel.cn/api/coding/paas/v4 2. 新出的一些没有模型列表的自定义模型提供商,如科大讯飞 https://maas-coding-api.cn-huabei-1.xf-yun.com/v2 * feat: 提高代码复用性 * fix(network): reuse shared SSL context * test(network): cover proxy and header forwarding * fix(network): support verify overrides --------- Co-authored-by: Taois <taoist.han@vertechs.com> Co-authored-by: 邹永赫 <1259085392@qq.com>
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
import ssl
|
|
|
|
import pytest
|
|
|
|
from astrbot.core.utils import network_utils
|
|
|
|
|
|
def test_create_proxy_client_reuses_shared_ssl_context(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
captured_calls: list[dict] = []
|
|
headers = {"X-Test-Header": "value"}
|
|
|
|
class _FakeAsyncClient:
|
|
def __init__(self, **kwargs):
|
|
captured_calls.append(kwargs)
|
|
|
|
monkeypatch.setattr(network_utils.httpx, "AsyncClient", _FakeAsyncClient)
|
|
|
|
network_utils.create_proxy_client("OpenAI")
|
|
network_utils.create_proxy_client("OpenAI", proxy="http://127.0.0.1:7890")
|
|
network_utils.create_proxy_client("OpenAI", headers=headers)
|
|
network_utils.create_proxy_client("OpenAI", proxy="")
|
|
|
|
assert len(captured_calls) == 4
|
|
assert "proxy" not in captured_calls[0]
|
|
assert captured_calls[1]["proxy"] == "http://127.0.0.1:7890"
|
|
assert captured_calls[2]["headers"] is headers
|
|
assert "proxy" not in captured_calls[3]
|
|
assert isinstance(captured_calls[0]["verify"], ssl.SSLContext)
|
|
assert captured_calls[0]["verify"] is captured_calls[1]["verify"]
|
|
assert captured_calls[1]["verify"] is captured_calls[2]["verify"]
|
|
assert captured_calls[2]["verify"] is captured_calls[3]["verify"]
|
|
|
|
|
|
def test_create_proxy_client_allows_verify_override(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
captured_calls: list[dict] = []
|
|
custom_verify = ssl.create_default_context()
|
|
|
|
class _FakeAsyncClient:
|
|
def __init__(self, **kwargs):
|
|
captured_calls.append(kwargs)
|
|
|
|
monkeypatch.setattr(network_utils.httpx, "AsyncClient", _FakeAsyncClient)
|
|
|
|
network_utils.create_proxy_client("OpenAI", verify=custom_verify)
|
|
|
|
assert len(captured_calls) == 1
|
|
assert captured_calls[0]["verify"] is custom_verify
|