-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
client_test.go
245 lines (212 loc) · 7.41 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
package paymail
import (
"fmt"
"net"
"testing"
"time"
"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tonicpow/go-paymail/tester"
)
// newTestClient will return a client for testing purposes
func newTestClient(t *testing.T, opts ...ClientOps) ClientInterface {
// Create a Resty Client
httpClient := tester.MockResty()
if t != nil {
require.NotNil(t, httpClient)
}
// Create a new client
client, err := NewClient(append([]ClientOps{WithRequestTracing(), WithDNSTimeout(15 * time.Second)}, opts...)...)
if t != nil {
require.NotNil(t, client)
require.NoError(t, err)
}
_ = client.WithCustomHTTPClient(httpClient)
// Set the customer resolver with known defaults
r := tester.NewCustomResolver(
client.GetResolver(),
map[string][]string{
testDomain: {"44.225.125.175", "35.165.117.200", "54.190.182.236"},
"norecords.com": {},
},
map[string][]*net.SRV{
DefaultServiceName + DefaultProtocol + testDomain: {{Target: "www." + testDomain, Port: 443, Priority: 10, Weight: 10}},
"invalid" + DefaultProtocol + testDomain: {{Target: "www." + testDomain, Port: 443, Priority: 10, Weight: 10}},
DefaultServiceName + DefaultProtocol + "relayx.io": {{Target: "relayx.io", Port: 443, Priority: 10, Weight: 10}},
DefaultServiceName + DefaultProtocol + "norecords.com": {},
},
map[string][]net.IPAddr{
"example.com": {net.IPAddr{IP: net.ParseIP("8.8.8.8"), Zone: "eth0"}},
},
)
// Set the custom resolver
client.WithCustomResolver(r)
return client
}
// TestNewClient will test the method NewClient()
func TestNewClient(t *testing.T) {
t.Parallel()
t.Run("default client", func(t *testing.T) {
client, err := NewClient()
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, defaultDNSTimeout, client.GetOptions().dnsTimeout)
assert.Equal(t, defaultDNSPort, client.GetOptions().dnsPort)
assert.Equal(t, defaultUserAgent, client.GetOptions().userAgent)
assert.Equal(t, defaultNameServerNetwork, client.GetOptions().nameServerNetwork)
assert.Equal(t, defaultNameServer, client.GetOptions().nameServer)
assert.Equal(t, defaultSSLTimeout, client.GetOptions().sslTimeout)
assert.Equal(t, defaultSSLDeadline, client.GetOptions().sslDeadline)
assert.Equal(t, defaultHTTPTimeout, client.GetOptions().httpTimeout)
assert.Equal(t, defaultRetryCount, client.GetOptions().retryCount)
assert.Equal(t, false, client.GetOptions().requestTracing)
assert.NotEqual(t, 0, len(client.GetOptions().brfcSpecs))
assert.Greater(t, len(client.GetBRFCs()), 6)
})
t.Run("custom http client", func(t *testing.T) {
customHTTPClient := resty.New()
customHTTPClient.SetTimeout(defaultHTTPTimeout)
client, err := NewClient()
assert.NoError(t, err)
assert.NotNil(t, client)
client.WithCustomHTTPClient(customHTTPClient)
})
t.Run("custom dns port", func(t *testing.T) {
client, err := NewClient(WithDNSPort("54"))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, "54", client.GetOptions().dnsPort)
})
t.Run("custom http timeout", func(t *testing.T) {
client, err := NewClient(WithHTTPTimeout(10 * time.Second))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, 10*time.Second, client.GetOptions().httpTimeout)
})
t.Run("custom name server", func(t *testing.T) {
client, err := NewClient(WithNameServer("9.9.9.9"))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, "9.9.9.9", client.GetOptions().nameServer)
})
t.Run("custom name server network", func(t *testing.T) {
client, err := NewClient(WithNameServerNetwork("tcp"))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, "tcp", client.GetOptions().nameServerNetwork)
})
t.Run("custom retry count", func(t *testing.T) {
client, err := NewClient(WithRetryCount(3))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, 3, client.GetOptions().retryCount)
})
t.Run("custom ssl timeout", func(t *testing.T) {
client, err := NewClient(WithSSLTimeout(7 * time.Second))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, 7*time.Second, client.GetOptions().sslTimeout)
})
t.Run("custom ssl deadline", func(t *testing.T) {
client, err := NewClient(WithSSLDeadline(7 * time.Second))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, 7*time.Second, client.GetOptions().sslDeadline)
})
t.Run("custom options", func(t *testing.T) {
client, err := NewClient(WithUserAgent("custom user agent"))
assert.NotNil(t, client)
assert.NoError(t, err)
})
t.Run("custom resolver", func(t *testing.T) {
r := tester.NewCustomResolver(nil, nil, nil, nil)
client, err := NewClient()
assert.NotNil(t, client)
assert.NoError(t, err)
client.WithCustomResolver(r)
})
t.Run("no brfcs", func(t *testing.T) {
var client ClientInterface
client, err := NewClient(WithBRFCSpecs(nil))
assert.NoError(t, err)
assert.NotNil(t, client)
})
}
// TestClient_GetBRFCs will test the method GetBRFCs()
func TestClient_GetBRFCs(t *testing.T) {
t.Parallel()
t.Run("get brfcs", func(t *testing.T) {
client, err := NewClient()
assert.NoError(t, err)
assert.NotNil(t, client)
brfcs := client.GetBRFCs()
assert.Equal(t, 23, len(brfcs))
assert.Equal(t, "b2aa66e26b43", brfcs[0].ID)
})
}
// 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()
assert.NoError(t, err)
assert.NotNil(t, client)
userAgent := client.GetUserAgent()
assert.Equal(t, defaultUserAgent, userAgent)
})
}
// TestClient_GetResolver will test the method GetResolver()
func TestClient_GetResolver(t *testing.T) {
t.Parallel()
t.Run("get resolver", func(t *testing.T) {
client, err := NewClient()
assert.NoError(t, err)
assert.NotNil(t, client)
r := client.GetResolver()
assert.NotNil(t, r)
})
}
// ExampleNewClient example using NewClient()
//
// See more examples in /examples/
func ExampleNewClient() {
client, err := NewClient()
if err != nil {
fmt.Printf("error loading client: %s", err.Error())
return
}
fmt.Printf("loaded client: %s", client.GetOptions().userAgent)
// Output:loaded client: go-paymail: v0.10.3
}
// BenchmarkNewClient benchmarks the method NewClient()
func BenchmarkNewClient(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = NewClient(nil)
}
}
// TestDefaultClientOptions will test the method defaultClientOptions()
func TestDefaultClientOptions(t *testing.T) {
t.Parallel()
options, err := defaultClientOptions()
assert.NoError(t, err)
assert.NotNil(t, options)
assert.Equal(t, defaultDNSTimeout, options.dnsTimeout)
assert.Equal(t, defaultDNSPort, options.dnsPort)
assert.Equal(t, defaultUserAgent, options.userAgent)
assert.Equal(t, defaultNameServerNetwork, options.nameServerNetwork)
assert.Equal(t, defaultNameServer, options.nameServer)
assert.Equal(t, defaultSSLTimeout, options.sslTimeout)
assert.Equal(t, defaultSSLDeadline, options.sslDeadline)
assert.Equal(t, defaultHTTPTimeout, options.httpTimeout)
assert.Equal(t, defaultRetryCount, options.retryCount)
assert.Equal(t, false, options.requestTracing)
assert.NotEqual(t, 0, len(options.brfcSpecs))
assert.Greater(t, len(options.brfcSpecs), 6)
}
// BenchmarkDefaultClientOptions benchmarks the method defaultClientOptions()
func BenchmarkDefaultClientOptions(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = defaultClientOptions()
}
}