From 184948d24b6cdd2c900f06adad981979df41945b Mon Sep 17 00:00:00 2001 From: milky0217 Date: Wed, 10 Jun 2026 09:21:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=83=A8=E7=BD=B2=E6=B5=8B=E8=AF=95test?= =?UTF-8?q?.sh=E4=BD=BF=E7=94=A8HTTPS=E5=8D=8F=E8=AE=AE(4433/4434=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3)+=E8=B7=B3=E8=BF=87mock=E6=94=AF=E4=BB=98=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/test.sh | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/test.sh b/lib/test.sh index e41548e..c03cdc8 100644 --- a/lib/test.sh +++ b/lib/test.sh @@ -16,9 +16,15 @@ rollback_and_exit() { } test_basic_connectivity() { + # 根据端口判定协议(4433/4434 生产环境使用 HTTPS) + local proto="http" + if [ "${BACKEND_PORT}" = "4433" ] || [ "${BACKEND_PORT}" = "4434" ]; then + proto="https" + fi + local base_url="${proto}://127.0.0.1:${BACKEND_PORT}" local max_retries=3 retry=0 while [ $retry -lt $max_retries ]; do - retry=$((retry+1)); log_info "尝试 ${retry}/${max_retries}: 测试本地后端 (127.0.0.1:${BACKEND_PORT})" + retry=$((retry+1)); log_info "尝试 ${retry}/${max_retries}: 测试本地后端 (${base_url})" local all=true for test in \ "/health:200" \ @@ -27,7 +33,7 @@ test_basic_connectivity() { "/:200"; do IFS=':' read -r path expected method extra <<< "$test" [ -z "$method" ] && method="GET" - local code; code=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 -X ${method} http://127.0.0.1:${BACKEND_PORT}${path} ${extra}" 2>/dev/null || echo "000") + local code; code=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 -X ${method} ${base_url}${path} ${extra}" 2>/dev/null || echo "000") if [ "$code" != "$expected" ] && [ "$expected" != "200,400" ] && ! { [ "$expected" = "400,200" ] && { [ "$code" = "400" ] || [ "$code" = "200" ]; }; }; then if { [ "$expected" = "400" ] && [ "$code" = "200" ]; } || { [ "$expected" = "401" ] && [ "$code" = "200" ]; }; then log_info " ${path} 正常 (HTTP ${code})"; continue @@ -56,25 +62,25 @@ test_database_connection() { } test_token_flow() { - local login; login=$(remote "curl -s --max-time 10 -X POST http://127.0.0.1:${BACKEND_PORT}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "") + local login; login=$(remote "curl -sk --max-time 10 -X POST ${base_url}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "") local token; token=$(echo "$login" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "") [ -z "$token" ] && log_warn " 未获取到 token(微信 code 无效是正常的)" || log_info " 获取到 token" # 测试受保护端点 - local profile; profile=$(remote "curl -s --max-time 10 http://127.0.0.1:${BACKEND_PORT}/api/user/profile -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "") + local profile; profile=$(remote "curl -sk --max-time 10 ${base_url}/api/user/profile -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "") echo "$profile" | grep -q '"success":true' && log_info " /api/user/profile JWT 验证成功" || log_warn " /api/user/profile 无权限(正常)" - local refresh; refresh=$(remote "curl -s --max-time 10 -X POST http://127.0.0.1:${BACKEND_PORT}/api/refresh-token -H 'Content-Type: application/json' -d '{\"refresh_token\":\"invalid\"}'" 2>/dev/null || echo "") + local refresh; refresh=$(remote "curl -sk --max-time 10 -X POST ${base_url}/api/refresh-token -H 'Content-Type: application/json' -d '{\"refresh_token\":\"invalid\"}'" 2>/dev/null || echo "") log_info " refresh_token 端点响应正常" log_info "Token 流程测试通过"; return 0 } test_weather_details_jwt() { - local login; login=$(remote "curl -s --max-time 10 -X POST http://127.0.0.1:${BACKEND_PORT}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "") + local login; login=$(remote "curl -sk --max-time 10 -X POST ${base_url}/api/login -H 'Content-Type: application/json' -d '{\"code\":\"test\"}'" 2>/dev/null || echo "") local token; token=$(echo "$login" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "") [ -z "$token" ] && { log_warn " 无 token,跳过 weather/details 测试(微信 code 无效是正常的)"; return 0; } - local resp; resp=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/weather/details?id=1' -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "") + local resp; resp=$(remote "curl -sk --max-time 10 '${base_url}/weather/details?id=1' -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "") if echo "$resp" | grep -q '"success":true' || echo "$resp" | grep -q "Forbidden"; then log_info " weather/details JWT 验证通过" else @@ -87,7 +93,7 @@ test_invalid_token_401() { local bad="eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjB9.bad" local all=true for path in "/api/user/profile" "/weather/details?id=1" "/api/weather"; do - local c; c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}${path}' -H 'Authorization: Bearer ${bad}'" 2>/dev/null || echo "000") + local c; c=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}${path}' -H 'Authorization: Bearer ${bad}'" 2>/dev/null || echo "000") [ "$c" = "401" ] && log_info " ${path} 返回 401 ✓" || { log_warn " ${path} 返回 ${c}"; all=false; } done $all && log_info "无效 Token 401 测试通过" && return 0 @@ -95,7 +101,7 @@ test_invalid_token_401() { } test_response_content() { - local r; r=$(remote "curl -s --max-time 10 http://127.0.0.1:${BACKEND_PORT}/health" 2>/dev/null || echo "") + local r; r=$(remote "curl -sk --max-time 10 ${base_url}/health" 2>/dev/null || echo "") echo "$r" | grep -q '"status"' && echo "$r" | grep -q '"database"' && \ log_info " /health JSON 格式正确 ✓" || { log_warn " /health 响应异常"; return 1; } echo "$r" | grep -q '"database":"connected"' && \ @@ -113,13 +119,13 @@ test_mock_login() { local key; key=$(remote "grep 'MOCK_LOGIN_KEY' /etc/systemd/system/${SERVICE_NAME} 2>/dev/null | head -1 | sed 's/.*MOCK_LOGIN_KEY=//'" 2>/dev/null || echo "") [ -z "$key" ] && { log_warn " 未配置 MOCK_LOGIN_KEY,跳过"; return 0; } - local resp; resp=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/mock-login' -H 'X-Mock-Key: ${key}'" 2>/dev/null || echo "") + local resp; resp=$(remote "curl -sk --max-time 10 '${base_url}/api/mock-login' -H 'X-Mock-Key: ${key}'" 2>/dev/null || echo "") local token; token=$(echo "$resp" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "") if [ -n "$token" ]; then log_info " mock-login 返回有效 JWT ✅" # 用获取到的 token 测试通知接口 - local notif; notif=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/notifications/unread-count' -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "") + local notif; notif=$(remote "curl -sk --max-time 10 '${base_url}/api/notifications/unread-count' -H 'Authorization: Bearer ${token}'" 2>/dev/null || echo "") echo "$notif" | grep -q '"success":true' && log_info " /api/notifications/unread-count 正常 ✅" || log_warn " 通知接口响应异常" else log_warn " mock-login 未返回 token" @@ -129,13 +135,13 @@ test_mock_login() { test_payment_endpoints() { local all=true - local c; c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/payment'" 2>/dev/null || echo "000") + local c; c=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}/payment'" 2>/dev/null || echo "000") [ "$c" = "200" ] && log_info " /payment 返回 200 ✅" || { log_warn " /payment 返回 ${c}"; all=false; } - c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/payment/page?package=monthly&jwt=bad'" 2>/dev/null || echo "000") + c=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}/payment/page?package=monthly&jwt=bad'" 2>/dev/null || echo "000") [ "$c" = "401" ] && log_info " /payment/page (bad JWT) 返回 401 ✅" || { log_warn " /payment/page 返回 ${c}"; all=false; } - c=$(remote "curl -s -o /dev/null -w '%{http_code}' --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/user/quota' -H 'Authorization: Bearer bad'" 2>/dev/null || echo "000") + c=$(remote "curl -sk -o /dev/null -w '%{http_code}' --max-time 10 '${base_url}/api/user/quota' -H 'Authorization: Bearer bad'" 2>/dev/null || echo "000") [ "$c" = "401" ] && log_info " /api/user/quota (bad JWT) 返回 401 ✅" || { log_warn " /api/user/quota 返回 ${c}"; all=false; } $all && log_info "支付端点测试通过" && return 0 @@ -203,14 +209,14 @@ test_payment_flow() { # 1. 获取 mock JWT local login_resp - login_resp=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/api/mock-login' -H 'X-Mock-Key: ${mock_key}'" 2>/dev/null || echo "") + login_resp=$(remote "curl -sk --max-time 10 '${base_url}/api/mock-login' -H 'X-Mock-Key: ${mock_key}'" 2>/dev/null || echo "") local token token=$(echo "$login_resp" | grep -o '"token":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "") if [ -n "$token" ]; then # 2. 创建月度订单 local order_page - order_page=$(remote "curl -s --max-time 10 'http://127.0.0.1:${BACKEND_PORT}/payment/page?package=monthly&jwt=${token}'" 2>/dev/null || echo "") + order_page=$(remote "curl -sk --max-time 10 '${base_url}/payment/page?package=monthly&jwt=${token}'" 2>/dev/null || echo "") # 3. 从页面提取订单号 local order_no @@ -219,7 +225,7 @@ test_payment_flow() { if [ -n "$order_no" ] && [ "$order_no" != "null" ]; then # 4. Mock 确认支付 local confirm_resp - confirm_resp=$(remote "curl -s --max-time 10 -X POST 'http://127.0.0.1:${BACKEND_PORT}/api/payment/mock-confirm' \ + confirm_resp=$(remote "curl -sk --max-time 10 -X POST '${base_url}/api/payment/mock-confirm' \ -H 'Authorization: Bearer ${token}' \ -H 'X-Mock-Key: ${mock_key}' \ -H 'Content-Type: application/json' \