-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault_test.go
More file actions
56 lines (49 loc) · 1.6 KB
/
default_test.go
File metadata and controls
56 lines (49 loc) · 1.6 KB
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
package restapi
import (
"net/http"
"testing"
)
func TestNewDefaultErrorRegistry_PreRegisteredCodes(t *testing.T) {
t.Parallel()
reg := newDefaultErrorRegistry()
cases := []struct {
code int
status int
msg string
}{
{CodeBadRequest, http.StatusBadRequest, "bad request"},
{CodeUnauthorized, http.StatusUnauthorized, "unauthorized"},
{CodeForbidden, http.StatusForbidden, "forbidden"},
{CodeNotFound, http.StatusNotFound, "resource not found"},
{CodeConflict, http.StatusConflict, "conflict"},
{CodeValidationFailed, http.StatusUnprocessableEntity, "validation failed"},
{CodeTooManyRequests, http.StatusTooManyRequests, "too many requests"},
{CodeUnavailable, http.StatusServiceUnavailable, "service unavailable"},
}
for _, c := range cases {
got, ok := reg.Lookup(c.code)
if !ok {
t.Errorf("code %d: Lookup returned fallback; expected registered entry", c.code)
continue
}
if got.StatusCode != c.status || got.Message != c.msg {
t.Errorf("code %d: got %+v; want StatusCode=%d, Message=%q",
c.code, got, c.status, c.msg)
}
}
// CodeInternal is the fallback, not a registered entry.
if reg.IsRegistered(CodeInternal) {
t.Errorf("CodeInternal must be the fallback, not a registered entry")
}
fb := reg.Fallback()
if fb.StatusCode != http.StatusInternalServerError || fb.ErrorCode != CodeInternal {
t.Errorf("fallback = %+v; want {500, CodeInternal, internal error}", fb)
}
}
func TestDefault_ReturnsPreInitialisedAPI(t *testing.T) {
t.Parallel()
d := Default()
if d == nil || d.registry == nil {
t.Fatal("Default() must return a fully initialised *API")
}
}