ccccccc / test.sh
cnmksjs's picture
Upload 49 files
24fd742 verified
#!/bin/bash
# 聊天应用测试脚本
echo "🧪 聊天应用功能测试"
echo "=================="
echo
# 测试后端API
echo "🔧 测试后端API..."
# 健康检查
echo -n " 健康检查: "
if curl -f -s http://localhost:5000/api/health > /dev/null; then
echo "✅ 通过"
HEALTH=$(curl -s http://localhost:5000/api/health | jq -r '.status')
echo " 状态: $HEALTH"
else
echo "❌ 失败"
fi
# 测试注册API
echo -n " 注册API: "
REGISTER_RESPONSE=$(curl -s -X POST http://localhost:5000/api/register \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"password": "testpass123"
}')
if echo "$REGISTER_RESPONSE" | jq -e '.token' > /dev/null 2>&1; then
echo "✅ 通过"
TOKEN=$(echo "$REGISTER_RESPONSE" | jq -r '.token')
else
echo "⚠️ 用户可能已存在"
# 尝试登录
LOGIN_RESPONSE=$(curl -s -X POST http://localhost:5000/api/login \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "testpass123"
}')
if echo "$LOGIN_RESPONSE" | jq -e '.token' > /dev/null 2>&1; then
echo " 登录成功"
TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.token')
else
echo " ❌ 登录失败"
TOKEN=""
fi
fi
# 测试获取消息API
if [ -n "$TOKEN" ]; then
echo -n " 消息API: "
if curl -f -s -H "Authorization: Bearer $TOKEN" http://localhost:5000/api/messages > /dev/null; then
echo "✅ 通过"
else
echo "❌ 失败"
fi
fi
echo
# 测试前端
echo "🌐 测试前端服务..."
echo -n " 前端可访问性: "
if curl -f -s http://localhost:3000 > /dev/null; then
echo "✅ 通过"
else
echo "❌ 失败"
fi
echo -n " 静态资源: "
if curl -f -s http://localhost:3000/vite.svg > /dev/null; then
echo "✅ 通过"
else
echo "❌ 失败"
fi
echo
# 测试数据库
echo "📊 测试数据库..."
echo -n " MongoDB连接: "
if docker exec chat-mongo mongosh --eval "db.adminCommand('ping')" > /dev/null 2>&1; then
echo "✅ 通过"
# 获取数据库统计
echo " 数据库统计:"
docker exec chat-mongo mongosh chatapp --eval "
print(' 用户数量: ' + db.users.countDocuments());
print(' 消息数量: ' + db.messages.countDocuments());
" 2>/dev/null
else
echo "❌ 失败"
fi
echo
# 测试Socket.IO连接
echo "🔌 测试Socket.IO..."
echo -n " WebSocket连接: "
# 使用Node.js测试Socket.IO连接
node -e "
const io = require('socket.io-client');
const socket = io('http://localhost:5000');
socket.on('connect', () => {
console.log('✅ 通过');
socket.disconnect();
process.exit(0);
});
socket.on('connect_error', (error) => {
console.log('❌ 失败:', error.message);
process.exit(1);
});
setTimeout(() => {
console.log('❌ 超时');
process.exit(1);
}, 5000);
" 2>/dev/null || echo "⚠️ 需要安装socket.io-client进行测试"
echo
# 性能测试
echo "⚡ 性能测试..."
echo -n " API响应时间: "
RESPONSE_TIME=$(curl -o /dev/null -s -w '%{time_total}' http://localhost:5000/api/health)
echo "${RESPONSE_TIME}s"
echo -n " 前端加载时间: "
FRONTEND_TIME=$(curl -o /dev/null -s -w '%{time_total}' http://localhost:3000)
echo "${FRONTEND_TIME}s"
echo
# 安全测试
echo "🔒 基础安全检查..."
echo -n " CORS配置: "
CORS_HEADER=$(curl -s -I http://localhost:5000/api/health | grep -i "access-control-allow-origin")
if [ -n "$CORS_HEADER" ]; then
echo "✅ 已配置"
else
echo "⚠️ 未检测到CORS头"
fi
echo -n " JWT验证: "
UNAUTH_RESPONSE=$(curl -s -o /dev/null -w '%{http_code}' http://localhost:5000/api/messages)
if [ "$UNAUTH_RESPONSE" = "401" ]; then
echo "✅ 正常(未授权访问被拒绝)"
else
echo "⚠️ 可能存在安全问题"
fi
echo
# 总结
echo "📋 测试总结"
echo "==========="
echo "✅ 通过的测试将显示绿色勾号"
echo "❌ 失败的测试将显示红色叉号"
echo "⚠️ 警告或需要注意的项目将显示黄色感叹号"
echo
echo "💡 如果有测试失败,请检查:"
echo " 1. 服务是否正常启动"
echo " 2. 端口是否被占用"
echo " 3. 防火墙设置"
echo " 4. Docker容器状态"
echo
echo "🔧 查看详细日志: make logs"
echo "📊 查看监控信息: ./monitor.sh"