Skip to content

Commit

Permalink
feat(enhancement): add SetBody method in Response struct #721
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Oct 8, 2023
1 parent 1f11e18 commit 1a27436
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ func TestPatchMethod(t *testing.T) {
assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())

resp.body = nil
resp.SetBody(nil)
assertEqual(t, "", resp.String())
}

Expand Down
12 changes: 12 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ func (r *Response) Body() []byte {
return r.body
}

// SetBody method is to set Response body in byte slice. Typically,
// its helpful for test cases.
//
// resp.SetBody([]byte("This is test body content"))
// resp.SetBody(nil)
//
// Since v2.10.0
func (r *Response) SetBody(b []byte) *Response {
r.body = b
return r
}

// Status method returns the HTTP status string for the executed request.
//
// Example: 200 OK
Expand Down
6 changes: 3 additions & 3 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,18 +616,18 @@ func TestClientRetryPost(t *testing.T) {

if resp != nil {
if resp.StatusCode() == http.StatusInternalServerError {
t.Logf("Got response body: %s", string(resp.body))
t.Logf("Got response body: %s", resp.String())
var usersResponse []map[string]interface{}
err := json.Unmarshal(resp.body, &usersResponse)
assertError(t, err)

if !reflect.DeepEqual(users, usersResponse) {
t.Errorf("Expected request body to be echoed back as response body. Instead got: %s", string(resp.body))
t.Errorf("Expected request body to be echoed back as response body. Instead got: %s", resp.String())
}

return
}
t.Errorf("Got unexpected response code: %d with body: %s", resp.StatusCode(), string(resp.body))
t.Errorf("Got unexpected response code: %d with body: %s", resp.StatusCode(), resp.String())
}
}

Expand Down

0 comments on commit 1a27436

Please sign in to comment.