#!/bin/bash # 通用数据同步服务 - 完整测试脚本 echo "============================================================" echo "通用数据同步服务 - API 测试" echo "============================================================" SERVER="http://localhost:5001" PASSWORD="admin123123123" # 颜色定义 GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color test_count=0 pass_count=0 # 测试函数 test_api() { test_count=$((test_count + 1)) local name="$1" local expected="$2" shift 2 local output output=$("$@" 2>&1) local result=$? if [ $result -eq 0 ] && echo "$output" | grep -q "$expected"; then echo -e "${GREEN}✓${NC} 测试 $test_count: $name" pass_count=$((pass_count + 1)) return 0 else echo -e "${RED}✗${NC} 测试 $test_count: $name" echo " 预期: $expected" echo " 实际: $output" return 1 fi } echo "" echo "1️⃣ 健康检查" test_api "健康检查" "ok" curl -s $SERVER/health echo "" echo "2️⃣ 列出所有数据(初始状态)" test_api "列出所有数据" "tags" curl -s $SERVER/api/data echo "" echo "3️⃣ 上传 Cookie 数据" test_api "上传 Cookie" "success" curl -s -X POST $SERVER/api/data \ -H "Content-Type: application/json" \ -d "{ \"password\": \"$PASSWORD\", \"tag\": \"test:user1\", \"type\": \"cookie\", \"data\": { \"cookies\": [{\"name\": \"session\", \"value\": \"test123\"}] }, \"metadata\": { \"expires_in\": 604800, \"note\": \"测试用户1\", \"login_url\": \"https://example.com\" } }" echo "" echo "4️⃣ 上传 Token 数据" test_api "上传 Token" "success" curl -s -X POST $SERVER/api/data \ -H "Content-Type: application/json" \ -d "{ \"password\": \"$PASSWORD\", \"tag\": \"api:service\", \"type\": \"token\", \"data\": { \"token\": \"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...\", \"token_type\": \"Bearer\" }, \"metadata\": { \"expires_in\": 2592000, \"note\": \"API 服务账号\" } }" echo "" echo "5️⃣ 获取数据(有密码)" test_api "获取数据" "test:user1" curl -s -H "X-Password: $PASSWORD" $SERVER/api/data/test:user1 echo "" echo "6️⃣ 获取数据(无密码,应失败)" test_api "无密码获取" "Invalid password" curl -s $SERVER/api/data/test:user1 echo "" echo "7️⃣ 列出所有数据(应有2条)" test_api "列出数据" "test:user1" curl -s $SERVER/api/data echo "" echo "8️⃣ 一键复制配置" test_api "复制配置" "copy_text" curl -s $SERVER/api/data/copy/test:user1 echo "" echo "9️⃣ 删除数据" test_api "删除数据" "success" curl -s -X DELETE -H "X-Password: $PASSWORD" $SERVER/api/data/test:user1 echo "" echo "🔟 验证删除(应返回404)" test_api "验证删除" "not found" curl -s -H "X-Password: $PASSWORD" $SERVER/api/data/test:user1 echo "" echo "1️⃣1️⃣ 错误密码测试" test_api "错误密码" "Invalid password" curl -s -X POST $SERVER/api/data \ -H "Content-Type: application/json" \ -d "{ \"password\": \"wrongpassword\", \"tag\": \"test:fail\", \"type\": \"cookie\", \"data\": {} }" echo "" echo "============================================================" echo "测试结果: $pass_count / $test_count 通过" echo "============================================================" if [ $pass_count -eq $test_count ]; then echo -e "${GREEN}✓ 所有测试通过!${NC}" exit 0 else echo -e "${RED}✗ 部分测试失败${NC}" exit 1 fi