From 1a27436748f64c061533e8e2f06d5a0d88a32bd9 Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Sat, 7 Oct 2023 20:12:52 -0700 Subject: [PATCH] feat(enhancement): add SetBody method in Response struct #721 --- request_test.go | 2 +- response.go | 12 ++++++++++++ retry_test.go | 6 +++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/request_test.go b/request_test.go index 15632ab0..fd1222c5 100644 --- a/request_test.go +++ b/request_test.go @@ -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()) } diff --git a/response.go b/response.go index bda7787b..63c95c41 100644 --- a/response.go +++ b/response.go @@ -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 diff --git a/retry_test.go b/retry_test.go index 05a4bafe..8d58cc16 100644 --- a/retry_test.go +++ b/retry_test.go @@ -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()) } }