Пример #1
0
// SetFlash sets a flash message, accepts 2 parameters the key(string) and the value(string)
// the value will be available on the NEXT request
func (ctx *Context) SetFlash(key string, value string) {
	cKey := flashMessageCookiePrefix + key
	cValue := base64.URLEncoding.EncodeToString([]byte(value))

	c := fasthttp.AcquireCookie()
	c.SetKey(cKey)
	c.SetValue(cValue)
	c.SetPath("/")
	c.SetHTTPOnly(true)
	ctx.RequestCtx.Response.Header.SetCookie(c)
	fasthttp.ReleaseCookie(c)

	// if any bug on the future: this works, and the above:
	//ctx.RequestCtx.Request.Header.SetCookie(cKey, cValue)
	//ctx.RequestCtx.Response.Header.Add("Set-Cookie", cKey+"="+cValue+"; Path:/; HttpOnly")
	//

	/*c := &fasthttp.Cookie{}
	c.SetKey(cKey)
	c.SetValue(cValue)
	c.SetPath("/")
	c.SetHTTPOnly(true)
	ctx.SetCookie(c)*/

}
Пример #2
0
// SetFlashBytes sets a flash message, accepts 2 parameters the key(string) and the value([]byte)
func (ctx *Context) SetFlashBytes(key string, value []byte) {
	c := fasthttp.AcquireCookie()
	c.SetKey(key)
	c.SetValue(base64.URLEncoding.EncodeToString(value))
	c.SetPath("/")
	c.SetHTTPOnly(true)
	ctx.RequestCtx.Response.Header.SetCookie(c)
	fasthttp.ReleaseCookie(c)
}
Пример #3
0
// SetCookieKV adds a cookie, receives just a key(string) and a value(string)
func (ctx *Context) SetCookieKV(key, value string) {
	c := fasthttp.AcquireCookie() // &fasthttp.Cookie{}
	c.SetKey(key)
	c.SetValue(value)
	c.SetHTTPOnly(true)
	c.SetExpire(time.Now().Add(time.Duration(120) * time.Minute))
	ctx.SetCookie(c)
	fasthttp.ReleaseCookie(c)
}
Пример #4
0
// RemoveCookie deletes a cookie by it's name/key
func (ctx *Context) RemoveCookie(name string) {
	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
	cookie.SetExpire(exp)
	ctx.Response.Header.SetCookie(cookie)
	fasthttp.ReleaseCookie(cookie)
}
Пример #5
0
// RemoveCookie deletes a cookie by it's name/key
func (ctx *Context) RemoveCookie(name string) {
	ctx.Response.Header.DelCookie(name)

	cookie := fasthttp.AcquireCookie()
	//cookie := &fasthttp.Cookie{}
	cookie.SetKey(name)
	cookie.SetValue("")
	cookie.SetPath("/")
	cookie.SetHTTPOnly(true)
	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.SetCookie(cookie)
	fasthttp.ReleaseCookie(cookie)
	// delete request's cookie also, which is temporarly available
	ctx.Request.Header.DelCookie(name)
}
Пример #6
0
func TestContextCookieSetGetRemove(t *testing.T) {
	iris.ResetDefault()
	key := "mykey"
	value := "myvalue"
	iris.Get("/set", func(ctx *iris.Context) {
		ctx.SetCookieKV(key, value) // should return non empty cookies
	})

	iris.Get("/set_advanced", func(ctx *iris.Context) {
		c := fasthttp.AcquireCookie()
		c.SetKey(key)
		c.SetValue(value)
		c.SetHTTPOnly(true)
		c.SetExpire(time.Now().Add(time.Duration((60 * 60 * 24 * 7 * 4)) * time.Second))
		ctx.SetCookie(c)
		fasthttp.ReleaseCookie(c)
	})

	iris.Get("/get", func(ctx *iris.Context) {
		ctx.Write(ctx.GetCookie(key)) // should return my value
	})

	iris.Get("/remove", func(ctx *iris.Context) {
		ctx.RemoveCookie(key)
		cookieFound := false
		ctx.VisitAllCookies(func(k, v string) {
			cookieFound = true
		})
		if cookieFound {
			t.Fatalf("Cookie has been found, when it shouldn't!")
		}
		ctx.Write(ctx.GetCookie(key)) // should return ""
	})

	e := httptest.New(iris.Default, t)
	e.GET("/set").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/get").Expect().Status(iris.StatusOK).Body().Equal(value)
	e.GET("/remove").Expect().Status(iris.StatusOK).Body().Equal("")
	// test again with advanced set
	e.GET("/set_advanced").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/get").Expect().Status(iris.StatusOK).Body().Equal(value)
	e.GET("/remove").Expect().Status(iris.StatusOK).Body().Equal("")
}