refactor: align framework cleanup with new bootstrap flow
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
@@ -25,16 +25,6 @@ func Success(c *fiber.Ctx, data any) error {
|
||||
})
|
||||
}
|
||||
|
||||
// Error 返回错误响应
|
||||
func Error(c *fiber.Ctx, httpStatus int, code int, message string) error {
|
||||
return c.Status(httpStatus).JSON(Response{
|
||||
Code: code,
|
||||
Data: nil,
|
||||
Message: message,
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
// SuccessWithMessage 返回带自定义消息的成功响应
|
||||
func SuccessWithMessage(c *fiber.Ctx, data any, message string) error {
|
||||
return c.JSON(Response{
|
||||
|
||||
@@ -36,17 +36,8 @@ func BenchmarkSuccess(b *testing.B) {
|
||||
})
|
||||
}
|
||||
|
||||
// BenchmarkError 测试错误响应性能
|
||||
func BenchmarkError(b *testing.B) {
|
||||
app := fiber.New()
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
_ = Error(ctx, 400, 1001, "无效的请求")
|
||||
app.ReleaseCtx(ctx)
|
||||
}
|
||||
}
|
||||
// BenchmarkError 基准测试已被删除 - Error() 函数已在重构中移除
|
||||
// 错误响应现在由全局 ErrorHandler 统一处理
|
||||
|
||||
// BenchmarkSuccessWithMessage 测试带自定义消息的成功响应性能
|
||||
func BenchmarkSuccessWithMessage(b *testing.B) {
|
||||
|
||||
@@ -111,107 +111,9 @@ func TestSuccess(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestError 测试错误响应(T035)
|
||||
func TestError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
httpStatus int
|
||||
code int
|
||||
message string
|
||||
}{
|
||||
{
|
||||
name: "internal server error",
|
||||
httpStatus: 500,
|
||||
code: errors.CodeInternalError,
|
||||
message: "Internal server error occurred",
|
||||
},
|
||||
{
|
||||
name: "missing token error",
|
||||
httpStatus: 401,
|
||||
code: errors.CodeMissingToken,
|
||||
message: "Authentication token is missing",
|
||||
},
|
||||
{
|
||||
name: "invalid token error",
|
||||
httpStatus: 401,
|
||||
code: errors.CodeInvalidToken,
|
||||
message: "Token is invalid or expired",
|
||||
},
|
||||
{
|
||||
name: "rate limit error",
|
||||
httpStatus: 429,
|
||||
code: errors.CodeTooManyRequests,
|
||||
message: "Too many requests, please try again later",
|
||||
},
|
||||
{
|
||||
name: "service unavailable error",
|
||||
httpStatus: 503,
|
||||
code: errors.CodeAuthServiceUnavailable,
|
||||
message: "Authentication service is currently unavailable",
|
||||
},
|
||||
{
|
||||
name: "bad request error",
|
||||
httpStatus: 400,
|
||||
code: 2000,
|
||||
message: "Invalid request parameters",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return Error(c, tt.httpStatus, tt.code, tt.message)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
resp, err := app.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to execute request: %v", err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
// 验证 HTTP 状态码
|
||||
if resp.StatusCode != tt.httpStatus {
|
||||
t.Errorf("Expected status code %d, got %d", tt.httpStatus, resp.StatusCode)
|
||||
}
|
||||
|
||||
// 验证响应头
|
||||
if resp.Header.Get("Content-Type") != "application/json" {
|
||||
t.Errorf("Expected Content-Type application/json, got %s", resp.Header.Get("Content-Type"))
|
||||
}
|
||||
|
||||
// 解析响应体
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read response body: %v", err)
|
||||
}
|
||||
|
||||
var response Response
|
||||
if err := sonic.Unmarshal(body, &response); err != nil {
|
||||
t.Fatalf("Failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
// 验证响应结构
|
||||
if response.Code != tt.code {
|
||||
t.Errorf("Expected code %d, got %d", tt.code, response.Code)
|
||||
}
|
||||
|
||||
if response.Message != tt.message {
|
||||
t.Errorf("Expected message '%s', got '%s'", tt.message, response.Message)
|
||||
}
|
||||
|
||||
if response.Data != nil {
|
||||
t.Errorf("Expected data to be nil in error response, got %v", response.Data)
|
||||
}
|
||||
|
||||
// 验证时间戳格式 RFC3339
|
||||
if _, err := time.Parse(time.RFC3339, response.Timestamp); err != nil {
|
||||
t.Errorf("Timestamp is not in RFC3339 format: %s", response.Timestamp)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
// TestError 测试已被删除 - Error() 函数已在重构中移除
|
||||
// 错误响应现在由全局 ErrorHandler 统一处理
|
||||
// 相关测试已迁移到 pkg/errors/handler_test.go
|
||||
|
||||
// TestSuccessWithMessage 测试带自定义消息的成功响应(T034)
|
||||
func TestSuccessWithMessage(t *testing.T) {
|
||||
@@ -413,10 +315,8 @@ func TestMultipleResponses(t *testing.T) {
|
||||
callCount := 0
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
callCount++
|
||||
if callCount%2 == 0 {
|
||||
return Success(c, map[string]int{"count": callCount})
|
||||
}
|
||||
return Error(c, 500, errors.CodeInternalError, "error occurred")
|
||||
// 只返回成功响应,因为 Error() 函数已被删除
|
||||
return Success(c, map[string]int{"count": callCount})
|
||||
})
|
||||
|
||||
// 发送多个请求
|
||||
|
||||
Reference in New Issue
Block a user