Future self, please forgive me and don't hit me with the baseball bat again!

This commit is contained in:
2025-05-22 16:57:34 -04:00
parent 71ff41d387
commit f0e84818b8
4 changed files with 232 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
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"])
}

View File

@@ -0,0 +1,27 @@
package errors
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestNewNotFoundError(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/notfound", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
err := NewNotFoundError(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusNotFound, rec.Code)
var resp map[string]string
e2 := json.Unmarshal(rec.Body.Bytes(), &resp)
assert.NoError(t, e2)
assert.Equal(t, "Not Found", resp["error"])
}