mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-07 04:38:19 +08:00
feat(context): add ShouldBindBodyWithProtoBuf shortcut
This commit is contained in:
parent
5f4f964325
commit
cb82095769
@ -967,6 +967,11 @@ func (c *Context) ShouldBindBodyWithPlain(obj any) error {
|
|||||||
return c.ShouldBindBodyWith(obj, binding.Plain)
|
return c.ShouldBindBodyWith(obj, binding.Plain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShouldBindBodyWithProtoBuf is a shortcut for c.ShouldBindBodyWith(obj, binding.ProtoBuf).
|
||||||
|
func (c *Context) ShouldBindBodyWithProtoBuf(obj any) error {
|
||||||
|
return c.ShouldBindBodyWith(obj, binding.ProtoBuf)
|
||||||
|
}
|
||||||
|
|
||||||
// ClientIP implements one best effort algorithm to return the real client IP.
|
// ClientIP implements one best effort algorithm to return the real client IP.
|
||||||
// It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not.
|
// It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not.
|
||||||
// If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-IP]).
|
// If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-IP]).
|
||||||
|
|||||||
@ -2916,6 +2916,21 @@ func TestContextShouldBindBodyWithPlain(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextShouldBindBodyWithProtoBuf(t *testing.T) {
|
||||||
|
label := "FOO"
|
||||||
|
protoBody, err := proto.Marshal(&testdata.Test{Label: &label})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(protoBody))
|
||||||
|
|
||||||
|
obj := testdata.Test{}
|
||||||
|
require.NoError(t, c.ShouldBindBodyWithProtoBuf(&obj))
|
||||||
|
require.NotNil(t, obj.Label)
|
||||||
|
assert.Equal(t, "FOO", *obj.Label)
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextGolangContext(t *testing.T) {
|
func TestContextGolangContext(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
c.Request, _ = http.NewRequest(http.MethodPost, "/", strings.NewReader(`{"foo":"bar", "bar":"foo"}`))
|
c.Request, _ = http.NewRequest(http.MethodPost, "/", strings.NewReader(`{"foo":"bar", "bar":"foo"}`))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user