Skip to content

Commit

Permalink
fix: add unit test for response logging
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaportPhilipBrowne committed Oct 2, 2024
1 parent 28ef42d commit 2378bfc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
5 changes: 4 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package megaport

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand Down Expand Up @@ -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))

Check failure on line 151 in client_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Write` is not checked (errcheck)
})
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 failure on line 182 in client_test.go

View workflow job for this annotation

GitHub Actions / lint

keys should be written in snake_case (sloglint)

// 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)

Check failure on line 191 in client_test.go

View workflow job for this annotation

GitHub Actions / lint

type assertion must be checked (forcetypeassert)
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"
Expand Down

0 comments on commit 2378bfc

Please sign in to comment.