From ed3793a1e1602989ba50bd20823e8521a4da84af Mon Sep 17 00:00:00 2001 From: xuecai Date: Sun, 17 Feb 2019 18:11:45 +0800 Subject: [PATCH] add tests for copyto (#545) * add tests for copyto * add HeaderCopy Test reflect.DeepEqual --- args_test.go | 5 +++++ header.go | 1 + header_test.go | 18 ++++++++++++++++++ http_test.go | 45 ++++++++++++++++++++++++++++++++++++++++----- uri_test.go | 17 +++++++++++++++++ 5 files changed, 81 insertions(+), 5 deletions(-) diff --git a/args_test.go b/args_test.go index db44750b83..1502414da5 100644 --- a/args_test.go +++ b/args_test.go @@ -269,6 +269,7 @@ func TestArgsCopyTo(t *testing.T) { testCopyTo(t, &a) a.Set("xxx", "yyy") + a.AddNoValue("ba") testCopyTo(t, &a) a.Del("foo") @@ -284,6 +285,10 @@ func testCopyTo(t *testing.T, a *Args) { var b Args a.CopyTo(&b) + if !reflect.DeepEqual(*a, b) { + t.Fatalf("ArgsCopyTo fail, a: \n%+v\nb: \n%+v\n", *a, b) + } + b.VisitAll(func(k, v []byte) { if _, ok := keys[string(k)]; !ok { t.Fatalf("unexpected key %q after copying from %q", k, a.String()) diff --git a/header.go b/header.go index f6e65928c8..190ac3238b 100644 --- a/header.go +++ b/header.go @@ -726,6 +726,7 @@ func (h *RequestHeader) CopyTo(dst *RequestHeader) { dst.cookiesCollected = h.cookiesCollected dst.rawHeaders = append(dst.rawHeaders[:0], h.rawHeaders...) dst.rawHeadersParsed = h.rawHeadersParsed + dst.rawHeadersCopy = append(dst.rawHeadersCopy[:0], h.rawHeadersCopy...) } // VisitAll calls f for each header. diff --git a/header_test.go b/header_test.go index e0f567f327..8dd767a319 100644 --- a/header_test.go +++ b/header_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "io/ioutil" + "reflect" "strings" "testing" ) @@ -1068,6 +1069,14 @@ func TestResponseHeaderCopyTo(t *testing.T) { if !bytes.Equal(h1.Peek("aaa-bbb"), h.Peek("AAA-BBB")) { t.Fatalf("unexpected aaa-bbb %q. Expected %q", h1.Peek("aaa-bbb"), h.Peek("aaa-bbb")) } + + // flush buf + h.bufKV = argsKV{} + h1.bufKV = argsKV{} + + if !reflect.DeepEqual(h, h1) { + t.Fatalf("ResponseHeaderCopyTo fail, src: \n%+v\ndst: \n%+v\n", h, h1) + } } func TestRequestHeaderCopyTo(t *testing.T) { @@ -1092,6 +1101,14 @@ func TestRequestHeaderCopyTo(t *testing.T) { if !bytes.Equal(h1.Peek("aaaxxx"), h.Peek("aaaxxx")) { t.Fatalf("unexpected aaaxxx %q. Expected %q", h1.Peek("aaaxxx"), h.Peek("aaaxxx")) } + + // flush buf + h.bufKV = argsKV{} + h1.bufKV = argsKV{} + + if !reflect.DeepEqual(h, h1) { + t.Fatalf("RequestHeaderCopyTo fail, src: \n%+v\ndst: \n%+v\n", h, h1) + } } func TestRequestHeaderConnectionClose(t *testing.T) { @@ -1124,6 +1141,7 @@ func TestRequestHeaderConnectionClose(t *testing.T) { if string(h1.Peek("Connection")) != "close" { t.Fatalf("unexpected connection value: %q. Expecting %q", h.Peek("Connection"), "close") } + } func TestRequestHeaderSetCookie(t *testing.T) { diff --git a/http_test.go b/http_test.go index dd4d3ca0e6..3253e95199 100644 --- a/http_test.go +++ b/http_test.go @@ -15,22 +15,57 @@ import ( "github.com/valyala/bytebufferpool" ) +func TestRequestCopyTo(t *testing.T) { + var req Request + + // empty copy + testRequestCopyTo(t, &req) + + // init + expectedContentType := "application/x-www-form-urlencoded; charset=UTF-8" + expectedHost := "test.com" + expectedBody := "0123=56789" + s := fmt.Sprintf("POST / HTTP/1.1\r\nHost: %s\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n%s", + expectedHost, expectedContentType, len(expectedBody), expectedBody) + br := bufio.NewReader(bytes.NewBufferString(s)) + if err := req.Read(br); err != nil { + t.Fatalf("unexpected error: %s", err) + } + testRequestCopyTo(t, &req) + +} + func TestResponseCopyTo(t *testing.T) { - resp := &Response{} - copyResp := &Response{} + var resp Response + + // empty copy + testResponseCopyTo(t, &resp) // init resp resp.laddr = zeroTCPAddr resp.SkipBody = true resp.Header.SetStatusCode(200) resp.SetBodyString("test") + testResponseCopyTo(t, &resp) - resp.CopyTo(copyResp) +} + +func testRequestCopyTo(t *testing.T, src *Request) { + var dst Request + src.CopyTo(&dst) - if !reflect.DeepEqual(resp, copyResp) { - t.Fatal("ResponseCopyTo fail") + if !reflect.DeepEqual(*src, dst) { + t.Fatalf("RequestCopyTo fail, src: \n%+v\ndst: \n%+v\n", *src, dst) } +} +func testResponseCopyTo(t *testing.T, src *Response) { + var dst Response + src.CopyTo(&dst) + + if !reflect.DeepEqual(*src, dst) { + t.Fatalf("ResponseCopyTo fail, src: \n%+v\ndst: \n%+v\n", *src, dst) + } } func TestResponseBodyStreamDeflate(t *testing.T) { diff --git a/uri_test.go b/uri_test.go index dcba56e9b5..7ed05f9d54 100644 --- a/uri_test.go +++ b/uri_test.go @@ -3,6 +3,7 @@ package fasthttp import ( "bytes" "fmt" + "reflect" "testing" "time" ) @@ -183,6 +184,22 @@ func testURIPathNormalize(t *testing.T, u *URI, requestURI, expectedPath string) } } +func TestURICopyTo(t *testing.T) { + var u URI + var copyU URI + u.CopyTo(©U) + if !reflect.DeepEqual(u, copyU) { + t.Fatalf("URICopyTo fail, u: \n%+v\ncopyu: \n%+v\n", u, copyU) + } + + u.UpdateBytes([]byte("https://google.com/foo?bar=baz&baraz#qqqq")) + u.CopyTo(©U) + if !reflect.DeepEqual(u, copyU) { + t.Fatalf("URICopyTo fail, u: \n%+v\ncopyu: \n%+v\n", u, copyU) + } + +} + func TestURIFullURI(t *testing.T) { var args Args