diff --git a/header.go b/header.go index 2994a84..a08a186 100644 --- a/header.go +++ b/header.go @@ -944,12 +944,38 @@ func (h *RequestHeader) SetCookieBytesKV(key, value []byte) { h.SetCookie(b2s(key), b2s(value)) } -// DelCookie removes cookie under the given key. +// DelClientCookie instructs the client to remove the given cookie. +// +// Use DelCookie if you want just removing the cookie from response header. +func (h *ResponseHeader) DelClientCookie(key string) { + h.DelCookie(key) + + c := AcquireCookie() + c.SetKey(key) + c.SetExpire(CookieExpireDelete) + h.SetCookie(c) + ReleaseCookie(c) +} + +// DelClientCookieBytes instructs the client to remove the given cookie. +// +// Use DelCookieBytes if you want just removing the cookie from response header. +func (h *ResponseHeader) DelClientCookieBytes(key []byte) { + h.DelClientCookie(b2s(key)) +} + +// DelCookie removes cookie under the given key from response header. +// +// Note that DelCookie doesn't remove the cookie from the client. +// Use DelClientCookie instead. func (h *ResponseHeader) DelCookie(key string) { h.cookies = delAllArgs(h.cookies, key) } -// DelCookieBytes removes cookie under the given key. +// DelCookieBytes removes cookie under the given key from response header. +// +// Note that DelCookieBytes doesn't remove the cookie from the client. +// Use DelClientCookieBytes instead. func (h *ResponseHeader) DelCookieBytes(key []byte) { h.DelCookie(b2s(key)) } diff --git a/header_test.go b/header_test.go index dcec5b6..7d55b75 100644 --- a/header_test.go +++ b/header_test.go @@ -10,6 +10,28 @@ import ( "testing" ) +func TestResponseHeaderDelClientCookie(t *testing.T) { + cookieName := "foobar" + + var h ResponseHeader + c := AcquireCookie() + c.SetKey(cookieName) + c.SetValue("aasdfsdaf") + h.SetCookie(c) + + h.DelClientCookieBytes([]byte(cookieName)) + if !h.Cookie(c) { + t.Fatalf("expecting cookie %q", c.Key()) + } + if !c.Expire().Equal(CookieExpireDelete) { + t.Fatalf("unexpected cookie expiration time: %s. Expecting %s", c.Expire(), CookieExpireDelete) + } + if len(c.Value()) > 0 { + t.Fatalf("unexpected cookie value: %q. Expecting empty value", c.Value()) + } + ReleaseCookie(c) +} + func TestResponseHeaderAdd(t *testing.T) { m := make(map[string]struct{}) var h ResponseHeader