Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
63 lines (59 loc) · 1.74 KB
/
Copy patherrors_test.go
File metadata and controls
63 lines (59 loc) · 1.74 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
package apify
import (
"context"
"testing"
)
// ApiError's Is* status-classification predicates match the response status, and only that
// status's predicate is true.
func TestAPIErrorStatusPredicates(t *testing.T) {
errorFor := func(status int) *APIError {
backend := &mockBackend{responses: constant(status, `{"error":{"type":"x","message":"m"}}`)}
client := testClient(backend, 0)
_, err := client.Me().MonthlyUsage(context.Background())
apiErr, ok := AsAPIError(err)
if !ok {
t.Fatalf("expected an *APIError for status %d, got %v", status, err)
}
return apiErr
}
notFoundErr := errorFor(404)
if !notFoundErr.IsNotFound() {
t.Error("expected IsNotFound() for a 404")
}
for _, predicate := range []struct {
name string
ok bool
}{
{"IsInvalidRequest", notFoundErr.IsInvalidRequest()},
{"IsUnauthorized", notFoundErr.IsUnauthorized()},
{"IsForbidden", notFoundErr.IsForbidden()},
{"IsConflict", notFoundErr.IsConflict()},
{"IsRateLimited", notFoundErr.IsRateLimited()},
{"IsServerError", notFoundErr.IsServerError()},
} {
if predicate.ok {
t.Errorf("expected %s() to be false for a 404", predicate.name)
}
}
if !errorFor(400).IsInvalidRequest() {
t.Error("expected IsInvalidRequest() for a 400")
}
if !errorFor(401).IsUnauthorized() {
t.Error("expected IsUnauthorized() for a 401")
}
if !errorFor(403).IsForbidden() {
t.Error("expected IsForbidden() for a 403")
}
if !errorFor(409).IsConflict() {
t.Error("expected IsConflict() for a 409")
}
if !errorFor(429).IsRateLimited() {
t.Error("expected IsRateLimited() for a 429")
}
if !errorFor(500).IsServerError() {
t.Error("expected IsServerError() for a 500")
}
if !errorFor(503).IsServerError() {
t.Error("expected IsServerError() for a 503")
}
}