-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_test.go
242 lines (209 loc) · 8.24 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package tonicpow
import (
"fmt"
"testing"
"time"
"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
// newTestClient will return a client for testing purposes
func newTestClient() (ClientInterface, error) {
// Create a Resty Client
client := resty.New()
// Get the underlying HTTP Client and set it to Mock
httpmock.ActivateNonDefault(client.GetClient())
// Add custom headers in request
headers := make(map[string][]string)
headers["custom_header_1"] = append(headers["custom_header_1"], "value_1")
// Create a new client
newClient, err := NewClient(
WithRequestTracing(),
WithAPIKey(testAPIKey),
WithEnvironment(EnvironmentDevelopment),
WithCustomHeaders(headers),
)
if err != nil {
return nil, err
}
newClient.WithCustomHTTPClient(client)
// Return the mocking client
return newClient, nil
}
// TestNewClient will test the method NewClient()
func TestNewClient(t *testing.T) {
t.Parallel()
t.Run("default client", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, defaultHTTPTimeout, client.Options().httpTimeout)
assert.Equal(t, defaultRetryCount, client.Options().retryCount)
assert.Equal(t, defaultUserAgent, client.Options().userAgent)
assert.Equal(t, false, client.Options().requestTracing)
assert.Equal(t, EnvironmentLive.apiURL, client.Options().env.URL())
assert.Equal(t, EnvironmentLive.name, client.Options().env.Name())
})
t.Run("missing api key", func(t *testing.T) {
client, err := NewClient(WithAPIKey(""))
assert.Error(t, err)
assert.Nil(t, client)
})
t.Run("custom http client", func(t *testing.T) {
customHTTPClient := resty.New()
customHTTPClient.SetTimeout(defaultHTTPTimeout)
client, err := NewClient(WithAPIKey(testAPIKey))
assert.NoError(t, err)
assert.NotNil(t, client)
client.WithCustomHTTPClient(customHTTPClient)
})
t.Run("custom http timeout", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithHTTPTimeout(10*time.Second))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, 10*time.Second, client.Options().httpTimeout)
})
t.Run("custom retry count", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithRetryCount(3))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, 3, client.Options().retryCount)
})
t.Run("custom headers", func(t *testing.T) {
headers := make(map[string][]string)
headers["custom_header_1"] = append(headers["custom_header_1"], "value_1")
headers["custom_header_2"] = append(headers["custom_header_2"], "value_1")
headers["custom_header_2"] = append(headers["custom_header_2"], "value_2")
client, err := NewClient(WithAPIKey(testAPIKey), WithCustomHeaders(headers))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, 2, len(client.Options().customHeaders))
assert.Equal(t, []string{"value_1"}, client.Options().customHeaders["custom_header_1"])
})
t.Run("custom options", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithUserAgent("custom user agent"))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, "custom user agent", client.GetUserAgent())
})
t.Run("custom Environment (live)", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironment(EnvironmentLive))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, liveAPIURL, client.Options().env.URL())
assert.Equal(t, environmentLiveName, client.Options().env.Name())
assert.Equal(t, environmentLiveAlias, client.Options().env.Alias())
client, err = NewClient(WithAPIKey(testAPIKey), WithEnvironmentString(environmentLiveName))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, liveAPIURL, client.Options().env.URL())
assert.Equal(t, environmentLiveName, client.Options().env.Name())
assert.Equal(t, environmentLiveAlias, client.Options().env.Alias())
})
t.Run("custom Environment (staging)", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironment(EnvironmentStaging))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, stagingAPIURL, client.Options().env.URL())
assert.Equal(t, environmentStagingName, client.Options().env.Name())
assert.Equal(t, environmentStagingAlias, client.Options().env.Alias())
client, err = NewClient(WithAPIKey(testAPIKey), WithEnvironmentString(environmentStagingName))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, stagingAPIURL, client.Options().env.URL())
assert.Equal(t, environmentStagingName, client.Options().env.Name())
assert.Equal(t, environmentStagingAlias, client.Options().env.Alias())
})
t.Run("custom Environment (development)", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironment(EnvironmentDevelopment))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, developmentURL, client.Options().env.URL())
assert.Equal(t, environmentDevelopmentName, client.Options().env.Name())
assert.Equal(t, environmentDevelopmentAlias, client.Options().env.Alias())
client, err = NewClient(WithAPIKey(testAPIKey), WithEnvironmentString(environmentDevelopmentName))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, developmentURL, client.Options().env.URL())
assert.Equal(t, environmentDevelopmentName, client.Options().env.Name())
assert.Equal(t, environmentDevelopmentAlias, client.Options().env.Alias())
})
t.Run("custom Environment (custom)", func(t *testing.T) {
client, err := NewClient(
WithAPIKey(testAPIKey),
WithCustomEnvironment("custom", "alias", "http://localhost:5000"),
)
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, "http://localhost:5000", client.Options().env.URL())
assert.Equal(t, "custom", client.Options().env.Name())
assert.Equal(t, "alias", client.Options().env.Alias())
})
t.Run("default no Environment", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironmentString(""))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, liveAPIURL, client.Options().env.URL())
assert.Equal(t, environmentLiveName, client.Options().env.Name())
})
}
// TestClient_GetUserAgent will test the method GetUserAgent()
func TestClient_GetUserAgent(t *testing.T) {
t.Parallel()
t.Run("get user agent", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey))
assert.NoError(t, err)
assert.NotNil(t, client)
userAgent := client.GetUserAgent()
assert.Equal(t, defaultUserAgent, userAgent)
})
}
// TestClient_GetEnvironment will test the method GetEnvironment()
func TestClient_GetEnvironment(t *testing.T) {
t.Parallel()
t.Run("get client Environment", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey))
assert.NoError(t, err)
assert.NotNil(t, client)
env := client.GetEnvironment()
assert.Equal(t, environmentLiveName, env.Name())
assert.Equal(t, environmentLiveAlias, env.Alias())
})
}
// ExampleNewClient example using NewClient()
//
// See more examples in /examples/
func ExampleNewClient() {
client, err := NewClient(WithAPIKey(testAPIKey))
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
fmt.Printf("loaded client: %s", client.Options().userAgent)
// Output:loaded client: go-tonicpow: v0.8.0
}
// BenchmarkNewClient benchmarks the method NewClient()
func BenchmarkNewClient(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewClient(WithAPIKey(testAPIKey))
}
}
// TestDefaultClientOptions will test the method defaultClientOptions()
func TestDefaultClientOptions(t *testing.T) {
t.Parallel()
options := defaultClientOptions()
assert.NotNil(t, options)
assert.Equal(t, defaultHTTPTimeout, options.httpTimeout)
assert.Equal(t, defaultRetryCount, options.retryCount)
assert.Equal(t, defaultUserAgent, options.userAgent)
assert.Equal(t, environmentLiveAlias, options.env.Alias())
assert.Equal(t, environmentLiveName, options.env.Name())
assert.Equal(t, false, options.requestTracing)
assert.Equal(t, liveAPIURL, options.env.URL())
}
// BenchmarkDefaultClientOptions benchmarks the method defaultClientOptions()
func BenchmarkDefaultClientOptions(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = defaultClientOptions()
}
}