[release-branch.go1.14] net/http: make Transport.RoundTrip preserve Requests

Ensure that the exact Request passed to Transport.RoundTrip
is returned in the Response. Do not replace the Request with
a copy when resetting the request body.

Updates #39533.
Fixes #40973.

Change-Id: Ie6fb080c24b0f6625b0761b7aa542af3d2411817
Reviewed-on: https://go-review.googlesource.com/c/go/+/237560
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-on: https://go-review.googlesource.com/c/go/+/249880
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Damien Neil 2020-06-11 13:30:23 -07:00 committed by Dmitri Shuralyov
parent 0d8ee358ec
commit fae8e09d26
2 changed files with 7 additions and 1 deletions

View File

@ -511,6 +511,7 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) {
}
}
origReq := req
req = setupRewindBody(req)
if altRT := t.alternateRoundTripper(req); altRT != nil {
@ -572,6 +573,7 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) {
resp, err = pconn.roundTrip(treq)
}
if err == nil {
resp.Request = origReq
return resp, nil
}

View File

@ -3490,7 +3490,8 @@ func TestRetryRequestsOnError(t *testing.T) {
for i := 0; i < 3; i++ {
t0 := time.Now()
res, err := c.Do(tc.req())
req := tc.req()
res, err := c.Do(req)
if err != nil {
if time.Since(t0) < MaxWriteWaitBeforeConnReuse/2 {
mu.Lock()
@ -3501,6 +3502,9 @@ func TestRetryRequestsOnError(t *testing.T) {
t.Skipf("connection likely wasn't recycled within %d, interfering with actual test; skipping", MaxWriteWaitBeforeConnReuse)
}
res.Body.Close()
if res.Request != req {
t.Errorf("Response.Request != original request; want identical Request")
}
}
mu.Lock()