-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_test.go
73 lines (68 loc) · 1.5 KB
/
option_test.go
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
package fincode
import (
"context"
"net/http"
"testing"
"github.com/brianvoe/gofakeit/v6"
"github.com/hashicorp/go-retryablehttp"
"github.com/pepabo/fincode-go/api"
"golang.org/x/sync/errgroup"
)
func TestWithHTTPClient(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, err := New(Endpoint(testEndpoint), WithHTTPClient(retryableHTTPClient(t)))
if err != nil {
t.Fatal(err)
}
faker := gofakeit.NewCrypto()
eg := new(errgroup.Group)
for range 10 {
eg.Go(func() error {
id := newID(t)
name := faker.Name()
email := faker.Email()
res, err := c.CustomersPost(ctx, &api.CustomersPostReq{
ID: id,
Name: name,
Email: email,
})
if err != nil {
return err
}
_, ok := res.(*api.CustomersPostOK)
if !ok {
t.Errorf("unexpected response type: %T", res)
}
{
res, err := c.CustomersIDDelete(ctx, api.CustomersIDDeleteParams{
ID: id,
})
if err != nil {
t.Fatal(err)
}
_, ok := res.(*api.CustomersIDDeleteOK)
if !ok {
t.Errorf("unexpected response type: %T", res)
}
}
return nil
})
}
if err := eg.Wait(); err != nil {
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()
}