violet/error-pages/error-pages_test.go

65 lines
1.6 KiB
Go
Raw Normal View History

2023-06-03 19:33:06 +01:00
package error_pages
import (
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
2023-06-04 22:28:48 +01:00
"testing/fstest"
2023-06-03 19:33:06 +01:00
)
func TestErrorPages_ServeError(t *testing.T) {
errorPages := New(nil)
rec := httptest.NewRecorder()
errorPages.ServeError(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)
assert.Equal(t, "418 I'm a teapot\n\n", string(a))
rec = httptest.NewRecorder()
errorPages.ServeError(rec, 469)
res = rec.Result()
assert.Equal(t, 469, res.StatusCode)
assert.Equal(t, "469 ", res.Status)
a, err = io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, "469 Unknown Error Code\n\n", string(a))
}
2023-06-04 22:28:48 +01:00
func TestErrorPagesWithCustom(t *testing.T) {
fs := fstest.MapFS{
"418.html": {
Data: []byte("418 Custom Error Page\n"),
},
"469.html": {
Data: []byte("469 Custom Error Page\n"),
},
}
errorPages := New(fs)
assert.NoError(t, errorPages.internalCompile(errorPages.m))
rec := httptest.NewRecorder()
errorPages.ServeError(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)
assert.Equal(t, "418 Custom Error Page\n", string(a))
rec = httptest.NewRecorder()
errorPages.ServeError(rec, 469)
res = rec.Result()
assert.Equal(t, 469, res.StatusCode)
assert.Equal(t, "469 ", res.Status)
a, err = io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, "469 Custom Error Page\n", string(a))
}