85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
测试特定的API端点
|
|
"""
|
|
|
|
import requests
|
|
import sys
|
|
|
|
BASE_URL = "http://localhost:5000"
|
|
|
|
def test_endpoint(method, endpoint, data=None):
|
|
"""测试单个API端点"""
|
|
try:
|
|
url = f"{BASE_URL}{endpoint}"
|
|
print(f"测试 {method} {endpoint}")
|
|
|
|
if method.upper() == 'GET':
|
|
response = requests.get(url, timeout=5)
|
|
elif method.upper() == 'POST':
|
|
response = requests.post(url, json=data, timeout=5)
|
|
else:
|
|
print(f"不支持的方法: {method}")
|
|
return False
|
|
|
|
print(f"状态码: {response.status_code}")
|
|
|
|
if response.status_code == 200:
|
|
try:
|
|
json_response = response.json()
|
|
print(f"响应: {json_response}")
|
|
return True
|
|
except:
|
|
print(f"响应不是JSON: {response.text[:200]}")
|
|
return False
|
|
else:
|
|
print(f"错误响应: {response.text[:200]}")
|
|
return False
|
|
|
|
except requests.exceptions.ConnectRefused:
|
|
print("连接被拒绝 - 服务器可能没有运行")
|
|
return False
|
|
except requests.exceptions.Timeout:
|
|
print("请求超时")
|
|
return False
|
|
except Exception as e:
|
|
print(f"请求失败: {e}")
|
|
return False
|
|
|
|
def main():
|
|
print("=" * 50)
|
|
print("API端点测试")
|
|
print("=" * 50)
|
|
|
|
# 测试各个可能有问题的端点
|
|
endpoints_to_test = [
|
|
("GET", "/api/products"),
|
|
("GET", "/api/stores"),
|
|
("GET", "/api/products/P001"),
|
|
("GET", "/api/products/P001/sales"),
|
|
]
|
|
|
|
results = []
|
|
for method, endpoint in endpoints_to_test:
|
|
print(f"\n{'-' * 30}")
|
|
result = test_endpoint(method, endpoint)
|
|
results.append((endpoint, result))
|
|
print()
|
|
|
|
print("=" * 50)
|
|
print("测试结果汇总:")
|
|
print("=" * 50)
|
|
|
|
for endpoint, result in results:
|
|
status = "✓ 成功" if result else "✗ 失败"
|
|
print(f"{endpoint}: {status}")
|
|
|
|
failed_count = sum(1 for _, result in results if not result)
|
|
if failed_count > 0:
|
|
print(f"\n发现 {failed_count} 个失败的端点")
|
|
print("请检查服务器日志获取详细错误信息")
|
|
else:
|
|
print("\n所有端点测试通过!")
|
|
|
|
if __name__ == "__main__":
|
|
main() |