forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
237 lines (206 loc) · 5.79 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package buffalo
import (
"fmt"
"net/http"
"os"
"testing"
"github.com/gobuffalo/httptest"
"github.com/gobuffalo/logger"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
//testLoggerHook is useful to test whats being logged.
type testLoggerHook struct {
errors []*logrus.Entry
}
func (lh *testLoggerHook) Fire(entry *logrus.Entry) error {
lh.errors = append(lh.errors, entry)
return nil
}
func (lh *testLoggerHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
}
}
func Test_defaultErrorHandler_SetsContentType(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnauthorized, fmt.Errorf("boom"))
})
w := httptest.New(app)
res := w.HTML("/").Get()
r.Equal(http.StatusUnauthorized, res.Code)
ct := res.Header().Get("content-type")
r.Equal("text/html; charset=utf-8", ct)
}
func Test_defaultErrorHandler_Logger(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnauthorized, fmt.Errorf("boom"))
})
testHook := &testLoggerHook{}
l := logrus.New()
l.SetOutput(os.Stdout)
l.AddHook(testHook)
log := logger.Logrus{
FieldLogger: l,
}
app.Logger = log
w := httptest.New(app)
res := w.HTML("/").Get()
r.Equal(http.StatusUnauthorized, res.Code)
r.Equal(http.StatusUnauthorized, testHook.errors[0].Data["status"])
}
func Test_defaultErrorHandler_JSON_development(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnauthorized, fmt.Errorf("boom"))
})
w := httptest.New(app)
res := w.JSON("/").Get()
r.Equal(http.StatusUnauthorized, res.Code)
ct := res.Header().Get("content-type")
r.Equal("application/json", ct)
b := res.Body.String()
r.Contains(b, `"code":401`)
r.Contains(b, `"error":"boom"`)
r.Contains(b, `"trace":"`)
}
func Test_defaultErrorHandler_XML_development(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnauthorized, fmt.Errorf("boom"))
})
w := httptest.New(app)
res := w.XML("/").Get()
r.Equal(http.StatusUnauthorized, res.Code)
ct := res.Header().Get("content-type")
r.Equal("text/xml", ct)
b := res.Body.String()
r.Contains(b, `<response code="401">`)
r.Contains(b, `<error>boom</error>`)
r.Contains(b, `<trace>`)
r.Contains(b, `</trace>`)
r.Contains(b, `</response>`)
}
func Test_defaultErrorHandler_JSON_production(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.Env = "production"
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnauthorized, fmt.Errorf("boom"))
})
w := httptest.New(app)
res := w.JSON("/").Get()
r.Equal(http.StatusUnauthorized, res.Code)
ct := res.Header().Get("content-type")
r.Equal("application/json", ct)
b := res.Body.String()
r.Contains(b, `"code":401`)
r.Contains(b, fmt.Sprintf(`"error":"%s"`, http.StatusText(http.StatusUnauthorized)))
r.NotContains(b, `"trace":"`)
}
func Test_defaultErrorHandler_XML_production(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.Env = "production"
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnauthorized, fmt.Errorf("boom"))
})
w := httptest.New(app)
res := w.XML("/").Get()
r.Equal(http.StatusUnauthorized, res.Code)
ct := res.Header().Get("content-type")
r.Equal("text/xml", ct)
b := res.Body.String()
r.Contains(b, `<response code="401">`)
r.Contains(b, fmt.Sprintf(`<error>%s</error>`, http.StatusText(http.StatusUnauthorized)))
r.NotContains(b, `<trace>`)
r.NotContains(b, `</trace>`)
r.Contains(b, `</response>`)
}
func Test_defaultErrorHandler_nil_error(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(http.StatusInternalServerError, nil)
})
w := httptest.New(app)
res := w.JSON("/").Get()
r.Equal(http.StatusInternalServerError, res.Code)
}
func Test_PanicHandler(t *testing.T) {
app := New(Options{})
app.GET("/string", func(c Context) error {
panic("string boom")
})
app.GET("/error", func(c Context) error {
panic(fmt.Errorf("error boom"))
})
table := []struct {
path string
expected string
}{
{"/string", "string boom"},
{"/error", "error boom"},
}
const stack = `github.com/gobuffalo/buffalo.Test_PanicHandler`
w := httptest.New(app)
for _, tt := range table {
t.Run(tt.path, func(st *testing.T) {
r := require.New(st)
res := w.HTML(tt.path).Get()
r.Equal(http.StatusInternalServerError, res.Code)
body := res.Body.String()
r.Contains(body, tt.expected)
r.Contains(body, stack)
})
}
}
func Test_defaultErrorMiddleware(t *testing.T) {
r := require.New(t)
app := New(Options{})
var x string
var ok bool
app.ErrorHandlers[http.StatusUnprocessableEntity] = func(code int, err error, c Context) error {
x, ok = c.Value("T").(string)
c.Response().WriteHeader(code)
c.Response().Write([]byte(err.Error()))
return nil
}
app.Use(func(next Handler) Handler {
return func(c Context) error {
c.Set("T", "t")
return c.Error(http.StatusUnprocessableEntity, fmt.Errorf("boom"))
}
})
app.GET("/", func(c Context) error {
return nil
})
w := httptest.New(app)
res := w.HTML("/").Get()
r.Equal(http.StatusUnprocessableEntity, res.Code)
r.True(ok)
r.Equal("t", x)
}
func Test_SetErrorMiddleware(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.ErrorHandlers.Default(func(code int, err error, c Context) error {
res := c.Response()
res.WriteHeader(http.StatusTeapot)
res.Write([]byte("i'm a teapot"))
return nil
})
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnprocessableEntity, fmt.Errorf("boom"))
})
w := httptest.New(app)
res := w.HTML("/").Get()
r.Equal(http.StatusTeapot, res.Code)
r.Equal("i'm a teapot", res.Body.String())
}