Skip to content

Commit

Permalink
fix: CopyTo doesn't copy bodyraw deeply (#1471)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Jan 14, 2023
1 parent fc2d704 commit e87f84c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down
32 changes: 32 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

0 comments on commit e87f84c

Please sign in to comment.