优化方法的使用

This commit is contained in:
yun
2026-09-19 01:31:34 +08:00
parent 00eba5cc75
commit 407fc49e0c
20 changed files with 1710 additions and 376 deletions
Regular → Executable
+81 -3
View File
@@ -1,14 +1,16 @@
package ginx_test
import (
"context"
"encoding/json"
"errors"
"net/http/httptest"
"testing"
"code.yun.ink/pkg/responsex"
respgin "code.yun.ink/pkg/responsex/ginx"
respgin "code.yun.ink/pkg/responsex/gin"
"github.com/gin-gonic/gin"
"github.com/yuninks/langx"
)
func init() {
@@ -35,6 +37,8 @@ func newGinCtx(w *httptest.ResponseRecorder) *gin.Context {
func TestSuccess(t *testing.T) {
w := httptest.NewRecorder()
c := newGinCtx(w)
// c.Set("trace_id","adada")
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), "trace_id", "adada"))
respgin.Success(c, "hello world")
@@ -169,8 +173,8 @@ func TestErrorStr(t *testing.T) {
respgin.ErrorStr(c, "string error")
body := decodeBody(t, w)
if body["message"] == "" {
t.Error("message should not be empty")
if body["message"] != "string error" {
t.Errorf("message = %v, want 'string error'", body["message"])
}
}
@@ -275,3 +279,77 @@ func TestCustom(t *testing.T) {
t.Errorf("code = %v, want 100", body["code"])
}
}
// ---------- Middleware:trace_id 桥接 ----------
func newEngine() *gin.Engine {
r := gin.New()
r.Use(respgin.Middleware())
return r
}
func TestMiddlewareBridgesTraceID(t *testing.T) {
r := newEngine()
r.GET("/t", func(c *gin.Context) {
c.Set("trace_id", "trace-from-gin")
respgin.Success(c, "ok")
})
w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest("GET", "/t", nil))
body := decodeBody(t, w)
if body["trace_id"] != "trace-from-gin" {
t.Errorf("trace_id = %v, want trace-from-gin (需要 Middleware 桥接)", body["trace_id"])
}
}
func TestMiddlewareTraceIDFromHeader(t *testing.T) {
r := newEngine()
r.GET("/t", func(c *gin.Context) {
respgin.Success(c, "ok")
})
req := httptest.NewRequest("GET", "/t", nil)
req.Header.Set("X-Request-Id", "req-header-id")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
body := decodeBody(t, w)
if body["trace_id"] != "req-header-id" {
t.Errorf("trace_id = %v, want req-header-id", body["trace_id"])
}
}
// ---------- Middleware:按 Accept-Language 翻译 ----------
func TestMiddlewareBridgesLanguage(t *testing.T) {
langx.AppendTrans("en", map[string]string{
"responsex_success": "success",
"not_found": "resource not found",
})
r := newEngine()
r.GET("/t", func(c *gin.Context) {
respgin.Success(c, "ok")
})
r.GET("/e", func(c *gin.Context) {
respgin.ErrorStr(c, "not_found")
})
req := httptest.NewRequest("GET", "/t", nil)
req.Header.Set("Accept-Language", "en")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if body := decodeBody(t, w); body["message"] != "success" {
t.Errorf("message = %v, want success", body["message"])
}
req = httptest.NewRequest("GET", "/e", nil)
req.Header.Set("Accept-Language", "en")
w = httptest.NewRecorder()
r.ServeHTTP(w, req)
if body := decodeBody(t, w); body["message"] != "resource not found" {
t.Errorf("message = %v, want 'resource not found'", body["message"])
}
}