-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
58 lines (48 loc) · 1.42 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
package httpify
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewClient(t *testing.T) {
options := Options{
RetryWaitMin: 1 * time.Second,
RetryWaitMax: 5 * time.Second,
Timeout: 10 * time.Second,
RetryMax: 3,
}
client := NewClient(options)
assert.NotNil(t, client)
assert.Equal(t, client.options, options)
assert.NotNil(t, client.HTTPClient)
}
func TestNewWithHTTPClient(t *testing.T) {
options := Options{
RetryWaitMin: 1 * time.Second,
RetryWaitMax: 5 * time.Second,
Timeout: 10 * time.Second,
RetryMax: 3,
}
customClient := &http.Client{Timeout: 20 * time.Second}
client := NewWithHTTPClient(customClient, options)
assert.NotNil(t, client)
assert.Equal(t, client.HTTPClient, customClient)
assert.Equal(t, client.options, options)
}
func TestSetKillIdleConnections(t *testing.T) {
options := Options{
KillIdleConn: true,
}
// Initialize the client with a default transport
client := NewClient(options)
// Check if the transport is of type *http.Transport
transport, ok := client.HTTPClient.Transport.(*http.Transport)
if !ok {
t.Fatalf("Transport is not of expected type *http.Transport, got %T", client.HTTPClient.Transport)
}
// Set transport properties
client.setKillIdleConnections()
// Validate that idle connections are disabled as per options
assert.True(t, transport.DisableKeepAlives, "Expected DisableKeepAlives to be true")
}