From 1a7ab6e4d5fdc72d6df30ef562102ae6e0d18518 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Thu, 18 Jun 2015 17:17:22 +0200 Subject: [PATCH] Fixes gin.Routes() tests --- gin.go | 7 ++++--- gin_test.go | 48 +++++++++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/gin.go b/gin.go index 580f853..e18d5b6 100644 --- a/gin.go +++ b/gin.go @@ -62,7 +62,8 @@ type ( ForwardedByClientIP bool } - RouteInfo struct { + RoutesInfo []RouteInfo + RouteInfo struct { Method string Path string Handler string @@ -195,14 +196,14 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { root.addRoute(path, handlers) } -func (engine *Engine) Routes() (routes []RouteInfo) { +func (engine *Engine) Routes() (routes RoutesInfo) { for _, tree := range engine.trees { routes = iterate("", tree.method, routes, tree.root) } return routes } -func iterate(path, method string, routes []RouteInfo, root *node) []RouteInfo { +func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo { path += root.path if len(root.handlers) > 0 { routes = append(routes, RouteInfo{ diff --git a/gin_test.go b/gin_test.go index 9d8fffa..fc9e821 100644 --- a/gin_test.go +++ b/gin_test.go @@ -181,15 +181,14 @@ func compareFunc(t *testing.T, a, b interface{}) { } func TestListOfRoutes(t *testing.T) { - handler := func(c *Context) {} router := New() - router.GET("/favicon.ico", handler) - router.GET("/", handler) + router.GET("/favicon.ico", handler_test1) + router.GET("/", handler_test1) group := router.Group("/users") { - group.GET("/", handler) - group.GET("/:id", handler) - group.POST("/:id", handler) + group.GET("/", handler_test2) + group.GET("/:id", handler_test1) + group.POST("/:id", handler_test2) } router.Static("/static", ".") @@ -197,32 +196,31 @@ func TestListOfRoutes(t *testing.T) { assert.Len(t, list, 7) assert.Contains(t, list, RouteInfo{ - Method: "GET", - Path: "/favicon.ico", + Method: "GET", + Path: "/favicon.ico", + Handler: "github.com/gin-gonic/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ - Method: "GET", - Path: "/", + Method: "GET", + Path: "/", + Handler: "github.com/gin-gonic/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ - Method: "GET", - Path: "/users/", + Method: "GET", + Path: "/users/", + Handler: "github.com/gin-gonic/gin.handler_test2", }) assert.Contains(t, list, RouteInfo{ - Method: "GET", - Path: "/users/:id", + Method: "GET", + Path: "/users/:id", + Handler: "github.com/gin-gonic/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ - Method: "POST", - Path: "/users/:id", + Method: "POST", + Path: "/users/:id", + Handler: "github.com/gin-gonic/gin.handler_test2", }) - assert.Contains(t, list, RouteInfo{ - Method: "GET", - Path: "/static/*filepath", - }) - assert.Contains(t, list, RouteInfo{ - Method: "HEAD", - Path: "/static/*filepath", - }) - } + +func handler_test1(c *Context) {} +func handler_test2(c *Context) {}