diff --git a/http.go b/http.go index 3d1429c730..2ae86e8763 100644 --- a/http.go +++ b/http.go @@ -771,7 +771,7 @@ func (req *Request) ResetBody() { func (req *Request) CopyTo(dst *Request) { req.copyToSkipBody(dst) if req.bodyRaw != nil { - dst.bodyRaw = req.bodyRaw + dst.bodyRaw = append(dst.bodyRaw, req.bodyRaw...) if dst.body != nil { dst.body.Reset() } @@ -803,7 +803,7 @@ func (req *Request) copyToSkipBody(dst *Request) { func (resp *Response) CopyTo(dst *Response) { resp.copyToSkipBody(dst) if resp.bodyRaw != nil { - dst.bodyRaw = resp.bodyRaw + dst.bodyRaw = append(dst.bodyRaw, resp.bodyRaw...) if dst.body != nil { dst.body.Reset() } diff --git a/http_test.go b/http_test.go index 52d8a670ab..ba5d675698 100644 --- a/http_test.go +++ b/http_test.go @@ -3033,3 +3033,35 @@ func testRequestMultipartFormPipeEmptyFormField(t *testing.T, boundary string, f return req.Body() } + +func TestReqCopeToRace(t *testing.T) { + req := AcquireRequest() + reqs := make([]*Request, 1000) + for i := 0; i < 1000; i++ { + req.SetBodyRaw([]byte(strconv.Itoa(i))) + tmpReq := AcquireRequest() + req.CopyTo(tmpReq) + reqs[i] = tmpReq + } + for i := 0; i < 1000; i++ { + if strconv.Itoa(i) != string(reqs[i].Body()) { + t.Fatalf("Unexpected req body %s. Expected %s", string(reqs[i].Body()), strconv.Itoa(i)) + } + } +} + +func TestRespCopeToRace(t *testing.T) { + resp := AcquireResponse() + resps := make([]*Response, 1000) + for i := 0; i < 1000; i++ { + resp.SetBodyRaw([]byte(strconv.Itoa(i))) + tmpResq := AcquireResponse() + resp.CopyTo(tmpResq) + resps[i] = tmpResq + } + for i := 0; i < 1000; i++ { + if strconv.Itoa(i) != string(resps[i].Body()) { + t.Fatalf("Unexpected resp body %s. Expected %s", string(resps[i].Body()), strconv.Itoa(i)) + } + } +}