Skip to content

Commit

Permalink
add tests for copyto (#545)
Browse files Browse the repository at this point in the history
* add tests for copyto
* add HeaderCopy Test reflect.DeepEqual
  • Loading branch information
xuecai authored and erikdubbelboer committed Feb 17, 2019
1 parent 6453619 commit ed3793a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
5 changes: 5 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 40 additions & 5 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fasthttp
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ed3793a

Please sign in to comment.