From 2378bfc507b9f5b8f79031c0262d32ce35493c88 Mon Sep 17 00:00:00 2001 From: MegaportPhilipBrowne Date: Wed, 2 Oct 2024 10:32:23 -0400 Subject: [PATCH] fix: add unit test for response logging --- client.go | 5 ++++- client_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index de26537..0201121 100644 --- a/client.go +++ b/client.go @@ -291,7 +291,10 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt respBody := resp.Body if c.LogResponseBody { - b, _ := io.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } // Base64 encode the response body encodedBody := base64.StdEncoding.EncodeToString(b) diff --git a/client_test.go b/client_test.go index 83879e6..13a3355 100644 --- a/client_test.go +++ b/client_test.go @@ -2,7 +2,11 @@ package megaport import ( "context" + "encoding/base64" + "encoding/json" "fmt" + "io" + "log/slog" "net/http" "net/http/httptest" "net/http/httputil" @@ -138,6 +142,58 @@ func (suite *ClientTestSuite) TestNewRequest_withCustomUserAgent() { } } +// TestNewRequest_withResponseLogging tests if the NewRequest function returns a request with response logging. +func (suite *ClientTestSuite) TestNewRequest_withResponseLogging() { + // Mock HTTP client and server response + mockResponse := `{"message": "success"}` + mockServer := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte(mockResponse)) + }) + server := httptest.NewServer(mockServer) + defer server.Close() + + // Create a new client with the mock server URL + c := NewClient(nil, nil) + url, _ := url.Parse(server.URL) + c.BaseURL = url + + // Create a new request + req, err := c.NewRequest(ctx, http.MethodGet, "/foo", nil) + if err != nil { + suite.FailNowf("New() unexpected error: %v", err.Error()) + } + + // Perform the request + resp, err := c.Do(ctx, req, nil) + if err != nil { + suite.FailNowf("Do() unexpected error: %v", err.Error()) + } + defer resp.Body.Close() + + // Read and log the response body + body, err := io.ReadAll(resp.Body) + if err != nil { + suite.FailNowf("ReadAll() unexpected error: %v", err.Error()) + } + + // Log the response body + encodedBody := base64.StdEncoding.EncodeToString(body) + c.Logger.DebugContext(ctx, "response body", slog.String("response_body_base64", encodedBody)) + + // Check the response + var result map[string]interface{} + if err := json.Unmarshal(body, &result); err != nil { + suite.FailNowf("Unmarshal() unexpected error: %v", err.Error()) + } + + expectedMessage := "success" + resultMsg := result["message"].(string) + if result["message"] != expectedMessage { + suite.FailNowf("Response message = %s; expected %s", resultMsg, expectedMessage) + } +} + // TestNewRequest_withCustomHeaders tests if the NewRequest function returns a request with custom headers. func (suite *ClientTestSuite) TestNewRequest_withCustomHeaders() { expectedIdentity := "identity"