Skip to content

Commit

Permalink
feat: Add GetRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Sep 25, 2022
1 parent 72397a7 commit 2ab720d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app.go
Expand Up @@ -638,6 +638,24 @@ func (app *App) GetRoute(name string) Route {
return Route{}
}

// GetRoutes Get all routes. When filterUseOption equal to true, it will filter the routes registered by the middleware.
func (app *App) GetRoutes(filterUseOption ...bool) []Route {
var rs []Route
var filterUse bool
if len(filterUseOption) != 0 {
filterUse = filterUseOption[0]
}
for _, routes := range app.stack {
for _, route := range routes {
if filterUse && route.use {
continue
}
rs = append(rs, *route)
}
}
return rs
}

// Use registers a middleware route that will match requests
// with the provided prefix (which is optional and defaults to "/").
//
Expand Down
30 changes: 30 additions & 0 deletions app_test.go
Expand Up @@ -1647,3 +1647,33 @@ func Test_App_SetTLSHandler(t *testing.T) {

utils.AssertEqual(t, "example.golang", c.ClientHelloInfo().ServerName)
}

func TestApp_GetRoutes(t *testing.T) {
app := New()
app.Use(func(c *Ctx) error {
return c.Next()
})
handler := func(c *Ctx) error {
return c.SendStatus(StatusOK)
}
app.Delete("/delete", handler).Name("delete")
app.Post("/post", handler).Name("post")
routes := app.GetRoutes(false)
utils.AssertEqual(t, 11, len(routes))
methodMap := map[string]string{"/delete": "delete", "/post": "post"}
for _, route := range routes {
name, ok := methodMap[route.Path]
if ok {
utils.AssertEqual(t, name, route.Name)
}
}

routes = app.GetRoutes(true)
utils.AssertEqual(t, 2, len(routes))
for _, route := range routes {
name, ok := methodMap[route.Path]
utils.AssertEqual(t, true, ok)
utils.AssertEqual(t, name, route.Name)
}

}

0 comments on commit 2ab720d

Please sign in to comment.