Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BindUri #1694

Merged
merged 4 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 42 additions & 2 deletions githubapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down