98 lines
3.0 KiB
Bash
Executable File
98 lines
3.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
BASE_URL="${BASE_URL:-http://127.0.0.1:8080}"
|
||
MINI_PROGRAM_ID="${MINI_PROGRAM_ID:-1}"
|
||
WX_CODE="${WX_CODE:-}"
|
||
TOKEN="${TOKEN:-}"
|
||
|
||
need_cmd() {
|
||
command -v "$1" >/dev/null 2>&1 || {
|
||
echo "缺少命令: $1" >&2
|
||
exit 1
|
||
}
|
||
}
|
||
|
||
need_cmd curl
|
||
need_cmd jq
|
||
|
||
echo "[1/8] 准备 token"
|
||
if [[ -z "$TOKEN" ]]; then
|
||
if [[ -z "$WX_CODE" ]]; then
|
||
echo "请提供 TOKEN 或 WX_CODE" >&2
|
||
exit 1
|
||
fi
|
||
|
||
TOKEN="$(curl -sS -X POST "${BASE_URL}/api/v1/auth/login" \
|
||
-H 'Content-Type: application/json' \
|
||
-d "{\"mini_program_id\":${MINI_PROGRAM_ID},\"code\":\"${WX_CODE}\"}" \
|
||
| jq -r '.data.session_key')"
|
||
fi
|
||
|
||
if [[ -z "$TOKEN" || "$TOKEN" == "null" ]]; then
|
||
echo "获取 token 失败" >&2
|
||
exit 1
|
||
fi
|
||
|
||
auth_header=( -H "Authorization: Bearer ${TOKEN}" )
|
||
|
||
assert_code_zero() {
|
||
local payload="$1"
|
||
local api_code
|
||
api_code="$(echo "$payload" | jq -r '.code')"
|
||
if [[ "$api_code" != "0" ]]; then
|
||
echo "接口业务码异常: ${api_code}, payload=${payload}" >&2
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
echo "[2/8] 创建物品"
|
||
create_payload="$(curl -sS -X POST "${BASE_URL}/api/expiry/items" \
|
||
"${auth_header[@]}" \
|
||
-H 'Content-Type: application/json' \
|
||
-d '{"name":"集成测试牛奶","category":"food","expiry_date":"2030-12-31","quantity":2,"location":"冰箱"}')"
|
||
assert_code_zero "$create_payload"
|
||
item_id="$(echo "$create_payload" | jq -r '.data.id')"
|
||
|
||
echo "[3/8] 查询列表"
|
||
list_payload="$(curl -sS "${BASE_URL}/api/expiry/items?status=all&page=1&page_size=20" "${auth_header[@]}")"
|
||
assert_code_zero "$list_payload"
|
||
|
||
echo "[4/8] 查询汇总"
|
||
summary_payload="$(curl -sS "${BASE_URL}/api/expiry/summary" "${auth_header[@]}")"
|
||
assert_code_zero "$summary_payload"
|
||
|
||
echo "[5/8] 更新物品"
|
||
update_payload="$(curl -sS -X PUT "${BASE_URL}/api/expiry/items/${item_id}" \
|
||
"${auth_header[@]}" \
|
||
-H 'Content-Type: application/json' \
|
||
-d '{"name":"集成测试牛奶(更新)","category":"food","expiry_date":"2030-12-30","quantity":1}')"
|
||
assert_code_zero "$update_payload"
|
||
|
||
echo "[6/8] 标记已使用"
|
||
status_payload="$(curl -sS -X POST "${BASE_URL}/api/expiry/items/${item_id}/status" \
|
||
"${auth_header[@]}" \
|
||
-H 'Content-Type: application/json' \
|
||
-d '{"status":"used"}')"
|
||
assert_code_zero "$status_payload"
|
||
|
||
echo "[7/8] 设置提醒"
|
||
settings_payload="$(curl -sS -X POST "${BASE_URL}/api/expiry/settings" \
|
||
"${auth_header[@]}" \
|
||
-H 'Content-Type: application/json' \
|
||
-d '{"remind_days":[10,5,1]}')"
|
||
assert_code_zero "$settings_payload"
|
||
|
||
echo "[8/8] 删除物品"
|
||
delete_payload="$(curl -sS -X DELETE "${BASE_URL}/api/expiry/items/${item_id}" "${auth_header[@]}")"
|
||
assert_code_zero "$delete_payload"
|
||
|
||
echo "全部接口集成测试通过"
|
||
|
||
if command -v hey >/dev/null 2>&1; then
|
||
echo "开始性能测试: /api/expiry/summary 并发 20,总请求 100"
|
||
hey -n 100 -c 20 -H "Authorization: Bearer ${TOKEN}" "${BASE_URL}/api/expiry/summary"
|
||
else
|
||
echo "未安装 hey,跳过性能测试(建议安装后执行:hey -n 100 -c 20 ${BASE_URL}/api/expiry/summary)"
|
||
fi
|