mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 17:30:23 +08:00
在 anyrouter/nekocode/sub2api/yescode 架构及 service.py 中, 将同步的 resolve_ops_proxy_config 替换为 resolve_ops_proxy_config_async, 通过 asyncio.to_thread 包装避免同步 DB 查询阻塞事件循环。
117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import gzip
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from src.services.proxy_node.resolver import (
|
|
build_post_kwargs,
|
|
build_post_kwargs_async,
|
|
build_stream_kwargs,
|
|
build_stream_kwargs_async,
|
|
resolve_ops_proxy_config_async,
|
|
resolve_proxy_info_async,
|
|
)
|
|
|
|
|
|
class TestProxyResolverCompression:
|
|
def test_build_post_kwargs_compresses_when_client_sent_gzip(self) -> None:
|
|
payload = {"message": "hello", "tokens": [1, 2, 3]}
|
|
kwargs = build_post_kwargs(
|
|
None,
|
|
url="https://example.com/v1/messages",
|
|
headers={"Content-Type": "application/json"},
|
|
payload=payload,
|
|
timeout=10.0,
|
|
client_content_encoding="gzip",
|
|
)
|
|
|
|
assert kwargs["headers"]["Content-Encoding"] == "gzip"
|
|
assert json.loads(gzip.decompress(kwargs["content"]).decode("utf-8")) == payload
|
|
|
|
def test_build_post_kwargs_keeps_plain_body_without_client_gzip(self) -> None:
|
|
payload = {"message": "plain"}
|
|
kwargs = build_post_kwargs(
|
|
None,
|
|
url="https://example.com/v1/messages",
|
|
headers={"Content-Type": "application/json"},
|
|
payload=payload,
|
|
timeout=10.0,
|
|
client_content_encoding=None,
|
|
)
|
|
|
|
assert "Content-Encoding" not in kwargs["headers"]
|
|
assert json.loads(kwargs["content"].decode("utf-8")) == payload
|
|
|
|
def test_build_stream_kwargs_drops_stale_content_encoding_header(self) -> None:
|
|
kwargs = build_stream_kwargs(
|
|
None,
|
|
url="https://example.com/v1/messages",
|
|
headers={"content-encoding": "gzip", "Content-Type": "application/json"},
|
|
payload={"message": "no-gzip"},
|
|
timeout=10.0,
|
|
client_content_encoding=None,
|
|
)
|
|
|
|
assert all(key.lower() != "content-encoding" for key in kwargs["headers"])
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_post_kwargs_async_compresses_when_client_sent_gzip(self) -> None:
|
|
payload = {"message": "hello", "tokens": [1, 2, 3]}
|
|
|
|
kwargs = await build_post_kwargs_async(
|
|
None,
|
|
url="https://example.com/v1/messages",
|
|
headers={"Content-Type": "application/json"},
|
|
payload=payload,
|
|
timeout=10.0,
|
|
client_content_encoding="gzip",
|
|
)
|
|
|
|
assert kwargs["headers"]["Content-Encoding"] == "gzip"
|
|
assert json.loads(gzip.decompress(kwargs["content"]).decode("utf-8")) == payload
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_build_stream_kwargs_async_drops_stale_content_encoding_header(self) -> None:
|
|
kwargs = await build_stream_kwargs_async(
|
|
None,
|
|
url="https://example.com/v1/messages",
|
|
headers={"content-encoding": "gzip", "Content-Type": "application/json"},
|
|
payload={"message": "no-gzip"},
|
|
timeout=10.0,
|
|
client_content_encoding=None,
|
|
)
|
|
|
|
assert all(key.lower() != "content-encoding" for key in kwargs["headers"])
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_resolve_proxy_info_async_masks_manual_proxy_url(self) -> None:
|
|
info = await resolve_proxy_info_async(
|
|
{
|
|
"enabled": True,
|
|
"url": "socks5://user:pass@proxy.example.com:1080",
|
|
}
|
|
)
|
|
|
|
assert info == {
|
|
"url": "socks5://proxy.example.com:1080",
|
|
"source": "provider",
|
|
}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_resolve_ops_proxy_config_async_preserves_legacy_proxy(
|
|
self,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(
|
|
"src.services.proxy_node.resolver.get_system_proxy_config",
|
|
lambda: None,
|
|
)
|
|
proxy, tunnel_node_id = await resolve_ops_proxy_config_async(
|
|
{"proxy": "http://proxy.example.com:8080"}
|
|
)
|
|
|
|
assert proxy == "http://proxy.example.com:8080"
|
|
assert tunnel_node_id is None
|