Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(enhancement): add SetBody method in Response struct #721 #724

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion request_test.go
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
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
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