-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_http_test.go
84 lines (77 loc) · 1.85 KB
/
client_http_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
package kickbox_test
import (
"errors"
"net/http"
"testing"
"github.com/wakumaku/kickbox"
"github.com/stretchr/testify/assert"
"golang.org/x/time/rate"
)
func TestClientOptions(t *testing.T) {
optionCases := []struct {
optFnc kickbox.ClientHTTPOption
returnsErr bool
expected string
}{
{
optFnc: kickbox.OverrideBaseURL(""),
returnsErr: true,
expected: "baseURL is empty",
},
{
optFnc: kickbox.OverrideBaseURL("http://baseURL"),
returnsErr: false,
},
{
optFnc: kickbox.MaxConcurrentConnections(0),
returnsErr: true,
expected: "max concurrent connection must be greater than zero",
},
{
optFnc: kickbox.MaxConcurrentConnections(5),
returnsErr: false,
},
{
optFnc: kickbox.CustomRateLimiter(nil),
returnsErr: true,
expected: "rate limiter is nil",
},
{
optFnc: kickbox.CustomRateLimiter(rate.NewLimiter(rate.Limit(1), 1)),
returnsErr: false,
},
{
optFnc: kickbox.CustomHTTPClient(nil),
returnsErr: true,
expected: "client is nil",
},
{
optFnc: kickbox.CustomHTTPClient(&http.Client{}),
returnsErr: false,
},
}
options := kickbox.ClientHTTPOptions{}
for _, oc := range optionCases {
err := oc.optFnc(&options)
if oc.returnsErr {
assert.EqualError(t, err, oc.expected)
} else {
assert.Nil(t, err)
}
}
}
func TestNewClient(t *testing.T) {
// Create a client with an empty API Key
client, err := kickbox.New("")
assert.Nil(t, client)
assert.NotNil(t, err)
assert.EqualError(t, err, "apikey is empty")
// Create a client with a failing option
failingOption := func(o *kickbox.ClientHTTPOptions) error {
return errors.New("expected error")
}
client, err = kickbox.New("api_key", failingOption)
assert.Nil(t, client)
assert.NotNil(t, err)
assert.EqualError(t, err, "applying optional settings: expected error")
}