-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
98 lines (88 loc) · 2.84 KB
/
Copy patherrors_test.go
File metadata and controls
98 lines (88 loc) · 2.84 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// errors_test.go
package cinc
import (
"errors"
"net/http"
"testing"
)
func TestErrorResponse_Is(t *testing.T) {
cases := map[int]error{
http.StatusNotFound: ErrNotFound,
http.StatusConflict: ErrConflict,
http.StatusForbidden: ErrForbidden,
http.StatusUnauthorized: ErrUnauthorized,
}
for code, sentinel := range cases {
err := newErrorResponse("GET", "/x", code, []byte(`{"error":["boom"]}`))
if !errors.Is(err, sentinel) {
t.Errorf("status %d: errors.Is(%v) = false", code, sentinel)
}
}
}
func TestErrorResponse_Message(t *testing.T) {
err := newErrorResponse("PUT", "/nodes/x", 404, []byte(`{"error":["no node"]}`))
if got := err.Error(); got == "" ||
!contains(got, "404") || !contains(got, "no node") {
t.Fatalf("Error() = %q, want code and message", got)
}
}
func TestErrorResponse_StringErrorField(t *testing.T) {
// Chef occasionally returns "error" as a bare string instead of a list.
err := newErrorResponse("GET", "/x", 400, []byte(`{"error":"single string"}`))
if got := err.Error(); !contains(got, "single string") {
t.Errorf("Error() = %q, want message text", got)
}
}
func TestErrorResponse_BareBody(t *testing.T) {
// Non-JSON body should be surfaced verbatim as the message.
err := newErrorResponse("GET", "/x", 502, []byte("upstream down"))
if got := err.Error(); !contains(got, "upstream down") {
t.Errorf("Error() = %q, want bare body text", got)
}
}
func TestErrorResponse_EmptyBody(t *testing.T) {
err := newErrorResponse("GET", "/x", 500, nil)
if got := err.Error(); !contains(got, "no message") {
t.Errorf("Error() = %q, want placeholder for empty body", got)
}
}
func TestErrorResponse_401HintAppended(t *testing.T) {
// On 401 we append a hint about checking the client key and clock skew.
err := newErrorResponse("GET", "/x", 401, []byte(`{"error":["bad sig"]}`))
if got := err.Error(); !contains(got, "clock") || !contains(got, "client key") {
t.Errorf("Error() = %q, want 401 hint", got)
}
}
func TestErrorResponse_UnwrapUnknownStatus(t *testing.T) {
err := newErrorResponse("GET", "/x", 500, nil)
if got := err.Unwrap(); got != nil {
t.Errorf("Unwrap on 500 = %v, want nil", got)
}
}
func TestErrorResponse_FieldsPopulated(t *testing.T) {
err := newErrorResponse("DELETE", "/y", 409, []byte(`{"error":["conflict"]}`))
if err.Method != "DELETE" {
t.Errorf("Method = %q", err.Method)
}
if err.Path != "/y" {
t.Errorf("Path = %q", err.Path)
}
if err.StatusCode != 409 {
t.Errorf("StatusCode = %d", err.StatusCode)
}
if len(err.Messages) != 1 || err.Messages[0] != "conflict" {
t.Errorf("Messages = %v", err.Messages)
}
}
func contains(s, sub string) bool {
return len(s) >= len(sub) && (s == sub ||
len(sub) == 0 || indexOf(s, sub) >= 0)
}
func indexOf(s, sub string) int {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return i
}
}
return -1
}