Skip to content

Commit

Permalink
fix: clean up test cruft from prior implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rainest committed Jan 20, 2023
1 parent 2132d38 commit 0643c89
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 12 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@

## [v0.36.0]

> Release date: 2023/01/20
> Release date: 2023/01/23
### Breaking changes

- `NewAPIError()` now returns a go-kong APIError along with the original raw error body.
- Added `NewAPIErrorWithRaw()` to return a go-kong APIError along with the original raw error body.
[#237](https://github.com/Kong/go-kong/pull/237)
[#267](https://github.com/Kong/go-kong/pull/267)

Expand Down
2 changes: 1 addition & 1 deletion kong/consumer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *ConsumerService) GetByCustomID(ctx context.Context,
}

if len(resp.Data) == 0 {
return nil, NewAPIError(http.StatusNotFound, "Not found", []byte{})
return nil, NewAPIError(http.StatusNotFound, "Not found")
}

return &resp.Data[0], nil
Expand Down
2 changes: 1 addition & 1 deletion kong/developer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *DeveloperService) GetByCustomID(ctx context.Context,
}

if len(resp.Data) == 0 {
return nil, NewAPIError(http.StatusNotFound, "Not found", []byte{})
return nil, NewAPIError(http.StatusNotFound, "Not found")
}

return &resp.Data[0], nil
Expand Down
3 changes: 1 addition & 2 deletions kong/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ type APIError struct {
raw []byte
}

func NewAPIError(code int, msg string, raw []byte) *APIError {
func NewAPIError(code int, msg string) *APIError {
return &APIError{
httpCode: code,
message: msg,
raw: raw,
}
}

Expand Down
4 changes: 2 additions & 2 deletions kong/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestIsNotFoundErr(T *testing.T) {
assert := assert.New(T)
var e error = NewAPIError(404, "", []byte{})
var e error = NewAPIError(404, "")
assert.True(IsNotFoundErr(e))
assert.False(IsNotFoundErr(nil))

Expand Down Expand Up @@ -79,5 +79,5 @@ func (ft *forbiddenTransport) RoundTrip(req *http.Request) (*http.Response, erro
return nil, NewAPIError(
http.StatusForbidden,
"Enterprise license missing or expired",
[]byte(`{ "message": "Enterprise license missing or expired" }`))
)
}
11 changes: 10 additions & 1 deletion kong/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ func hasError(res *http.Response) error {
}

body, _ := io.ReadAll(res.Body) // TODO error in error?
return NewAPIError(res.StatusCode, messageFromBody(body), body)
return NewAPIError(res.StatusCode, messageFromBody(body))
}

func hasErrorRaw(res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode <= 399 {
return nil
}

body, _ := io.ReadAll(res.Body) // TODO error in error?
return NewAPIErrorWithRaw(res.StatusCode, messageFromBody(body), body)
}
67 changes: 66 additions & 1 deletion kong/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,71 @@ import (
)

func TestHasError(T *testing.T) {
for _, tt := range []struct {
name string
response http.Response
want error
}{
{
name: "code 200",
response: http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader("")),
},
},
{
name: "code 404",
response: http.Response{
StatusCode: 404,
Body: io.NopCloser(strings.NewReader(`{"message": "potayto pohtato", "some": "other field"}`)),
},
want: &APIError{
httpCode: 404,
message: "potayto pohtato",
},
},
{
name: "code 404, message field missing",
response: http.Response{
StatusCode: 404,
Body: io.NopCloser(strings.NewReader(`{"nothing": "nothing"}`)),
},
want: &APIError{
httpCode: 404,
message: "",
},
},
{
name: "code 404, empty body",
response: http.Response{
StatusCode: 404,
Body: io.NopCloser(strings.NewReader(``)),
},
want: &APIError{
httpCode: 404,
message: "<failed to parse response body: unexpected end of JSON input>",
},
},
{
name: "code 404, unparseable json",
response: http.Response{
StatusCode: 404,
Body: io.NopCloser(strings.NewReader(`This is not json`)),
},
want: &APIError{
httpCode: 404,
message: "<failed to parse response body: invalid character 'T' looking for beginning of value>",
},
},
} {
T.Run(tt.name, func(T *testing.T) {
got := hasError(&tt.response)
assert.Equal(T, tt.want, got)
})
}
}

func TestHasErrorRaw(T *testing.T) {
for _, tt := range []struct {
name string
response http.Response
Expand Down Expand Up @@ -72,7 +137,7 @@ func TestHasError(T *testing.T) {
},
} {
T.Run(tt.name, func(T *testing.T) {
got := hasError(&tt.response)
got := hasErrorRaw(&tt.response)
assert.Equal(T, tt.want, got)
})
}
Expand Down

0 comments on commit 0643c89

Please sign in to comment.