From 33be8fb95a595f2f39f7fce0f600ee89f9ad1c99 Mon Sep 17 00:00:00 2001 From: ezilber-akamai Date: Fri, 27 Sep 2024 15:44:38 -0400 Subject: [PATCH] Fixed failing integration tests --- errors.go | 6 ++- errors_test.go | 36 +++++++++----- logger.go | 1 - test/integration/example_test.go | 84 +++++++++++++++----------------- 4 files changed, 67 insertions(+), 60 deletions(-) diff --git a/errors.go b/errors.go index 41e3800b..6081168e 100644 --- a/errors.go +++ b/errors.go @@ -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 { @@ -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)) @@ -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 diff --git a/errors_test.go b/errors_test.go index d96027f0..45ed16b7 100644 --- a/errors_test.go +++ b/errors_test.go @@ -10,6 +10,8 @@ import ( "io/ioutil" "net/http" "net/http/httptest" + "strconv" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -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) @@ -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()) } }) @@ -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()) } }) } diff --git a/logger.go b/logger.go index 2e9c0924..115766ca 100644 --- a/logger.go +++ b/logger.go @@ -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 diff --git a/test/integration/example_test.go b/test/integration/example_test.go index 3be785f3..eef225ef 100644 --- a/test/integration/example_test.go +++ b/test/integration/example_test.go @@ -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 @@ -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