File size: 1,347 Bytes
04f1444 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package apierrors
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/openmeterio/openmeter/openmeter/meter"
"github.com/openmeterio/openmeter/openmeter/productcatalog/feature"
)
func TestGenericErrorEncoder(t *testing.T) {
encoder := GenericErrorEncoder()
t.Run("FeatureNotFoundError returns 404", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
err := &feature.FeatureNotFoundError{ID: "feat-123"}
handled := encoder(r.Context(), err, w, r)
require.True(t, handled)
assert.Equal(t, http.StatusNotFound, w.Code)
var body map[string]interface{}
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
assert.Contains(t, body["detail"], "feature not found: feat-123")
})
t.Run("MeterNotFoundError returns 404", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
err := meter.NewMeterNotFoundError("meter-456")
handled := encoder(r.Context(), err, w, r)
require.True(t, handled)
assert.Equal(t, http.StatusNotFound, w.Code)
var body map[string]interface{}
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
assert.Contains(t, body["detail"], "meter not found: meter-456")
})
}
|