[#2072] registered RemoveTrailingSlash middleware only for the /api/* routes
This commit is contained in:
parent
e735d9d21b
commit
254e691e92
|
@ -36,8 +36,8 @@ func InitApi(app core.App) (*echo.Echo, error) {
|
||||||
// default middlewares
|
// default middlewares
|
||||||
e.Pre(middleware.RemoveTrailingSlashWithConfig(middleware.RemoveTrailingSlashConfig{
|
e.Pre(middleware.RemoveTrailingSlashWithConfig(middleware.RemoveTrailingSlashConfig{
|
||||||
Skipper: func(c echo.Context) bool {
|
Skipper: func(c echo.Context) bool {
|
||||||
// ignore Admin UI route(s)
|
// enable by default only for the API routes
|
||||||
return strings.HasPrefix(c.Request().URL.Path, trailedAdminPath)
|
return !strings.HasPrefix(c.Request().URL.Path, "/api/")
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
e.Use(middleware.Recover())
|
e.Use(middleware.Recover())
|
||||||
|
|
|
@ -136,3 +136,76 @@ func TestCustomRoutesAndErrorsHandling(t *testing.T) {
|
||||||
scenario.Test(t)
|
scenario.Test(t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoveTrailingSlashMiddleware(t *testing.T) {
|
||||||
|
scenarios := []tests.ApiScenario{
|
||||||
|
{
|
||||||
|
Name: "non /api/* route (exact match)",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Url: "/custom",
|
||||||
|
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
||||||
|
e.AddRoute(echo.Route{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/custom",
|
||||||
|
Handler: func(c echo.Context) error {
|
||||||
|
return c.String(200, "test123")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContent: []string{"test123"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "non /api/* route (with trailing slash)",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Url: "/custom/",
|
||||||
|
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
||||||
|
e.AddRoute(echo.Route{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/custom",
|
||||||
|
Handler: func(c echo.Context) error {
|
||||||
|
return c.String(200, "test123")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
ExpectedStatus: 404,
|
||||||
|
ExpectedContent: []string{`"data":{}`},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/api/* route (exact match)",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Url: "/api/custom",
|
||||||
|
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
||||||
|
e.AddRoute(echo.Route{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/api/custom",
|
||||||
|
Handler: func(c echo.Context) error {
|
||||||
|
return c.String(200, "test123")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContent: []string{"test123"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "/api/* route (with trailing slash)",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Url: "/api/custom/",
|
||||||
|
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
||||||
|
e.AddRoute(echo.Route{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/api/custom",
|
||||||
|
Handler: func(c echo.Context) error {
|
||||||
|
return c.String(200, "test123")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
ExpectedContent: []string{"test123"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, scenario := range scenarios {
|
||||||
|
scenario.Test(t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue