99 lines
3.0 KiB
Go
99 lines
3.0 KiB
Go
package bodydata
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/http/request"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func newParseBodyContext(body string, contentType string) *gin.Context {
|
|
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
ctx.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString(body))
|
|
if contentType != "" {
|
|
ctx.Request.Header.Set("Content-Type", contentType)
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
func TestDecodeSupportsVendorJSON(t *testing.T) {
|
|
ctx := newParseBodyContext(`{"name":"test"}`, "application/vnd.example+json")
|
|
result := map[string]any{}
|
|
if _, err := decodeDefault(ctx, &result); err != nil {
|
|
t.Fatalf("decodeDefault() error = %v", err)
|
|
}
|
|
if result["name"] != "test" {
|
|
t.Fatalf("unexpected result: %#v", result)
|
|
}
|
|
}
|
|
|
|
// TestDecodeBuiltInFormats 验证固定诊断解码保留已有格式,不依赖运行时注册。
|
|
func TestDecodeBuiltInFormats(t *testing.T) {
|
|
for _, test := range []struct {
|
|
media, body string
|
|
}{
|
|
{"application/json", `{"name":"test"}`},
|
|
{"application/x-www-form-urlencoded", "name=test"},
|
|
{"application/xml", "<name>test</name>"},
|
|
{"xml", "<name>test</name>"},
|
|
{"application/yaml", "name: test"},
|
|
} {
|
|
t.Run(test.media, func(t *testing.T) {
|
|
var (
|
|
result struct {
|
|
Name string `json:"name" xml:"name" yaml:"name"`
|
|
}
|
|
)
|
|
if _, err := decodeDefault(newParseBodyContext(test.body, test.media), &result); err != nil || result.Name != "test" {
|
|
t.Fatalf("built-in format changed: value=%q err=%v", result.Name, err)
|
|
}
|
|
})
|
|
}
|
|
if _, err := decodeDefault(newParseBodyContext("name=test", "application/unregistered"), &struct{}{}); err == nil {
|
|
t.Fatal("unsupported diagnostic format was accepted")
|
|
}
|
|
}
|
|
|
|
func TestDecodeSupportsEmptyFormBody(t *testing.T) {
|
|
ctx := newParseBodyContext("", "application/x-www-form-urlencoded")
|
|
result := map[string]any{}
|
|
if _, err := decodeDefault(ctx, &result); err != nil {
|
|
t.Fatalf("decodeDefault() error = %v", err)
|
|
}
|
|
if len(result) != 0 {
|
|
t.Fatalf("unexpected result: %#v", result)
|
|
}
|
|
}
|
|
|
|
func TestReadBodyWithLimitCachesAndRestoresBody(t *testing.T) {
|
|
ctx := newParseBodyContext("request-body", "text/plain")
|
|
firstRead, err := request.ReadBodyWithLimit(ctx, 32)
|
|
if err != nil {
|
|
t.Fatalf("first ReadBodyWithLimit() error = %v", err)
|
|
}
|
|
secondRead, err := request.ReadBodyWithLimit(ctx, 32)
|
|
if err != nil {
|
|
t.Fatalf("second ReadBodyWithLimit() error = %v", err)
|
|
}
|
|
if string(firstRead) != "request-body" || string(secondRead) != "request-body" {
|
|
t.Fatalf("unexpected reads: %q, %q", firstRead, secondRead)
|
|
}
|
|
}
|
|
|
|
func TestReadBodyWithLimitReturnsMaxBytesError(t *testing.T) {
|
|
ctx := newParseBodyContext("too-large", "text/plain")
|
|
_, err := request.ReadBodyWithLimit(ctx, 3)
|
|
var (
|
|
maxBytesError *http.MaxBytesError
|
|
)
|
|
if !errors.As(err, &maxBytesError) {
|
|
t.Fatalf("ReadBodyWithLimit() error = %v, want *http.MaxBytesError", err)
|
|
}
|
|
}
|
|
|
|
func decodeDefault(ctx *gin.Context, target any) ([]byte, error) { return Decode(ctx, target, 0) }
|