More unit tests

This commit is contained in:
Manu Mtz-Almeida 2015-06-04 04:32:18 +02:00
parent f199a21486
commit ee021d06ea
2 changed files with 40 additions and 0 deletions

View File

@ -26,10 +26,36 @@ func TestCreateEngine(t *testing.T) {
assert.Equal(t, "/", router.BasePath)
assert.Equal(t, router.engine, router)
assert.Empty(t, router.Handlers)
}
func TestAddRoute(t *testing.T) {
router := New()
router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
assert.Len(t, router.trees, 1)
assert.NotNil(t, router.trees.get("GET"))
assert.Nil(t, router.trees.get("POST"))
router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
assert.Len(t, router.trees, 2)
assert.NotNil(t, router.trees.get("GET"))
assert.NotNil(t, router.trees.get("POST"))
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
assert.Len(t, router.trees, 2)
}
func TestAddRouteFails(t *testing.T) {
router := New()
assert.Panics(t, func() { router.addRoute("", "/", HandlersChain{func(_ *Context) {}}) })
assert.Panics(t, func() { router.addRoute("GET", "a", HandlersChain{func(_ *Context) {}}) })
assert.Panics(t, func() { router.addRoute("GET", "/", HandlersChain{}) })
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
assert.Panics(t, func() {
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
})
}
func TestCreateDefaultRouter(t *testing.T) {

View File

@ -109,3 +109,17 @@ func TestRouterGroupInvalidStaticFile(t *testing.T) {
router.StaticFile("/path/*param", "favicon.ico")
})
}
func TestRouterGroupTooManyHandlers(t *testing.T) {
router := New()
handlers1 := make([]HandlerFunc, 40)
router.Use(handlers1...)
handlers2 := make([]HandlerFunc, 26)
assert.Panics(t, func() {
router.Use(handlers2...)
})
assert.Panics(t, func() {
router.GET("/", handlers2...)
})
}