cli/pkg/cmd/attestation/api/mock_httpClient_test.go
Meredith Lancaster b7f6af03b5 update no attestations found err
Signed-off-by: Meredith Lancaster <malancas@github.com>
2025-01-13 12:42:10 -07:00

69 lines
1.5 KiB
Go

package api
import (
"bytes"
"fmt"
"io"
"net/http"
"github.com/cli/cli/v2/pkg/cmd/attestation/test/data"
"github.com/golang/snappy"
"github.com/stretchr/testify/mock"
)
type mockHttpClient struct {
mock.Mock
}
func (m *mockHttpClient) Get(url string) (*http.Response, error) {
m.On("OnGetSuccess").Return()
m.MethodCalled("OnGetSuccess")
var compressed []byte
compressed = snappy.Encode(compressed, data.SigstoreBundleRaw)
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader(compressed)),
}, nil
}
type failHttpClient struct {
mock.Mock
}
func (m *failHttpClient) Get(url string) (*http.Response, error) {
m.On("OnGetFail").Return()
m.MethodCalled("OnGetFail")
return &http.Response{
StatusCode: 500,
}, fmt.Errorf("failed to fetch with %s", url)
}
type failAfterNCallsHttpClient struct {
mock.Mock
FailOnCallN int
FailOnAllSubsequentCalls bool
NumCalls int
}
func (m *failAfterNCallsHttpClient) Get(url string) (*http.Response, error) {
m.On("OnGetFailAfterNCalls").Return()
m.NumCalls++
if m.NumCalls == m.FailOnCallN || (m.NumCalls > m.FailOnCallN && m.FailOnAllSubsequentCalls) {
m.MethodCalled("OnGetFailAfterNCalls")
return &http.Response{
StatusCode: 500,
}, nil
}
m.MethodCalled("OnGetFailAfterNCalls")
var compressed []byte
compressed = snappy.Encode(compressed, data.SigstoreBundleRaw)
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader(compressed)),
}, nil
}