-
Notifications
You must be signed in to change notification settings - Fork 45
/
errorhandler_test.go
152 lines (143 loc) · 3.98 KB
/
errorhandler_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
package binding
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
var errorTestCases = []errorTestCase{
{
description: "No errors",
errors: Errors{},
expected: errorTestResult{
statusCode: http.StatusOK,
},
},
{
description: "Deserialization error",
errors: Errors{
{
Classification: DeserializationError,
Message: "Some parser error here",
},
},
expected: errorTestResult{
statusCode: http.StatusBadRequest,
contentType: jsonContentType,
body: `[{"classification":"DeserializationError","message":"Some parser error here"}]`,
},
},
{
description: "Content-Type error",
errors: Errors{
{
Classification: ContentTypeError,
Message: "Empty Content-Type",
},
},
expected: errorTestResult{
statusCode: http.StatusUnsupportedMediaType,
contentType: jsonContentType,
body: `[{"classification":"ContentTypeError","message":"Empty Content-Type"}]`,
},
},
{
description: "Requirement error",
errors: Errors{
{
FieldNames: []string{"some_field"},
Classification: RequiredError,
Message: "Required",
},
},
expected: errorTestResult{
statusCode: StatusUnprocessableEntity,
contentType: jsonContentType,
body: `[{"fieldNames":["some_field"],"classification":"RequiredError","message":"Required"}]`,
},
},
{
description: "Bad header error",
errors: Errors{
{
Classification: "HeaderError",
Message: "The X-Something header must be specified",
},
},
expected: errorTestResult{
statusCode: StatusUnprocessableEntity,
contentType: jsonContentType,
body: `[{"classification":"HeaderError","message":"The X-Something header must be specified"}]`,
},
},
{
description: "Custom field error",
errors: Errors{
{
FieldNames: []string{"month", "year"},
Classification: "DateError",
Message: "The month and year must be in the future",
},
},
expected: errorTestResult{
statusCode: StatusUnprocessableEntity,
contentType: jsonContentType,
body: `[{"fieldNames":["month","year"],"classification":"DateError","message":"The month and year must be in the future"}]`,
},
},
{
description: "Multiple errors",
errors: Errors{
{
FieldNames: []string{"foo"},
Classification: RequiredError,
Message: "Required",
},
{
FieldNames: []string{"foo"},
Classification: "LengthError",
Message: "The length of the 'foo' field is too short",
},
},
expected: errorTestResult{
statusCode: StatusUnprocessableEntity,
contentType: jsonContentType,
body: `[{"fieldNames":["foo"],"classification":"RequiredError","message":"Required"},{"fieldNames":["foo"],"classification":"LengthError","message":"The length of the 'foo' field is too short"}]`,
},
},
}
func TestErrorHandler(t *testing.T) {
for _, testCase := range errorTestCases {
performErrorTest(t, testCase)
}
}
func performErrorTest(t *testing.T, testCase errorTestCase) {
httpRecorder := httptest.NewRecorder()
ErrorHandler(testCase.errors, httpRecorder)
actualBody, _ := ioutil.ReadAll(httpRecorder.Body)
actualContentType := httpRecorder.Header().Get("Content-Type")
if httpRecorder.Code != testCase.expected.statusCode {
t.Errorf("For '%s': expected status code %d but got %d instead",
testCase.description, testCase.expected.statusCode, httpRecorder.Code)
}
if actualContentType != testCase.expected.contentType {
t.Errorf("For '%s': expected content-type '%s' but got '%s' instead",
testCase.description, testCase.expected.contentType, actualContentType)
}
if string(actualBody) != testCase.expected.body {
t.Errorf("For '%s': expected body to be '%s' but got '%s' instead",
testCase.description, testCase.expected.body, actualBody)
}
}
type (
errorTestCase struct {
description string
errors Errors
expected errorTestResult
}
errorTestResult struct {
statusCode int
contentType string
body string
}
)