package httpx_test import ( "bytes" "net/http" "net/http/httptest" "testing" "github.com/yourorg/article-platform/internal/platform/httpx" "golang.org/x/text/encoding/simplifiedchinese" ) type sampleReq struct { Nickname string `json:"nickname"` } func TestDecode_UTF8(t *testing.T) { body := []byte(`{"nickname":"朋友圈测试员"}`) req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(body)) var dst sampleReq if err := httpx.Decode(req, &dst); err != nil { t.Fatalf("expected nil err, got: %v", err) } if dst.Nickname != "朋友圈测试员" { t.Fatalf("expected '朋友圈测试员', got: %q", dst.Nickname) } } func TestDecode_GBK_AutoConvert(t *testing.T) { // GBK 编码的中文字符串(如 Windows cmd / PowerShell 提交) gbkBytes, err := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(`{"nickname":"朋友圈测试员"}`)) if err != nil { t.Fatalf("gbk encode failed: %v", err) } req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(gbkBytes)) var dst sampleReq if err := httpx.Decode(req, &dst); err != nil { t.Fatalf("expected nil err, got: %v", err) } if dst.Nickname != "朋友圈测试员" { t.Fatalf("expected auto-converted '朋友圈测试员', got: %q", dst.Nickname) } }