diff --git a/gin_test.go b/gin_test.go index ddecc3ee..28bba734 100644 --- a/gin_test.go +++ b/gin_test.go @@ -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) { diff --git a/routergroup_test.go b/routergroup_test.go index 25ddda69..f46b51b4 100644 --- a/routergroup_test.go +++ b/routergroup_test.go @@ -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...) + }) +}