[#2914] register the eagerRequestDataCache middleware only for the api grroup to avoid conflicts with custom routes
This commit is contained in:
parent
c3844250e8
commit
f0bcffec8b
|
@ -104,7 +104,7 @@ func InitApi(app core.App) (*echo.Echo, error) {
|
||||||
bindStaticAdminUI(app, e)
|
bindStaticAdminUI(app, e)
|
||||||
|
|
||||||
// default routes
|
// default routes
|
||||||
api := e.Group("/api")
|
api := e.Group("/api", eagerRequestDataCache(app))
|
||||||
bindSettingsApi(app, api)
|
bindSettingsApi(app, api)
|
||||||
bindAdminApi(app, api)
|
bindAdminApi(app, api)
|
||||||
bindCollectionApi(app, api)
|
bindCollectionApi(app, api)
|
||||||
|
@ -126,10 +126,6 @@ func InitApi(app core.App) (*echo.Echo, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// note: it is after the OnBeforeServe hook to ensure that the implicit
|
|
||||||
// cache is after any user custom defined middlewares
|
|
||||||
e.Use(eagerRequestDataCache(app))
|
|
||||||
|
|
||||||
// catch all any route
|
// catch all any route
|
||||||
api.Any("/*", func(c echo.Context) error {
|
api.Any("/*", func(c echo.Context) error {
|
||||||
return echo.ErrNotFound
|
return echo.ErrNotFound
|
||||||
|
|
|
@ -214,15 +214,16 @@ func TestRemoveTrailingSlashMiddleware(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEagerRequestDataCache(t *testing.T) {
|
func TestEagerRequestDataCache(t *testing.T) {
|
||||||
|
|
||||||
scenarios := []tests.ApiScenario{
|
scenarios := []tests.ApiScenario{
|
||||||
{
|
{
|
||||||
Name: "[UNKNOWN] unsupported eager cached request method",
|
Name: "custom non-api group route",
|
||||||
Method: "UNKNOWN",
|
Method: "POST",
|
||||||
Url: "/custom",
|
Url: "/custom",
|
||||||
Body: strings.NewReader(`{"name":"test123"}`),
|
Body: strings.NewReader(`{"name":"test123"}`),
|
||||||
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
||||||
e.AddRoute(echo.Route{
|
e.AddRoute(echo.Route{
|
||||||
Method: "UNKNOWN",
|
Method: "POST",
|
||||||
Path: "/custom",
|
Path: "/custom",
|
||||||
Handler: func(c echo.Context) error {
|
Handler: func(c echo.Context) error {
|
||||||
data := &struct {
|
data := &struct {
|
||||||
|
@ -240,52 +241,75 @@ func TestEagerRequestDataCache(t *testing.T) {
|
||||||
t.Fatalf("Expected empty request data body, got, %v", r.Data)
|
t.Fatalf("Expected empty request data body, got, %v", r.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.String(200, data.Name)
|
return c.NoContent(200)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContent: []string{"test123"},
|
|
||||||
},
|
},
|
||||||
}
|
{
|
||||||
|
Name: "api group route with unsupported eager cache request method",
|
||||||
// supported eager cache request methods
|
Method: "GET",
|
||||||
supportedMethods := []string{"POST", "PUT", "PATCH", "DELETE"}
|
Url: "/api/admins",
|
||||||
for _, m := range supportedMethods {
|
|
||||||
scenarios = append(
|
|
||||||
scenarios,
|
|
||||||
tests.ApiScenario{
|
|
||||||
Name: fmt.Sprintf("[%s] valid cached json body request", m),
|
|
||||||
Method: http.MethodPost,
|
|
||||||
Url: "/custom",
|
|
||||||
Body: strings.NewReader(`{"name":"test123"}`),
|
Body: strings.NewReader(`{"name":"test123"}`),
|
||||||
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
||||||
e.AddRoute(echo.Route{
|
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
Method: http.MethodPost,
|
return func(c echo.Context) error {
|
||||||
Path: "/custom",
|
next(c)
|
||||||
Handler: func(c echo.Context) error {
|
|
||||||
|
// ensure that the body is always read at least once
|
||||||
|
// (bind errors due to eof are not important)
|
||||||
data := &struct {
|
data := &struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}{}
|
}{}
|
||||||
|
c.Bind(data)
|
||||||
|
|
||||||
if err := c.Bind(data); err != nil {
|
// since the unknown method is not eager cache support
|
||||||
return err
|
// it should fail reading the json body twice
|
||||||
|
r := apis.RequestData(c)
|
||||||
|
if v := cast.ToString(r.Data["name"]); v != "" {
|
||||||
|
t.Fatalf("Expected empty request data body, got, %v", r.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "api group route with supported eager cache request method",
|
||||||
|
Method: "POST",
|
||||||
|
Url: "/api/admins",
|
||||||
|
RequestHeaders: map[string]string{
|
||||||
|
"Authorization": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhZG1pbiIsImV4cCI6MjIwODk4NTI2MX0.M1m--VOqGyv0d23eeUc0r9xE8ZzHaYVmVFw1VZW6gT8",
|
||||||
|
},
|
||||||
|
Body: strings.NewReader(`{"name":"test123"}`),
|
||||||
|
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
||||||
|
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
return func(c echo.Context) error {
|
||||||
|
next(c)
|
||||||
|
|
||||||
|
// ensure that the body is always read at least once
|
||||||
|
// (bind errors due to eof are not important)
|
||||||
|
data := &struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}{}
|
||||||
|
c.Bind(data)
|
||||||
|
|
||||||
// try to read the body again
|
// try to read the body again
|
||||||
r := apis.RequestData(c)
|
r := apis.RequestData(c)
|
||||||
|
fmt.Println(r)
|
||||||
if v := cast.ToString(r.Data["name"]); v != "test123" {
|
if v := cast.ToString(r.Data["name"]); v != "test123" {
|
||||||
t.Fatalf("Expected request data with name %q, got, %q", "test123", v)
|
t.Fatalf("Expected request data with name %q, got, %q", "test123", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.String(200, data.Name)
|
return nil
|
||||||
},
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
ExpectedStatus: 200,
|
ExpectedStatus: 200,
|
||||||
ExpectedContent: []string{"test123"},
|
|
||||||
},
|
},
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, scenario := range scenarios {
|
for _, scenario := range scenarios {
|
||||||
|
|
Loading…
Reference in New Issue