fix: CopyTo doesn't copy bodyraw deeply (#1471)

This commit is contained in:
kinggo
2023-01-14 10:44:11 +08:00
committed by GitHub
parent fc2d704c1f
commit e87f84c51a
2 changed files with 34 additions and 2 deletions
+2 -2
View File
@@ -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()
}
+32
View File
@@ -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))
}
}
}