mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
Merge 76811fa6f3d84bea5213424eec8845784673b05a into cc4e11438cd6c0bcc632fe3492d3817dfa21c337
This commit is contained in:
commit
360e76fe71
18
gin.go
18
gin.go
@ -68,10 +68,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 slice.
|
||||
@ -376,10 +377,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 {
|
||||
|
42
gin_test.go
42
gin_test.go
@ -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", ".")
|
||||
|
||||
@ -493,29 +497,34 @@ func TestListOfRoutes(t *testing.T) {
|
||||
|
||||
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},
|
||||
})
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user