完善初始化请求链路与异常响应
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-08-30 20:03:37 +08:00
1 parent 19c596a6d4
commit ffa7378480
4 files changed
+54 -4

No files matched your search

+3
View File
@@ -204,4 +204,7 @@ func Setup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
return
}
// 初始化接口必须始终返回统一 JSON,避免未知步骤被当成空响应,
// 进而让前端误判请求已完成并进入错误页面。
response.NewErrorResponse(response.ParamsError, "不支持的初始化操作", "").FPrint(w)
}
+34
View File
@@ -0,0 +1,34 @@
package controllers
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
)
func TestSetupReturnsJSONForUnsupportedOperation(t *testing.T) {
request := httptest.NewRequest("POST", "/api/v1/setup", strings.NewReader(`{"step":"unknown","action":"unknown"}`))
response := httptest.NewRecorder()
Setup(&appcontext.Context{}, response, request)
if response.Code != 200 {
t.Fatalf("setup status = %d, want 200", response.Code)
}
if contentType := response.Header().Get("Content-Type"); !strings.HasPrefix(contentType, "application/json") {
t.Fatalf("setup content type = %q, want JSON", contentType)
}
var payload struct {
ErrorNo int `json:"errorNo"`
ErrorMsg string `json:"errorMsg"`
}
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
t.Fatalf("decode setup response: %v", err)
}
if payload.ErrorNo != 100 || payload.ErrorMsg != "不支持的初始化操作" {
t.Fatalf("unexpected setup response: %+v", payload)
}
}