-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
errorkind_test.go
85 lines (82 loc) · 1.75 KB
/
errorkind_test.go
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
package requests_test
import (
"context"
"errors"
"io"
"testing"
"github.com/carlmjohnson/requests"
"github.com/carlmjohnson/requests/internal/be"
)
func TestErrorKind(t *testing.T) {
t.Parallel()
var none requests.ErrorKind = -1
kinds := []requests.ErrorKind{
requests.ErrURL,
requests.ErrRequest,
requests.ErrTransport,
requests.ErrValidator,
requests.ErrHandler,
}
ctx := context.Background()
res200 := requests.ReplayString("HTTP/1.1 200 OK\n\n")
for _, tc := range []struct {
ctx context.Context
want requests.ErrorKind
b *requests.Builder
}{
{ctx, none, requests.
URL("").
Transport(res200),
},
{ctx, requests.ErrURL, requests.
URL("http://%2020").
Transport(res200),
},
{ctx, requests.ErrURL, requests.
URL("hello world").
Transport(res200),
},
{ctx, none, requests.
URL("http://world/#hello").
Transport(res200),
},
{ctx, requests.ErrRequest, requests.
URL("").
Body(func() (io.ReadCloser, error) {
return nil, errors.New("x")
}).
Transport(res200),
},
{ctx, requests.ErrRequest, requests.
URL("").
Method(" ").
Transport(res200),
},
{nil, requests.ErrRequest, requests.
URL("").
Transport(res200),
},
{ctx, requests.ErrTransport, requests.
URL("").
Transport(requests.ReplayString("")),
},
{ctx, requests.ErrValidator, requests.
URL("").
Transport(requests.ReplayString("HTTP/1.1 404 Nope\n\n")),
},
{ctx, requests.ErrHandler, requests.
URL("").
Transport(res200).
ToJSON(nil),
},
} {
err := tc.b.Fetch(tc.ctx)
for _, kind := range kinds {
match := errors.Is(err, kind)
be.Equal(t, kind == tc.want, match)
}
var askind = none
be.Equal(t, tc.want != none, errors.As(err, &askind))
be.Equal(t, tc.want, askind)
}
}