Skip to content

Commit

Permalink
Fixed more unit and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ezilber-akamai committed Sep 27, 2024
1 parent 5a8b707 commit 39f6fed
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func TestDoRequest_Non2xxStatusCode(t *testing.T) {
t.Fatal("expected error, got nil")
}

httpError, ok := err.(Error)
httpError, ok := err.(*Error)
if !ok {
t.Fatalf("expected error to be of type Error, got %T", err)
}
Expand Down
5 changes: 2 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package linodego

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -99,8 +98,8 @@ func coupleAPIErrors(resp *http.Response, err error) (*http.Response, error) {
return nil, &Error{Code: resp.StatusCode, Message: msg}
}

var apiError APIError
if err := json.NewDecoder(resp.Body).Decode(&apiError); err != nil {
apiError, ok := getAPIError(resp)
if !ok {
return nil, NewError(fmt.Errorf("failed to decode response body: %w", err))
}

Expand Down
4 changes: 2 additions & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestCoupleAPIErrors(t *testing.T) {
httpClient: ts.Client(),
}

expectedError := Error{
expectedError := &Error{
Code: http.StatusInternalServerError,
Message: "Unexpected Content-Type: Expected: application/json, Received: text/html\nResponse body: " + rawResponse,
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestCoupleAPIErrors(t *testing.T) {
},
}

expectedError := Error{
expectedError := &Error{
Code: http.StatusBadGateway,
Message: http.StatusText(http.StatusBadGateway),
}
Expand Down
7 changes: 5 additions & 2 deletions retries.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,20 @@ func getAPIError(resp *http.Response) (*APIError, bool) {
return nil, false
}

// Create a buffer to hold the body
// Read the body into a byte slice
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, false
}
resp.Body = io.NopCloser(bytes.NewReader(body)) // Restore body
// Restore the body for potential further use
resp.Body = io.NopCloser(bytes.NewReader(body))

// Decode the body directly from the byte slice
var apiError APIError
err = json.Unmarshal(body, &apiError)
if err != nil {
return nil, false
}

return &apiError, true
}

0 comments on commit 39f6fed

Please sign in to comment.