mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
Update API handler tests to expect numeric `code`, `success` messages, and new envelope fields (`trace_id`, `details`), matching recent response envelope changes.
30 lines
686 B
Go
30 lines
686 B
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
type testEnvelope struct {
|
|
Code int `json:"code"`
|
|
Data json.RawMessage `json:"data"`
|
|
Message string `json:"message"`
|
|
TraceID string `json:"trace_id"`
|
|
Details any `json:"details,omitempty"`
|
|
}
|
|
|
|
func decodeEnvelope(t *testing.T, rr *httptest.ResponseRecorder, out any) testEnvelope {
|
|
t.Helper()
|
|
var env testEnvelope
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &env); err != nil {
|
|
t.Fatalf("decode envelope: %v", err)
|
|
}
|
|
if out != nil {
|
|
if err := json.Unmarshal(env.Data, out); err != nil {
|
|
t.Fatalf("decode envelope data: %v", err)
|
|
}
|
|
}
|
|
return env
|
|
}
|