|
| 1 | +package kernel_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + kernel "github.com/kernel/kernel-go-sdk" |
| 12 | + "github.com/kernel/kernel-go-sdk/option" |
| 13 | +) |
| 14 | + |
| 15 | +type vaultRetryTransport func(*http.Request) (*http.Response, error) |
| 16 | + |
| 17 | +func (f vaultRetryTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } |
| 18 | + |
| 19 | +func TestVaultFillDoesNotRetry(t *testing.T) { |
| 20 | + for _, status := range []int{0, 409, 429, 500} { |
| 21 | + t.Run(fmt.Sprint(status), func(t *testing.T) { |
| 22 | + calls := 0 |
| 23 | + client := kernel.NewClient(option.WithAPIKey("test"), option.WithMaxRetries(1), option.WithHTTPClient(&http.Client{Transport: vaultRetryTransport(func(r *http.Request) (*http.Response, error) { |
| 24 | + calls++ |
| 25 | + if status == 0 { |
| 26 | + return nil, io.ErrUnexpectedEOF |
| 27 | + } |
| 28 | + return &http.Response{StatusCode: status, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader("{}")), Request: r}, nil |
| 29 | + })})) |
| 30 | + _, err := client.Vaults.Items.PerformOperation(context.Background(), "login", kernel.VaultItemPerformOperationParams{ |
| 31 | + IDOrName: "vault", OfFill: &kernel.FillVaultItemOperationRequestParam{BrowserID: "browser", Fields: []kernel.VaultFillFieldParam{{Field: "password", Selector: "#password"}}}, |
| 32 | + }) |
| 33 | + if err == nil { |
| 34 | + t.Fatal("expected error") |
| 35 | + } |
| 36 | + if calls != 1 { |
| 37 | + t.Fatalf("got %d attempts, want 1", calls) |
| 38 | + } |
| 39 | + }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestVaultReadKeepsClientRetries(t *testing.T) { |
| 44 | + calls := 0 |
| 45 | + client := kernel.NewClient(option.WithAPIKey("test"), option.WithMaxRetries(1), option.WithHTTPClient(&http.Client{Transport: vaultRetryTransport(func(r *http.Request) (*http.Response, error) { |
| 46 | + calls++ |
| 47 | + return nil, io.ErrUnexpectedEOF |
| 48 | + })})) |
| 49 | + _, err := client.Vaults.Items.Get(context.Background(), "login", kernel.VaultItemGetParams{IDOrName: "vault"}) |
| 50 | + if err == nil { |
| 51 | + t.Fatal("expected error") |
| 52 | + } |
| 53 | + if calls != 2 { |
| 54 | + t.Fatalf("got %d attempts, want 2", calls) |
| 55 | + } |
| 56 | +} |
0 commit comments