From ac5587d0c8ed5a13affd1d4642f4cb7047e26f39 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Mon, 28 Sep 2026 23:03:45 +0800 Subject: [PATCH] fix(handler): read the data envelope in the server endpoint tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TestServerGetInfoAll, TestServerGetInfoSingleAndMissing and TestServerGetStatusAll indexed the decoded response by uuid at the top level, but utils.SendSuccessResponse wraps the payload as {success, message?, data:{...}} — the shape the console reads as response.data[uuid] (frontend/src/api/index.js). The uuid lookups therefore always missed, and unmarshalling the nil raw message surfaced as "unexpected end of JSON input". decodeResponse now decodes the envelope, asserts it is a success carrying a data object, and returns that object. The handlers are unchanged: they already return the documented shape. --- handler/server_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/handler/server_test.go b/handler/server_test.go index e5480ec..68a5379 100644 --- a/handler/server_test.go +++ b/handler/server_test.go @@ -51,13 +51,27 @@ func setupAdminToken() { utils.AddToken("test-admin-token", 1, "admin", "tester") } +// decodeResponse decodes a success envelope and returns its data object, which +// the getInfo and getStatus handlers key by uuid. Responses are wrapped by +// utils.SendSuccessResponse as {success, message?, data:{...}}, the shape the +// console reads as `response.data[uuid]` (see frontend/src/api/index.js), so +// the tests index the returned map by uuid rather than by envelope key. func decodeResponse(t *testing.T, w *httptest.ResponseRecorder) map[string]json.RawMessage { t.Helper() - var body map[string]json.RawMessage + var body struct { + Success bool `json:"success"` + Data map[string]json.RawMessage `json:"data"` + } if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { t.Fatalf("decode response: %v; body=%s", err, w.Body.String()) } - return body + if !body.Success { + t.Fatalf("response is not a success envelope: %s", w.Body.String()) + } + if body.Data == nil { + t.Fatalf("response has no data object: %s", w.Body.String()) + } + return body.Data } func TestServerGetInfoAll(t *testing.T) {