From 4cfbd9c7b350994802a823bd15a88044fb12e39a Mon Sep 17 00:00:00 2001 From: thinkerou Date: Mon, 10 Dec 2018 21:04:00 +0800 Subject: [PATCH 1/3] add BindUri --- context.go | 18 ++++++++++++++---- githubapi_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/context.go b/context.go index 478e8c0944..c94926e1c6 100644 --- a/context.go +++ b/context.go @@ -530,15 +530,25 @@ func (c *Context) BindYAML(obj interface{}) error { return c.MustBindWith(obj, binding.YAML) } +// 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 interface{}) error { + if err := c.ShouldBindUri(obj); err != nil { + c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) + return err + } + return nil +} + // MustBindWith binds the passed struct pointer using the specified binding engine. // It will abort the request with HTTP 400 if any error occurs. // See the binding package. -func (c *Context) MustBindWith(obj interface{}, b binding.Binding) (err error) { - if err = c.ShouldBindWith(obj, b); err != nil { +func (c *Context) MustBindWith(obj interface{}, b binding.Binding) error { + if err := c.ShouldBindWith(obj, b); err != nil { c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) + return err } - - return + return nil } // ShouldBind checks the Content-Type to select a binding engine automatically, diff --git a/githubapi_test.go b/githubapi_test.go index 6b56a2b7a9..1cc79c9fb2 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -290,8 +290,8 @@ func TestShouldBindUri(t *testing.T) { router := Default() type Person struct { - Name string `uri:"name"` - Id string `uri:"id"` + Name string `uri:"name" binding:"required"` + Id string `uri:"id" binding:"required"` } router.Handle("GET", "/rest/:name/:id", func(c *Context) { var person Person @@ -304,6 +304,46 @@ func TestShouldBindUri(t *testing.T) { path, _ := exampleFromPath("/rest/:name/:id") w := performRequest(router, "GET", path) assert.Equal(t, "ShouldBindUri test OK", w.Body.String()) + assert.Equal(t, http.StatusOK, w.Code) +} + +func TestBindUri(t *testing.T) { + DefaultWriter = os.Stdout + router := Default() + + type Person struct { + Name string `uri:"name" binding:"required"` + Id string `uri:"id" binding:"required"` + } + router.Handle("GET", "/rest/:name/:id", func(c *Context) { + var person Person + assert.NoError(t, c.BindUri(&person)) + assert.True(t, "" != person.Name) + assert.True(t, "" != person.Id) + c.String(http.StatusOK, "BindUri test OK") + }) + + path, _ := exampleFromPath("/rest/:name/:id") + w := performRequest(router, "GET", path) + assert.Equal(t, "BindUri test OK", w.Body.String()) + assert.Equal(t, http.StatusOK, w.Code) +} + +func TestBindUriError(t *testing.T) { + DefaultWriter = os.Stdout + router := Default() + + type Member struct { + number string `uri:"num" binding:"required,uuid"` + } + router.Handle("GET", "/new/rest/:num", func(c *Context) { + var m Member + c.BindUri(&m) + }) + + path1, _ := exampleFromPath("/new/rest/:num") + w1 := performRequest(router, "GET", path1) + assert.Equal(t, http.StatusBadRequest, w1.Code) } func githubConfigRouter(router *Engine) { From 711f77e3edcfae26ee44bf405a82a6c9913ea13a Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 12 Dec 2018 18:59:19 +0800 Subject: [PATCH 2/3] fix bug --- githubapi_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/githubapi_test.go b/githubapi_test.go index 1cc79c9fb2..30a59299be 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -334,7 +334,7 @@ func TestBindUriError(t *testing.T) { router := Default() type Member struct { - number string `uri:"num" binding:"required,uuid"` + Number string `uri:"num" binding:"required,uuid"` } router.Handle("GET", "/new/rest/:num", func(c *Context) { var m Member From d944de715a184060e639af5ce2ec884eaf35447a Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 12 Dec 2018 19:37:37 +0800 Subject: [PATCH 3/3] fix code style --- githubapi_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/githubapi_test.go b/githubapi_test.go index 30a59299be..5253425aac 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -313,7 +313,7 @@ func TestBindUri(t *testing.T) { type Person struct { Name string `uri:"name" binding:"required"` - Id string `uri:"id" binding:"required"` + Id string `uri:"id" binding:"required"` } router.Handle("GET", "/rest/:name/:id", func(c *Context) { var person Person