优化结构

This commit is contained in:
Yvan
2026-08-28 00:29:40 +08:00
parent 04ff85116c
commit 03a4131f6e
118 changed files with 758 additions and 834 deletions
+33
View File
@@ -0,0 +1,33 @@
package httpx
import (
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestResponseHelpers(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.GET("/ok", OK)
r.GET("/fail", func(c *gin.Context) { Fail(c, "bad") })
r.GET("/auth", func(c *gin.Context) { NoAuth(c, "login") })
for _, tc := range []struct {
path string
statusCode int
body string
}{
{"/ok", 200, `{"code":0,"data":{},"msg":"操作成功"}`},
{"/fail", 200, `{"code":7,"data":{},"msg":"bad"}`},
{"/auth", 401, `{"code":7,"data":null,"msg":"login"}`},
} {
req := httptest.NewRequest("GET", tc.path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != tc.statusCode || w.Body.String() != tc.body {
t.Fatalf("%s: status=%d body=%q", tc.path, w.Code, w.Body.String())
}
}
}