synced with master
This commit is contained in:
commit
8e2246113a
|
@ -83,6 +83,11 @@
|
||||||
- (@todo docs) Use a default fetch function that will return all relations in case the fetchFunc argument of `Dao.ExpandRecord()` and `Dao.ExpandRecords()` is `nil`.
|
- (@todo docs) Use a default fetch function that will return all relations in case the fetchFunc argument of `Dao.ExpandRecord()` and `Dao.ExpandRecords()` is `nil`.
|
||||||
|
|
||||||
|
|
||||||
|
## v0.16.9
|
||||||
|
|
||||||
|
- Register the `eagerRequestDataCache` middleware only for the internal `api` group routes to avoid conflicts with custom route handlers ([#2914](https://github.com/pocketbase/pocketbase/issues/2914)).
|
||||||
|
|
||||||
|
|
||||||
## v0.16.8
|
## v0.16.8
|
||||||
|
|
||||||
- Fixed unique validator detailed error message not being returned when camelCase field name is used ([#2868](https://github.com/pocketbase/pocketbase/issues/2868)).
|
- Fixed unique validator detailed error message not being returned when camelCase field name is used ([#2868](https://github.com/pocketbase/pocketbase/issues/2868)).
|
||||||
|
|
|
@ -49,7 +49,6 @@ func InitApi(app core.App) (*echo.Echo, error) {
|
||||||
e.Use(middleware.Recover())
|
e.Use(middleware.Recover())
|
||||||
e.Use(middleware.Secure())
|
e.Use(middleware.Secure())
|
||||||
e.Use(LoadAuthContext(app))
|
e.Use(LoadAuthContext(app))
|
||||||
e.Use(eagerRequestDataCache(app))
|
|
||||||
|
|
||||||
// custom error handler
|
// custom error handler
|
||||||
e.HTTPErrorHandler = func(c echo.Context, err error) {
|
e.HTTPErrorHandler = func(c echo.Context, err error) {
|
||||||
|
@ -121,7 +120,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)
|
||||||
|
|
|
@ -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,74 @@ 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",
|
||||||
|
Method: "GET",
|
||||||
|
Url: "/api/admins",
|
||||||
|
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 {
|
||||||
|
// it is not important whether the route handler return an error since
|
||||||
|
// we just need to ensure that the eagerRequestDataCache was registered
|
||||||
|
next(c)
|
||||||
|
|
||||||
// supported eager cache request methods
|
// ensure that the body was read at least once
|
||||||
supportedMethods := []string{"POST", "PUT", "PATCH", "DELETE"}
|
data := &struct {
|
||||||
for _, m := range supportedMethods {
|
Name string `json:"name"`
|
||||||
scenarios = append(
|
}{}
|
||||||
scenarios,
|
c.Bind(data)
|
||||||
tests.ApiScenario{
|
|
||||||
Name: fmt.Sprintf("[%s] valid cached json body request", m),
|
|
||||||
Method: http.MethodPost,
|
|
||||||
Url: "/custom",
|
|
||||||
Body: strings.NewReader(`{"name":"test123"}`),
|
|
||||||
BeforeTestFunc: func(t *testing.T, app *tests.TestApp, e *echo.Echo) {
|
|
||||||
e.AddRoute(echo.Route{
|
|
||||||
Method: http.MethodPost,
|
|
||||||
Path: "/custom",
|
|
||||||
Handler: func(c echo.Context) error {
|
|
||||||
data := &struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
}{}
|
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
|
||||||
// try to read the body again
|
return nil
|
||||||
r := apis.RequestData(c)
|
}
|
||||||
if v := cast.ToString(r.Data["name"]); v != "test123" {
|
})
|
||||||
t.Fatalf("Expected request data with name %q, got, %q", "test123", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.String(200, data.Name)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
},
|
|
||||||
ExpectedStatus: 200,
|
|
||||||
ExpectedContent: []string{"test123"},
|
|
||||||
},
|
},
|
||||||
)
|
ExpectedStatus: 200,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "api group route with supported eager cache request method",
|
||||||
|
Method: "POST",
|
||||||
|
Url: "/api/admins",
|
||||||
|
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 {
|
||||||
|
// it is not important whether the route handler return an error since
|
||||||
|
// we just need to ensure that the eagerRequestDataCache was registered
|
||||||
|
next(c)
|
||||||
|
|
||||||
|
// ensure that the body was read at least once
|
||||||
|
data := &struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}{}
|
||||||
|
c.Bind(data)
|
||||||
|
|
||||||
|
// try to read the body again
|
||||||
|
r := apis.RequestData(c)
|
||||||
|
fmt.Println(r)
|
||||||
|
if v := cast.ToString(r.Data["name"]); v != "test123" {
|
||||||
|
t.Fatalf("Expected request data with name %q, got, %q", "test123", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
ExpectedStatus: 200,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, scenario := range scenarios {
|
for _, scenario := range scenarios {
|
||||||
|
|
Loading…
Reference in New Issue