Skip to content

Commit

Permalink
fix: support null body on status 200
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Sep 28, 2024
1 parent 2ee4206 commit 2a54493
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ func (c *client) handleResponse(req *internalRequest, body []byte, internalError
} else {
internalError.ResponseToString = string(body)

if internalError.ResponseToString == nullBody {
req.withResponse = nil
return nil
}

var err error
if resp, ok := req.withResponse.(json.Unmarshaler); ok {
err = resp.UnmarshalJSON(body)
Expand Down
16 changes: 16 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func TestExecuteRequest(t *testing.T) {
msg := []byte(`{"message":"post successful"}`)
_, _ = w.Write(msg)

} else if r.Method == http.MethodGet && r.URL.Path == "/test-null-body" {
w.WriteHeader(http.StatusOK)
msg := []byte(`null`)
_, _ = w.Write(msg)
} else if r.Method == http.MethodPost && r.URL.Path == "/test-post-encoding" {
w.WriteHeader(http.StatusCreated)
msg := []byte(`{"message":"post successful"}`)
Expand Down Expand Up @@ -236,6 +240,18 @@ func TestExecuteRequest(t *testing.T) {
expectedResp: nil,
wantErr: false,
},
{
name: "Test null body response",
internalReq: &internalRequest{
endpoint: "/test-null-body",
method: http.MethodGet,
withResponse: make([]byte, 0),
contentType: "application/json",
acceptedStatusCodes: []int{http.StatusOK},
},
expectedResp: nil,
wantErr: false,
},
{
name: "400 Bad Request",
internalReq: &internalRequest{
Expand Down

0 comments on commit 2a54493

Please sign in to comment.