Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return HandlerChain from Routes() #2518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func (c HandlersChain) Last() HandlerFunc {

// RouteInfo represents a request route's specification which contains method and path and its handler.
type RouteInfo struct {
Method string
Path string
Handler string
HandlerFunc HandlerFunc
Method string
Path string
Handler string
HandlerFunc HandlerFunc
HandlersChain []HandlerFunc
}

// RoutesInfo defines a RouteInfo array.
Expand Down Expand Up @@ -287,10 +288,11 @@ func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
if len(root.handlers) > 0 {
handlerFunc := root.handlers.Last()
routes = append(routes, RouteInfo{
Method: method,
Path: path,
Handler: nameOfFunction(handlerFunc),
HandlerFunc: handlerFunc,
Method: method,
Path: path,
Handler: nameOfFunction(handlerFunc),
HandlerFunc: handlerFunc,
HandlersChain: root.handlers,
})
}
for _, child := range root.children {
Expand Down
42 changes: 26 additions & 16 deletions gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,37 +444,46 @@ func TestListOfRoutes(t *testing.T) {
{
group.GET("/", handlerTest2)
group.GET("/:id", handlerTest1)
group.POST("/:id", handlerTest2)
}
postGroup := group.Group("")
postGroup.Use(handlerTest1)
{
postGroup.POST("/:id", handlerTest2)
}
router.Static("/static", ".")

list := router.Routes()

assert.Len(t, list, 7)
assertRoutePresent(t, list, RouteInfo{
Method: "GET",
Path: "/favicon.ico",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
Method: "GET",
Path: "/favicon.ico",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
HandlersChain: []HandlerFunc{handlerTest1},
})
assertRoutePresent(t, list, RouteInfo{
Method: "GET",
Path: "/",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
Method: "GET",
Path: "/",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
HandlersChain: []HandlerFunc{handlerTest1},
})
assertRoutePresent(t, list, RouteInfo{
Method: "GET",
Path: "/users/",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
Method: "GET",
Path: "/users/",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
HandlersChain: []HandlerFunc{handlerTest2},
})
assertRoutePresent(t, list, RouteInfo{
Method: "GET",
Path: "/users/:id",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
Method: "GET",
Path: "/users/:id",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
HandlersChain: []HandlerFunc{handlerTest1},
})
assertRoutePresent(t, list, RouteInfo{
Method: "POST",
Path: "/users/:id",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
Method: "POST",
Path: "/users/:id",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
HandlersChain: []HandlerFunc{handlerTest1, handlerTest2},
})
}

Expand Down Expand Up @@ -535,6 +544,7 @@ func TestEngineHandleContextManyReEntries(t *testing.T) {
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
for _, gotRoute := range gotRoutes {
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
assert.ObjectsAreEqualValues(gotRoute.HandlersChain, wantRoute.HandlersChain)
assert.Regexp(t, wantRoute.Handler, gotRoute.Handler)
return
}
Expand Down