Skip to content

Commit

Permalink
Restore Response.Body when AutoReadResponse is enabled(#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Aug 14, 2022
1 parent 6984f39 commit 6e38959
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package req

import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
Expand Down Expand Up @@ -1191,6 +1192,8 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
if err != nil {
return
}
// restore body for re-reads
resp.Body = ioutil.NopCloser(bytes.NewReader(resp.body))
}

for _, f := range r.client.afterResponse {
Expand Down
11 changes: 11 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,14 @@ func TestDownloadCallback(t *testing.T) {
assertSuccess(t, resp, err)
tests.AssertEqual(t, true, n > 0)
}

func TestRestoreResponseBody(t *testing.T) {
c := tc()
resp, err := c.R().Get("/")
assertSuccess(t, resp, err)
tests.AssertNoError(t, err)
tests.AssertEqual(t, true, len(resp.Bytes()) > 0)
body, err := ioutil.ReadAll(resp.Body)
tests.AssertNoError(t, err)
tests.AssertEqual(t, true, len(body) > 0)
}

0 comments on commit 6e38959

Please sign in to comment.