Files
fasthttp/cookie_test.go
T
2015-11-05 14:28:07 +02:00

103 lines
3.5 KiB
Go

package fasthttp
import (
"strings"
"testing"
)
func TestCookieParse(t *testing.T) {
testCookieParse(t, "foo", "foo")
testCookieParse(t, "foo=bar", "foo=bar")
testCookieParse(t, "foo=", "foo=")
testCookieParse(t, "foo=bar; domain=aaa.com; path=/foo/bar", "foo=bar; domain=aaa.com; path=/foo/bar")
testCookieParse(t, " xxx = yyy ; path=/a/b;;;domain=foobar.com ; expires= Tue, 10 Nov 2009 23:00:00 GMT ; ;;",
"xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b")
}
func testCookieParse(t *testing.T, s, expectedS string) {
var c Cookie
if err := c.Parse(s); err != nil {
t.Fatalf("unexpected error: %s", err)
}
result := string(c.AppendBytes(nil))
if result != expectedS {
t.Fatalf("unexpected cookies %q. Expected %q. Original %q", result, expectedS, s)
}
}
func TestCookieAppendBytes(t *testing.T) {
c := &Cookie{}
testCookieAppendBytes(t, c, "", "bar", "bar")
testCookieAppendBytes(t, c, "foo", "", "foo=")
testCookieAppendBytes(t, c, "ффф", "12 лодлы", "%D1%84%D1%84%D1%84=12%20%D0%BB%D0%BE%D0%B4%D0%BB%D1%8B")
c.Domain = []byte("foobar.com")
testCookieAppendBytes(t, c, "a", "b", "a=b; domain=foobar.com")
c.Path = []byte("/a/b")
testCookieAppendBytes(t, c, "aa", "bb", "aa=bb; domain=foobar.com; path=/a/b")
c.Expire = CookieExpireDelete
testCookieAppendBytes(t, c, "xxx", "yyy", "xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b")
}
func testCookieAppendBytes(t *testing.T, c *Cookie, key, value, expectedS string) {
c.Key = []byte(key)
c.Value = []byte(value)
result := string(c.AppendBytes(nil))
if result != expectedS {
t.Fatalf("Unexpected cookie %q. Expected %q", result, expectedS)
}
}
func TestParseRequestCookies(t *testing.T) {
testParseRequestCookies(t, "", "")
testParseRequestCookies(t, "=", "")
testParseRequestCookies(t, "foo", "foo")
testParseRequestCookies(t, "=foo", "foo")
testParseRequestCookies(t, "bar=", "bar=")
testParseRequestCookies(t, "xxx=aa;bb=c; =d; ;;e=g", "xxx=aa; bb=c; d; e=g")
testParseRequestCookies(t, "a;b;c; d=1;d=2", "a; b; c; d=1; d=2")
testParseRequestCookies(t, " %D0%B8%D0%B2%D0%B5%D1%82=a%20b%3Bc ;s%20s=aaa ", "%D0%B8%D0%B2%D0%B5%D1%82=a%20b%3Bc; s%20s=aaa")
}
func testParseRequestCookies(t *testing.T, s, expectedS string) {
cookies := parseRequestCookies(nil, []byte(s))
ss := string(appendRequestCookieBytes(nil, cookies))
if ss != expectedS {
t.Fatalf("Unexpected cookies after parsing: %q. Expected %q. String to parse %q", ss, expectedS, s)
}
}
func TestAppendRequestCookieBytes(t *testing.T) {
testAppendRequestCookieBytes(t, "=", "")
testAppendRequestCookieBytes(t, "foo=", "foo=")
testAppendRequestCookieBytes(t, "=bar", "bar")
testAppendRequestCookieBytes(t, "привет=a b;c&s s=aaa", "%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82=a%20b%3Bc; s%20s=aaa")
}
func testAppendRequestCookieBytes(t *testing.T, s, expectedS string) {
var cookies []argsKV
for _, ss := range strings.Split(s, "&") {
tmp := strings.SplitN(ss, "=", 2)
if len(tmp) != 2 {
t.Fatalf("Cannot find '=' in %q, part of %q", ss, s)
}
cookies = append(cookies, argsKV{
key: []byte(tmp[0]),
value: []byte(tmp[1]),
})
}
prefix := "foobar"
result := string(appendRequestCookieBytes([]byte(prefix), cookies))
if result[:len(prefix)] != prefix {
t.Fatalf("unexpected prefix %q. Expected %q for cookie %q", result[:len(prefix)], prefix, s)
}
result = result[len(prefix):]
if result != expectedS {
t.Fatalf("Unexpected result %q. Expected %q for cookie %q", result, expectedS, s)
}
}