Properly encode cookie without name

This commit is contained in:
Aliaksandr Valialkin
2015-11-04 19:02:49 +02:00
parent a0e1b7f448
commit 4a823fa707
2 changed files with 10 additions and 8 deletions
+4 -2
View File
@@ -7,8 +7,10 @@ import (
func appendCookieBytes(dst []byte, cookies []argsKV) []byte {
for i, n := 0, len(cookies); i < n; i++ {
kv := &cookies[i]
dst = appendQuotedArg(dst, kv.key)
dst = append(dst, '=')
if len(kv.key) > 0 {
dst = appendQuotedArg(dst, kv.key)
dst = append(dst, '=')
}
dst = appendQuotedArg(dst, kv.value)
if i+1 < n {
dst = append(dst, ';', ' ')
+6 -6
View File
@@ -8,11 +8,11 @@ import (
func TestParseCookies(t *testing.T) {
testParseCookies(t, "", "")
testParseCookies(t, "=", "")
testParseCookies(t, "foo", "=foo")
testParseCookies(t, "=foo", "=foo")
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, "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")
}
@@ -26,9 +26,9 @@ func testParseCookies(t *testing.T, s, expectedS string) {
}
func TestAppendCookieBytes(t *testing.T) {
testAppendCookieBytes(t, "=", "=")
testAppendCookieBytes(t, "=", "")
testAppendCookieBytes(t, "foo=", "foo=")
testAppendCookieBytes(t, "=bar", "=bar")
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")
}