@@ -25,6 +25,7 @@ import (
2525 "testing"
2626 "time"
2727
28+ "github.com/cenkalti/backoff/v4"
2829 "github.com/stretchr/testify/assert"
2930
3031 "github.com/lacework/go-sdk/api"
@@ -87,6 +88,7 @@ func TestNewClientWithOptions(t *testing.T) {
8788 api .WithExpirationTime (1800 ),
8889 api .WithApiV2 (),
8990 api .WithTimeout (time .Minute * 5 ),
91+ api .WithRetries (backoff .NewExponentialBackOff ()),
9092 api .WithLogLevel ("DEBUG" ),
9193 api .WithHeader ("User-Agent" , "test-agent" ),
9294 api .WithTokenFromKeys ("KEY" , "SECRET" ), // this option has to be the last one
@@ -121,6 +123,7 @@ func TestCopyClientWithOptions(t *testing.T) {
121123 api .WithURL (fakeServer .URL ()),
122124 api .WithExpirationTime (1800 ),
123125 api .WithTimeout (time .Minute * 5 ),
126+ api .WithRetries (backoff .NewExponentialBackOff ()),
124127 api .WithLogLevel ("DEBUG" ),
125128 api .WithHeader ("User-Agent" , "test-agent" ),
126129 api .WithTokenFromKeys ("KEY" , "SECRET" ), // this option has to be the last one
@@ -138,6 +141,7 @@ func TestCopyClientWithOptions(t *testing.T) {
138141 if assert .Nil (t , err ) {
139142 assert .Equal (t , c .ApiVersion (), newExactClient .ApiVersion (), "copy client mismatch" )
140143 assert .Equal (t , c .URL (), newExactClient .URL (), "copy client mismatch" )
144+ assert .Equal (t , c .Retries (), newExactClient .Retries (), "copy retrying mistmatch" )
141145 assert .True (t , newExactClient .ValidAuth ())
142146 }
143147
@@ -151,12 +155,14 @@ func TestCopyClientWithOptions(t *testing.T) {
151155 api .WithExpirationTime (3600 ),
152156 api .WithApiV2 (),
153157 api .WithTimeout (time .Minute * 60 ), // LOL!
158+ api .WithRetries (nil ),
154159 api .WithLogLevel ("INFO" ),
155160 api .WithOrgAccess (),
156161 )
157162 if assert .Nil (t , err ) {
158163 assert .NotEqual (t , c .ApiVersion (), newModifiedClient .ApiVersion (), "copy modified client mismatch" )
159164 assert .NotEqual (t , c .URL (), newModifiedClient .URL (), "copy modified client mismatch" )
165+ assert .NotEqual (t , c .Retries (), newModifiedClient .Retries (), "copy modified retrying policy" )
160166 assert .Equal (t , "v2" , newModifiedClient .ApiVersion (), "copy modified API version should be v2" )
161167 assert .Equal (t , "https://new.lacework.net/" , newModifiedClient .URL (), "copy modified client mismatch" )
162168 assert .True (t , newExactClient .ValidAuth ())
@@ -232,3 +238,45 @@ func TestTLSHandshakeTimeout(t *testing.T) {
232238 _ , err = clientWithTimeout .V2 .AlertChannels .List ()
233239 assert .NoError (t , err )
234240}
241+
242+ func TestClientWithRetries (t * testing.T ) {
243+ fakeServer := lacework .MockUnstartedServer ()
244+ fakeServer .UseApiV2 ()
245+ apiPath := "AlertChannels"
246+ fakeServer .MockToken ("TOKEN" )
247+ fakeServer .Server .StartTLS ()
248+ defer fakeServer .Close ()
249+
250+ requestNumber := 0
251+ fakeServer .MockAPI (apiPath , func (w http.ResponseWriter , r * http.Request ) {
252+ requestNumber += 1
253+ if requestNumber == 3 {
254+ w .WriteHeader (200 )
255+ fmt .Fprintf (w , "{}" )
256+ } else {
257+ w .WriteHeader (500 )
258+ }
259+ })
260+
261+ transport := & http.Transport {TLSClientConfig : & tls.Config {InsecureSkipVerify : true }}
262+ client , err := api .NewClient ("test" ,
263+ api .WithApiV2 (),
264+ api .WithToken ("TOKEN" ),
265+ api .WithURL (fakeServer .URL ()),
266+ api .WithTransport (transport ),
267+ )
268+
269+ _ , err = client .V2 .AlertChannels .List ()
270+ assert .Error (t , err )
271+
272+ clientWithRetries , err := api .NewClient ("test" ,
273+ api .WithApiV2 (),
274+ api .WithToken ("TOKEN" ),
275+ api .WithURL (fakeServer .URL ()),
276+ api .WithTransport (transport ),
277+ api .WithRetries (backoff .NewExponentialBackOff ()),
278+ )
279+
280+ _ , err = clientWithRetries .V2 .AlertChannels .List ()
281+ assert .NoError (t , err )
282+ }
0 commit comments