The SPA catch-all answered unknown /api paths with the index page and status 200, so the client SDK failed to parse the body. A catch-all per HTTP method answers them with a JSON 404 in the error shape of PocketBase; the routes registered before keep precedence. The comment on DataFields no longer claims the fields of the read-only collections were client writable.
63 lines
2.8 KiB
Go
63 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/pocketbase/pocketbase/tests"
|
|
|
|
"github.com/wotra/wotra/internal/tracking"
|
|
)
|
|
|
|
func TestUnknownApiRoutesAnswerJSON(t *testing.T) {
|
|
const notFound = `{"data":{},"message":"Not found.","status":404}`
|
|
cases := []struct {
|
|
name string
|
|
method string
|
|
url string
|
|
status int
|
|
content []string
|
|
absent []string
|
|
}{
|
|
{name: "GET", method: http.MethodGet, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}},
|
|
{name: "POST", method: http.MethodPost, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}},
|
|
{name: "PUT", method: http.MethodPut, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}},
|
|
{name: "PATCH", method: http.MethodPatch, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}},
|
|
{name: "DELETE", method: http.MethodDelete, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}},
|
|
{name: "OPTIONS", method: http.MethodOptions, url: "/api/does-not-exist", status: http.StatusNotFound, content: []string{notFound}},
|
|
{name: "HEAD", method: http.MethodHead, url: "/api/does-not-exist", status: http.StatusNotFound},
|
|
{name: "nested path", method: http.MethodGet, url: "/api/does-not-exist/children", status: http.StatusNotFound, content: []string{notFound}},
|
|
// The routes registered before keep precedence over the catch-all.
|
|
{name: "PocketBase route", method: http.MethodGet, url: "/api/health", status: http.StatusOK,
|
|
content: []string{`"message":"API is healthy."`}, absent: []string{`"Not found."`}},
|
|
{name: "collection route", method: http.MethodGet, url: "/api/collections/entries/records", status: http.StatusOK,
|
|
absent: []string{`"Not found."`}},
|
|
{name: "app route", method: http.MethodGet, url: "/api/export/entries.csv", status: http.StatusUnauthorized,
|
|
content: []string{`"status":401`}, absent: []string{`"Not found."`}},
|
|
// A method the app route does not have is still an unknown route.
|
|
{name: "app route with another method", method: http.MethodGet, url: "/api/days/2026-05-11/close", status: http.StatusNotFound,
|
|
content: []string{notFound}},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
app, err := tests.NewTestApp()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(app.Cleanup)
|
|
tracking.Register(app)
|
|
registerUI(app)
|
|
(&tests.ApiScenario{
|
|
Name: c.name,
|
|
Method: c.method,
|
|
URL: c.url,
|
|
ExpectedStatus: c.status,
|
|
ExpectedContent: c.content,
|
|
NotExpectedContent: c.absent,
|
|
TestAppFactory: func(testing.TB) *tests.TestApp { return app },
|
|
DisableTestAppCleanup: true,
|
|
}).Test(t)
|
|
})
|
|
}
|
|
}
|