mirror of https://github.com/gin-gonic/gin.git
Adds unit tests for Bind()
This commit is contained in:
parent
179e947425
commit
0e08a109f9
|
@ -97,3 +97,30 @@ func TestJoinPaths(t *testing.T) {
|
|||
assert.Equal(t, joinPaths("/a/", "/hola/"), "/a/hola/")
|
||||
assert.Equal(t, joinPaths("/a/", "/hola//"), "/a/hola/")
|
||||
}
|
||||
|
||||
type bindTestStruct struct {
|
||||
Foo string `form:"foo" binding:"required"`
|
||||
Bar int `form:"bar" binding:"min=4"`
|
||||
}
|
||||
|
||||
func TestBindMiddleware(t *testing.T) {
|
||||
var value *bindTestStruct
|
||||
var called bool
|
||||
router := New()
|
||||
router.GET("/", Bind(bindTestStruct{}), func(c *Context) {
|
||||
called = true
|
||||
value = c.MustGet(BindKey).(*bindTestStruct)
|
||||
})
|
||||
performRequest(router, "GET", "/?foo=hola&bar=10")
|
||||
assert.True(t, called)
|
||||
assert.Equal(t, value.Foo, "hola")
|
||||
assert.Equal(t, value.Bar, 10)
|
||||
|
||||
called = false
|
||||
performRequest(router, "GET", "/?foo=hola&bar=1")
|
||||
assert.False(t, called)
|
||||
|
||||
assert.Panics(t, func() {
|
||||
Bind(&bindTestStruct{})
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue