sky/internal/platform/httpx/httpx_test.go
root 6ed55254a0 chore: initial import of sky project
Go + PostgreSQL + Valkey backend (cmd/internal/migrations),
uni-app mobile frontend, PC web (web/), admin console (admin/),
Android WebView container (android_project/).
Build artifacts, node_modules, local env and debug screenshots excluded.
2026-11-09 22:22:44 +08:00

47 lines
1.2 KiB
Go

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)
}
}