violet/utils/response_test.go

33 lines
911 B
Go
Raw Normal View History

2023-06-04 22:28:48 +01:00
package utils
import (
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestRespondHttpStatus(t *testing.T) {
rec := httptest.NewRecorder()
RespondHttpStatus(rec, http.StatusTeapot)
res := rec.Result()
assert.Equal(t, http.StatusTeapot, res.StatusCode)
assert.Equal(t, "418 I'm a teapot", res.Status)
a, err := io.ReadAll(res.Body)
assert.NoError(t, err)
2023-06-20 17:33:43 +01:00
assert.Equal(t, "418 I'm a teapot\n", string(a))
2023-06-04 22:28:48 +01:00
}
func TestRespondVioletError(t *testing.T) {
rec := httptest.NewRecorder()
RespondVioletError(rec, http.StatusTeapot, "Hidden Error Message")
res := rec.Result()
assert.Equal(t, http.StatusTeapot, res.StatusCode)
assert.Equal(t, "418 I'm a teapot", res.Status)
a, err := io.ReadAll(res.Body)
assert.NoError(t, err)
2023-06-20 17:33:43 +01:00
assert.Equal(t, "418 I'm a teapot\n", string(a))
2023-06-04 22:28:48 +01:00
assert.Equal(t, "Hidden Error Message", res.Header.Get("X-Violet-Error"))
}