mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 05:16:35 +08:00
feature: add BindForm function
This commit is contained in:
parent
3010cbd7f4
commit
2cf4ce1085
@ -661,6 +661,11 @@ func (c *Context) BindHeader(obj any) error {
|
||||
return c.MustBindWith(obj, binding.Header)
|
||||
}
|
||||
|
||||
// BindForm is a shortcut for c.MustBindWith(obj, binding.Form)
|
||||
func (c *Context) BindForm(obj any) error {
|
||||
return c.MustBindWith(obj, binding.Form)
|
||||
}
|
||||
|
||||
// BindUri binds the passed struct pointer using binding.Uri.
|
||||
// It will abort the request with HTTP 400 if any error occurs.
|
||||
func (c *Context) BindUri(obj any) error {
|
||||
|
@ -1628,6 +1628,33 @@ func TestContextBindWithXML(t *testing.T) {
|
||||
assert.Equal(t, 0, w.Body.Len())
|
||||
}
|
||||
|
||||
func TestContextBindForm(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
|
||||
c.Request.ParseForm()
|
||||
|
||||
c.Request.Form.Add("rate", "8000")
|
||||
c.Request.Form.Add("domain", "music")
|
||||
c.Request.Form.Add("limit", "1000")
|
||||
|
||||
var testHeader struct {
|
||||
Rate int `form:"rate"`
|
||||
Domain string `form:"domain"`
|
||||
Limit int `form:"limit"`
|
||||
Fake string `form:"fake"`
|
||||
}
|
||||
|
||||
assert.NoError(t, c.BindForm(&testHeader))
|
||||
assert.Equal(t, 8000, testHeader.Rate)
|
||||
assert.Equal(t, "music", testHeader.Domain)
|
||||
assert.Equal(t, 1000, testHeader.Limit)
|
||||
assert.Equal(t, "", testHeader.Fake)
|
||||
assert.Equal(t, 0, w.Body.Len())
|
||||
}
|
||||
|
||||
func TestContextBindHeader(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
Loading…
x
Reference in New Issue
Block a user