-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
error_test.go
70 lines (60 loc) · 1.83 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
package huma
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// Ensure the default error model satisfies these interfaces.
var _ StatusError = (*ErrorModel)(nil)
var _ ContentTypeFilter = (*ErrorModel)(nil)
func TestError(t *testing.T) {
err := &ErrorModel{
Status: 400,
Detail: "test err",
}
// Add some children.
err.Add(&ErrorDetail{
Message: "test detail",
Location: "body.foo",
Value: "bar",
})
err.Add(fmt.Errorf("plain error"))
// Confirm errors were added.
assert.Equal(t, "test err", err.Error())
assert.Len(t, err.Errors, 2)
assert.Equal(t, "test detail (body.foo: bar)", err.Errors[0].Error())
assert.Equal(t, "plain error", err.Errors[1].Error())
// Ensure problem content types.
assert.Equal(t, "application/problem+json", err.ContentType("application/json"))
assert.Equal(t, "application/problem+cbor", err.ContentType("application/cbor"))
assert.Equal(t, "other", err.ContentType("other"))
}
func TestErrorResponses(t *testing.T) {
// NotModified has a slightly different signature.
assert.Equal(t, 304, Status304NotModified().GetStatus())
for _, item := range []struct {
constructor func(msg string, errs ...error) StatusError
expected int
}{
{Error400BadRequest, 400},
{Error401Unauthorized, 401},
{Error403Forbidden, 403},
{Error404NotFound, 404},
{Error405MethodNotAllowed, 405},
{Error406NotAcceptable, 406},
{Error409Conflict, 409},
{Error410Gone, 410},
{Error412PreconditionFailed, 412},
{Error415UnsupportedMediaType, 415},
{Error422UnprocessableEntity, 422},
{Error429TooManyRequests, 429},
{Error500InternalServerError, 500},
{Error501NotImplemented, 501},
{Error502BadGateway, 502},
{Error503ServiceUnavailable, 503},
{Error504GatewayTimeout, 504},
} {
err := item.constructor("test")
assert.Equal(t, item.expected, err.GetStatus())
}
}