Skip to content

Commit 1335e8d

Browse files
authored
feat(enhancement): add SetBody method in Response struct #721 (#724)
1 parent 1f11e18 commit 1335e8d

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

request_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ func TestPatchMethod(t *testing.T) {
12281228
assertError(t, err)
12291229
assertEqual(t, http.StatusOK, resp.StatusCode())
12301230

1231-
resp.body = nil
1231+
resp.SetBody(nil)
12321232
assertEqual(t, "", resp.String())
12331233
}
12341234

response.go

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ func (r *Response) Body() []byte {
3737
return r.body
3838
}
3939

40+
// SetBody method is to set Response body in byte slice. Typically,
41+
// its helpful for test cases.
42+
//
43+
// resp.SetBody([]byte("This is test body content"))
44+
// resp.SetBody(nil)
45+
//
46+
// Since v2.10.0
47+
func (r *Response) SetBody(b []byte) *Response {
48+
r.body = b
49+
return r
50+
}
51+
4052
// Status method returns the HTTP status string for the executed request.
4153
//
4254
// Example: 200 OK

retry_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -616,18 +616,18 @@ func TestClientRetryPost(t *testing.T) {
616616

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

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

628628
return
629629
}
630-
t.Errorf("Got unexpected response code: %d with body: %s", resp.StatusCode(), string(resp.body))
630+
t.Errorf("Got unexpected response code: %d with body: %s", resp.StatusCode(), resp.String())
631631
}
632632
}
633633

0 commit comments

Comments
 (0)