mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
Return HandlerChain from Routes()
This commit is contained in:
parent
540b1eff70
commit
76811fa6f3
2
gin.go
2
gin.go
@ -46,6 +46,7 @@ type RouteInfo struct {
|
|||||||
Path string
|
Path string
|
||||||
Handler string
|
Handler string
|
||||||
HandlerFunc HandlerFunc
|
HandlerFunc HandlerFunc
|
||||||
|
HandlersChain []HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoutesInfo defines a RouteInfo array.
|
// RoutesInfo defines a RouteInfo array.
|
||||||
@ -291,6 +292,7 @@ func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
|
|||||||
Path: path,
|
Path: path,
|
||||||
Handler: nameOfFunction(handlerFunc),
|
Handler: nameOfFunction(handlerFunc),
|
||||||
HandlerFunc: handlerFunc,
|
HandlerFunc: handlerFunc,
|
||||||
|
HandlersChain: root.handlers,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for _, child := range root.children {
|
for _, child := range root.children {
|
||||||
|
12
gin_test.go
12
gin_test.go
@ -444,7 +444,11 @@ func TestListOfRoutes(t *testing.T) {
|
|||||||
{
|
{
|
||||||
group.GET("/", handlerTest2)
|
group.GET("/", handlerTest2)
|
||||||
group.GET("/:id", handlerTest1)
|
group.GET("/:id", handlerTest1)
|
||||||
group.POST("/:id", handlerTest2)
|
}
|
||||||
|
postGroup := group.Group("")
|
||||||
|
postGroup.Use(handlerTest1)
|
||||||
|
{
|
||||||
|
postGroup.POST("/:id", handlerTest2)
|
||||||
}
|
}
|
||||||
router.Static("/static", ".")
|
router.Static("/static", ".")
|
||||||
|
|
||||||
@ -455,26 +459,31 @@ func TestListOfRoutes(t *testing.T) {
|
|||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/favicon.ico",
|
Path: "/favicon.ico",
|
||||||
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
||||||
|
HandlersChain: []HandlerFunc{handlerTest1},
|
||||||
})
|
})
|
||||||
assertRoutePresent(t, list, RouteInfo{
|
assertRoutePresent(t, list, RouteInfo{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/",
|
Path: "/",
|
||||||
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
||||||
|
HandlersChain: []HandlerFunc{handlerTest1},
|
||||||
})
|
})
|
||||||
assertRoutePresent(t, list, RouteInfo{
|
assertRoutePresent(t, list, RouteInfo{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/users/",
|
Path: "/users/",
|
||||||
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
||||||
|
HandlersChain: []HandlerFunc{handlerTest2},
|
||||||
})
|
})
|
||||||
assertRoutePresent(t, list, RouteInfo{
|
assertRoutePresent(t, list, RouteInfo{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: "/users/:id",
|
Path: "/users/:id",
|
||||||
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
||||||
|
HandlersChain: []HandlerFunc{handlerTest1},
|
||||||
})
|
})
|
||||||
assertRoutePresent(t, list, RouteInfo{
|
assertRoutePresent(t, list, RouteInfo{
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Path: "/users/:id",
|
Path: "/users/:id",
|
||||||
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
||||||
|
HandlersChain: []HandlerFunc{handlerTest1, handlerTest2},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,6 +544,7 @@ func TestEngineHandleContextManyReEntries(t *testing.T) {
|
|||||||
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
|
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
|
||||||
for _, gotRoute := range gotRoutes {
|
for _, gotRoute := range gotRoutes {
|
||||||
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
|
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
|
||||||
|
assert.ObjectsAreEqualValues(gotRoute.HandlersChain, wantRoute.HandlersChain)
|
||||||
assert.Regexp(t, wantRoute.Handler, gotRoute.Handler)
|
assert.Regexp(t, wantRoute.Handler, gotRoute.Handler)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user