-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrors_test.go
58 lines (52 loc) · 1.28 KB
/
errors_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
package gotado
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsError(t *testing.T) {
tests := map[string]struct {
resp *http.Response
wantErr error
}{
"NoError": {
resp: makeResponse(200, ""),
wantErr: nil,
},
"NilError": {
resp: nil,
wantErr: fmt.Errorf("response is nil"),
},
"NoJsonError": {
resp: makeResponse(404, "not found"),
wantErr: fmt.Errorf("unable to decode API error: invalid character 'o' in literal null (expecting 'u')"),
},
"InvalidJsonError": {
resp: makeResponse(301, `{"foo": "bar"}`),
wantErr: fmt.Errorf("API returned empty error"),
},
"EmptyError": {
resp: makeResponse(500, `{"errors":[]}`),
wantErr: fmt.Errorf("API returned empty error"),
},
"SingleError": {
resp: makeResponse(500, `{"errors":[{"code":"1","title":"One"}]}`),
wantErr: fmt.Errorf("1: One"),
},
"MultiError": {
resp: makeResponse(500, `{"errors":[{"code":"1","title":"One"},{"code":"2","title":"Two"}]}`),
wantErr: fmt.Errorf("1: One, 2: Two"),
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := isError(tc.resp)
if tc.wantErr != nil {
assert.EqualError(t, err, tc.wantErr.Error())
} else {
assert.NoError(t, err)
}
})
}
}