Merge d8ef3f68a4c0e491938b5419a7fc31a2cfc088e1 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
jee 2026-10-25 15:34:32 +08:00 committed by GitHub
commit 52fecfcc59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 1 deletions

View File

@ -1403,6 +1403,17 @@ func TestPlainBinding(t *testing.T) {
require.NoError(t, p.Bind(req, ptr))
}
func TestPlainBindingNamedBytes(t *testing.T) {
data := []byte(`{"message":"raw body"}`)
var body json.RawMessage
require.NoError(t, Plain.Bind(requestWithBody(http.MethodPost, "/", string(data)), &body))
assert.Equal(t, data, []byte(body))
body = nil
require.NoError(t, Plain.BindBody(data, &body))
assert.Equal(t, data, []byte(body))
}
func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
assert.Equal(t, name, b.Name())

View File

@ -47,7 +47,7 @@ func decodePlain(data []byte, obj any) error {
return nil
}
if _, ok := v.Interface().([]byte); ok {
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
v.SetBytes(data)
return nil
}

View File

@ -2598,6 +2598,24 @@ func TestContextShouldBindPlain(t *testing.T) {
assert.Equal(t, 0, w.Body.Len())
}
func TestContextShouldBindPlainNamedBytes(t *testing.T) {
type bodyBytes []byte
data := []byte{'a', 0, 0xff, 'z'}
router := New()
router.POST("/", func(c *Context) {
var body bodyBytes
if err := c.ShouldBindPlain(&body); err != nil {
c.String(http.StatusBadRequest, "%s", err)
return
}
c.Data(http.StatusOK, "application/octet-stream", []byte(body))
})
w := httptest.NewRecorder()
router.ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(data)))
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, data, w.Body.Bytes())
}
func TestContextShouldBindHeader(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)