@@ -18,6 +18,8 @@ import (
18
18
"github.com/stretchr/testify/assert"
19
19
"github.com/stretchr/testify/require"
20
20
"github.com/xmidt-org/ancla/model"
21
+ "github.com/xmidt-org/arrange/arrangehttp"
22
+ "github.com/xmidt-org/arrange/arrangetls"
21
23
)
22
24
23
25
const failingURL = "nowhere://"
@@ -28,65 +30,132 @@ var (
28
30
errFails = errors .New ("fails" )
29
31
)
30
32
31
- func TestValidateBasicConfig (t * testing.T ) {
33
+ func TestValidateOptions (t * testing.T ) {
32
34
type testCase struct {
33
35
Description string
34
- Input * BasicClientConfig
35
- Client * http. Client
36
+ ValidateOption Option
37
+ Client BasicClient
36
38
ExpectedErr error
37
- ExpectedConfig * BasicClientConfig
38
39
}
39
40
40
- allDefaultsCaseConfig := & BasicClientConfig {
41
- Address : "example.com" ,
42
- Bucket : "bucket-name" ,
41
+ tcs := []testCase {
42
+ {
43
+ Description : "Nil http client" ,
44
+ ValidateOption : validateHTTPClient (),
45
+ Client : BasicClient {},
46
+ ExpectedErr : ErrHttpClient ,
47
+ },
48
+ {
49
+ Description : "Empty bucket" ,
50
+ ValidateOption : validateBucket (),
51
+ Client : BasicClient {},
52
+ ExpectedErr : ErrBucketEmpty ,
53
+ },
54
+ {
55
+ Description : "Empty store base url" ,
56
+ ValidateOption : validateStoreBaseURL (),
57
+ Client : BasicClient {},
58
+ ExpectedErr : ErrStoreBaseURLEmpty ,
59
+ },
60
+ }
61
+
62
+ for _ , tc := range tcs {
63
+ t .Run (tc .Description , func (t * testing.T ) {
64
+ assert := assert .New (t )
65
+ err := tc .ValidateOption .apply (& tc .Client )
66
+ assert .ErrorIs (err , tc .ExpectedErr )
67
+ })
43
68
}
44
- allDefinedCaseConfig := & BasicClientConfig {
45
- Address : "example.com" ,
46
- Bucket : "amazing-bucket" ,
69
+ }
70
+
71
+ func TestValidateBasicConfig (t * testing.T ) {
72
+ type testCase struct {
73
+ Description string
74
+ Input BasicClientConfig
75
+ Transport http.RoundTripper
76
+ Client * http.Client
77
+ ExpectedErr error
47
78
}
48
79
49
80
tcs := []testCase {
50
81
{
51
82
Description : "No address" ,
52
- Input : & BasicClientConfig {
53
- Bucket : "bucket-name" ,
83
+ Input : BasicClientConfig {
84
+ Bucket : "bucket-name" ,
85
+ HTTPClient : arrangehttp.ClientConfig {},
54
86
},
87
+ Transport : http .DefaultTransport ,
55
88
ExpectedErr : ErrAddressEmpty ,
56
89
},
57
90
{
58
91
Description : "No bucket" ,
59
- Input : & BasicClientConfig {
60
- Address : "example.com" ,
92
+ Input : BasicClientConfig {
93
+ Address : "example.com" ,
94
+ HTTPClient : arrangehttp.ClientConfig {},
61
95
},
96
+ Transport : http .DefaultTransport ,
62
97
ExpectedErr : ErrBucketEmpty ,
63
98
},
64
99
{
65
- Description : "All default values" ,
66
- Input : & BasicClientConfig {
100
+ Description : "Nil http transport" ,
101
+ Input : BasicClientConfig {
102
+ Address : "example.com" ,
103
+ Bucket : "bucket-name" ,
104
+ HTTPClient : arrangehttp.ClientConfig {},
105
+ },
106
+ Transport : nil ,
107
+ ExpectedErr : ErrHttpTransport ,
108
+ },
109
+ {
110
+ Description : "Bad http client config" ,
111
+ Input : BasicClientConfig {
67
112
Address : "example.com" ,
68
113
Bucket : "bucket-name" ,
114
+ HTTPClient : arrangehttp.ClientConfig {
115
+ TLS : & arrangetls.Config {
116
+ Certificates : arrangetls.ExternalCertificates {arrangetls.ExternalCertificate {CertificateFile : "junk-crt" }},
117
+ },
118
+ },
69
119
},
70
- ExpectedConfig : allDefaultsCaseConfig ,
120
+ Transport : http .DefaultTransport ,
121
+ ExpectedErr : ErrHttpClientConfig ,
71
122
},
72
123
{
73
124
Description : "All defined" ,
74
- Input : & BasicClientConfig {
75
- Address : "example.com" ,
76
- Bucket : "amazing-bucket" ,
125
+ Input : BasicClientConfig {
126
+ Address : "example.com" ,
127
+ Bucket : "amazing-bucket" ,
128
+ HTTPClient : arrangehttp.ClientConfig {},
77
129
},
78
- ExpectedConfig : allDefinedCaseConfig ,
130
+ Transport : http . DefaultTransport ,
79
131
},
80
132
}
81
133
82
134
for _ , tc := range tcs {
83
135
t .Run (tc .Description , func (t * testing.T ) {
136
+ opts := append (
137
+ defaultOptions ,
138
+ Address (tc .Input .Address ),
139
+ Bucket (tc .Input .Bucket ),
140
+ HTTPClient (tc .Input .HTTPClient ),
141
+ HTTPTransport (tc .Transport ),
142
+ defaultValidateOptions ,
143
+ )
144
+
84
145
assert := assert .New (t )
85
- err := validateBasicConfig (tc .Input )
86
- assert .Equal (tc .ExpectedErr , err )
87
- if tc .ExpectedErr == nil {
88
- assert .Equal (tc .ExpectedConfig , tc .Input )
146
+ client := BasicClient {}
147
+ errs := opts .apply (& client )
148
+ if tc .ExpectedErr != nil {
149
+ assert .ErrorIs (errs , tc .ExpectedErr )
150
+
151
+ return
89
152
}
153
+
154
+ assert .NoError (errs )
155
+ assert .Equal (tc .Input .Address + storeAPIPath , client .storeBaseURL )
156
+ assert .Equal (tc .Input .Bucket , client .bucket )
157
+ assert .Equal (tc .Transport , client .client .Transport )
158
+ assert .NotNil (client .client )
90
159
})
91
160
}
92
161
}
@@ -174,10 +243,12 @@ func TestSendRequest(t *testing.T) {
174
243
server := httptest .NewServer (echoHandler )
175
244
defer server .Close ()
176
245
177
- client , err := NewBasicClient (BasicClientConfig {
178
- Address : "example.com" ,
179
- Bucket : "bucket-name" ,
180
- })
246
+ opts := Options {
247
+ Address ("example.com" ),
248
+ Bucket ("bucket-name" ),
249
+ HTTPClient (arrangehttp.ClientConfig {}),
250
+ }
251
+ client , err := NewBasicClient (opts )
181
252
182
253
acquirer .On ("Acquire" ).Return (tc .MockAuth , tc .MockError )
183
254
client .auth = acquirer
@@ -285,10 +356,12 @@ func TestGetItems(t *testing.T) {
285
356
rw .Write (tc .ResponsePayload )
286
357
}))
287
358
288
- client , err := NewBasicClient (BasicClientConfig {
289
- Address : server .URL ,
290
- Bucket : bucket ,
291
- })
359
+ opts := Options {
360
+ Address (server .URL ),
361
+ Bucket (bucket ),
362
+ HTTPClient (arrangehttp.ClientConfig {}),
363
+ }
364
+ client , err := NewBasicClient (opts )
292
365
293
366
require .Nil (err )
294
367
@@ -440,10 +513,12 @@ func TestPushItem(t *testing.T) {
440
513
}
441
514
}))
442
515
443
- client , err := NewBasicClient (BasicClientConfig {
444
- Address : server .URL ,
445
- Bucket : bucket ,
446
- })
516
+ opts := Options {
517
+ Address (server .URL ),
518
+ Bucket (bucket ),
519
+ HTTPClient (arrangehttp.ClientConfig {}),
520
+ }
521
+ client , err := NewBasicClient (opts )
447
522
448
523
acquirer .On ("Acquire" ).Return (tc .MockAuth , tc .MockError )
449
524
client .auth = acquirer
@@ -552,10 +627,12 @@ func TestRemoveItem(t *testing.T) {
552
627
rw .Write (tc .ResponsePayload )
553
628
}))
554
629
555
- client , err := NewBasicClient (BasicClientConfig {
556
- Address : server .URL ,
557
- Bucket : bucket ,
558
- })
630
+ opts := Options {
631
+ Address (server .URL ),
632
+ Bucket (bucket ),
633
+ HTTPClient (arrangehttp.ClientConfig {}),
634
+ }
635
+ client , err := NewBasicClient (opts )
559
636
560
637
acquirer .On ("Acquire" ).Return (tc .MockAuth , tc .MockError )
561
638
client .auth = acquirer
0 commit comments