Files
whale-town-front/scripts/network/ApiTestScript.gd
moyin 77af0bda39 test:完善API测试框架
- 添加Godot内置API测试脚本
- 实现Python API客户端测试套件
- 添加快速测试和完整测试脚本
- 支持跨平台测试运行(Windows/Linux)
- 更新测试文档和使用指南
2025-12-25 23:09:12 +08:00

183 lines
5.6 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
extends Node
# API测试脚本 - 验证更新后的接口逻辑
class_name ApiTestScript
# 测试用例
var test_cases = [
{
"name": "测试网络连接",
"operation": "network_test",
"method": "get_app_status"
},
{
"name": "测试发送邮箱验证码",
"operation": "send_code",
"method": "send_email_verification",
"params": ["test@example.com"]
},
{
"name": "测试邮箱冲突检测",
"operation": "send_code",
"method": "send_email_verification",
"params": ["existing@example.com"] # 假设这个邮箱已存在
},
{
"name": "测试登录",
"operation": "login",
"method": "login",
"params": ["testuser", "password123"]
},
{
"name": "测试注册",
"operation": "register",
"method": "register",
"params": ["newuser", "newpassword123", "新用户", "newuser@example.com", "123456"]
}
]
func _ready():
print("=== API测试脚本启动 ===")
print("测试最新API v1.1.1的接口逻辑和toast显示")
# 延迟一下确保NetworkManager已初始化
await get_tree().create_timer(1.0).timeout
# 运行测试用例
run_tests()
func run_tests():
print("\n🧪 开始运行API测试用例...")
for i in range(test_cases.size()):
var test_case = test_cases[i]
print("\n--- 测试 %d: %s ---" % [i + 1, test_case.name])
await run_single_test(test_case)
# 测试间隔
await get_tree().create_timer(2.0).timeout
print("\n✅ 所有测试用例执行完成")
func run_single_test(test_case: Dictionary):
var operation = test_case.operation
var method = test_case.method
var params = test_case.get("params", [])
print("操作类型: ", operation)
print("调用方法: ", method)
print("参数: ", params)
# 创建回调函数
var callback = func(success: bool, data: Dictionary, error_info: Dictionary):
handle_test_response(test_case.name, operation, success, data, error_info)
# 调用对应的NetworkManager方法
var request_id = ""
match method:
"get_app_status":
request_id = NetworkManager.get_app_status(callback)
"send_email_verification":
if params.size() > 0:
request_id = NetworkManager.send_email_verification(params[0], callback)
"login":
if params.size() >= 2:
request_id = NetworkManager.login(params[0], params[1], callback)
"register":
if params.size() >= 5:
request_id = NetworkManager.register(params[0], params[1], params[2], params[3], params[4], callback)
_:
print("❌ 未知的测试方法: ", method)
return
if request_id != "":
print("✅ 请求已发送ID: ", request_id)
else:
print("❌ 请求发送失败")
func handle_test_response(test_name: String, operation: String, success: bool, data: Dictionary, error_info: Dictionary):
print("\n=== %s 响应结果 ===" % test_name)
print("成功: ", success)
print("数据: ", data)
print("错误信息: ", error_info)
# 使用ResponseHandler处理响应
var result = ResponseHandler.handle_response(operation, success, data, error_info)
print("\n--- ResponseHandler处理结果 ---")
print("处理成功: ", result.success)
print("消息: ", result.message)
print("Toast类型: ", result.toast_type)
print("是否显示Toast: ", result.should_show_toast)
# 模拟Toast显示
if result.should_show_toast:
print("🍞 Toast显示: [%s] %s" % [result.toast_type.to_upper(), result.message])
# 检查特殊情况
check_special_cases(data, error_info)
func check_special_cases(data: Dictionary, error_info: Dictionary):
var response_code = error_info.get("response_code", 0)
var error_code = data.get("error_code", "")
print("\n--- 特殊情况检查 ---")
# 检查409冲突
if response_code == 409:
print("✅ 检测到409冲突状态码 - 邮箱冲突检测正常工作")
# 检查206测试模式
if response_code == 206 or error_code == "TEST_MODE_ONLY":
print("✅ 检测到206测试模式 - 测试模式处理正常工作")
if data.has("data") and data.data.has("verification_code"):
print("🔑 测试模式验证码: ", data.data.verification_code)
# 检查429频率限制
if response_code == 429 or error_code == "TOO_MANY_REQUESTS":
print("✅ 检测到429频率限制 - 频率限制处理正常工作")
if data.has("throttle_info"):
print("⏰ 限制信息: ", data.throttle_info)
# 检查其他重要状态码
match response_code:
200:
print("✅ 200 成功响应")
201:
print("✅ 201 创建成功")
400:
print("⚠️ 400 请求参数错误")
401:
print("⚠️ 401 认证失败")
404:
print("⚠️ 404 资源不存在")
500:
print("❌ 500 服务器内部错误")
503:
print("❌ 503 服务不可用")
# 手动触发测试的方法
func test_email_conflict():
print("\n🧪 手动测试邮箱冲突检测...")
var callback = func(success: bool, data: Dictionary, error_info: Dictionary):
handle_test_response("邮箱冲突测试", "send_code", success, data, error_info)
NetworkManager.send_email_verification("existing@example.com", callback)
func test_rate_limit():
print("\n🧪 手动测试频率限制...")
var callback = func(success: bool, data: Dictionary, error_info: Dictionary):
handle_test_response("频率限制测试", "send_code", success, data, error_info)
# 快速发送多个请求来触发频率限制
for i in range(3):
NetworkManager.send_email_verification("test@example.com", callback)
await get_tree().create_timer(0.1).timeout
func test_test_mode():
print("\n🧪 手动测试测试模式...")
var callback = func(success: bool, data: Dictionary, error_info: Dictionary):
handle_test_response("测试模式测试", "send_code", success, data, error_info)
NetworkManager.send_email_verification("testmode@example.com", callback)