-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
72 lines (67 loc) · 1.27 KB
/
client_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
package bonusly
import (
"errors"
"io/ioutil"
"net/http"
"reflect"
"strings"
"testing"
)
type errReadCloser struct {
rerr error
cerr error
}
func (r errReadCloser) Read(p []byte) (n int, err error) {
return 0, r.rerr
}
func (r errReadCloser) Close() error {
return r.cerr
}
func Test_readAndCloseBody(t *testing.T) {
type args struct {
r *http.Response
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
"nil-response",
args{r: nil},
nil,
true,
},
{
"ok",
args{r: &http.Response{Body: ioutil.NopCloser(strings.NewReader("Test"))}},
[]byte("Test"),
false,
},
{
"error-read",
args{r: &http.Response{Body: errReadCloser{rerr: errors.New("read error")}}},
nil,
true,
},
{
"error-read-close",
args{r: &http.Response{Body: errReadCloser{rerr: errors.New("read error"), cerr: errors.New("close error")}}},
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readAndCloseBody(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("readAndCloseBody() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("readAndCloseBody() got = %v, want %v", got, tt.want)
}
})
}
}