Skip to content

Commit

Permalink
Set retryableHTTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Jun 27, 2024
1 parent 4f93bf9 commit c28097f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
3 changes: 2 additions & 1 deletion customers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
const testEndpoint = "https://api.test.fincode.jp/v1"

func TestCustomers(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := New(Endpoint(testEndpoint))
c, err := New(Endpoint(testEndpoint), WithHTTPClient(retryableHTTPClient(t)))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
go.opentelemetry.io/otel/metric v1.25.0
go.opentelemetry.io/otel/trace v1.25.0
go.uber.org/multierr v1.11.0
golang.org/x/sync v0.7.0
)

require (
Expand Down Expand Up @@ -142,7 +143,6 @@ require (
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
26 changes: 16 additions & 10 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@ import (
)

func TestWithHTTPClient(t *testing.T) {
t.Parallel()
ctx := context.Background()
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
retryClient.Logger = nil
retryClient.CheckRetry = func(ctx context.Context, res *http.Response, err error) (bool, error) {
if res.StatusCode == 403 {
return true, nil
}
return false, nil
}
c, err := New(Endpoint(testEndpoint), WithHTTPClient(retryClient.StandardClient()))
c, err := New(Endpoint(testEndpoint), WithHTTPClient(retryableHTTPClient(t)))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -65,3 +57,17 @@ func TestWithHTTPClient(t *testing.T) {
t.Error(err)
}
}

func retryableHTTPClient(t *testing.T) *http.Client {
t.Helper()
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
retryClient.Logger = nil
retryClient.CheckRetry = func(ctx context.Context, res *http.Response, err error) (bool, error) {
if res.StatusCode == 403 {
return true, nil
}
return false, nil
}
return retryClient.StandardClient()
}
3 changes: 2 additions & 1 deletion payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestPayments(t *testing.T) {
t.Parallel()
ctx := context.Background()
ts := NewPaymentServer(t)
opts := []runn.Option{
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestPayments(t *testing.T) {
t.Logf("customer_id: %s", customerID)
t.Logf("card_id: %s", cardID)

c, err := New(Endpoint(testEndpoint))
c, err := New(Endpoint(testEndpoint), WithHTTPClient(retryableHTTPClient(t)))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewPaymentServer(t *testing.T) *httptest.Server {
t.Helper()
ctx := context.Background()
faker := gofakeit.NewCrypto()
c, err := New(Endpoint(testEndpoint))
c, err := New(Endpoint(testEndpoint), WithHTTPClient(retryableHTTPClient(t)))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c28097f

Please sign in to comment.