diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2015-11-05 12:07:54 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2015-11-05 12:07:54 +0200 |
commit | 87105b99cbed413898d7a0859672c8add5baddd7 (patch) | |
tree | d0e52c25f9fae6ff54867c9ee587ff7f83dcc6de /cookie_test.go | |
parent | Enforce MaxConnsPerIP limit to connections served via Server.ServeConn() (diff) | |
download | fasthttp-87105b99cbed413898d7a0859672c8add5baddd7.tar.gz fasthttp-87105b99cbed413898d7a0859672c8add5baddd7.tar.bz2 fasthttp-87105b99cbed413898d7a0859672c8add5baddd7.zip |
Added response cookies support
Diffstat (limited to 'cookie_test.go')
-rw-r--r-- | cookie_test.go | 88 |
1 files changed, 67 insertions, 21 deletions
diff --git a/cookie_test.go b/cookie_test.go index 12bec57..3ec6c52 100644 --- a/cookie_test.go +++ b/cookie_test.go @@ -5,34 +5,80 @@ import ( "testing" ) -func TestParseCookies(t *testing.T) { - testParseCookies(t, "", "") - testParseCookies(t, "=", "") - testParseCookies(t, "foo", "foo") - testParseCookies(t, "=foo", "foo") - testParseCookies(t, "bar=", "bar=") - testParseCookies(t, "xxx=aa;bb=c; =d; ;;e=g", "xxx=aa; bb=c; d; e=g") - testParseCookies(t, "a;b;c; d=1;d=2", "c; d=2") - testParseCookies(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 testParseCookies(t *testing.T, s, expectedS string) { +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([]byte(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", "c; 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) { var kv argsKV - cookies := parseCookies(nil, []byte(s), &kv) - ss := string(appendCookieBytes(nil, cookies)) + cookies := parseRequestCookies(nil, []byte(s), &kv) + 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 TestAppendCookieBytes(t *testing.T) { - testAppendCookieBytes(t, "=", "") - testAppendCookieBytes(t, "foo=", "foo=") - testAppendCookieBytes(t, "=bar", "bar") - testAppendCookieBytes(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) { + 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 testAppendCookieBytes(t *testing.T, s, expectedS string) { +func testAppendRequestCookieBytes(t *testing.T, s, expectedS string) { var cookies []argsKV for _, ss := range strings.Split(s, "&") { tmp := strings.SplitN(ss, "=", 2) @@ -46,7 +92,7 @@ func testAppendCookieBytes(t *testing.T, s, expectedS string) { } prefix := "foobar" - result := string(appendCookieBytes([]byte(prefix), cookies)) + 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) } |