fix(handler): read the data envelope in the server endpoint tests
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.
This commit is contained in:
+16
-2
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user