-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttperrorhandler_test.go
220 lines (207 loc) · 5.4 KB
/
httperrorhandler_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
package rmhttp
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// ------------------------------------------------------------------------------------------------
// HTTP ERROR HANDLER TESTS
// ------------------------------------------------------------------------------------------------
type CustomError struct{}
func (ce CustomError) Error() string {
return "custom error"
}
// Test_HTTPErrorHandlerMiddleware checks that the expected HTTPErrors are generated by this
// middleware when errors are returned from the handler, or a response status is in the
// error range.
func Test_HTTPErrorHandlerMiddleware(t *testing.T) {
testPattern := "/test"
testBody := "test body"
ErrSentinel := errors.New("sentinel error")
tests := []struct {
name string
expectedCode int
errorExpected bool
route *Route
}{
{
"an HTTPError is created when response status is 400 and no error is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(createTestHandlerFunc(http.StatusBadRequest, testBody, nil)),
),
},
{
"an HTTPError is created when response status is 500 and no error is returned from handler",
http.StatusInternalServerError,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(createTestHandlerFunc(http.StatusInternalServerError, testBody, nil)),
),
},
{
"an HTTPError is not created when response status is 200 and no error is returned from handler",
http.StatusOK,
false,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(createTestHandlerFunc(http.StatusOK, testBody, nil)),
),
},
{
"an HTTPError is created when response status is 400 and an error is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusBadRequest, testBody, errors.New("error!")),
),
),
},
{
"an HTTPError is created when response status is 200 and an error is returned from handler",
http.StatusInternalServerError,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusOK, testBody, errors.New("error!")),
),
),
},
{
"the HTTPError has priority when response status is 200 and an HTTPError is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusOK,
testBody,
NewHTTPError(errors.New("error!"), http.StatusBadRequest),
),
),
),
},
{
"the HTTPError has priority when response status is 400 and an HTTPError is returned from handler",
http.StatusForbidden,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusBadRequest,
testBody,
NewHTTPError(errors.New("error!"), http.StatusForbidden),
),
),
),
},
{
"an HTTPError is created with the correct status code when a registered sentinel error is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusOK,
testBody,
ErrSentinel,
),
),
),
},
{
"an HTTPError is created with the correct status code when a registered custom error is returned from handler",
http.StatusForbidden,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusOK,
testBody,
CustomError{},
),
),
),
},
{
"an HTTPError is created with the correct status code when a registered sentinel error is wrapped and returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusOK,
testBody,
fmt.Errorf("wrapped err: %w", ErrSentinel),
),
),
),
},
{
"an HTTPError is created with the correct status code when a registered custom error is wrapped and returned from handler",
http.StatusForbidden,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusOK,
testBody,
fmt.Errorf("wrapped err: %w", CustomError{}),
),
),
),
},
}
registeredErrors := map[error]int{
ErrSentinel: 400,
CustomError{}: 403,
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.route.Handler = applyMiddleware(
test.route.Handler,
[]MiddlewareFunc{HTTPErrorHandlerMiddleware(registeredErrors)},
)
url := fmt.Sprintf("http://%s%s", testAddress, testPattern)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Errorf("failed to create request: %v", err)
}
w := httptest.NewRecorder()
requestErr := test.route.Handler.ServeHTTPWithError(w, req)
res := w.Result()
defer res.Body.Close()
assert.Equal(t, test.expectedCode, res.StatusCode, "they should be equal")
if test.errorExpected {
HTTPErr, ok := requestErr.(HTTPError)
if !ok {
t.Fatal("cannot convert error to HTTPError")
}
assert.IsType(t, HTTPError{}, requestErr, "they should be equal")
assert.Equal(t, test.expectedCode, HTTPErr.Code, "they should be equal")
} else {
assert.NoError(t, requestErr, "it should be nil")
}
})
}
}