parent
f76ccb25f1
commit
59695e7ba8
18
context.go
18
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,
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user