Skip to content

Commit

Permalink
🐛 bug: fix path checking on route naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammed Efe Cetin committed Oct 13, 2023
1 parent cb89cce commit 5fa0f81
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (app *App) Name(name string) Router {

for _, routes := range app.stack {
for _, route := range routes {
if route.Path == app.latestRoute.path {
if route.Path == app.latestRoute.Path {
route.Name = name

if route.group != nil {
Expand Down
47 changes: 47 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1860,3 +1860,50 @@ func Test_Middleware_Route_Naming_With_Use(t *testing.T) {
}
}
}

func Test_Route_Naming_Issue_2671(t *testing.T) {
app := New()

app.Get("/", emptyHandler).Name("index")
utils.AssertEqual(t, "/", app.GetRoute("index").Path)

app.Get("/a/:a_id", emptyHandler).Name("a")
utils.AssertEqual(t, "/a/:a_id", app.GetRoute("a").Path)

app.Post("/b/:bId", emptyHandler).Name("b")
utils.AssertEqual(t, "/b/:bId", app.GetRoute("b").Path)

c := app.Group("/c")
c.Get("", emptyHandler).Name("c.get")
utils.AssertEqual(t, "/c", app.GetRoute("c.get").Path)

c.Post("", emptyHandler).Name("c.post")
utils.AssertEqual(t, "/c", app.GetRoute("c.post").Path)

c.Get("/d", emptyHandler).Name("c.get.d")
utils.AssertEqual(t, "/c/d", app.GetRoute("c.get.d").Path)

d := app.Group("/d/:d_id")
d.Get("", emptyHandler).Name("d.get")
utils.AssertEqual(t, "/d/:d_id", app.GetRoute("d.get").Path)

d.Post("", emptyHandler).Name("d.post")
utils.AssertEqual(t, "/d/:d_id", app.GetRoute("d.post").Path)

e := app.Group("/e/:eId")
e.Get("", emptyHandler).Name("e.get")
utils.AssertEqual(t, "/e/:eId", app.GetRoute("e.get").Path)

e.Post("", emptyHandler).Name("e.post")
utils.AssertEqual(t, "/e/:eId", app.GetRoute("e.post").Path)

e.Get("f", emptyHandler).Name("e.get.f")
utils.AssertEqual(t, "/e/:eId/f", app.GetRoute("e.get.f").Path)

postGroup := app.Group("/post/:postId")
postGroup.Get("", emptyHandler).Name("post.get")
utils.AssertEqual(t, "/post/:postId", app.GetRoute("post.get").Path)

postGroup.Post("", emptyHandler).Name("post.update")
utils.AssertEqual(t, "/post/:postId", app.GetRoute("post.update").Path)
}

0 comments on commit 5fa0f81

Please sign in to comment.