mirror of https://github.com/gofiber/fiber.git
🐛 bug: fix method validation on route naming (#2686)
* 🐛 bug: fix route naming issue when using same path for different methods
* fix linter
* add new testcase for HEAD route
* add comments to tests
* fix tests
pull/2689/head
parent
37ad7c7990
commit
94acde8fe5
6
app.go
6
app.go
|
@ -620,9 +620,11 @@ func (app *App) Name(name string) Router {
|
|||
|
||||
for _, routes := range app.stack {
|
||||
for _, route := range routes {
|
||||
if route.Path == app.latestRoute.Path {
|
||||
route.Name = name
|
||||
isMethodValid := route.Method == app.latestRoute.Method || app.latestRoute.use ||
|
||||
(app.latestRoute.Method == MethodGet && route.Method == MethodHead)
|
||||
|
||||
if route.Path == app.latestRoute.Path && isMethodValid {
|
||||
route.Name = name
|
||||
if route.group != nil {
|
||||
route.Name = route.group.name + route.Name
|
||||
}
|
||||
|
|
31
app_test.go
31
app_test.go
|
@ -1859,7 +1859,7 @@ func Test_Middleware_Route_Naming_With_Use(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_Route_Naming_Issue_2671(t *testing.T) {
|
||||
func Test_Route_Naming_Issue_2671_2685(t *testing.T) {
|
||||
app := New()
|
||||
|
||||
app.Get("/", emptyHandler).Name("index")
|
||||
|
@ -1904,4 +1904,33 @@ func Test_Route_Naming_Issue_2671(t *testing.T) {
|
|||
|
||||
postGroup.Post("", emptyHandler).Name("post.update")
|
||||
utils.AssertEqual(t, "/post/:postId", app.GetRoute("post.update").Path)
|
||||
|
||||
// Add testcase for routes use the same PATH on different methods
|
||||
app.Get("/users", nil).Name("get-users")
|
||||
app.Post("/users", nil).Name("add-user")
|
||||
getUsers := app.GetRoute("get-users")
|
||||
utils.AssertEqual(t, getUsers.Path, "/users")
|
||||
|
||||
addUser := app.GetRoute("add-user")
|
||||
utils.AssertEqual(t, addUser.Path, "/users")
|
||||
|
||||
// Add testcase for routes use the same PATH on different methods (for groups)
|
||||
newGrp := app.Group("/name-test")
|
||||
newGrp.Get("/users", nil).Name("grp-get-users")
|
||||
newGrp.Post("/users", nil).Name("grp-add-user")
|
||||
getUsers = app.GetRoute("grp-get-users")
|
||||
utils.AssertEqual(t, getUsers.Path, "/name-test/users")
|
||||
|
||||
addUser = app.GetRoute("grp-add-user")
|
||||
utils.AssertEqual(t, addUser.Path, "/name-test/users")
|
||||
|
||||
// Add testcase for HEAD route naming
|
||||
app.Get("/simple-route", emptyHandler).Name("simple-route")
|
||||
app.Head("/simple-route", emptyHandler).Name("simple-route2")
|
||||
|
||||
sRoute := app.GetRoute("simple-route")
|
||||
utils.AssertEqual(t, sRoute.Path, "/simple-route")
|
||||
|
||||
sRoute2 := app.GetRoute("simple-route2")
|
||||
utils.AssertEqual(t, sRoute2.Path, "/simple-route")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue