Skip to content

Commit

Permalink
🐛 bug: fix path checking on route naming (#2676)
Browse files Browse the repository at this point in the history
* 🐛 bug: fix path checking on route naming

* fix several tests

* fix several tests
  • Loading branch information
efectn committed Oct 16, 2023
1 parent cb89cce commit d736d3a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
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
49 changes: 47 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,6 @@ func (invalidView) Render(io.Writer, string, interface{}, ...string) error { pan

// go test -run Test_App_Init_Error_View
func Test_App_Init_Error_View(t *testing.T) {
t.Parallel()
app := New(Config{Views: invalidView{}})

defer func() {
Expand Down Expand Up @@ -1618,7 +1617,6 @@ func Test_App_Server(t *testing.T) {
}

func Test_App_Error_In_Fasthttp_Server(t *testing.T) {
t.Parallel()
app := New()
app.config.ErrorHandler = func(ctx *Ctx, err error) error {
return errors.New("fake error")
Expand Down Expand Up @@ -1860,3 +1858,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)
}
2 changes: 0 additions & 2 deletions listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ func Test_App_Master_Process_Show_Startup_MessageWithAppNameNonAscii(t *testing.
}

func Test_App_print_Route(t *testing.T) {
t.Parallel()
app := New(Config{EnablePrintRoutes: true})
app.Get("/", emptyHandler).Name("routeName")
printRoutesMessage := captureOutput(func() {
Expand All @@ -316,7 +315,6 @@ func Test_App_print_Route(t *testing.T) {
}

func Test_App_print_Route_with_group(t *testing.T) {
t.Parallel()
app := New(Config{EnablePrintRoutes: true})
app.Get("/", emptyHandler)

Expand Down

0 comments on commit d736d3a

Please sign in to comment.