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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
package httperrors
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// creates a new implementation of http.ResponseWriter that allows the
// casting of values in order to aid testing efforts.
type testResponseWriter struct {
status int
content string
http.ResponseWriter
}
func newTestResponseWriter(w http.ResponseWriter) *testResponseWriter {
return &testResponseWriter{0, "", w}
}
func (w *testResponseWriter) Status() int {
return w.status
}
func (w *testResponseWriter) Content() string {
return w.content
}
func (w *testResponseWriter) Header() http.Header {
return w.ResponseWriter.Header()
}
func (w *testResponseWriter) Write(data []byte) (int, error) {
w.content = string(data)
return w.ResponseWriter.Write(data)
}
func (w *testResponseWriter) WriteHeader(statusCode int) {
w.status = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
var (
testingContent = content{
http.StatusNotFound,
"Title",
"533",
"Header test",
"subheader text",
}
)
func TestGenerateemailHTML(t *testing.T) {
actual := generateErrorHTML(testingContent)
assert.Contains(t, actual, testingContent.title)
assert.Contains(t, actual, testingContent.statusString)
assert.Contains(t, actual, testingContent.header)
assert.Contains(t, actual, testingContent.subHeader)
}
func TestServeErrorPage(t *testing.T) {
w := newTestResponseWriter(httptest.NewRecorder())
serveErrorPage(w, testingContent)
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
assert.Equal(t, w.Header().Get("X-Content-Type-Options"), "nosniff")
assert.Equal(t, w.Status(), testingContent.status)
}
func TestServe401(t *testing.T) {
w := newTestResponseWriter(httptest.NewRecorder())
Serve401(w)
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
assert.Equal(t, w.Header().Get("X-Content-Type-Options"), "nosniff")
assert.Equal(t, w.Status(), content401.status)
assert.Contains(t, w.Content(), content401.title)
assert.Contains(t, w.Content(), content401.statusString)
assert.Contains(t, w.Content(), content401.header)
assert.Contains(t, w.Content(), content401.subHeader)
}
func TestServe404(t *testing.T) {
w := newTestResponseWriter(httptest.NewRecorder())
Serve404(w)
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
assert.Equal(t, w.Header().Get("X-Content-Type-Options"), "nosniff")
assert.Equal(t, w.Status(), content404.status)
assert.Contains(t, w.Content(), content404.title)
assert.Contains(t, w.Content(), content404.statusString)
assert.Contains(t, w.Content(), content404.header)
assert.Contains(t, w.Content(), content404.subHeader)
}
func TestServe500(t *testing.T) {
w := newTestResponseWriter(httptest.NewRecorder())
Serve500(w)
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
assert.Equal(t, w.Header().Get("X-Content-Type-Options"), "nosniff")
assert.Equal(t, w.Status(), content500.status)
assert.Contains(t, w.Content(), content500.title)
assert.Contains(t, w.Content(), content500.statusString)
assert.Contains(t, w.Content(), content500.header)
assert.Contains(t, w.Content(), content500.subHeader)
}
func TestServe502(t *testing.T) {
w := newTestResponseWriter(httptest.NewRecorder())
Serve502(w)
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
assert.Equal(t, w.Header().Get("X-Content-Type-Options"), "nosniff")
assert.Equal(t, w.Status(), content502.status)
assert.Contains(t, w.Content(), content502.title)
assert.Contains(t, w.Content(), content502.statusString)
assert.Contains(t, w.Content(), content502.header)
assert.Contains(t, w.Content(), content502.subHeader)
}
|