1
|
|
|
package client |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/http" |
5
|
|
|
"testing" |
6
|
|
|
|
7
|
|
|
"github.com/stretchr/testify/assert" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
func TestWithHTTPClient(t *testing.T) { |
11
|
|
|
t.Run("httpClient is not set when the httpClient is nil", func(t *testing.T) { |
12
|
|
|
// Setup |
13
|
|
|
t.Parallel() |
14
|
|
|
|
15
|
|
|
// Arrange |
16
|
|
|
config := defaultClientConfig() |
17
|
|
|
|
18
|
|
|
// Act |
19
|
|
|
WithHTTPClient(nil).apply(config) |
20
|
|
|
|
21
|
|
|
// Assert |
22
|
|
|
assert.NotNil(t, config.httpClient) |
23
|
|
|
}) |
24
|
|
|
|
25
|
|
|
t.Run("httpClient is set when the httpClient is not nil", func(t *testing.T) { |
26
|
|
|
// Setup |
27
|
|
|
t.Parallel() |
28
|
|
|
|
29
|
|
|
// Arrange |
30
|
|
|
config := defaultClientConfig() |
31
|
|
|
newClient := &http.Client{Timeout: 300} |
32
|
|
|
|
33
|
|
|
// Act |
34
|
|
|
WithHTTPClient(newClient).apply(config) |
35
|
|
|
|
36
|
|
|
// Assert |
37
|
|
|
assert.NotNil(t, config.httpClient) |
38
|
|
|
assert.Equal(t, newClient.Timeout, config.httpClient.Timeout) |
39
|
|
|
}) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
func TestWithBaseURL(t *testing.T) { |
43
|
|
|
t.Run("baseURL is set successfully", func(t *testing.T) { |
44
|
|
|
// Setup |
45
|
|
|
t.Parallel() |
46
|
|
|
|
47
|
|
|
// Arrange |
48
|
|
|
baseURL := "https://example.com" |
49
|
|
|
config := defaultClientConfig() |
50
|
|
|
|
51
|
|
|
// Act |
52
|
|
|
WithBaseURL(baseURL).apply(config) |
53
|
|
|
|
54
|
|
|
// Assert |
55
|
|
|
assert.Equal(t, config.baseURL, config.baseURL) |
56
|
|
|
}) |
57
|
|
|
|
58
|
|
|
t.Run("tailing / is trimmed from baseURL", func(t *testing.T) { |
59
|
|
|
// Setup |
60
|
|
|
t.Parallel() |
61
|
|
|
|
62
|
|
|
// Arrange |
63
|
|
|
baseURL := "https://example.com/" |
64
|
|
|
config := defaultClientConfig() |
65
|
|
|
|
66
|
|
|
// Act |
67
|
|
|
WithBaseURL(baseURL).apply(config) |
68
|
|
|
|
69
|
|
|
// Assert |
70
|
|
|
assert.Equal(t, "https://example.com", config.baseURL) |
71
|
|
|
}) |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
func TestWithDelay(t *testing.T) { |
75
|
|
|
t.Run("delay is set successfully", func(t *testing.T) { |
76
|
|
|
// Setup |
77
|
|
|
t.Parallel() |
78
|
|
|
|
79
|
|
|
// Arrange |
80
|
|
|
config := defaultClientConfig() |
81
|
|
|
delay := 1 |
82
|
|
|
|
83
|
|
|
// Act |
84
|
|
|
WithDelay(delay).apply(config) |
85
|
|
|
|
86
|
|
|
// Assert |
87
|
|
|
assert.Equal(t, delay, config.delay) |
88
|
|
|
}) |
89
|
|
|
|
90
|
|
|
t.Run("delay is not set when value < 0", func(t *testing.T) { |
91
|
|
|
// Setup |
92
|
|
|
t.Parallel() |
93
|
|
|
|
94
|
|
|
// Arrange |
95
|
|
|
config := defaultClientConfig() |
96
|
|
|
delay := -1 |
97
|
|
|
|
98
|
|
|
// Act |
99
|
|
|
WithDelay(delay).apply(config) |
100
|
|
|
|
101
|
|
|
// Assert |
102
|
|
|
assert.Equal(t, 0, config.delay) |
103
|
|
|
}) |
104
|
|
|
} |
105
|
|
|
|