forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
101 lines (73 loc) · 2.43 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"go.goblog.app/app/pkgs/contenttype"
)
func Test_errors(t *testing.T) {
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
_ = app.initConfig(false)
app.initMarkdown()
app.initSessions()
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)
})
}