-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
79 lines (77 loc) · 1.63 KB
/
client_test.go
File metadata and controls
79 lines (77 loc) · 1.63 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
73
74
75
76
77
78
79
package requests
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestClientOption(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("query strings: %v", r.URL.Query())
t.Logf("headers: %v", r.Header)
time.Sleep(100 * time.Millisecond)
w.WriteHeader(http.StatusOK)
}))
defer testServer.Close()
type args struct {
url string
options []ClientOption
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "with timeout",
args: args{
url: testServer.URL,
options: []ClientOption{
WithTimeout(time.Millisecond),
},
},
wantErr: true,
},
{
name: "with transport",
args: args{
url: testServer.URL,
options: []ClientOption{
WithTransport(func() http.RoundTripper {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.DisableKeepAlives = true
return transport
}()),
},
},
wantErr: false,
},
{
name: "with interceptor",
args: args{
url: testServer.URL,
options: []ClientOption{
WithInterceptor(func(ctx context.Context, r *Request, do Do) (*Response, error) {
t.Logf("method: %s", r.Method)
return do(ctx, r)
}),
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cli := NewClient(tt.args.options...)
got, err := cli.Get(tt.args.url)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
t.Logf("response body: %+v\n", got.Text())
}
})
}
}