Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
72 lines (64 loc) · 1.98 KB
/
Copy patherrors_test.go
File metadata and controls
72 lines (64 loc) · 1.98 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
package forges
import (
"errors"
"net/http"
"testing"
)
var statusSentinels = []error{ErrUnauthorized, ErrRateLimited, ErrServerError}
func assertSentinel(t *testing.T, err, want error) {
t.Helper()
for _, sentinel := range statusSentinels {
if got := errors.Is(err, sentinel); got != (sentinel == want) {
t.Errorf("errors.Is(%v, %v) = %v", err, sentinel, got)
}
}
}
var statusTests = []struct {
status int
want error
}{
{http.StatusBadRequest, nil},
{http.StatusUnauthorized, ErrUnauthorized},
{http.StatusForbidden, ErrUnauthorized},
{http.StatusNotFound, nil},
{http.StatusConflict, nil},
{http.StatusTooManyRequests, ErrRateLimited},
{http.StatusInternalServerError, ErrServerError},
{http.StatusBadGateway, ErrServerError},
{599, ErrServerError},
{600, nil},
}
func TestClassifyStatus(t *testing.T) {
for _, tt := range statusTests {
t.Run(http.StatusText(tt.status), func(t *testing.T) {
original := errors.New("original message")
got := ClassifyStatus(tt.status, original)
assertSentinel(t, got, tt.want)
if !errors.Is(got, original) {
t.Errorf("result should wrap the original error, got %v", got)
}
if got.Error() != original.Error() {
t.Errorf("message = %q, want %q", got.Error(), original.Error())
}
})
}
}
func TestClassifyStatusNilError(t *testing.T) {
if err := ClassifyStatus(http.StatusInternalServerError, nil); err != nil {
t.Errorf("ClassifyStatus(500, nil) = %v, want nil", err)
}
}
func TestClassifyStatusKeepsErrorTypes(t *testing.T) {
var httpErr *HTTPError
err := ClassifyStatus(http.StatusForbidden, &HTTPError{StatusCode: http.StatusForbidden})
if !errors.As(err, &httpErr) || httpErr.StatusCode != http.StatusForbidden {
t.Errorf("errors.As should find the HTTPError in %v", err)
}
}
func TestHTTPErrorIs(t *testing.T) {
for _, tt := range statusTests {
t.Run(http.StatusText(tt.status), func(t *testing.T) {
assertSentinel(t, &HTTPError{StatusCode: tt.status, URL: "https://example.com"}, tt.want)
})
}
}