aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar xuecai <bluesnow1st@sina.com> 2019-02-17 18:11:45 +0800
committerGravatar Erik Dubbelboer <erik@dubbelboer.com> 2019-02-17 10:11:45 +0000
commited3793a1e1602989ba50bd20823e8521a4da84af (patch)
treefda5d2754821cf023d595abd03b4469e94cd095c
parentRemove methods that had been deprecated in 2015 (diff)
downloadfasthttp-ed3793a1e1602989ba50bd20823e8521a4da84af.tar.gz
fasthttp-ed3793a1e1602989ba50bd20823e8521a4da84af.tar.bz2
fasthttp-ed3793a1e1602989ba50bd20823e8521a4da84af.zip
add tests for copyto (#545)v1.2.0
* add tests for copyto * add HeaderCopy Test reflect.DeepEqual
-rw-r--r--args_test.go5
-rw-r--r--header.go1
-rw-r--r--header_test.go18
-rw-r--r--http_test.go45
-rw-r--r--uri_test.go17
5 files changed, 81 insertions, 5 deletions
diff --git a/args_test.go b/args_test.go
index db44750..1502414 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 f6e6592..190ac32 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 e0f567f..8dd767a 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 dd4d3ca..3253e95 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 dcba56e..7ed05f9 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(&copyU)
+ 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(&copyU)
+ 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