Skip to content

Commit

Permalink
Fixed failing integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ezilber-akamai committed Sep 27, 2024
1 parent 77adeb4 commit 33be8fb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 60 deletions.
6 changes: 4 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func coupleAPIErrors(resp *http.Response, err error) (*http.Response, error) {
// If the upstream server fails to respond to the request,
// the HTTP server will respond with a default error page with Content-Type "text/html".
if resp.StatusCode == http.StatusBadGateway && responseContentType == "text/html" {
return nil, &Error{Code: http.StatusBadGateway, Message: http.StatusText(http.StatusBadGateway)}
//return nil, &Error{Code: http.StatusBadGateway, Message: http.StatusText(http.StatusBadGateway)}
return nil, &Error{Code: http.StatusBadGateway, Message: http.StatusText(http.StatusBadGateway), Response: resp}
}

if responseContentType != expectedContentType {
Expand All @@ -93,6 +94,7 @@ func coupleAPIErrors(resp *http.Response, err error) (*http.Response, error) {
return nil, &Error{Code: resp.StatusCode, Message: msg}
}

// Must check if there is no list of reasons in the error before making a call to NewError
apiError, ok := getAPIError(resp)
if !ok {
return nil, NewError(fmt.Errorf("failed to decode response body: %w", err))
Expand All @@ -102,7 +104,7 @@ func coupleAPIErrors(resp *http.Response, err error) (*http.Response, error) {
return resp, nil
}

return nil, &Error{Code: resp.StatusCode, Message: apiError.Errors[0].Error()}
return nil, NewError(resp)
}

return resp, nil
Expand Down
36 changes: 25 additions & 11 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -203,11 +205,6 @@ func TestCoupleAPIErrors(t *testing.T) {
httpClient: ts.Client(),
}

expectedError := &Error{
Code: http.StatusInternalServerError,
Message: "Unexpected Content-Type: Expected: application/json, Received: text/html\nResponse body: " + rawResponse,
}

req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, ts.URL+route, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
Expand All @@ -219,11 +216,23 @@ func TestCoupleAPIErrors(t *testing.T) {
if err != nil {
t.Fatalf("failed to send request: %v", err)
}

expectedError := &Error{
Response: resp,
Code: http.StatusInternalServerError,
Message: "Unexpected Content-Type: Expected: application/json, Received: text/html\nResponse body: " + rawResponse,
}

defer resp.Body.Close()

_, err = coupleAPIErrors(resp, nil)
if diff := cmp.Diff(expectedError, err); diff != "" {
t.Errorf("expected error to match but got diff:\n%s", diff)

if !strings.Contains(err.Error(), strconv.Itoa(expectedError.Code)) {
t.Errorf("expected error code %d, got %d", expectedError.Code, resp.StatusCode)
}

if !strings.Contains(err.Error(), expectedError.Message) {
t.Errorf("expected error message %s, got %s", expectedError.Message, err.Error())
}
})

Expand All @@ -249,13 +258,18 @@ func TestCoupleAPIErrors(t *testing.T) {
}

expectedError := &Error{
Code: http.StatusBadGateway,
Message: http.StatusText(http.StatusBadGateway),
Response: resp,
Code: http.StatusBadGateway,
Message: http.StatusText(http.StatusBadGateway),
}

_, err := coupleAPIErrors(resp, nil)
if !cmp.Equal(err, expectedError) {
t.Errorf("expected error %#v to match error %#v", err, expectedError)
if !strings.Contains(err.Error(), strconv.Itoa(expectedError.Code)) {
t.Errorf("expected error code %d, got %d", expectedError.Code, resp.StatusCode)
}

if !strings.Contains(err.Error(), expectedError.Message) {
t.Errorf("expected error message %s, got %s", expectedError.Message, err.Error())
}
})
}
Expand Down
1 change: 0 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type logger struct {
l *log.Logger
}

// nolint: unused
func createLogger() *logger {
l := &logger{l: log.New(os.Stderr, "", log.Ldate|log.Lmicroseconds)}
return l
Expand Down
84 changes: 38 additions & 46 deletions test/integration/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,25 @@ func ExampleClient_ListTypes_all() {
// ExampleGetType_missing demonstrates the Error type, which allows inspecting
// the request and response. Error codes will be the HTTP status code,
// or sub-100 for errors before the request was issued.
//func ExampleClient_GetType_missing() {
// // Example readers, Ignore this bit of setup code needed to record test fixtures
//
// fmt.Println("starting the test")
//
// linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetType_missing")
// defer teardown()
//
// fmt.Println("made it here")
//
// _, err := linodeClient.GetType(context.Background(), "missing-type")
// if err != nil {
// fmt.Println(err)
// fmt.Println(reflect.TypeOf(err))
//
// if v, ok := err.(*linodego.Error); ok {
// fmt.Println("Request was:", v.Response.Request.URL)
// fmt.Println("Response was:", v.Response.Status)
// fmt.Println("Error was:", v)
// }
// }
//
// // Output:
// // Request was: https://api.linode.com/v4beta/linode/types/missing-type
// // Response was: 404 Not Found
// // Error was: [404] Not found
//}
func ExampleClient_GetType_missing() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetType_missing")
defer teardown()

_, err := linodeClient.GetType(context.Background(), "missing-type")
if err != nil {
if v, ok := err.(*linodego.Error); ok {
fmt.Println("Request was:", v.Response.Request.URL)
fmt.Println("Response was:", v.Response.Status)
fmt.Println("Error was:", v)
}
}

// Output:
// Request was: https://api.linode.com/v4beta/linode/types/missing-type
// Response was: 404 Not Found
// Error was: [404] Not found
}

// ExampleListKernels_all Demonstrates how to list all Linode Kernels. Paginated
// responses are automatically traversed and concatenated when the ListOptions are nil
Expand Down Expand Up @@ -173,25 +165,25 @@ func ExampleClient_GetKernel_specific() {
// First Label still starts: Latest 32
}

//func ExampleClient_GetImage_missing() {
// // Example readers, Ignore this bit of setup code needed to record test fixtures
// linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetImage_missing")
// defer teardown()
//
// _, err := linodeClient.GetImage(context.Background(), "not-found")
// if err != nil {
// if v, ok := err.(*linodego.Error); ok {
// fmt.Println("Request was:", v.Response.Request.URL)
// fmt.Println("Response was:", v.Response.Status)
// fmt.Println("Error was:", v)
// }
// }
//
// // Output:
// // Request was: https://api.linode.com/v4beta/images/not-found
// // Response was: 404 Not Found
// // Error was: [404] Not found
//}
func ExampleClient_GetImage_missing() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
linodeClient, teardown := createTestClient(nil, "fixtures/ExampleGetImage_missing")
defer teardown()

_, err := linodeClient.GetImage(context.Background(), "not-found")
if err != nil {
if v, ok := err.(*linodego.Error); ok {
fmt.Println("Request was:", v.Response.Request.URL)
fmt.Println("Response was:", v.Response.Status)
fmt.Println("Error was:", v)
}
}

// Output:
// Request was: https://api.linode.com/v4beta/images/not-found
// Response was: 404 Not Found
// Error was: [404] Not found
}

func ExampleClient_ListImages_all() {
// Example readers, Ignore this bit of setup code needed to record test fixtures
Expand Down

0 comments on commit 33be8fb

Please sign in to comment.