package errors import ( "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" ) func TestNewBadRequestError(t *testing.T) { e := echo.New() req := httptest.NewRequest(http.MethodPost, "/", nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) fields := map[string]string{"field1": "error1", "field2": "error2"} err := NewBadRequestError(c, fields) assert.NoError(t, err) assert.Equal(t, http.StatusBadRequest, rec.Code) var resp map[string]interface{} b := json.Unmarshal(rec.Body.Bytes(), &resp) assert.NoError(t, b) assert.Equal(t, "Bad Request", resp["error"]) assert.Equal(t, map[string]interface{}{"field1": "error1", "field2": "error2"}, resp["fields"]) }