Skip to content

Commit 5020be8

Browse files
committed
feat: add tests for vv_generator and upstream relay functionality
1 parent 20067b6 commit 5020be8

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ dependencies = [
1010
"fastapi-limiter>=0.1.6",
1111
"fastapi-utils>=0.8.0",
1212
"fastapi[standard]>=0.115.12",
13+
"httpx[socks]>=0.28.1",
1314
"itsdangerous>=2.2.0",
1415
"passlib[bcrypt]>=1.7.4",
1516
"pre-commit>=4.2.0",
1617
"pyjwt>=2.10.1",
18+
"pytest>=9.1.1",
1719
"python-dotenv>=1.1.0",
1820
"python-jose[cryptography]>=3.4.0",
1921
"redis>=6.0.0",

test_upstream_relay.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import re
2+
import asyncio
3+
import sys
4+
5+
import httpx
6+
7+
from _utils import vv_generator
8+
9+
10+
def check_vv_generator_format() -> bool:
11+
"""
12+
校验 vv_generator 返回一个非空的十六进制字符串。
13+
纯本地检查,不依赖网络。
14+
"""
15+
vv = vv_generator()
16+
17+
if not isinstance(vv, str):
18+
print("❌ vv 应该是字符串")
19+
return False
20+
21+
if len(vv) <= 20:
22+
print(f"❌ vv 看起来太短,可能生成失败: {vv}")
23+
return False
24+
25+
if not re.fullmatch(r"[0-9a-f]+", vv):
26+
print(f"❌ vv 包含非十六进制字符: {vv}")
27+
return False
28+
29+
print(f"✅ vv_generator 格式正常: {vv}")
30+
return True
31+
32+
33+
async def check_upstream_relay_search() -> bool:
34+
"""
35+
请求上游轻量接口,验证 relay 是否可达。
36+
会真正访问 api.olelive.com。
37+
"""
38+
vv = vv_generator()
39+
url = f"https://api.olelive.com/v1/pub/index/vod/hot/4/0/1?_vv={vv}"
40+
headers = {"User-Agent": "animeapi-test/1.0"}
41+
42+
try:
43+
async with httpx.AsyncClient(timeout=30.0) as client:
44+
resp = await client.get(url, headers=headers)
45+
46+
if resp.status_code != 200:
47+
print(f"❌ 上游返回非 200 状态: {resp.status_code}")
48+
print(f"响应内容: {resp.text}")
49+
return False
50+
51+
try:
52+
data = resp.json()
53+
except Exception as e:
54+
print(f"❌ 上游返回内容无法解析为 JSON: {e}")
55+
print(f"响应内容: {resp.text}")
56+
return False
57+
58+
if not isinstance(data, (dict, list)):
59+
print(f"❌ 上游返回的数据不是 JSON 对象或数组: {type(data)}")
60+
return False
61+
62+
print("✅ 上游 relay 可达,返回 JSON 数据")
63+
print(data)
64+
return True
65+
66+
except httpx.TimeoutException:
67+
print("❌ 请求上游超时")
68+
return False
69+
except httpx.RequestError as e:
70+
print(f"❌ 请求上游失败: {e}")
71+
return False
72+
73+
74+
async def main() -> int:
75+
print("开始检查 vv_generator...")
76+
vv_ok = check_vv_generator_format()
77+
78+
print("\n开始检查上游 relay...")
79+
upstream_ok = await check_upstream_relay_search()
80+
81+
if vv_ok and upstream_ok:
82+
print("\n🎉 全部检查通过")
83+
return 0
84+
85+
print("\n💥 检查未通过")
86+
return 1
87+
88+
89+
if __name__ == "__main__":
90+
exit_code = asyncio.run(main())
91+
sys.exit(exit_code)

uv.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)