🐛 bug: fix path checking on route naming (#2676)

* 🐛 bug: fix path checking on route naming

* fix several tests

* fix several tests
pull/2679/head
M. Efe Çetin 2023-10-16 10:02:53 +03:00 committed by GitHub
parent cb89cce4ca
commit d736d3a644
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 5 deletions

2
app.go
View File

@ -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 {

View File

@ -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)
}

View File

@ -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)