From d736d3a64433385b0fc0189e16a36a2f4facb67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Efe=20=C3=87etin?= Date: Mon, 16 Oct 2023 10:02:53 +0300 Subject: [PATCH] :bug: bug: fix path checking on route naming (#2676) * :bug: bug: fix path checking on route naming * fix several tests * fix several tests --- app.go | 2 +- app_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- listen_test.go | 2 -- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index 5ec5d0dbc7..c298a0ec2c 100644 --- a/app.go +++ b/app.go @@ -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 { diff --git a/app_test.go b/app_test.go index 9e0e617049..d0acc68c6c 100644 --- a/app_test.go +++ b/app_test.go @@ -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() { @@ -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") @@ -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) +} diff --git a/listen_test.go b/listen_test.go index 80f6441301..6a44bf4412 100644 --- a/listen_test.go +++ b/listen_test.go @@ -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() { @@ -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)