-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
316 lines (272 loc) · 8.89 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package dotwallet
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newTestClient will return a client for testing purposes
func newTestClient() (*Client, 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(
WithCredentials(testClientID, testClientSecret),
WithCustomHeaders(headers),
WithCustomHTTPClient(client),
WithHost(testHost),
WithRedirectURI(testRedirectURI),
WithRequestTracing(),
)
if err != nil {
return nil, err
}
// Return the mocking client
return newClient, nil
}
// TestNewClient will test the method NewClient()
func TestNewClient(t *testing.T) {
t.Run("missing client_id", func(t *testing.T) {
customHTTPClient := resty.New()
customHTTPClient.SetTimeout(defaultHTTPTimeout)
httpmock.ActivateNonDefault(customHTTPClient.GetClient())
newClient, err := NewClient(
WithCredentials("", testClientSecret),
WithCustomHTTPClient(customHTTPClient),
)
require.Error(t, err)
assert.Nil(t, newClient)
})
t.Run("missing client_secret", func(t *testing.T) {
customHTTPClient := resty.New()
customHTTPClient.SetTimeout(defaultHTTPTimeout)
httpmock.ActivateNonDefault(customHTTPClient.GetClient())
newClient, err := NewClient(
WithCredentials(testClientID, ""),
WithCustomHTTPClient(customHTTPClient),
)
require.Error(t, err)
assert.Nil(t, newClient)
})
t.Run("test client, get access token, defaults", func(t *testing.T) {
c, err := newTestClient()
require.NoError(t, err)
assert.NotNil(t, c)
mockUpdateApplicationAccessToken()
err = c.UpdateApplicationAccessToken()
require.NoError(t, err)
assert.NotNil(t, c.options.token)
assert.Equal(t, defaultHTTPTimeout, c.options.httpTimeout)
assert.Equal(t, defaultRetryCount, c.options.retryCount)
assert.Equal(t, defaultUserAgent, c.options.userAgent)
assert.Equal(t, testTokenType, c.options.token.TokenType)
assert.Equal(t, testExpiresIn, c.options.token.ExpiresIn)
assert.Equal(t, time.Now().UTC().Unix()+c.options.token.ExpiresIn, c.options.token.ExpiresAt)
assert.Equal(t, testAccessToken, c.options.token.AccessToken)
assert.Equal(t, testClientID, c.options.clientID)
assert.Equal(t, testClientSecret, c.options.clientSecret)
assert.Equal(t, testRedirectURI, c.options.redirectURI)
assert.Equal(t, testHost, c.options.host)
})
t.Run("custom HTTP client", func(t *testing.T) {
customHTTPClient := resty.New()
customHTTPClient.SetTimeout(defaultHTTPTimeout)
httpmock.ActivateNonDefault(customHTTPClient.GetClient())
newClient, err := NewClient(
WithCredentials(testClientID, testClientSecret),
WithCustomHTTPClient(customHTTPClient),
)
require.NoError(t, err)
assert.NotNil(t, newClient)
})
t.Run("custom HTTP timeout", func(t *testing.T) {
newClient, err := NewClient(
WithCredentials(testClientID, testClientSecret),
WithHTTPTimeout(15*time.Second),
)
require.NoError(t, err)
assert.NotNil(t, newClient)
assert.Equal(t, 15*time.Second, newClient.options.httpTimeout)
})
t.Run("custom host", func(t *testing.T) {
newClient, err := NewClient(
WithCredentials(testClientID, testClientSecret),
WithHost(testHost),
)
require.NoError(t, err)
assert.NotNil(t, newClient)
assert.Equal(t, testHost, newClient.options.host)
})
t.Run("custom retry count", func(t *testing.T) {
newClient, err := NewClient(
WithCredentials(testClientID, testClientSecret),
WithRetryCount(4),
)
require.NoError(t, err)
assert.NotNil(t, newClient)
assert.Equal(t, 4, newClient.options.retryCount)
})
t.Run("custom user agent", func(t *testing.T) {
newClient, err := NewClient(
WithCredentials(testClientID, testClientSecret),
WithUserAgent("custom-user-agent"),
)
require.NoError(t, err)
assert.NotNil(t, newClient)
assert.Equal(t, "custom-user-agent", newClient.options.userAgent)
})
t.Run("custom headers", func(t *testing.T) {
customHTTPClient := resty.New()
httpmock.ActivateNonDefault(customHTTPClient.GetClient())
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(
WithCredentials(testClientID, testClientSecret),
WithCustomHTTPClient(customHTTPClient),
WithCustomHeaders(headers),
)
require.NoError(t, err)
assert.NotNil(t, client)
assert.Len(t, client.options.customHeaders, 2)
assert.Equal(t, []string{"value_1"}, client.options.customHeaders["custom_header_1"])
})
t.Run("auto-load the application token", func(t *testing.T) {
customHTTPClient := resty.New()
httpmock.ActivateNonDefault(customHTTPClient.GetClient())
mockUpdateApplicationAccessToken()
client, err := NewClient(
WithHost(testHost),
WithCredentials(testClientID, testClientSecret),
WithCustomHTTPClient(customHTTPClient),
WithAutoLoadToken(),
)
require.NoError(t, err)
assert.NotNil(t, client)
assert.NotNil(t, client.Token())
})
t.Run("auto-load fails", func(t *testing.T) {
customHTTPClient := resty.New()
httpmock.ActivateNonDefault(customHTTPClient.GetClient())
mockAccessTokenFailed(http.StatusBadRequest)
client, err := NewClient(
WithHost(testHost),
WithCredentials(testClientID, testClientSecret),
WithCustomHTTPClient(customHTTPClient),
WithAutoLoadToken(),
)
require.Error(t, err)
assert.Nil(t, client)
assert.Nil(t, client.Token())
})
t.Run("override the application access token", func(t *testing.T) {
newClient, err := NewClient(
WithCredentials(testClientID, testClientSecret),
WithHTTPTimeout(15*time.Second),
WithApplicationAccessToken(DotAccessToken{
AccessToken: testAccessToken,
ExpiresAt: time.Now().UTC().Unix() + testExpiresIn,
ExpiresIn: testExpiresIn,
TokenType: testTokenType,
}),
)
require.NoError(t, err)
assert.NotNil(t, newClient)
token := newClient.Token()
assert.Equal(t, testAccessToken, token.AccessToken)
})
}
// TestClient_GetUserAgent will test the method GetUserAgent()
func TestClient_GetUserAgent(t *testing.T) {
t.Run("get user agent", func(t *testing.T) {
c, err := newTestClient()
require.NoError(t, err)
assert.NotNil(t, c)
userAgent := c.GetUserAgent()
assert.Equal(t, defaultUserAgent, userAgent)
})
}
// TestClient_NewState will test the method NewState()
func TestClient_NewState(t *testing.T) {
t.Run("generate a new state", func(t *testing.T) {
c, err := newTestClient()
require.NoError(t, err)
assert.NotNil(t, c)
var state string
state, err = c.NewState()
require.NoError(t, err)
assert.Len(t, state, 64)
})
}
// TestClient_Token will test the method Token()
func TestClient_Token(t *testing.T) {
t.Run("token is present", func(t *testing.T) {
c, err := newTestClient()
require.NoError(t, err)
assert.NotNil(t, c)
mockUpdateApplicationAccessToken()
err = c.UpdateApplicationAccessToken()
require.NoError(t, err)
token := c.Token()
assert.NotNil(t, token)
})
t.Run("token is not present", func(t *testing.T) {
c, err := newTestClient()
require.NoError(t, err)
assert.NotNil(t, c)
mockAccessTokenFailed(http.StatusBadRequest)
err = c.UpdateApplicationAccessToken()
require.Error(t, err)
token := c.Token()
assert.Nil(t, token)
})
}
// ExampleNewClient example using NewClient()
//
// See more examples in /examples/
func ExampleNewClient() {
client, err := NewClient(
WithCredentials("your-client-id", "your-secret-id"),
WithRedirectURI("http://localhost:3000/v1/auth/dotwallet/callback"),
)
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
fmt.Printf("loaded client: %s", client.options.userAgent)
// Output:loaded client: dotwallet-go-sdk: v0.2.0
}
// BenchmarkNewClient benchmarks the method NewClient()
func BenchmarkNewClient(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewClient(
WithCredentials(testClientID, testClientSecret),
)
}
}
// TestDefaultClientOptions will test the method defaultClientOptions()
func TestDefaultClientOptions(t *testing.T) {
options := defaultClientOptions()
assert.NotNil(t, options)
assert.Equal(t, defaultHost, options.host)
assert.Equal(t, defaultHTTPTimeout, options.httpTimeout)
assert.Equal(t, defaultRetryCount, options.retryCount)
assert.Equal(t, defaultUserAgent, options.userAgent)
assert.False(t, options.requestTracing)
}
// BenchmarkDefaultClientOptions benchmarks the method defaultClientOptions()
func BenchmarkDefaultClientOptions(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = defaultClientOptions()
}
}