forked from ikonglong/go-apperror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_test.go
More file actions
83 lines (78 loc) · 2.47 KB
/
Copy pathcode_test.go
File metadata and controls
83 lines (78 loc) · 2.47 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
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
package apperror
import "testing"
func TestCodeNoDuplicateValue(t *testing.T) {
seen := map[int]Code{}
for _, c := range AllCodes() {
if dup, ok := seen[c.Value()]; ok {
t.Fatalf("Code value duplication found: %s and %s share value %d",
dup.Name(), c.Name(), c.Value())
}
seen[c.Value()] = c
}
}
func TestCodeStringRepresentation(t *testing.T) {
cases := map[Code]string{
CodeOK: "OK(0)",
CodeIllegalInput: "ILLEGAL_INPUT(3)",
CodePermissionDenied: "PERMISSION_DENIED(7)",
CodeTooManyRequests: "TOO_MANY_REQUESTS(8)",
CodeUnauthenticated: "UNAUTHENTICATED(16)",
CodeNotFound: "NOT_FOUND(5)",
}
for c, want := range cases {
if got := c.String(); got != want {
t.Errorf("Code(%d).String() = %q, want %q", int(c), got, want)
}
}
}
func TestOpCodeForHTTPStatus(t *testing.T) {
cases := map[int]Code{
200: CodeOK,
400: CodeIllegalInput,
401: CodeUnauthenticated,
403: CodePermissionDenied,
404: CodeNotFound,
409: CodeAlreadyExists,
429: CodeTooManyRequests,
499: CodeOpCancelled,
500: CodeInternalError,
501: CodeUnimplemented,
503: CodeUnavailable,
504: CodeTimeout,
600: CodeUnknownError,
}
for status, want := range cases {
if got := OpCodeFor(status); got != want {
t.Errorf("OpCodeFor(%d) = %s, want %s", status, got, want)
}
}
}
func TestHTTPStatusForCodeMapping(t *testing.T) {
cases := map[Code]HTTPStatus{
CodeOK: StatusOK,
CodeIllegalInput: StatusBadRequest,
CodeFailedPrecondition: StatusBadRequest,
CodeOutOfRange: StatusBadRequest,
CodeUnauthenticated: StatusUnauthorized,
CodePermissionDenied: StatusForbidden,
CodeNotFound: StatusNotFound,
CodeOpConflict: StatusConflict,
CodeAlreadyExists: StatusConflict,
CodeTooManyRequests: StatusTooManyRequests,
CodeOpCancelled: StatusClientClosedRequest,
CodeIllegalState: StatusInternalServerError,
CodeUnknownError: StatusInternalServerError,
CodeInternalError: StatusInternalServerError,
CodeUnimplemented: StatusNotImplemented,
CodeUnavailable: StatusServiceUnavailable,
CodeTimeout: StatusTimeout,
CodeAuthorizationExpired: StatusUnauthorized,
CodeIllegalArg: StatusInternalServerError,
}
for code, want := range cases {
got, ok := HTTPStatusFor(code)
if !ok || got != want {
t.Errorf("HTTPStatusFor(%s) = (%s, %v), want (%s, true)", code, got, ok, want)
}
}
}