Allow setting cookies via headers' Set*() methods

This commit is contained in:
Aliaksandr Valialkin
2015-11-10 17:09:12 +02:00
parent a658557d83
commit d4e9c2ea22
2 changed files with 52 additions and 2 deletions
+5 -2
View File
@@ -282,7 +282,10 @@ func (h *ResponseHeader) SetCanonical(key, value []byte) {
case bytes.Equal(strDate, key):
// Date is managed automatically.
case bytes.Equal(strSetCookie, key):
// Cookie must be managed via SetCookie.
var kv *argsKV
h.cookies, kv = allocArg(h.cookies)
kv.key = getCookieKey(kv.key, value)
kv.value = append(kv.value[:0], value...)
default:
h.h = setArg(h.h, key, value)
}
@@ -367,7 +370,7 @@ func (h *RequestHeader) SetCanonical(key, value []byte) {
case bytes.Equal(strConnection, key):
// Connection is managed automatically.
case bytes.Equal(strCookie, key):
// Cookie must be managed via SetCookie.
h.cookies = parseRequestCookies(h.cookies, value)
default:
h.h = setArg(h.h, key, value)
}
+47
View File
@@ -10,6 +10,53 @@ import (
"testing"
)
func TestRequestHeaderSetCookie(t *testing.T) {
var h RequestHeader
h.Set("Cookie", "foo=bar; baz=aaa")
h.Set("cOOkie", "xx=yyy")
if h.GetCookie("foo") != "bar" {
t.Fatalf("Unexpected cookie %q. Expecting %q", h.GetCookie("foo"), "bar")
}
if h.GetCookie("baz") != "aaa" {
t.Fatalf("Unexpected cookie %q. Expecting %q", h.GetCookie("baz"), "aaa")
}
if h.GetCookie("xx") != "yyy" {
t.Fatalf("unexpected cookie %q. Expecting %q", h.GetCookie("xx"), "yyy")
}
}
func TestResponseHeaderSetCookie(t *testing.T) {
var h ResponseHeader
h.Set("set-cookie", "foo=bar; path=/aa/bb; domain=aaa.com")
h.Set("Set-Cookie", "aaaaa=bxx")
var c Cookie
c.Key = []byte("foo")
if !h.GetCookie(&c) {
t.Fatalf("cannot obtain %q cookie", c.Key)
}
if string(c.Value) != "bar" {
t.Fatalf("unexpected cookie value %q. Expected %q", c.Value, "bar")
}
if string(c.Path) != "/aa/bb" {
t.Fatalf("unexpected cookie path %q. Expected %q", c.Path, "/aa/bb")
}
if string(c.Domain) != "aaa.com" {
t.Fatalf("unexpected cookie domain %q. Expected %q", c.Domain, "aaa.com")
}
c.Key = []byte("aaaaa")
if !h.GetCookie(&c) {
t.Fatalf("cannot obtain %q cookie", c.Key)
}
if string(c.Value) != "bxx" {
t.Fatalf("unexpected cookie value %q. Expecting %q", c.Value, "bxx")
}
}
func TestResponseHeaderVisitAll(t *testing.T) {
var h ResponseHeader