add mock http client

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-12-16 12:22:20 -07:00
parent fb020f2a79
commit e4431a3f55
4 changed files with 32 additions and 1 deletions

View file

@ -25,6 +25,9 @@ func NewClientWithMockGHClient(hasNextPage bool) Client {
githubAPI: mockAPIClient{
OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage,
},
httpClient: mockHttpClient{
OnGet: fetcher.OnGetSuccess,
},
logger: l,
}
}
@ -33,6 +36,9 @@ func NewClientWithMockGHClient(hasNextPage bool) Client {
githubAPI: mockAPIClient{
OnRESTWithNext: fetcher.OnRESTSuccess,
},
httpClient: mockHttpClient{
OnGet: fetcher.OnGetSuccess,
},
logger: l,
}
}

View file

@ -25,7 +25,7 @@ func (m MockClient) GetTrustDomain() (string, error) {
}
func makeTestAttestation() Attestation {
return Attestation{Bundle: data.SigstoreBundle(nil)}
return Attestation{Bundle: data.SigstoreBundle(nil), BundleURL: "https://example.com"}
}
func OnGetByRepoAndDigestSuccess(repo, digest string, limit int) ([]*Attestation, error) {

View file

@ -0,0 +1,25 @@
package api
import (
"io"
"net/http"
"strings"
"github.com/cli/cli/v2/pkg/cmd/attestation/test/data"
)
type mockHttpClient struct {
OnGet func(url string) (*http.Response, error)
}
func (m mockHttpClient) Get(url string) (*http.Response, error) {
return m.OnGet(url)
}
func (m *mockDataGenerator) OnGetSuccess(url string) (*http.Response, error) {
bundle := data.SigstoreBundle(nil)
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(bundle.String())),
}, nil
}