@ -3,7 +3,7 @@
# 测试公共函数库
#
set -e
# 注意:不在库文件中设置 set -e, 由调用脚本控制
# 配置
BASE_URL = " ${ BASE_URL :- http : //localhost : 4000 } "
@ -37,9 +37,12 @@ warn() {
echo -e " ${ YELLOW } [WARN] ${ NC } $1 "
}
# 检查上一个命令是否成功
# 检查上一个命令是否成功(传递状态码)
# $1: 描述
# $2: 状态码(可选,默认使用 $?)
check_success( ) {
if [ $? -eq 0 ] ; then
local status = ${ 2 :- $? }
if [ $status -eq 0 ] ; then
success " $1 "
return 0
else
@ -67,10 +70,15 @@ check_http_code() {
local expected = " ${ 2 :- 200 } "
local code
# 处理空响应(成功时可能返回空体)
if [ -z " $response " ] ; then
code = "200"
else
# 提取 code 字段,如果不存在则认为是 200
code = $( echo " $response " | jq -r '.code // 200' )
fi
if [ " $code " = = " $expected " ] ; then
if [ " $code " = " $expected " ] ; then
return 0
else
error " 期望状态码: $expected , 实际: $code "
@ -81,9 +89,9 @@ check_http_code() {
# 检查服务是否运行
check_service( ) {
if ! curl -s " $BASE_URL / _api.json " > /dev/null 2>& 1; then
if ! curl -s " $BASE_URL / api/auth/login-methods " > /dev/null 2>& 1; then
error "服务未启动,请先启动服务"
info "运行: go run cli/main.go -db =sqlite -dsn /tmp/vb.sqlite -p 4000"
info "运行: go run cli/main.go -db .type =sqlite -db. dsn /tmp/vb.sqlite -p 4000"
exit 1
fi
success "服务正在运行"
@ -97,32 +105,38 @@ check_service() {
api_get( ) {
local path = " $1 "
local token = " ${ 2 :- } "
local headers = ""
local curl_opts = ( "-s" "-X" "GET" )
if [ -n " $token " ] ; then
headers= " -H Authorization: Bearer $token "
curl_opts+= ( "-H" " Authorization: Bearer $token " )
fi
curl -s -X GET " $BASE_URL $path " $headers
curl " ${ curl_opts [@] } " " $BASE_URL $path "
}
# POST 请求
api_post( ) {
local path = " $1 "
local data = " $2 "
local token = " ${ 3 :- } "
local extra_headers = " ${ 4 :- } "
local headers = "-H Content-Type: application/json"
local curl_opts = ( "-s" "-X" "POST" "-H" "Content-Type: application/json" )
if [ -n " $token " ] ; then
headers= " $headers -H Authorization: Bearer $token "
curl_opts+= ( "-H" " Authorization: Bearer $token " )
fi
if [ -n " $extra_headers " ] ; then
headers = " $headers $extra_headers "
# 将 extra_headers 按空格分割添加到数组
for header in $extra_headers ; do
curl_opts += ( "-H" " $header " )
done
fi
curl -s -X POST " $BASE_URL $path " $headers -d " $data "
curl " ${ curl_opts [@] } " " $BASE_URL $path " -d " $data "
}
# PATCH 请求
@ -131,35 +145,41 @@ api_patch() {
local data = " $2 "
local token = " ${ 3 :- } "
local extra_headers = " ${ 4 :- } "
local headers = "-H Content-Type: application/json"
local curl_opts = ( "-s" "-X" "PATCH" "-H" "Content-Type: application/json" )
if [ -n " $token " ] ; then
headers= " $headers -H Authorization: Bearer $token "
curl_opts+= ( "-H" " Authorization: Bearer $token " )
fi
if [ -n " $extra_headers " ] ; then
headers = " $headers $extra_headers "
for header in $extra_headers ; do
curl_opts += ( "-H" " $header " )
done
fi
curl -s -X PATCH " $BASE_URL $path " $headers -d " $data "
curl " ${ curl_opts [@] } " " $BASE_URL $path " -d " $data "
}
# DELETE 请求
api_delete( ) {
local path = " $1 "
local token = " $2 "
local token = " ${ 2 :- } "
local extra_headers = " ${ 3 :- } "
local headers = ""
local curl_opts = ( "-s" "-X" "DELETE" )
if [ -n " $token " ] ; then
headers= " -H Authorization: Bearer $token "
curl_opts+= ( "-H" " Authorization: Bearer $token " )
fi
if [ -n " $extra_headers " ] ; then
headers = " $headers $extra_headers "
for header in $extra_headers ; do
curl_opts += ( "-H" " $header " )
done
fi
curl -s -X DELETE " $BASE_URL $path " $headers
curl " ${ curl_opts [@] } " " $BASE_URL $path "
}
# ========================================
@ -187,12 +207,18 @@ login_user() {
" {\"username\": \" $username \", \"password\": \" $password \"} "
}
# 提取 token
# 提取 access_ token
get_token( ) {
local login_response = " $1 "
echo " $login_response " | jq -r '.access_token'
}
# 提取 refresh_token
get_refresh_token( ) {
local login_response = " $1 "
echo " $login_response " | jq -r '.refresh_token'
}
# 提取用户ID
get_user_id( ) {
local login_response = " $1 "