58 lines
1.9 KiB
Go
58 lines
1.9 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 TestAutoMapsDocumentsReportOwned(t *testing.T) {
|
|
req := httptest.NewRequestWithContext(context.Background(), http.MethodPut, "/reports", nil)
|
|
recorder := httptest.NewRecorder()
|
|
|
|
Auto(zap.NewNop(), mservice.Type("documents_report"), merrors.DocumentsReportOwned("report has an active payout")).ServeHTTP(recorder, req)
|
|
|
|
var payload struct {
|
|
Data ErrorResponse `json:"data"`
|
|
}
|
|
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &payload))
|
|
require.Equal(t, http.StatusConflict, recorder.Code)
|
|
require.Equal(t, "documents_report_owned", payload.Data.Error)
|
|
require.Equal(t, "report has an active payout", payload.Data.Details)
|
|
}
|
|
|
|
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"])
|
|
}
|