-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_web_api.py
More file actions
127 lines (106 loc) · 3.56 KB
/
test_web_api.py
File metadata and controls
127 lines (106 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
"""
快速测试Web API
Quick API Test
"""
import requests
import json
import os
from pathlib import Path
# 配置
BASE_URL = "http://127.0.0.1:5000"
UPLOAD_FOLDER = "uploads"
SAMPLE_IMAGE = "samples/sample1.jpg"
def test_api():
"""测试Web API"""
print("=" * 60)
print("🧪 Web API 快速测试")
print("=" * 60)
print()
# 检查样本图片
if not os.path.exists(SAMPLE_IMAGE):
print(f"❌ 样本图片不存在: {SAMPLE_IMAGE}")
return False
print(f"📝 使用样本图片: {SAMPLE_IMAGE}")
print()
# 步骤1: 上传文件
print("[步骤1] 上传图片...")
try:
with open(SAMPLE_IMAGE, 'rb') as f:
files = {'file': f}
response = requests.post(
f"{BASE_URL}/api/upload",
files=files,
timeout=10
)
if response.status_code != 200:
print(f"❌ 上传失败: {response.status_code}")
print(f" 响应: {response.text}")
return False
result = response.json()
if not result.get('success'):
print(f"❌ 上传失败: {result.get('error')}")
return False
filepath = result.get('filepath')
print(f"✅ 上传成功: {filepath}")
print()
except Exception as e:
print(f"❌ 上传异常: {e}")
return False
# 步骤2: 处理图片
print("[步骤2] 处理图片(添加水印)...")
try:
process_data = {
'filepath': filepath,
'fragments_count': 4,
'perturbation_strength': 0.2,
'watermark_text': '© Protected',
'add_invisible': False,
'copyright_info': 'Test Image'
}
response = requests.post(
f"{BASE_URL}/api/process",
json=process_data,
timeout=30
)
if response.status_code != 200:
print(f"❌ 处理失败: {response.status_code}")
print(f" 响应: {response.text}")
return False
result = response.json()
if not result.get('success'):
print(f"❌ 处理失败: {result.get('error')}")
return False
output_path = result.get('output_filepath')
print(f"✅ 处理成功: {output_path}")
print(f" 处理时间: {result.get('statistics', {}).get('processing_time')}ms")
print()
except Exception as e:
print(f"❌ 处理异常: {e}")
return False
# 步骤3: 验证输出
print("[步骤3] 验证输出...")
if os.path.exists(output_path):
file_size = os.path.getsize(output_path)
print(f"✅ 输出文件存在")
print(f" 文件大小: {file_size:,} 字节")
print()
else:
print(f"❌ 输出文件不存在: {output_path}")
return False
print("=" * 60)
print("✅ 所有测试通过!Web API 正常工作")
print("=" * 60)
return True
if __name__ == '__main__':
# 检查Flask服务是否运行
try:
response = requests.get(f"{BASE_URL}/", timeout=2)
print("✅ Flask服务已运行\n")
except:
print("❌ Flask服务未运行")
print("请先运行: python run_web_server.py\n")
exit(1)
# 运行测试
success = test_api()
exit(0 if success else 1)