Skip to content

Commit ed3793a

Browse files
xuecaierikdubbelboer
authored andcommitted
add tests for copyto (valyala#545)
* add tests for copyto * add HeaderCopy Test reflect.DeepEqual
1 parent 6453619 commit ed3793a

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

args_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func TestArgsCopyTo(t *testing.T) {
269269
testCopyTo(t, &a)
270270

271271
a.Set("xxx", "yyy")
272+
a.AddNoValue("ba")
272273
testCopyTo(t, &a)
273274

274275
a.Del("foo")
@@ -284,6 +285,10 @@ func testCopyTo(t *testing.T, a *Args) {
284285
var b Args
285286
a.CopyTo(&b)
286287

288+
if !reflect.DeepEqual(*a, b) {
289+
t.Fatalf("ArgsCopyTo fail, a: \n%+v\nb: \n%+v\n", *a, b)
290+
}
291+
287292
b.VisitAll(func(k, v []byte) {
288293
if _, ok := keys[string(k)]; !ok {
289294
t.Fatalf("unexpected key %q after copying from %q", k, a.String())

header.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ func (h *RequestHeader) CopyTo(dst *RequestHeader) {
726726
dst.cookiesCollected = h.cookiesCollected
727727
dst.rawHeaders = append(dst.rawHeaders[:0], h.rawHeaders...)
728728
dst.rawHeadersParsed = h.rawHeadersParsed
729+
dst.rawHeadersCopy = append(dst.rawHeadersCopy[:0], h.rawHeadersCopy...)
729730
}
730731

731732
// VisitAll calls f for each header.

header_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9+
"reflect"
910
"strings"
1011
"testing"
1112
)
@@ -1068,6 +1069,14 @@ func TestResponseHeaderCopyTo(t *testing.T) {
10681069
if !bytes.Equal(h1.Peek("aaa-bbb"), h.Peek("AAA-BBB")) {
10691070
t.Fatalf("unexpected aaa-bbb %q. Expected %q", h1.Peek("aaa-bbb"), h.Peek("aaa-bbb"))
10701071
}
1072+
1073+
// flush buf
1074+
h.bufKV = argsKV{}
1075+
h1.bufKV = argsKV{}
1076+
1077+
if !reflect.DeepEqual(h, h1) {
1078+
t.Fatalf("ResponseHeaderCopyTo fail, src: \n%+v\ndst: \n%+v\n", h, h1)
1079+
}
10711080
}
10721081

10731082
func TestRequestHeaderCopyTo(t *testing.T) {
@@ -1092,6 +1101,14 @@ func TestRequestHeaderCopyTo(t *testing.T) {
10921101
if !bytes.Equal(h1.Peek("aaaxxx"), h.Peek("aaaxxx")) {
10931102
t.Fatalf("unexpected aaaxxx %q. Expected %q", h1.Peek("aaaxxx"), h.Peek("aaaxxx"))
10941103
}
1104+
1105+
// flush buf
1106+
h.bufKV = argsKV{}
1107+
h1.bufKV = argsKV{}
1108+
1109+
if !reflect.DeepEqual(h, h1) {
1110+
t.Fatalf("RequestHeaderCopyTo fail, src: \n%+v\ndst: \n%+v\n", h, h1)
1111+
}
10951112
}
10961113

10971114
func TestRequestHeaderConnectionClose(t *testing.T) {
@@ -1124,6 +1141,7 @@ func TestRequestHeaderConnectionClose(t *testing.T) {
11241141
if string(h1.Peek("Connection")) != "close" {
11251142
t.Fatalf("unexpected connection value: %q. Expecting %q", h.Peek("Connection"), "close")
11261143
}
1144+
11271145
}
11281146

11291147
func TestRequestHeaderSetCookie(t *testing.T) {

http_test.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,57 @@ import (
1515
"github.com/valyala/bytebufferpool"
1616
)
1717

18+
func TestRequestCopyTo(t *testing.T) {
19+
var req Request
20+
21+
// empty copy
22+
testRequestCopyTo(t, &req)
23+
24+
// init
25+
expectedContentType := "application/x-www-form-urlencoded; charset=UTF-8"
26+
expectedHost := "test.com"
27+
expectedBody := "0123=56789"
28+
s := fmt.Sprintf("POST / HTTP/1.1\r\nHost: %s\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n%s",
29+
expectedHost, expectedContentType, len(expectedBody), expectedBody)
30+
br := bufio.NewReader(bytes.NewBufferString(s))
31+
if err := req.Read(br); err != nil {
32+
t.Fatalf("unexpected error: %s", err)
33+
}
34+
testRequestCopyTo(t, &req)
35+
36+
}
37+
1838
func TestResponseCopyTo(t *testing.T) {
19-
resp := &Response{}
20-
copyResp := &Response{}
39+
var resp Response
40+
41+
// empty copy
42+
testResponseCopyTo(t, &resp)
2143

2244
// init resp
2345
resp.laddr = zeroTCPAddr
2446
resp.SkipBody = true
2547
resp.Header.SetStatusCode(200)
2648
resp.SetBodyString("test")
49+
testResponseCopyTo(t, &resp)
2750

28-
resp.CopyTo(copyResp)
51+
}
52+
53+
func testRequestCopyTo(t *testing.T, src *Request) {
54+
var dst Request
55+
src.CopyTo(&dst)
2956

30-
if !reflect.DeepEqual(resp, copyResp) {
31-
t.Fatal("ResponseCopyTo fail")
57+
if !reflect.DeepEqual(*src, dst) {
58+
t.Fatalf("RequestCopyTo fail, src: \n%+v\ndst: \n%+v\n", *src, dst)
3259
}
60+
}
3361

62+
func testResponseCopyTo(t *testing.T, src *Response) {
63+
var dst Response
64+
src.CopyTo(&dst)
65+
66+
if !reflect.DeepEqual(*src, dst) {
67+
t.Fatalf("ResponseCopyTo fail, src: \n%+v\ndst: \n%+v\n", *src, dst)
68+
}
3469
}
3570

3671
func TestResponseBodyStreamDeflate(t *testing.T) {

uri_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fasthttp
33
import (
44
"bytes"
55
"fmt"
6+
"reflect"
67
"testing"
78
"time"
89
)
@@ -183,6 +184,22 @@ func testURIPathNormalize(t *testing.T, u *URI, requestURI, expectedPath string)
183184
}
184185
}
185186

187+
func TestURICopyTo(t *testing.T) {
188+
var u URI
189+
var copyU URI
190+
u.CopyTo(&copyU)
191+
if !reflect.DeepEqual(u, copyU) {
192+
t.Fatalf("URICopyTo fail, u: \n%+v\ncopyu: \n%+v\n", u, copyU)
193+
}
194+
195+
u.UpdateBytes([]byte("https://google.com/foo?bar=baz&baraz#qqqq"))
196+
u.CopyTo(&copyU)
197+
if !reflect.DeepEqual(u, copyU) {
198+
t.Fatalf("URICopyTo fail, u: \n%+v\ncopyu: \n%+v\n", u, copyU)
199+
}
200+
201+
}
202+
186203
func TestURIFullURI(t *testing.T) {
187204
var args Args
188205

0 commit comments

Comments
 (0)