-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient_test.go
More file actions
333 lines (269 loc) Β· 8.39 KB
/
Copy pathclient_test.go
File metadata and controls
333 lines (269 loc) Β· 8.39 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package whatsonchain
import (
"context"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestNewClient tests the new NewClient function with default options
func TestNewClient(t *testing.T) {
t.Setenv(EnvAPIKey, "")
client, err := NewClient(context.Background())
require.NoError(t, err)
require.NotNil(t, client)
// Check defaults
assert.Equal(t, ChainBSV, client.Chain())
assert.Equal(t, NetworkMain, client.Network())
assert.Equal(t, defaultUserAgent, client.UserAgent())
assert.Equal(t, defaultRateLimit, client.RateLimit())
assert.Empty(t, client.APIKey())
}
// TestWithChain tests the WithChain option
func TestWithChain(t *testing.T) {
t.Parallel()
tests := []struct {
name string
chain ChainType
}{
{"BSV", ChainBSV},
{"BTC", ChainBTC},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(context.Background(), WithChain(tt.chain))
require.NoError(t, err)
assert.Equal(t, tt.chain, client.Chain())
})
}
}
// TestWithNetwork tests the WithNetwork option
func TestWithNetwork(t *testing.T) {
t.Parallel()
tests := []struct {
name string
network NetworkType
}{
{"main net", NetworkMain},
{"test net", NetworkTest},
{"stn net", NetworkStn},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(context.Background(), WithNetwork(tt.network))
require.NoError(t, err)
assert.Equal(t, tt.network, client.Network())
})
}
}
// TestWithAPIKey tests the WithAPIKey option
func TestWithAPIKey(t *testing.T) {
t.Parallel()
apiKey := "12345"
client, err := NewClient(context.Background(), WithAPIKey(apiKey))
require.NoError(t, err)
assert.Equal(t, apiKey, client.APIKey())
}
// TestWithUserAgent tests the WithUserAgent option
func TestWithUserAgent(t *testing.T) {
t.Parallel()
userAgent := "custom-user-agent/1.0"
client, err := NewClient(context.Background(), WithUserAgent(userAgent))
require.NoError(t, err)
assert.Equal(t, userAgent, client.UserAgent())
}
// TestWithRateLimit tests the WithRateLimit option
func TestWithRateLimit(t *testing.T) {
t.Parallel()
rateLimit := 10
client, err := NewClient(context.Background(), WithRateLimit(rateLimit))
require.NoError(t, err)
assert.Equal(t, rateLimit, client.RateLimit())
}
// TestWithHTTPClient tests the WithHTTPClient option
func TestWithHTTPClient(t *testing.T) {
t.Parallel()
customClient := http.DefaultClient
client, err := NewClient(context.Background(), WithHTTPClient(customClient))
require.NoError(t, err)
assert.Equal(t, customClient, client.HTTPClient())
}
// TestWithRequestTimeout tests the WithRequestTimeout option
func TestWithRequestTimeout(t *testing.T) {
t.Parallel()
timeout := 60 * time.Second
client, err := NewClient(context.Background(), WithRequestTimeout(timeout))
require.NoError(t, err)
assert.Equal(t, timeout, client.RequestTimeout())
}
// TestWithRequestRetryCount tests the WithRequestRetryCount option
func TestWithRequestRetryCount(t *testing.T) {
t.Parallel()
retryCount := 5
client, err := NewClient(context.Background(), WithRequestRetryCount(retryCount))
require.NoError(t, err)
assert.Equal(t, retryCount, client.RequestRetryCount())
}
// TestWithRequestRetryCount_NoRetry tests zero retry count
func TestWithRequestRetryCount_NoRetry(t *testing.T) {
t.Parallel()
client, err := NewClient(context.Background(), WithRequestRetryCount(0))
require.NoError(t, err)
assert.Equal(t, 0, client.RequestRetryCount())
}
// TestWithBackoff tests the WithBackoff option
func TestWithBackoff(t *testing.T) {
t.Parallel()
initialTimeout := 5 * time.Millisecond
maxTimeout := 50 * time.Millisecond
exponentFactor := 3.0
maxJitter := 5 * time.Millisecond
client, err := NewClient(
context.Background(),
WithBackoff(initialTimeout, maxTimeout, exponentFactor, maxJitter),
)
require.NoError(t, err)
initial, maxBackoff, factor, jitter := client.BackoffConfig()
assert.Equal(t, initialTimeout, initial)
assert.Equal(t, maxTimeout, maxBackoff)
assert.InEpsilon(t, exponentFactor, factor, 0.0001)
assert.Equal(t, maxJitter, jitter)
}
// TestWithDialer tests the WithDialer option
func TestWithDialer(t *testing.T) {
t.Parallel()
keepAlive := 30 * time.Second
timeout := 10 * time.Second
client, err := NewClient(
context.Background(),
WithDialer(keepAlive, timeout),
)
require.NoError(t, err)
ka, to := client.DialerConfig()
assert.Equal(t, keepAlive, ka)
assert.Equal(t, timeout, to)
}
// TestWithTransport tests the WithTransport option
func TestWithTransport(t *testing.T) {
t.Parallel()
idleTimeout := 30 * time.Second
tlsTimeout := 10 * time.Second
expectContinueTimeout := 5 * time.Second
maxIdleConnections := 20
client, err := NewClient(
context.Background(),
WithTransport(idleTimeout, tlsTimeout, expectContinueTimeout, maxIdleConnections),
)
require.NoError(t, err)
idle, tls, expect, maxIdle := client.TransportConfig()
assert.Equal(t, idleTimeout, idle)
assert.Equal(t, tlsTimeout, tls)
assert.Equal(t, expectContinueTimeout, expect)
assert.Equal(t, maxIdleConnections, maxIdle)
}
// TestMultipleOptions tests combining multiple options
func TestMultipleOptions(t *testing.T) {
t.Parallel()
apiKey := "test-key"
userAgent := "test-agent"
chain := ChainBTC
network := NetworkTest
rateLimit := 5
client, err := NewClient(
context.Background(),
WithAPIKey(apiKey),
WithUserAgent(userAgent),
WithChain(chain),
WithNetwork(network),
WithRateLimit(rateLimit),
)
require.NoError(t, err)
assert.Equal(t, apiKey, client.APIKey())
assert.Equal(t, userAgent, client.UserAgent())
assert.Equal(t, chain, client.Chain())
assert.Equal(t, network, client.Network())
assert.Equal(t, rateLimit, client.RateLimit())
}
// TestSetters tests all setter methods
func TestSetAPIKey(t *testing.T) {
t.Parallel()
client, err := NewClient(context.Background())
require.NoError(t, err)
newKey := "new-api-key"
client.SetAPIKey(newKey)
assert.Equal(t, newKey, client.APIKey())
}
// TestSetUserAgent tests the SetUserAgent method
func TestSetUserAgent(t *testing.T) {
t.Parallel()
client, err := NewClient(context.Background())
require.NoError(t, err)
newAgent := "new-user-agent"
client.SetUserAgent(newAgent)
assert.Equal(t, newAgent, client.UserAgent())
}
// TestSetRateLimit tests the SetRateLimit method
func TestSetRateLimit(t *testing.T) {
t.Parallel()
client, err := NewClient(context.Background())
require.NoError(t, err)
newLimit := 15
client.SetRateLimit(newLimit)
assert.Equal(t, newLimit, client.RateLimit())
}
// TestSetChain tests the SetChain method
func TestSetChain(t *testing.T) {
t.Parallel()
client, err := NewClient(context.Background())
require.NoError(t, err)
require.NoError(t, client.SetChain(ChainBTC))
assert.Equal(t, ChainBTC, client.Chain())
}
// TestSetNetwork tests the SetNetwork method
func TestSetNetwork(t *testing.T) {
t.Parallel()
client, err := NewClient(context.Background())
require.NoError(t, err)
require.NoError(t, client.SetNetwork(NetworkTest))
assert.Equal(t, NetworkTest, client.Network())
}
// TestNewClient_EnvAPIKey tests that WHATS_ON_CHAIN_API_KEY env var is auto-loaded
func TestNewClient_EnvAPIKey(t *testing.T) {
t.Setenv(EnvAPIKey, "env-api-key-123")
client, err := NewClient(context.Background())
require.NoError(t, err)
assert.Equal(t, "env-api-key-123", client.APIKey())
}
// TestNewClient_EnvAPIKey_ExplicitOverrides tests that WithAPIKey takes precedence over env var
func TestNewClient_EnvAPIKey_ExplicitOverrides(t *testing.T) {
t.Setenv(EnvAPIKey, "env-api-key-123")
client, err := NewClient(context.Background(), WithAPIKey("explicit-key"))
require.NoError(t, err)
assert.Equal(t, "explicit-key", client.APIKey())
}
// TestNewClient_EnvAPIKey_NotSet tests that no API key is set when env var is absent
func TestNewClient_EnvAPIKey_NotSet(t *testing.T) {
t.Setenv(EnvAPIKey, "")
client, err := NewClient(context.Background())
require.NoError(t, err)
assert.Empty(t, client.APIKey())
}
// BenchmarkNewClient benchmarks the NewClient method
func BenchmarkNewClient(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewClient(context.Background())
}
}
// BenchmarkNewClientWithOptions benchmarks NewClient with options
func BenchmarkNewClientWithOptions(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewClient(
context.Background(),
WithChain(ChainBTC),
WithNetwork(NetworkTest),
WithAPIKey("test-key"),
WithRateLimit(10),
)
}
}