diff --git a/context.go b/context.go index 21bf8188..87393756 100644 --- a/context.go +++ b/context.go @@ -744,15 +744,20 @@ func (ctx *Context) SetCookieKV(key, value string) { // RemoveCookie deletes a cookie by it's name/key func (ctx *Context) RemoveCookie(name string) { + ctx.Response.Header.DelCookie(name) + cookie := fasthttp.AcquireCookie() cookie.SetKey(name) cookie.SetValue("") cookie.SetPath("/") cookie.SetHTTPOnly(true) - exp := time.Now().Add(-time.Duration(1) * time.Minute) //RFC says 1 second, but make sure 1 minute because we are using fasthttp + exp := time.Now().Add(-time.Duration(1) * time.Minute) //RFC says 1 second, but let's do it 1 minute to make sure is working... cookie.SetExpire(exp) ctx.Response.Header.SetCookie(cookie) fasthttp.ReleaseCookie(cookie) + // delete request's cookie also, which is temporarly available + ctx.Request.Header.DelCookie(name) + } // GetFlashes returns all the flash messages for available for this request diff --git a/context_test.go b/context_test.go index 0ae12964..71101cec 100644 --- a/context_test.go +++ b/context_test.go @@ -447,8 +447,13 @@ func TestContextFlashMessages(t *testing.T) { Get("/get_last_flash", func(ctx *Context) { for i, v := range values { if i == len(values)-1 { - val, _ := ctx.GetFlash(v.Key) - ctx.JSON(StatusOK, map[string]string{v.Key: val}) + val, err := ctx.GetFlash(v.Key) + if err == nil { + ctx.JSON(StatusOK, map[string]string{v.Key: val}) + } else { + ctx.JSON(StatusOK, nil) // return nil + } + } } }) @@ -576,6 +581,10 @@ func TestContextSessions(t *testing.T) { // the cookie and all values should be empty }) + // request cookie should be empty + Get("/after_destroy", func(ctx *Context) { + }) + e := Tester(t) e.POST("/set").WithJSON(values).Expect().Status(StatusOK).Cookies().NotEmpty() @@ -588,7 +597,10 @@ func TestContextSessions(t *testing.T) { // test destory which also clears first d := e.GET("/destroy").Expect().Status(StatusOK) d.JSON().Object().Empty() - d.Cookies().ContainsOnly(Config.Sessions.Cookie) + // This removed: d.Cookies().Empty(). Reason: + // httpexpect counts the cookies setted or deleted at the response time, but cookie is not removed, to be really removed needs to SetExpire(now-1second) so, + // test if the cookies removed on the next request, like the browser's behavior. + e.GET("/after_destroy").Expect().Status(StatusOK).Cookies().Empty() // set and clear again e.POST("/set").WithJSON(values).Expect().Status(StatusOK).Cookies().NotEmpty() e.GET("/clear").Expect().Status(StatusOK).JSON().Object().Empty()