Merge remote-tracking branch 'refs/remotes/origin/dev'

This commit is contained in:
Makis Maropoulos 2016-07-08 22:12:13 +02:00
commit 4fd3460662
2 changed files with 21 additions and 4 deletions

View File

@ -744,15 +744,20 @@ func (ctx *Context) SetCookieKV(key, value string) {
// RemoveCookie deletes a cookie by it's name/key // RemoveCookie deletes a cookie by it's name/key
func (ctx *Context) RemoveCookie(name string) { func (ctx *Context) RemoveCookie(name string) {
ctx.Response.Header.DelCookie(name)
cookie := fasthttp.AcquireCookie() cookie := fasthttp.AcquireCookie()
cookie.SetKey(name) cookie.SetKey(name)
cookie.SetValue("") cookie.SetValue("")
cookie.SetPath("/") cookie.SetPath("/")
cookie.SetHTTPOnly(true) 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) cookie.SetExpire(exp)
ctx.Response.Header.SetCookie(cookie) ctx.Response.Header.SetCookie(cookie)
fasthttp.ReleaseCookie(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 // GetFlashes returns all the flash messages for available for this request

View File

@ -447,8 +447,13 @@ func TestContextFlashMessages(t *testing.T) {
Get("/get_last_flash", func(ctx *Context) { Get("/get_last_flash", func(ctx *Context) {
for i, v := range values { for i, v := range values {
if i == len(values)-1 { if i == len(values)-1 {
val, _ := ctx.GetFlash(v.Key) val, err := ctx.GetFlash(v.Key)
ctx.JSON(StatusOK, map[string]string{v.Key: val}) 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 // the cookie and all values should be empty
}) })
// request cookie should be empty
Get("/after_destroy", func(ctx *Context) {
})
e := Tester(t) e := Tester(t)
e.POST("/set").WithJSON(values).Expect().Status(StatusOK).Cookies().NotEmpty() 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 // test destory which also clears first
d := e.GET("/destroy").Expect().Status(StatusOK) d := e.GET("/destroy").Expect().Status(StatusOK)
d.JSON().Object().Empty() 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 // set and clear again
e.POST("/set").WithJSON(values).Expect().Status(StatusOK).Cookies().NotEmpty() e.POST("/set").WithJSON(values).Expect().Status(StatusOK).Cookies().NotEmpty()
e.GET("/clear").Expect().Status(StatusOK).JSON().Object().Empty() e.GET("/clear").Expect().Status(StatusOK).JSON().Object().Empty()