Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [Bug]: Naming of routes works wrong after mount #2688 #2689

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,79 @@ func Test_App_UseMountedErrorHandlerForBestPrefixMatch(t *testing.T) {
utils.AssertEqual(t, "hi, i'm a custom sub sub fiber error", string(b), "Third fiber Response body")
}

// go test -run Test_Mount_Route_Names
func Test_Mount_Route_Names(t *testing.T) {
// create sub-app with 2 handlers:
subApp1 := New()
subApp1.Get("/users", func(c *Ctx) error {
url, err := c.GetRouteURL("add-user", Map{})
utils.AssertEqual(t, err, nil)
utils.AssertEqual(t, url, "/app1/users", "handler: app1.add-user") // the prefix is /app1 because of the mount
// if subApp1 is not mounted, expected url just /users
return nil
}).Name("get-users")
subApp1.Post("/users", func(c *Ctx) error {
route := c.App().GetRoute("get-users")
utils.AssertEqual(t, route.Method, MethodGet, "handler: app1.get-users method")
utils.AssertEqual(t, route.Path, "/app1/users", "handler: app1.get-users path")
return nil
}).Name("add-user")

// create sub-app with 2 handlers inside a group:
subApp2 := New()
app2Grp := subApp2.Group("/users").Name("users.")
app2Grp.Get("", nil).Name("get")
app2Grp.Post("", nil).Name("add")

// put both sub-apps into root app
rootApp := New()
_ = rootApp.Mount("/app1", subApp1)
_ = rootApp.Mount("/app2", subApp2)

rootApp.startupProcess()

// take route directly from sub-app
route := subApp1.GetRoute("get-users")
utils.AssertEqual(t, route.Method, MethodGet)
utils.AssertEqual(t, route.Path, "/users")

route = subApp1.GetRoute("add-user")
utils.AssertEqual(t, route.Method, MethodPost)
utils.AssertEqual(t, route.Path, "/users")

// take route directly from sub-app with group
route = subApp2.GetRoute("users.get")
utils.AssertEqual(t, route.Method, MethodGet)
utils.AssertEqual(t, route.Path, "/users")

route = subApp2.GetRoute("users.add")
utils.AssertEqual(t, route.Method, MethodPost)
utils.AssertEqual(t, route.Path, "/users")

// take route from root app (using names of sub-apps)
route = rootApp.GetRoute("add-user")
utils.AssertEqual(t, route.Method, MethodPost)
utils.AssertEqual(t, route.Path, "/app1/users")

route = rootApp.GetRoute("users.add")
utils.AssertEqual(t, route.Method, MethodPost)
utils.AssertEqual(t, route.Path, "/app2/users")

// GetRouteURL inside handler
req := httptest.NewRequest(MethodGet, "/app1/users", nil)
resp, err := rootApp.Test(req)

utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")

// ctx.App().GetRoute() inside handler
req = httptest.NewRequest(MethodPost, "/app1/users", nil)
resp, err = rootApp.Test(req)

utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
}

// go test -run Test_Ctx_Render_Mount
func Test_Ctx_Render_Mount(t *testing.T) {
t.Parallel()
Expand Down
5 changes: 3 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Router interface {

// Route is a struct that holds all metadata for each registered handler.
type Route struct {
// always keep in sync with the copy method "app.copyRoute"
// ### important: always keep in sync with the copy method "app.copyRoute" ###
// Data for routing
pos uint32 // Position in stack -> important for the sort of the matched routes
use bool // USE matches path prefixes
Expand Down Expand Up @@ -214,13 +214,14 @@ func (*App) copyRoute(route *Route) *Route {
// Path data
path: route.path,
routeParser: route.routeParser,
Params: route.Params,

// misc
pos: route.pos,

// Public data
Path: route.Path,
Params: route.Params,
Name: route.Name,
Method: route.Method,
Handlers: route.Handlers,
}
Expand Down