Files
sxchnge/backend/pkg/api/http/response/response_test.go
T

43 lines
1.3 KiB
Go

package response
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.sendico.io/tech/sxchnge/bff/pkg/merrors"
"git.sendico.io/tech/sxchnge/bff/pkg/mservice"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestAutoMapsServiceUnavailable(t *testing.T) {
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/health", nil)
recorder := httptest.NewRecorder()
Auto(zap.NewNop(), mservice.Type("test"), merrors.ServiceUnavailable("dependency down")).ServeHTTP(recorder, req)
require.Equal(t, http.StatusServiceUnavailable, recorder.Code)
require.True(t, strings.Contains(recorder.Body.String(), "service_unavailable"))
}
func TestErrorWithDetailsPreservesStructuredPayload(t *testing.T) {
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/actions", nil)
recorder := httptest.NewRecorder()
ErrorWithDetails(zap.NewNop(), mservice.Type("test"), http.StatusConflict, "conflict", map[string]string{
"operation_ref": "operation-1",
}).ServeHTTP(recorder, req)
var payload struct {
Data struct {
Details map[string]string `json:"details"`
} `json:"data"`
}
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &payload))
require.Equal(t, "operation-1", payload.Data.Details["operation_ref"])
}