Merge 76811fa6f3d84bea5213424eec8845784673b05a into cc4e11438cd6c0bcc632fe3492d3817dfa21c337

This commit is contained in:
superatrain 2024-08-04 02:40:57 -04:00 committed by GitHub
commit 360e76fe71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 24 deletions

2
gin.go
View File

@ -72,6 +72,7 @@ type RouteInfo struct {
Path string
Handler string
HandlerFunc HandlerFunc
HandlersChain []HandlerFunc
}
// RoutesInfo defines a RouteInfo slice.
@ -380,6 +381,7 @@ func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
Path: path,
Handler: nameOfFunction(handlerFunc),
HandlerFunc: handlerFunc,
HandlersChain: root.handlers,
})
}
for _, child := range root.children {

View File

@ -485,7 +485,11 @@ 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", ".")
@ -496,26 +500,31 @@ func TestListOfRoutes(t *testing.T) {
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$",
HandlersChain: []HandlerFunc{handlerTest1},
})
assertRoutePresent(t, list, RouteInfo{
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$",
HandlersChain: []HandlerFunc{handlerTest1},
})
assertRoutePresent(t, list, RouteInfo{
Method: "POST",
Path: "/users/:id",
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
HandlersChain: []HandlerFunc{handlerTest1, handlerTest2},
})
}
@ -689,6 +698,7 @@ func parseCIDR(cidr string) *net.IPNet {
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
}