1
|
|
|
package mtnmomo |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/http" |
5
|
|
|
"testing" |
6
|
|
|
|
7
|
|
|
"github.com/stretchr/testify/assert" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
const ( |
11
|
|
|
testSubscriptionKey = "subscriptionKey" |
12
|
|
|
testAPIKey = "apiKey" |
13
|
|
|
testAPIUser = "apiUser" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
func TestWithHTTPClient(t *testing.T) { |
17
|
|
|
t.Run("httpClient is not set when the httpClient is nil", func(t *testing.T) { |
18
|
|
|
// Setup |
19
|
|
|
t.Parallel() |
20
|
|
|
|
21
|
|
|
// Arrange |
22
|
|
|
config := defaultClientConfig() |
23
|
|
|
|
24
|
|
|
// Act |
25
|
|
|
WithHTTPClient(nil).apply(config) |
26
|
|
|
|
27
|
|
|
// Assert |
28
|
|
|
assert.NotNil(t, config.httpClient) |
29
|
|
|
}) |
30
|
|
|
|
31
|
|
|
t.Run("httpClient is set when the httpClient is not nil", func(t *testing.T) { |
32
|
|
|
// Setup |
33
|
|
|
t.Parallel() |
34
|
|
|
|
35
|
|
|
// Arrange |
36
|
|
|
config := defaultClientConfig() |
37
|
|
|
newClient := &http.Client{Timeout: 300} |
38
|
|
|
|
39
|
|
|
// Act |
40
|
|
|
WithHTTPClient(newClient).apply(config) |
41
|
|
|
|
42
|
|
|
// Assert |
43
|
|
|
assert.NotNil(t, config.httpClient) |
44
|
|
|
assert.Equal(t, newClient.Timeout, config.httpClient.Timeout) |
45
|
|
|
}) |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
func TestWithBaseURL(t *testing.T) { |
49
|
|
|
t.Run("baseURL is set successfully", func(t *testing.T) { |
50
|
|
|
// Setup |
51
|
|
|
t.Parallel() |
52
|
|
|
|
53
|
|
|
// Arrange |
54
|
|
|
baseURL := "https://example.com" |
55
|
|
|
config := defaultClientConfig() |
56
|
|
|
|
57
|
|
|
// Act |
58
|
|
|
WithBaseURL(baseURL).apply(config) |
59
|
|
|
|
60
|
|
|
// Assert |
61
|
|
|
assert.Equal(t, config.baseURL, config.baseURL) |
62
|
|
|
}) |
63
|
|
|
|
64
|
|
|
t.Run("tailing / is trimmed from baseURL", func(t *testing.T) { |
65
|
|
|
// Setup |
66
|
|
|
t.Parallel() |
67
|
|
|
|
68
|
|
|
// Arrange |
69
|
|
|
baseURL := "https://example.com/" |
70
|
|
|
config := defaultClientConfig() |
71
|
|
|
|
72
|
|
|
// Act |
73
|
|
|
WithBaseURL(baseURL).apply(config) |
74
|
|
|
|
75
|
|
|
// Assert |
76
|
|
|
assert.Equal(t, "https://example.com", config.baseURL) |
77
|
|
|
}) |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
func TestWithSubscriptionKey(t *testing.T) { |
81
|
|
|
t.Run("subscriptionKey is set successfully", func(t *testing.T) { |
82
|
|
|
// Setup |
83
|
|
|
t.Parallel() |
84
|
|
|
|
85
|
|
|
// Arrange |
86
|
|
|
config := defaultClientConfig() |
87
|
|
|
|
88
|
|
|
// Act |
89
|
|
|
WithSubscriptionKey(testSubscriptionKey).apply(config) |
90
|
|
|
|
91
|
|
|
// Assert |
92
|
|
|
assert.Equal(t, testSubscriptionKey, config.subscriptionKey) |
93
|
|
|
}) |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
func TestWithAPIKey(t *testing.T) { |
97
|
|
|
t.Run("apiKey is set successfully", func(t *testing.T) { |
98
|
|
|
// Setup |
99
|
|
|
t.Parallel() |
100
|
|
|
|
101
|
|
|
// Arrange |
102
|
|
|
config := defaultClientConfig() |
103
|
|
|
|
104
|
|
|
// Act |
105
|
|
|
WithSubscriptionKey(testAPIKey).apply(config) |
106
|
|
|
|
107
|
|
|
// Assert |
108
|
|
|
assert.Equal(t, testAPIKey, config.apiKey) |
109
|
|
|
}) |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
func TestWithAPIUser(t *testing.T) { |
113
|
|
|
t.Run("apiUser is set successfully", func(t *testing.T) { |
114
|
|
|
// Setup |
115
|
|
|
t.Parallel() |
116
|
|
|
|
117
|
|
|
// Arrange |
118
|
|
|
config := defaultClientConfig() |
119
|
|
|
|
120
|
|
|
// Act |
121
|
|
|
WithSubscriptionKey(testAPIUser).apply(config) |
122
|
|
|
|
123
|
|
|
// Assert |
124
|
|
|
assert.Equal(t, testAPIUser, config.apiUser) |
125
|
|
|
}) |
126
|
|
|
} |
127
|
|
|
|