-
Notifications
You must be signed in to change notification settings - Fork 8
/
error_test.go
345 lines (333 loc) · 8.43 KB
/
error_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package jsonapi_test
import (
"encoding/json"
"net/http"
"strconv"
"testing"
. "github.com/mfcochauxlaberge/jsonapi"
"github.com/stretchr/testify/assert"
)
func TestError(t *testing.T) {
assert := assert.New(t)
tests := []struct {
name string
err Error
expectedString string
}{
{
name: "empty",
err: func() Error {
e := NewError()
return e
}(),
expectedString: "",
}, {
name: "title",
err: func() Error {
e := NewError()
e.Title = "An error"
return e
}(),
expectedString: "An error",
}, {
name: "detail",
err: func() Error {
e := NewError()
e.Detail = "An error occurred."
return e
}(),
expectedString: "An error occurred.",
}, {
name: "http status code",
err: func() Error {
e := NewError()
e.Status = strconv.Itoa(http.StatusInternalServerError)
return e
}(),
expectedString: "500 Internal Server Error",
}, {
name: "http status code and title",
err: func() Error {
e := NewError()
e.Status = strconv.Itoa(http.StatusInternalServerError)
e.Title = "Internal server error"
return e
}(),
expectedString: "500 Internal Server Error: Internal server error",
}, {
name: "http status code and detail",
err: func() Error {
e := NewError()
e.Status = strconv.Itoa(http.StatusInternalServerError)
e.Detail = "An internal server error occurred."
return e
}(),
expectedString: "500 Internal Server Error: An internal server error occurred.",
},
}
for _, test := range tests {
assert.Equal(test.err.Error(), test.expectedString, test.name)
}
}
func TestErrorConstructors(t *testing.T) {
assert := assert.New(t)
tests := []struct {
name string
err Error
expected string
}{
{
name: "NewError",
err: func() Error {
e := NewError()
return e
}(),
expected: "",
}, {
name: "NewErrBadRequest",
err: func() Error {
e := NewErrBadRequest("bad request", "error detail")
return e
}(),
expected: "400 Bad Request: error detail",
}, {
name: "NewErrMalformedFilterParameter",
err: func() Error {
e := NewErrMalformedFilterParameter("filter")
return e
}(),
expected: "400 Bad Request: " +
"The filter parameter is not a string or a valid JSON object.",
}, {
name: "NewErrInvalidPageNumberParameter",
err: func() Error {
e := NewErrInvalidPageNumberParameter("9")
return e
}(),
expected: "400 Bad Request: " +
"The page number parameter is not positive integer (including 0).",
}, {
name: "NewErrInvalidPageSizeParameter",
err: func() Error {
e := NewErrInvalidPageSizeParameter("9")
return e
}(),
expected: "400 Bad Request: " +
"The page size parameter is not positive integer (including 0).",
}, {
name: "NewErrInvalidFieldValueInBody",
err: func() Error {
e := NewErrInvalidFieldValueInBody("field", "bad", "int")
return e
}(),
expected: "400 Bad Request: " +
"The field value is invalid for the expected type.",
}, {
name: "NewErrDuplicateFieldInFieldsParameter",
err: func() Error {
e := NewErrDuplicateFieldInFieldsParameter("type", "field")
return e
}(),
expected: "400 Bad Request: " +
"The fields parameter contains the same field more than once.",
}, {
name: "NewErrMissingDataMember",
err: func() Error {
e := NewErrMissingDataMember()
return e
}(),
expected: "400 Bad Request: Missing data top-level member in payload.",
}, {
name: "NewErrUnknownFieldInBody",
err: func() Error {
e := NewErrUnknownFieldInBody("type", "field")
return e
}(),
expected: "400 Bad Request: \"field\" is not a known field.",
}, {
name: "NewErrUnknownFieldInURL",
err: func() Error {
e := NewErrUnknownFieldInURL("field")
return e
}(),
expected: "400 Bad Request: \"field\" is not a known field.",
}, {
name: "NewErrUnknownParameter",
err: func() Error {
e := NewErrUnknownParameter("param")
return e
}(),
expected: "400 Bad Request: \"param\" is not a known parameter.",
}, {
name: "NewErrUnknownRelationshipInPath",
err: func() Error {
e := NewErrUnknownRelationshipInPath("type", "rel", "path")
return e
}(),
expected: "400 Bad Request: \"rel\" is not a relationship of \"type\".",
}, {
name: "NewErrUnknownTypeInURL",
err: func() Error {
e := NewErrUnknownTypeInURL("type")
return e
}(),
expected: "400 Bad Request: \"type\" is not a known type.",
}, {
name: "NewErrUnknownFieldInFilterParameter",
err: func() Error {
e := NewErrUnknownFieldInFilterParameter("field")
return e
}(),
expected: "400 Bad Request: \"field\" is not a known field.",
}, {
name: "NewErrUnknownOperatorInFilterParameter",
err: func() Error {
e := NewErrUnknownOperatorInFilterParameter("=>")
return e
}(),
expected: "400 Bad Request: \"=>\" is not a known operator.",
}, {
name: "NewErrInvalidValueInFilterParameter",
err: func() Error {
e := NewErrInvalidValueInFilterParameter("value", "string")
return e
}(),
expected: "400 Bad Request: \"value\" is not a known value.",
}, {
name: "NewErrUnknownCollationInFilterParameter",
err: func() Error {
e := NewErrUnknownCollationInFilterParameter("collation")
return e
}(),
expected: "400 Bad Request: \"collation\" is not a known collation.",
}, {
name: "NewErrUnknownFilterParameterLabel",
err: func() Error {
e := NewErrUnknownFilterParameterLabel("label")
return e
}(),
expected: "400 Bad Request: \"label\" is not a known filter query label.",
}, {
name: "NewErrUnauthorized",
err: func() Error {
e := NewErrUnauthorized()
return e
}(),
expected: "401 Unauthorized: Authentification is required to perform this request.",
}, {
name: "NewErrForbidden",
err: func() Error {
e := NewErrForbidden()
return e
}(),
expected: "403 Forbidden: Permission is required to perform this request.",
}, {
name: "NewErrNotFound",
err: func() Error {
e := NewErrNotFound()
return e
}(),
expected: "404 Not Found: The URI does not exist.",
}, {
name: "NewErrPayloadTooLarge",
err: func() Error {
e := NewErrPayloadTooLarge()
return e
}(),
expected: "413 Request Entity Too Large: That's what she said.",
}, {
name: "NewErrRequestURITooLong",
err: func() Error {
e := NewErrRequestURITooLong()
return e
}(),
expected: "414 Request URI Too Long: URI too long",
}, {
name: "NewErrUnsupportedMediaType",
err: func() Error {
e := NewErrUnsupportedMediaType()
return e
}(),
expected: "415 Unsupported Media Type: Unsupported media type",
}, {
name: "NewErrTooManyRequests",
err: func() Error {
e := NewErrTooManyRequests()
return e
}(),
expected: "429 Too Many Requests: Too many requests",
}, {
name: "NewErrRequestHeaderFieldsTooLarge",
err: func() Error {
e := NewErrRequestHeaderFieldsTooLarge()
return e
}(),
expected: "431 Request Header Fields Too Large: Header fields too large",
}, {
name: "NewErrInternalServerError",
err: func() Error {
e := NewErrInternalServerError()
return e
}(),
expected: "500 Internal Server Error: Internal server error",
}, {
name: "NewErrServiceUnavailable",
err: func() Error {
e := NewErrServiceUnavailable()
return e
}(),
expected: "503 Service Unavailable: Service unavailable",
}, {
name: "NewErrNotImplemented",
err: func() Error {
e := NewErrNotImplemented()
return e
}(),
expected: "501 Not Implemented: Not Implemented",
},
}
for _, test := range tests {
assert.Equal(test.expected, test.err.Error(), test.name)
}
}
func TestErrorMarshalJSON(t *testing.T) {
assert := assert.New(t)
jaerr := Error{
ID: "c1897530-fdf5-4a42-88fb-1c1c4bd0962f",
Code: "Code",
Status: "Status",
Title: "Title",
Detail: "Detail",
Links: map[string]string{
"link": "http://example.com",
},
Source: map[string]any{
"parameter": "param",
"pointer": "/data",
},
Meta: map[string]any{
"meta": 123,
},
}
payload, err := json.Marshal(jaerr)
assert.NoError(err)
assert.JSONEq(string(payload), `
{
"code": "Code",
"detail": "Detail",
"id": "c1897530-fdf5-4a42-88fb-1c1c4bd0962f",
"links": {
"link": "http://example.com"
},
"meta": {
"meta": 123
},
"source": {
"parameter": "param",
"pointer": "/data"
},
"status": "Status",
"title": "Title"
}
`)
}