Adds unit tests for Bind()

This commit is contained in:
Manu Mtz-Almeida 2015-07-04 19:56:43 +02:00
parent 179e947425
commit 0e08a109f9
1 changed files with 27 additions and 0 deletions

View File

@ -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/")
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{})
})
}