优化方法的使用
This commit is contained in:
Executable
+92
@@ -0,0 +1,92 @@
|
||||
package responsex_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"code.yun.ink/pkg/responsex"
|
||||
"github.com/yuninks/langx"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ---------- 健壮性修复 ----------
|
||||
|
||||
func TestGetOffsetNegativeSize(t *testing.T) {
|
||||
offset, limit := responsex.GetOffset(1, -5)
|
||||
if limit != 10 || offset != 0 {
|
||||
t.Errorf("GetOffset(1,-5) = (%d,%d), want (0,10)", offset, limit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPageNegativeSize(t *testing.T) {
|
||||
p := responsex.GetPage(1, -5, 100)
|
||||
if p.TotalPage != 0 {
|
||||
t.Errorf("TotalPage = %d, want 0(负数 size 不应算出负页数)", p.TotalPage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseMarshalFailed(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
// channel 无法序列化
|
||||
responsex.Success(context.Background(), w, make(chan int), responsex.SetIgnoreLog())
|
||||
|
||||
if w.Code != 500 {
|
||||
t.Errorf("http status = %d, want 500", w.Code)
|
||||
}
|
||||
body := decodeBody(t, w)
|
||||
if body["code"] != float64(400) {
|
||||
t.Errorf("code = %v, want 400", body["code"])
|
||||
}
|
||||
if body["message"] == "" {
|
||||
t.Error("message 不应为空(不能回空 body)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilLoggerDoesNotPanic(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
// 序列化失败路径会调用 logger,此处不得 panic
|
||||
responsex.Success(context.Background(), w, make(chan int), responsex.SetLogger(nil))
|
||||
|
||||
if w.Code != 500 {
|
||||
t.Errorf("http status = %d, want 500", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultSuccessMessage(t *testing.T) {
|
||||
responsex.InitOptions(responsex.SetDefaultSuccessMessage("操作成功"))
|
||||
defer responsex.InitOptions(responsex.SetDefaultSuccessMessage("请求成功"))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
responsex.Success(context.Background(), w, "x", responsex.SetIgnoreLog())
|
||||
|
||||
if body := decodeBody(t, w); body["message"] != "操作成功" {
|
||||
t.Errorf("message = %v, want 操作成功", body["message"])
|
||||
}
|
||||
}
|
||||
|
||||
// 多语言成功文案:注册专用 key 后,按 ctx 语言返回
|
||||
func TestSuccessMessageFromLangx(t *testing.T) {
|
||||
langx.AppendTrans("zh", map[string]string{"responsex_success": "请求成功"})
|
||||
langx.AppendTrans("en", map[string]string{"responsex_success": "success"})
|
||||
|
||||
ctxEn := langx.SetCtxLang(context.Background(), "en")
|
||||
ctxZh := langx.SetCtxLang(context.Background(), "zh")
|
||||
|
||||
if got := responsex.SuccessMessageCtx(ctxEn); got != "success" {
|
||||
t.Errorf("SuccessMessageCtx(en) = %q, want success", got)
|
||||
}
|
||||
if got := responsex.SuccessMessageCtx(ctxZh); got != "请求成功" {
|
||||
t.Errorf("SuccessMessageCtx(zh) = %q, want 请求成功", got)
|
||||
}
|
||||
|
||||
// Success 走同一套解析逻辑
|
||||
w := httptest.NewRecorder()
|
||||
responsex.Success(ctxEn, w, "x", responsex.SetIgnoreLog())
|
||||
if body := decodeBody(t, w); body["message"] != "success" {
|
||||
t.Errorf("Success(en) message = %v, want success", body["message"])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user