mirror of https://github.com/gin-gonic/gin.git
Fixes gin.Routes() tests
This commit is contained in:
parent
451f3b988b
commit
1a7ab6e4d5
5
gin.go
5
gin.go
|
@ -62,6 +62,7 @@ type (
|
||||||
ForwardedByClientIP bool
|
ForwardedByClientIP bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RoutesInfo []RouteInfo
|
||||||
RouteInfo struct {
|
RouteInfo struct {
|
||||||
Method string
|
Method string
|
||||||
Path string
|
Path string
|
||||||
|
@ -195,14 +196,14 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
||||||
root.addRoute(path, handlers)
|
root.addRoute(path, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) Routes() (routes []RouteInfo) {
|
func (engine *Engine) Routes() (routes RoutesInfo) {
|
||||||
for _, tree := range engine.trees {
|
for _, tree := range engine.trees {
|
||||||
routes = iterate("", tree.method, routes, tree.root)
|
routes = iterate("", tree.method, routes, tree.root)
|
||||||
}
|
}
|
||||||
return routes
|
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
|
path += root.path
|
||||||
if len(root.handlers) > 0 {
|
if len(root.handlers) > 0 {
|
||||||
routes = append(routes, RouteInfo{
|
routes = append(routes, RouteInfo{
|
||||||
|
|
28
gin_test.go
28
gin_test.go
|
@ -181,15 +181,14 @@ func compareFunc(t *testing.T, a, b interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListOfRoutes(t *testing.T) {
|
func TestListOfRoutes(t *testing.T) {
|
||||||
handler := func(c *Context) {}
|
|
||||||
router := New()
|
router := New()
|
||||||
router.GET("/favicon.ico", handler)
|
router.GET("/favicon.ico", handler_test1)
|
||||||
router.GET("/", handler)
|
router.GET("/", handler_test1)
|
||||||
group := router.Group("/users")
|
group := router.Group("/users")
|
||||||
{
|
{
|
||||||
group.GET("/", handler)
|
group.GET("/", handler_test2)
|
||||||
group.GET("/:id", handler)
|
group.GET("/:id", handler_test1)
|
||||||
group.POST("/:id", handler)
|
group.POST("/:id", handler_test2)
|
||||||
}
|
}
|
||||||
router.Static("/static", ".")
|
router.Static("/static", ".")
|
||||||
|
|
||||||
|
@ -199,30 +198,29 @@ func TestListOfRoutes(t *testing.T) {
|
||||||
assert.Contains(t, list, RouteInfo{
|
assert.Contains(t, list, RouteInfo{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/favicon.ico",
|
Path: "/favicon.ico",
|
||||||
|
Handler: "github.com/gin-gonic/gin.handler_test1",
|
||||||
})
|
})
|
||||||
assert.Contains(t, list, RouteInfo{
|
assert.Contains(t, list, RouteInfo{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/",
|
Path: "/",
|
||||||
|
Handler: "github.com/gin-gonic/gin.handler_test1",
|
||||||
})
|
})
|
||||||
assert.Contains(t, list, RouteInfo{
|
assert.Contains(t, list, RouteInfo{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/users/",
|
Path: "/users/",
|
||||||
|
Handler: "github.com/gin-gonic/gin.handler_test2",
|
||||||
})
|
})
|
||||||
assert.Contains(t, list, RouteInfo{
|
assert.Contains(t, list, RouteInfo{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/users/:id",
|
Path: "/users/:id",
|
||||||
|
Handler: "github.com/gin-gonic/gin.handler_test1",
|
||||||
})
|
})
|
||||||
assert.Contains(t, list, RouteInfo{
|
assert.Contains(t, list, RouteInfo{
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Path: "/users/:id",
|
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) {}
|
||||||
|
|
Loading…
Reference in New Issue