diff --git a/group_test.go b/group_test.go index 56b2a465f..45ea90430 100644 --- a/group_test.go +++ b/group_test.go @@ -982,3 +982,16 @@ func TestGroup_RouteNotFoundUsesRouterConfig(t *testing.T) { assert.Equal(t, "custom-404 POST /v0/*", rec.Body.String()) assert.True(t, middlewareCalled, "group middleware must wrap the auto 404 route") } + +func TestGroupRouteWithoutLeadingSlash(t *testing.T) { + e := New() + g := e.Group("/v1") + + route := g.GET("posts", func(c *Context) error { + return c.NoContent(http.StatusNoContent) + }) + + assert.Equal(t, "/v1/posts", route.Path) + status, _ := request(http.MethodGet, "/v1/posts", e) + assert.Equal(t, http.StatusNoContent, status) +} diff --git a/route.go b/route.go index 2468a8816..ced317511 100644 --- a/route.go +++ b/route.go @@ -45,7 +45,11 @@ func (r Route) ToRouteInfo(params []string) RouteInfo { // WithPrefix recreates Route with added group prefix and group middlewares it is grouped to. func (r Route) WithPrefix(pathPrefix string, middlewares []MiddlewareFunc) Route { - r.Path = pathPrefix + r.Path + if pathPrefix != "" && r.Path != "" && pathPrefix[len(pathPrefix)-1] != '/' && r.Path[0] != '/' { + r.Path = pathPrefix + "/" + r.Path + } else { + r.Path = pathPrefix + r.Path + } if len(middlewares) > 0 { m := make([]MiddlewareFunc, 0, len(middlewares)+len(r.Middlewares))