diff --git a/internal/api/mock/calls.go b/internal/api/mock/calls.go index 4f4d2d1..136f1b4 100644 --- a/internal/api/mock/calls.go +++ b/internal/api/mock/calls.go @@ -6,6 +6,8 @@ import ( "net/http" "os" "strings" + + "github.com/cli/go-gh/v2/pkg/api" ) type Call struct { @@ -20,7 +22,7 @@ type RawCall struct { Verb string `json:"verb"` Endpoint string `json:"endpoint"` Data any `json:"data"` - Error error `json:"error"` + Error RawError `json:"error"` Response RawResponse `json:"response"` } @@ -30,6 +32,10 @@ type RawResponse struct { Body any `json:"body"` } +type RawError struct { + StatusCode int `json:"status_code"` +} + func LoadCallsFromFile(path string) ([]Call, error) { rawCalls := []RawCall{} @@ -43,23 +49,28 @@ func LoadCallsFromFile(path string) ([]Call, error) { } calls := make([]Call, len(rawCalls)) - for i, call := range rawCalls { - body, err := json.Marshal(call.Response.Body) + for i, rawCall := range rawCalls { + body, err := json.Marshal(rawCall.Response.Body) if err != nil { return nil, err } - calls[i] = Call{ - Verb: call.Verb, - Endpoint: call.Endpoint, - Data: call.Data, - Error: call.Error, + call := Call{ + Verb: rawCall.Verb, + Endpoint: rawCall.Endpoint, + Data: rawCall.Data, Response: &http.Response{ - Header: http.Header(call.Response.Headers), - StatusCode: call.Response.StatusCode, + Header: http.Header(rawCall.Response.Headers), + StatusCode: rawCall.Response.StatusCode, Body: io.NopCloser(strings.NewReader(string(body))), }, } + + if rawCall.Error.StatusCode != 0 { + call.Error = &api.HTTPError{StatusCode: rawCall.Error.StatusCode} + } + + calls[i] = call } return calls, nil diff --git a/internal/api/mock/mock.go b/internal/api/mock/mock.go index 5fe3539..1cdc519 100644 --- a/internal/api/mock/mock.go +++ b/internal/api/mock/mock.go @@ -22,7 +22,7 @@ type MockError struct { } func (e *MockError) Error() string { - return fmt.Sprintf("mock error: %s %s: %s", e.verb, e.endpoint, e.message) + return fmt.Sprintf("mock error for call [%s %s]: %s", e.verb, e.endpoint, e.message) } func New(c []Call) api.Requestor { @@ -47,7 +47,7 @@ func (m *Mock) call(verb, endpoint string) (Call, error) { return Call{}, &MockError{ verb, endpoint, - fmt.Sprintf("unexpected call: mismatch, expected %s %s", call.Verb, call.Endpoint), + fmt.Sprintf("unexpected call: mismatch, expected [%s %s]", call.Verb, call.Endpoint), } } diff --git a/test/integration/001/calls.json b/test/integration/001/calls.json index 0d4060f..c381fc5 100644 --- a/test/integration/001/calls.json +++ b/test/integration/001/calls.json @@ -12,6 +12,13 @@ "body": [{ "id": "1", "subject": { "url": "enrichment#1" } }] } }, + { + "verb": "GET", + "endpoint": "https://api.github.com/notifications?all=true&page=2", + "error": { + "status_code": 404 + } + }, { "verb": "GET", "endpoint": "https://api.github.com/notifications?all=true&page=2",