clean up mock api client

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-04 10:20:59 -07:00
parent ff1eb37f9e
commit fe5d85e169
3 changed files with 14 additions and 70 deletions

View file

@ -16,7 +16,6 @@ const (
)
type apiClient interface {
REST(hostname, method, p string, body io.Reader, data interface{}) error
RESTWithNext(hostname, method, p string, body io.Reader, data interface{}) (string, error)
}

View file

@ -20,7 +20,6 @@ func NewClientWithMockGHClient() Client {
return &LiveClient{
api: mockAPIClient{
OnREST: fetcher.OnRESTSuccess,
OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage,
},
}
@ -125,8 +124,7 @@ func TestGetByDigest_NoAttestationsFound(t *testing.T) {
c := LiveClient{
api: mockAPIClient{
OnREST: fetcher.OnRESTNoAttestations,
OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage,
OnRESTWithNext: fetcher.OnRESTWithNextNoAttestations,
},
}
@ -148,8 +146,7 @@ func TestGetByDigest_Error(t *testing.T) {
c := LiveClient{
api: mockAPIClient{
OnREST: fetcher.OnRESTError,
OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage,
OnRESTWithNext: fetcher.OnRESTWithNextError,
},
}

View file

@ -9,14 +9,9 @@ import (
)
type mockAPIClient struct {
OnREST func(hostname, method, p string, body io.Reader, data interface{}) error
OnRESTWithNext func(hostname, method, p string, body io.Reader, data interface{}) (string, error)
}
func (m mockAPIClient) REST(hostname, method, p string, body io.Reader, data interface{}) error {
return m.OnREST(hostname, method, p, body, data)
}
func (m mockAPIClient) RESTWithNext(hostname, method, p string, body io.Reader, data interface{}) (string, error) {
return m.OnRESTWithNext(hostname, method, p, body, data)
}
@ -25,10 +20,6 @@ type mockDataGenerator struct {
NumAttestations int
}
func (m mockDataGenerator) OnRESTSuccess(hostname, method, p string, body io.Reader, data interface{}) error {
return m.OnRESTSuccessHelper(hostname, method, p, body, data, false)
}
func (m mockDataGenerator) OnRESTSuccessWithNextPage(hostname, method, p string, body io.Reader, data interface{}) (string, error) {
// if path doesn't contain after, it means first time hitting the mock server
// so return the first page and return the link header in the response
@ -40,31 +31,6 @@ func (m mockDataGenerator) OnRESTSuccessWithNextPage(hostname, method, p string,
return m.OnRESTWithNextSuccessHelper(hostname, method, p, body, data, false)
}
func (m mockDataGenerator) OnRESTSuccessHelper(hostname, method, p string, body io.Reader, data interface{}, hasNext bool) error {
atts := make([]*Attestation, m.NumAttestations)
for j := 0; j < m.NumAttestations; j++ {
att := makeTestAttestation()
atts[j] = &att
}
resp := AttestationsResponse{
Attestations: atts,
}
// // Convert the attestations to JSON
b, err := json.Marshal(resp)
if err != nil {
return err
}
err = json.Unmarshal(b, &data)
if err != nil {
return err
}
return nil
}
func (m mockDataGenerator) OnRESTWithNextSuccessHelper(hostname, method, p string, body io.Reader, data interface{}, hasNext bool) (string, error) {
atts := make([]*Attestation, m.NumAttestations)
for j := 0; j < m.NumAttestations; j++ {
@ -95,43 +61,25 @@ func (m mockDataGenerator) OnRESTWithNextSuccessHelper(hostname, method, p strin
return "", nil
}
func (m mockDataGenerator) OnRESTNoAttestations(hostname, method, p string, body io.Reader, data interface{}) error {
var resp AttestationsResponse
resp.Attestations = make([]*Attestation, 0)
data = resp
// Convert the attestations to JSON
// jsonResponse, err := json.Marshal(resp)
// if err != nil {
// return err
// }
// // Create a buffer containing the JSON response
// responseReader := bytes.NewBuffer(jsonResponse)
return nil
}
func (m mockDataGenerator) OnRESTWithNextNoAttestations(hostname, method, p string, body io.Reader, data interface{}) (string, error) {
var resp AttestationsResponse
resp.Attestations = make([]*Attestation, 0)
resp := AttestationsResponse{
Attestations: make([]*Attestation, 0),
}
data = resp
// // Convert the attestations to JSON
b, err := json.Marshal(resp)
if err != nil {
return "", err
}
// Convert the attestations to JSON
// data, err := json.Marshal(resp)
// if err != nil {
// return err
// }
err = json.Unmarshal(b, &data)
if err != nil {
return "", err
}
return "", nil
}
func (m mockDataGenerator) OnRESTError(hostname, method, p string, body io.Reader, data interface{}) error {
return errors.New("failed to get attestations")
}
func (m mockDataGenerator) OnRESTWithNextError(hostname, method, p string, body io.Reader, data interface{}) (string, error) {
return "", errors.New("failed to get attestations")
}