fix mock api client
Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
parent
501add44c0
commit
ff1eb37f9e
3 changed files with 38 additions and 66 deletions
|
|
@ -78,7 +78,7 @@ func (c *LiveClient) getAttestations(url, name, digest string, limit int) ([]*At
|
|||
var err error
|
||||
// if no attestation or less than limit, then keep fetching
|
||||
for url != "" && len(attestations) < limit {
|
||||
url, err = c.api.RESTWithNext(c.host, http.MethodGet, url, nil, resp)
|
||||
url, err = c.api.RESTWithNext(c.host, http.MethodGet, url, nil, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -86,7 +86,7 @@ func (c *LiveClient) getAttestations(url, name, digest string, limit int) ([]*At
|
|||
attestations = append(attestations, resp.Attestations...)
|
||||
}
|
||||
|
||||
if len(resp.Attestations) == 0 {
|
||||
if len(attestations) == 0 {
|
||||
return nil, newErrNoAttestations(name, digest)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,19 +26,6 @@ func NewClientWithMockGHClient() Client {
|
|||
}
|
||||
}
|
||||
|
||||
func NewClientWithMockGHClientWithNextPage() Client {
|
||||
fetcher := mockDataGenerator{
|
||||
NumAttestations: 5,
|
||||
}
|
||||
|
||||
return &LiveClient{
|
||||
api: mockAPIClient{
|
||||
OnREST: fetcher.OnRESTSuccess,
|
||||
OnRESTWithNext: fetcher.OnRESTSuccessWithNextPage,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetURL(t *testing.T) {
|
||||
c := LiveClient{}
|
||||
|
||||
|
|
@ -62,14 +49,14 @@ func TestGetByDigest(t *testing.T) {
|
|||
attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, len(attestations), 5)
|
||||
assert.Equal(t, 5, len(attestations))
|
||||
bundle := (attestations)[0].Bundle
|
||||
assert.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle+json;version=0.1")
|
||||
|
||||
attestations, err = c.GetByOwnerAndDigest(testOwner, testDigest, DefaultLimit)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, len(attestations), 5)
|
||||
assert.Equal(t, 5, len(attestations))
|
||||
bundle = (attestations)[0].Bundle
|
||||
assert.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle+json;version=0.1")
|
||||
}
|
||||
|
|
@ -82,7 +69,7 @@ func TestGetByDigestGreaterThanLimit(t *testing.T) {
|
|||
attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, limit)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, len(attestations), 3)
|
||||
assert.Equal(t, 3, len(attestations))
|
||||
bundle := (attestations)[0].Bundle
|
||||
assert.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle+json;version=0.1")
|
||||
|
||||
|
|
@ -95,7 +82,7 @@ func TestGetByDigestGreaterThanLimit(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetByDigestWithNextPage(t *testing.T) {
|
||||
c := NewClientWithMockGHClientWithNextPage()
|
||||
c := NewClientWithMockGHClient()
|
||||
attestations, err := c.GetByRepoAndDigest(testRepo, testDigest, DefaultLimit)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
|
@ -112,7 +99,7 @@ func TestGetByDigestWithNextPage(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetByDigestGreaterThanLimitWithNextPage(t *testing.T) {
|
||||
c := NewClientWithMockGHClientWithNextPage()
|
||||
c := NewClientWithMockGHClient()
|
||||
|
||||
limit := 7
|
||||
// The method should return five results when the limit is not set
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -46,25 +47,20 @@ func (m mockDataGenerator) OnRESTSuccessHelper(hostname, method, p string, body
|
|||
atts[j] = &att
|
||||
}
|
||||
|
||||
var resp AttestationsResponse
|
||||
resp.Attestations = atts
|
||||
|
||||
data = resp
|
||||
resp := AttestationsResponse{
|
||||
Attestations: atts,
|
||||
}
|
||||
|
||||
// // Convert the attestations to JSON
|
||||
// jsonResponse, err := json.Marshal(resp)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
b, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// // Create a buffer containing the JSON response
|
||||
// responseReader := bytes.NewBuffer(jsonResponse)
|
||||
|
||||
// linkHeader := ""
|
||||
// if hasNext {
|
||||
// // Create a link header with the next page
|
||||
// linkHeader = fmt.Sprintf("<%s&after=2>; rel=\"next\"", p)
|
||||
// }
|
||||
err = json.Unmarshal(b, &data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -76,38 +72,27 @@ func (m mockDataGenerator) OnRESTWithNextSuccessHelper(hostname, method, p strin
|
|||
atts[j] = &att
|
||||
}
|
||||
|
||||
var resp AttestationsResponse
|
||||
resp.Attestations = atts
|
||||
|
||||
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)
|
||||
|
||||
// b, err := io.ReadAll(resp.Body)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
// err = json.Unmarshal(b, &data)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
|
||||
|
||||
linkHeader := ""
|
||||
if hasNext {
|
||||
// Create a link header with the next page
|
||||
linkHeader = fmt.Sprintf("<%s&after=2>; rel=\"next\"", p)
|
||||
resp := AttestationsResponse{
|
||||
Attestations: atts,
|
||||
}
|
||||
|
||||
return linkHeader, nil
|
||||
// // 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
|
||||
}
|
||||
|
||||
if hasNext {
|
||||
// return a link header with the next page
|
||||
return fmt.Sprintf("<%s&after=2>; rel=\"next\"", p), nil
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (m mockDataGenerator) OnRESTNoAttestations(hostname, method, p string, body io.Reader, data interface{}) error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue