-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
93 lines (77 loc) · 3.28 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
package bsvrates
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestNewClient test new client
func TestNewClient(t *testing.T) {
t.Parallel()
t.Run("valid new client", func(t *testing.T) {
client := NewClient(nil, nil)
assert.NotNil(t, client)
// Test default providers
assert.Equal(t, ProviderCoinPaprika, client.Providers()[0])
assert.Equal(t, ProviderWhatsOnChain, client.Providers()[1])
})
t.Run("custom http client", func(t *testing.T) {
client := NewClient(nil, http.DefaultClient, ProviderCoinPaprika)
assert.NotNil(t, client)
// Test custom providers
assert.Equal(t, ProviderCoinPaprika, client.Providers()[0])
})
}
// BenchmarkNewClient benchmarks the NewClient method
func BenchmarkNewClient(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewClient(nil, nil)
}
}
// TestDefaultClientOptions tests setting DefaultClientOptions()
func TestDefaultClientOptions(t *testing.T) {
t.Parallel()
t.Run("default client options", func(t *testing.T) {
options := DefaultClientOptions()
assert.Equal(t, defaultUserAgent, options.UserAgent)
assert.Equal(t, 2.0, options.BackOffExponentFactor)
assert.Equal(t, 2*time.Millisecond, options.BackOffInitialTimeout)
assert.Equal(t, 2*time.Millisecond, options.BackOffMaximumJitterInterval)
assert.Equal(t, 10*time.Millisecond, options.BackOffMaxTimeout)
assert.Equal(t, 20*time.Second, options.DialerKeepAlive)
assert.Equal(t, 5*time.Second, options.DialerTimeout)
assert.Equal(t, 2, options.RequestRetryCount)
assert.Equal(t, 10*time.Second, options.RequestTimeout)
assert.Equal(t, 3*time.Second, options.TransportExpectContinueTimeout)
assert.Equal(t, 20*time.Second, options.TransportIdleTimeout)
assert.Equal(t, 10, options.TransportMaxIdleConnections)
assert.Equal(t, 5*time.Second, options.TransportTLSHandshakeTimeout)
})
t.Run("no retry", func(t *testing.T) {
options := DefaultClientOptions()
options.RequestRetryCount = 0
client := NewClient(options, nil)
assert.NotNil(t, client)
})
}
// TestClientOptions_ToWhatsOnChainOptions tests setting ToWhatsOnChainOptions()
func TestClientOptions_ToWhatsOnChainOptions(t *testing.T) {
t.Parallel()
t.Run("convert default options to WhatsOnChain options", func(t *testing.T) {
options := DefaultClientOptions()
whatsOnChainOptions := options.ToWhatsOnChainOptions()
assert.Contains(t, whatsOnChainOptions.UserAgent, defaultUserAgent)
assert.Equal(t, 2.0, whatsOnChainOptions.BackOffExponentFactor)
assert.Equal(t, 2*time.Millisecond, whatsOnChainOptions.BackOffInitialTimeout)
assert.Equal(t, 2*time.Millisecond, whatsOnChainOptions.BackOffMaximumJitterInterval)
assert.Equal(t, 10*time.Millisecond, whatsOnChainOptions.BackOffMaxTimeout)
assert.Equal(t, 20*time.Second, whatsOnChainOptions.DialerKeepAlive)
assert.Equal(t, 5*time.Second, whatsOnChainOptions.DialerTimeout)
assert.Equal(t, 2, whatsOnChainOptions.RequestRetryCount)
assert.Equal(t, 10*time.Second, whatsOnChainOptions.RequestTimeout)
assert.Equal(t, 3*time.Second, whatsOnChainOptions.TransportExpectContinueTimeout)
assert.Equal(t, 20*time.Second, whatsOnChainOptions.TransportIdleTimeout)
assert.Equal(t, 10, whatsOnChainOptions.TransportMaxIdleConnections)
assert.Equal(t, 5*time.Second, whatsOnChainOptions.TransportTLSHandshakeTimeout)
})
}