mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-16 16:17:38 +03:00
Issue #83: Added DelCookie* methods to RequestHeader and ResponseHeader
This commit is contained in:
@@ -929,21 +929,53 @@ func (h *ResponseHeader) SetCookie(cookie *Cookie) {
|
||||
|
||||
// SetCookie sets 'key: value' cookies.
|
||||
func (h *RequestHeader) SetCookie(key, value string) {
|
||||
h.bufKV.key = append(h.bufKV.key[:0], key...)
|
||||
h.SetCookieBytesK(h.bufKV.key, value)
|
||||
h.parseRawHeaders()
|
||||
h.collectCookies()
|
||||
h.cookies = setArg(h.cookies, key, value)
|
||||
}
|
||||
|
||||
// SetCookieBytesK sets 'key: value' cookies.
|
||||
func (h *RequestHeader) SetCookieBytesK(key []byte, value string) {
|
||||
h.bufKV.value = append(h.bufKV.value[:0], value...)
|
||||
h.SetCookieBytesKV(key, h.bufKV.value)
|
||||
h.SetCookie(b2s(key), value)
|
||||
}
|
||||
|
||||
// SetCookieBytesKV sets 'key: value' cookies.
|
||||
func (h *RequestHeader) SetCookieBytesKV(key, value []byte) {
|
||||
h.SetCookie(b2s(key), b2s(value))
|
||||
}
|
||||
|
||||
// DelCookie removes cookie under the given key.
|
||||
func (h *ResponseHeader) DelCookie(key string) {
|
||||
h.cookies = delAllArgs(h.cookies, key)
|
||||
}
|
||||
|
||||
// DelCookieBytes removes cookie under the given key.
|
||||
func (h *ResponseHeader) DelCookieBytes(key []byte) {
|
||||
h.DelCookie(b2s(key))
|
||||
}
|
||||
|
||||
// DelCookie removes cookie under the given key.
|
||||
func (h *RequestHeader) DelCookie(key string) {
|
||||
h.parseRawHeaders()
|
||||
h.collectCookies()
|
||||
h.cookies = setArgBytes(h.cookies, key, value)
|
||||
h.cookies = delAllArgs(h.cookies, key)
|
||||
}
|
||||
|
||||
// DelCookieBytes removes cookie under the given key.
|
||||
func (h *RequestHeader) DelCookieBytes(key []byte) {
|
||||
h.DelCookie(b2s(key))
|
||||
}
|
||||
|
||||
// DelAllCookies removes all the cookies from response headers.
|
||||
func (h *ResponseHeader) DelAllCookies() {
|
||||
h.cookies = h.cookies[:0]
|
||||
}
|
||||
|
||||
// DelAllCookies removes all the cookies from request headers.
|
||||
func (h *RequestHeader) DelAllCookies() {
|
||||
h.parseRawHeaders()
|
||||
h.collectCookies()
|
||||
h.cookies = h.cookies[:0]
|
||||
}
|
||||
|
||||
// Add adds the given 'key: value' header.
|
||||
|
||||
@@ -1117,6 +1117,8 @@ func TestResponseHeaderCookie(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
h.DelAllCookies()
|
||||
|
||||
var h1 ResponseHeader
|
||||
br := bufio.NewReader(w)
|
||||
if err := h1.Read(br); err != nil {
|
||||
@@ -1131,6 +1133,14 @@ func TestResponseHeaderCookie(t *testing.T) {
|
||||
t.Fatalf("unexpected cookie\n%v\nExpected\n%v\n", &c, &expectedC1)
|
||||
}
|
||||
|
||||
h1.DelCookie("foobar")
|
||||
if h.Cookie(&c) {
|
||||
t.Fatalf("Unexpected cookie found: %v", &c)
|
||||
}
|
||||
if h1.Cookie(&c) {
|
||||
t.Fatalf("Unexpected cookie found: %v", &c)
|
||||
}
|
||||
|
||||
c.SetKey("йцук")
|
||||
if !h1.Cookie(&c) {
|
||||
t.Fatalf("cannot find cookie %q", c.Key())
|
||||
@@ -1138,6 +1148,14 @@ func TestResponseHeaderCookie(t *testing.T) {
|
||||
if !equalCookie(&expectedC2, &c) {
|
||||
t.Fatalf("unexpected cookie\n%v\nExpected\n%v\n", &c, &expectedC2)
|
||||
}
|
||||
|
||||
h1.DelCookie("йцук")
|
||||
if h.Cookie(&c) {
|
||||
t.Fatalf("Unexpected cookie found: %v", &c)
|
||||
}
|
||||
if h1.Cookie(&c) {
|
||||
t.Fatalf("Unexpected cookie found: %v", &c)
|
||||
}
|
||||
}
|
||||
|
||||
func equalCookie(c1, c2 *Cookie) bool {
|
||||
@@ -1192,9 +1210,25 @@ func TestRequestHeaderCookie(t *testing.T) {
|
||||
if !bytes.Equal(h1.Cookie("foo"), h.Cookie("foo")) {
|
||||
t.Fatalf("Unexpected cookie value %q. Exepcted %q", h1.Cookie("foo"), h.Cookie("foo"))
|
||||
}
|
||||
h1.DelCookie("foo")
|
||||
if len(h1.Cookie("foo")) > 0 {
|
||||
t.Fatalf("Unexpected cookie found: %q", h1.Cookie("foo"))
|
||||
}
|
||||
if !bytes.Equal(h1.Cookie("привет"), h.Cookie("привет")) {
|
||||
t.Fatalf("Unexpected cookie value %q. Expected %q", h1.Cookie("привет"), h.Cookie("привет"))
|
||||
}
|
||||
h1.DelCookie("привет")
|
||||
if len(h1.Cookie("привет")) > 0 {
|
||||
t.Fatalf("Unexpected cookie found: %q", h1.Cookie("привет"))
|
||||
}
|
||||
|
||||
h.DelAllCookies()
|
||||
if len(h.Cookie("foo")) > 0 {
|
||||
t.Fatalf("Unexpected cookie found: %q", h.Cookie("foo"))
|
||||
}
|
||||
if len(h.Cookie("привет")) > 0 {
|
||||
t.Fatalf("Unexpected cookie found: %q", h.Cookie("привет"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestHeaderSetGet(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user