From 2cf4ce10858c11440480266a0a0e5fe27e6465ab Mon Sep 17 00:00:00 2001 From: iliya Date: Fri, 27 Jan 2023 20:45:14 +0330 Subject: [PATCH 1/3] feature: add BindForm function --- context.go | 5 +++++ context_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/context.go b/context.go index b1352b9b..85b3ef30 100644 --- a/context.go +++ b/context.go @@ -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 { diff --git a/context_test.go b/context_test.go index 85e0a616..c6e88a95 100644 --- a/context_test.go +++ b/context_test.go @@ -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) From 72f42b88a45e5069ff251e881c6170dde5347c5f Mon Sep 17 00:00:00 2001 From: iliya Date: Sat, 4 Feb 2023 12:06:00 +0330 Subject: [PATCH 2/3] feature: add ShouldBindForm function --- context.go | 5 +++++ context_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/context.go b/context.go index 85b3ef30..5e3050fe 100644 --- a/context.go +++ b/context.go @@ -740,6 +740,11 @@ func (c *Context) ShouldBindUri(obj any) error { return binding.Uri.BindUri(m, obj) } +// ShouldBindForm is shortcut for c.ShouldBindWith(obj, binding.Form). +func (c *Context) ShouldBindForm(obj any) error { + return c.ShouldBindWith(obj, binding.Form) +} + // ShouldBindWith binds the passed struct pointer using the specified binding engine. // See the binding package. func (c *Context) ShouldBindWith(obj any, b binding.Binding) error { diff --git a/context_test.go b/context_test.go index c6e88a95..4913a637 100644 --- a/context_test.go +++ b/context_test.go @@ -1655,6 +1655,33 @@ func TestContextBindForm(t *testing.T) { assert.Equal(t, 0, w.Body.Len()) } +func TestContextShouldBindForm(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.ShouldBindForm(&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) From 303e21a421a36c1aa50ebb2266f32bd646b99d4d Mon Sep 17 00:00:00 2001 From: iliya Date: Tue, 14 Feb 2023 08:04:42 +0330 Subject: [PATCH 3/3] pass linter --- context_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 4913a637..d975ed3c 100644 --- a/context_test.go +++ b/context_test.go @@ -1634,7 +1634,9 @@ func TestContextBindForm(t *testing.T) { c.Request, _ = http.NewRequest("POST", "/", nil) - c.Request.ParseForm() + if err := c.Request.ParseForm(); err != nil { + t.Error(err.Error()) + } c.Request.Form.Add("rate", "8000") c.Request.Form.Add("domain", "music")