diff --git a/header.go b/header.go index bcfb411..f5ceed6 100644 --- a/header.go +++ b/header.go @@ -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) } diff --git a/header_test.go b/header_test.go index deee189..80d97dd 100644 --- a/header_test.go +++ b/header_test.go @@ -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