mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-21 01:43:38 +08:00
Merge d8ef3f68a4c0e491938b5419a7fc31a2cfc088e1 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646
This commit is contained in:
commit
52fecfcc59
@ -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())
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user