GoBlog/errors_test.go

113 lines
2.6 KiB
Go
Raw Normal View History

2021-06-19 06:37:16 +00:00
package main
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"git.jlel.se/jlelse/GoBlog/pkgs/contenttype"
"github.com/stretchr/testify/assert"
)
func Test_errors(t *testing.T) {
app := &goBlog{
cfg: &config{
Server: &configServer{
PublicAddress: "https://example.com",
},
Blogs: map[string]*configBlog{
"en": {
Lang: "en",
},
},
DefaultBlog: "en",
User: &configUser{},
},
}
app.initMarkdown()
_ = app.initTemplateStrings()
_ = app.initRendering()
t.Run("Test 404, no HTML", func(t *testing.T) {
h := http.HandlerFunc(app.serve404)
req := httptest.NewRequest(http.MethodGet, "/abc", nil)
req.Header.Set("Accept", contenttype.JSON)
rec := httptest.NewRecorder()
h(rec, req)
res := rec.Result()
resBody, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
resString := string(resBody)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.Contains(t, resString, "not found")
assert.Contains(t, res.Header.Get("Content-Type"), "text/plain")
})
t.Run("Test 404, HTML", func(t *testing.T) {
h := http.HandlerFunc(app.serve404)
req := httptest.NewRequest(http.MethodGet, "/abc", nil)
req.Header.Set("Accept", contenttype.HTML)
rec := httptest.NewRecorder()
h(rec, req)
res := rec.Result()
resBody, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
resString := string(resBody)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.Contains(t, resString, "not found")
assert.Contains(t, res.Header.Get("Content-Type"), contenttype.HTML)
})
t.Run("Test Method Not Allowed, no HTML", func(t *testing.T) {
h := http.HandlerFunc(app.serveNotAllowed)
req := httptest.NewRequest(http.MethodGet, "/abc", nil)
req.Header.Set("Accept", contenttype.JSON)
rec := httptest.NewRecorder()
h(rec, req)
res := rec.Result()
resBody, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
resString := string(resBody)
assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode)
assert.Contains(t, resString, "Method Not Allowed")
assert.Contains(t, res.Header.Get("Content-Type"), "text/plain")
})
t.Run("Test Method Not Allowed", func(t *testing.T) {
h := http.HandlerFunc(app.serveNotAllowed)
req := httptest.NewRequest(http.MethodGet, "/abc", nil)
req.Header.Set("Accept", contenttype.HTML)
rec := httptest.NewRecorder()
h(rec, req)
res := rec.Result()
resBody, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
resString := string(resBody)
assert.Equal(t, http.StatusMethodNotAllowed, res.StatusCode)
assert.Contains(t, resString, "Method Not Allowed")
assert.Contains(t, res.Header.Get("Content-Type"), contenttype.HTML)
})
}