Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
151 lines (141 loc) · 4.47 KB
/
Copy patherrors_test.go
File metadata and controls
151 lines (141 loc) · 4.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
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
package intercom
import (
"errors"
"fmt"
"net/http"
"strings"
"testing"
)
func TestParseErrorResponse(t *testing.T) {
err := parseErrorResponse(401, []byte(`{"type":"error.list","request_id":"req-1","errors":[{"code":"unauthorized","message":"Access Token Invalid"}]}`))
apiErr, ok := err.(*ErrorResponse)
if !ok {
t.Fatalf("error type = %T, want *ErrorResponse", err)
}
if apiErr.StatusCode != 401 {
t.Fatalf("StatusCode = %d", apiErr.StatusCode)
}
if apiErr.RequestID != "req-1" {
t.Fatalf("RequestID = %q", apiErr.RequestID)
}
if !strings.Contains(apiErr.Error(), "unauthorized: Access Token Invalid") {
t.Fatalf("Error() = %q", apiErr.Error())
}
}
func TestErrorResponseError(t *testing.T) {
tests := []struct {
name string
err *ErrorResponse
want string
}{
{
name: "nil",
want: "intercom: unknown error",
},
{
name: "message only",
err: &ErrorResponse{
StatusCode: 400,
Errors: []Error{{Message: "Bad request"}},
},
want: "intercom: API error: status 400: Bad request",
},
{
name: "code only",
err: &ErrorResponse{
StatusCode: 404,
Errors: []Error{{Code: "not_found"}},
},
want: "intercom: API error: status 404: not_found",
},
{
name: "empty errors",
err: &ErrorResponse{
StatusCode: 500,
Errors: []Error{{}},
},
want: "intercom: API error: status 500",
},
{
name: "multiple errors",
err: &ErrorResponse{
StatusCode: 422,
Errors: []Error{
{Code: "invalid", Message: "Invalid field"},
{Code: "missing", Message: "Missing field"},
},
},
want: "intercom: API error: status 422: invalid: Invalid field; missing: Missing field",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.err.Error(); got != tt.want {
t.Fatalf("Error() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseInvalidErrorResponse(t *testing.T) {
headers := http.Header{
requestIDHeader: []string{"req-header"},
"X-Custom-Response": []string{"original"},
}
err := parseErrorResponse(500, []byte(`not json`), headers)
apiErr, ok := err.(*ErrorResponse)
if !ok {
t.Fatalf("error type = %T, want *ErrorResponse", err)
}
if apiErr.StatusCode != 500 {
t.Fatalf("StatusCode = %d", apiErr.StatusCode)
}
if apiErr.Body != "not json" {
t.Fatalf("Body = %q", apiErr.Body)
}
if apiErr.RequestID != "req-header" {
t.Fatalf("RequestID = %q", apiErr.RequestID)
}
headers.Set("X-Custom-Response", "changed")
if got := apiErr.Headers.Get("X-Custom-Response"); got != "original" {
t.Fatalf("Headers = %q, want original", got)
}
if len(apiErr.Errors) != 1 {
t.Fatalf("Errors length = %d", len(apiErr.Errors))
}
if apiErr.Errors[0].Code != "invalid_error_response" {
t.Fatalf("Error code = %q", apiErr.Errors[0].Code)
}
}
func TestErrorStatusHelpers(t *testing.T) {
tests := []struct {
name string
err error
test func(error) bool
want bool
}{
{name: "bad request", err: &ErrorResponse{StatusCode: http.StatusBadRequest}, test: IsBadRequest, want: true},
{name: "unauthorized", err: &ErrorResponse{StatusCode: http.StatusUnauthorized}, test: IsUnauthorized, want: true},
{name: "forbidden", err: &ErrorResponse{StatusCode: http.StatusForbidden}, test: IsForbidden, want: true},
{name: "not found wrapped", err: fmt.Errorf("lookup: %w", &ErrorResponse{StatusCode: http.StatusNotFound}), test: IsNotFound, want: true},
{name: "conflict", err: &ErrorResponse{StatusCode: http.StatusConflict}, test: IsConflict, want: true},
{name: "rate limited", err: &ErrorResponse{StatusCode: http.StatusTooManyRequests}, test: IsRateLimited, want: true},
{name: "server error lower bound", err: &ErrorResponse{StatusCode: 500}, test: IsServerError, want: true},
{name: "server error upper bound", err: &ErrorResponse{StatusCode: 599}, test: IsServerError, want: true},
{name: "not server error", err: &ErrorResponse{StatusCode: 499}, test: IsServerError},
{name: "non API error", err: errors.New("boom"), test: IsNotFound},
{name: "nil error", test: IsServerError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.test(tt.err); got != tt.want {
t.Fatalf("helper returned %v, want %v", got, tt.want)
}
})
}
if !IsStatus(&ErrorResponse{StatusCode: http.StatusConflict}, http.StatusNotFound, http.StatusConflict) {
t.Fatal("IsStatus did not match one of multiple statuses")
}
if IsStatus(&ErrorResponse{StatusCode: http.StatusConflict}) {
t.Fatal("IsStatus matched an empty status list")
}
}