Skip to content

Commit

Permalink
test(integration): add error handling for responses
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 committed Sep 14, 2024
1 parent fe59cbf commit 21cf60c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
31 changes: 21 additions & 10 deletions internal/api/mock/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"os"
"strings"

"github.com/cli/go-gh/v2/pkg/api"
)

type Call struct {
Expand All @@ -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"`
}

Expand All @@ -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{}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/api/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/integration/001/calls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 21cf60c

Please sign in to comment.