From 0e08a109f91490a5d6b595816e2791873336bb4b Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sat, 4 Jul 2015 19:56:43 +0200 Subject: [PATCH] Adds unit tests for Bind() --- utils_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/utils_test.go b/utils_test.go index ba0cc20d..11a5b684 100644 --- a/utils_test.go +++ b/utils_test.go @@ -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{}) + }) +}