mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): wrap JSON responses in envelope
Add response envelope middleware to standardize JSON responses as
`{code,data,message}` with consistent business codes across endpoints.
Update Swagger annotations and tests to reflect the new response shape.
BREAKING CHANGE: API responses are now wrapped in a response envelope; clients must read payloads from `data` and handle `code`/`message` fields.
This commit is contained in:
@@ -2,12 +2,12 @@ package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/middleware"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/driver/sqlite"
|
||||
@@ -29,6 +29,7 @@ func setupAlertTestDB(t *testing.T) *gorm.DB {
|
||||
func setupAlertRouter(db *gorm.DB) *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(middleware.ResponseEnvelope())
|
||||
handler := NewAlertHandler(db)
|
||||
|
||||
r.GET("/admin/alerts/thresholds", handler.GetAlertThresholds)
|
||||
@@ -53,9 +54,7 @@ func TestGetAlertThresholdsDefault(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp AlertThresholdView
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
decodeEnvelope(t, w, &resp)
|
||||
|
||||
// Should return defaults
|
||||
if resp.GlobalQPS != 100 {
|
||||
@@ -91,9 +90,7 @@ func TestUpdateAlertThresholds(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp AlertThresholdView
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
decodeEnvelope(t, w, &resp)
|
||||
|
||||
if resp.GlobalQPS != 500 {
|
||||
t.Errorf("expected GlobalQPS=500, got %d", resp.GlobalQPS)
|
||||
@@ -162,9 +159,7 @@ func TestCreateAlertWithTrafficSpikeType(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp AlertView
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
decodeEnvelope(t, w, &resp)
|
||||
|
||||
if resp.Type != "traffic_spike" {
|
||||
t.Errorf("expected type=traffic_spike, got %s", resp.Type)
|
||||
@@ -203,9 +198,7 @@ func TestListAlertsWithTypeFilter(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp ListAlertsResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
decodeEnvelope(t, w, &resp)
|
||||
|
||||
if resp.Total != 2 {
|
||||
t.Errorf("expected 2 traffic_spike alerts, got %d", resp.Total)
|
||||
@@ -242,9 +235,7 @@ func TestAlertStatsIncludesAllAlerts(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp AlertStats
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
decodeEnvelope(t, w, &resp)
|
||||
|
||||
if resp.Total != 3 {
|
||||
t.Errorf("expected total=3, got %d", resp.Total)
|
||||
|
||||
Reference in New Issue
Block a user