-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconfig_http_test.go
More file actions
383 lines (340 loc) · 9.15 KB
/
Copy pathconfig_http_test.go
File metadata and controls
383 lines (340 loc) · 9.15 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package salesforce
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
func TestWithRoundTripper(t *testing.T) {
tests := []struct {
name string
roundTripper http.RoundTripper
wantErr bool
errorMsg string
}{
{
name: "valid_round_tripper",
roundTripper: &http.Transport{
MaxIdleConns: 10,
},
wantErr: false,
},
{
name: "nil_round_tripper",
roundTripper: nil,
wantErr: true,
errorMsg: "round tripper cannot be nil",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &configuration{}
option := WithRoundTripper(tt.roundTripper)
err := option(config)
if (err != nil) != tt.wantErr {
t.Errorf("WithRoundTripper() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err.Error() != tt.errorMsg {
t.Errorf("WithRoundTripper() error message = %v, want %v", err.Error(), tt.errorMsg)
return
}
if !tt.wantErr {
if config.roundTripper != tt.roundTripper {
t.Errorf(
"WithRoundTripper() roundTripper = %v, want %v",
config.roundTripper,
tt.roundTripper,
)
}
if config.httpClient != nil {
t.Errorf(
"WithRoundTripper() should clear httpClient, got %v",
config.httpClient,
)
}
}
})
}
}
func TestConfigurationHTTPClientDefaults(t *testing.T) {
t.Run("default_http_client", func(t *testing.T) {
config := configuration{}
config.setDefaults()
config.configureHttpClient()
if config.httpClient == nil {
t.Error("setDefaults() should set a default HTTP client")
}
if config.httpClient.Timeout != httpDefaultTimeout {
t.Errorf(
"setDefaults() HTTP client timeout = %v, want %v",
config.httpClient.Timeout,
httpDefaultTimeout,
)
}
transport, ok := config.httpClient.Transport.(*http.Transport)
if !ok {
t.Error("setDefaults() HTTP client should use http.Transport")
}
if transport.MaxIdleConns != httpDefaultMaxIdleConnections {
t.Errorf(
"setDefaults() HTTP transport MaxIdleConns = %v, want %v",
transport.MaxIdleConns,
httpDefaultMaxIdleConnections,
)
}
if transport.IdleConnTimeout != httpDefaultIdleConnTimeout {
t.Errorf(
"setDefaults() HTTP transport IdleConnTimeout = %v, want %v",
transport.IdleConnTimeout,
httpDefaultIdleConnTimeout,
)
}
})
t.Run("with_custom_round_tripper", func(t *testing.T) {
config := configuration{}
customRT := &http.Transport{MaxIdleConns: httpDefaultMaxIdleConnections}
config.roundTripper = customRT
config.setDefaults()
config.configureHttpClient()
if config.httpClient == nil {
t.Error("setDefaults() should create HTTP client with custom round tripper")
}
if config.httpClient.Transport != customRT {
t.Errorf(
"setDefaults() HTTP client transport = %v, want %v",
config.httpClient.Transport,
customRT,
)
}
if config.httpClient.Timeout != httpDefaultTimeout {
t.Errorf(
"setDefaults() HTTP client timeout = %v, want %v",
config.httpClient.Timeout,
httpDefaultTimeout,
)
}
})
t.Run("with_custom_http_client", func(t *testing.T) {
config := configuration{}
customClient := &http.Client{Timeout: httpDefaultTimeout}
config.httpClient = customClient
config.setDefaults()
if config.httpClient != customClient {
t.Errorf(
"setDefaults() should preserve custom HTTP client, got %v, want %v",
config.httpClient,
customClient,
)
}
})
}
func TestConfigurationWithProxy(t *testing.T) {
// 1. Create a dummy target server (we expect the proxy to intercept before this is hit)
targetHit := false
targetServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
targetHit = true
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("target reached"))
if err != nil {
t.Errorf("Failed to write response: %v", err)
}
}),
)
defer targetServer.Close()
// 2. Create a proxy server
proxyHit := false
proxyServer := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proxyHit = true
// Verify this is a proxy request for the target server
if r.URL.Host != targetServer.Listener.Addr().String() {
t.Errorf("Proxy received request for unexpected host: %s", r.URL.Host)
}
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("proxy reached"))
if err != nil {
t.Errorf("Failed to write response: %v", err)
}
}),
)
defer proxyServer.Close()
proxyURL, _ := url.Parse(proxyServer.URL)
// 3. Configure custom round tripper with proxy
customRoundTripper := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
config := &configuration{}
config.setDefaults()
err := WithRoundTripper(customRoundTripper)(config)
if err != nil {
t.Fatalf("Expected no error from WithRoundTripper, got %v", err)
}
config.configureHttpClient()
// 4. Make a request to the target server using the configured client
req, err := http.NewRequest(http.MethodGet, targetServer.URL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
resp, err := config.httpClient.Do(req)
if err != nil {
t.Fatalf("Expected no error executing request, got %v", err)
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
t.Errorf("Failed to close response body: %v", closeErr)
}
}()
body, _ := io.ReadAll(resp.Body)
// 5. Verify the request went through the proxy and not directly to the target
if !proxyHit {
t.Errorf("Expected request to go through proxy but it did not")
}
if targetHit {
t.Errorf("Expected request to be intercepted by proxy but target was hit directly")
}
if string(body) != "proxy reached" {
t.Errorf("Expected response from proxy, got %s", string(body))
}
}
func TestSalesforceGetHTTPClient(t *testing.T) {
customClient := &http.Client{
Timeout: 45 * time.Second,
}
// Create a Salesforce struct directly to avoid network calls during testing
sf := &Salesforce{
auth: &authentication{
AccessToken: "test-token",
InstanceUrl: "https://test.my.salesforce.com",
},
config: &configuration{
httpClient: customClient,
},
}
if sf.GetHTTPClient() != customClient {
t.Errorf("GetHTTPClient() = %v, want %v", sf.GetHTTPClient(), customClient)
}
}
func TestSalesforceAPIVersionInRequests(t *testing.T) {
customVersion := "v64.0"
// Create a Salesforce struct directly to avoid network calls during testing
sf := &Salesforce{
auth: &authentication{
AccessToken: "test-token",
InstanceUrl: "https://test.my.salesforce.com",
},
config: &configuration{
apiVersion: customVersion,
},
}
if sf.GetAPIVersion() != customVersion {
t.Errorf("GetAPIVersion() = %v, want %v", sf.GetAPIVersion(), customVersion)
}
// Verify the configuration is passed correctly to doRequest by checking it's stored in the struct
if sf.config.apiVersion != customVersion {
t.Errorf("config.apiVersion = %v, want %v", sf.config.apiVersion, customVersion)
}
}
func TestWithHTTPTimeout(t *testing.T) {
tests := []struct {
name string
timeout time.Duration
wantErr bool
errorMsg string
}{
{
name: "valid_timeout",
timeout: 30 * time.Second,
wantErr: false,
},
{
name: "valid_timeout_1_second",
timeout: 1 * time.Second,
wantErr: false,
},
{
name: "valid_timeout_1_minute",
timeout: 1 * time.Minute,
wantErr: false,
},
{
name: "zero_timeout",
timeout: 0,
wantErr: true,
errorMsg: "HTTP timeout must be greater than 0",
},
{
name: "negative_timeout",
timeout: -1 * time.Second,
wantErr: true,
errorMsg: "HTTP timeout must be greater than 0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &configuration{}
config.setDefaults() // Set defaults to have a baseline
option := WithHTTPTimeout(tt.timeout)
err := option(config)
if (err != nil) != tt.wantErr {
t.Errorf("WithHTTPTimeout() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && err.Error() != tt.errorMsg {
t.Errorf("WithHTTPTimeout() error message = %v, want %v", err.Error(), tt.errorMsg)
return
}
if !tt.wantErr {
if config.httpTimeout != tt.timeout {
t.Errorf(
"WithHTTPTimeout() httpTimeout = %v, want %v",
config.httpTimeout,
tt.timeout,
)
}
}
})
}
}
func TestWithValidateAuthentication(t *testing.T) {
tests := []struct {
name string
validate bool
wantErr bool
}{
{
name: "validate_true",
validate: true,
wantErr: false,
},
{
name: "validate_false",
validate: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &configuration{}
config.setDefaults() // Set defaults to have a baseline
option := WithValidateAuthentication(tt.validate)
err := option(config)
if (err != nil) != tt.wantErr {
t.Errorf("WithValidateAuthentication() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if config.shouldValidateAuthentication != tt.validate {
t.Errorf(
"WithValidateAuthentication() shouldValidateAuthentication = %v, want %v",
config.shouldValidateAuthentication,
tt.validate,
)
}
}
})
}
}