mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 21:15:56 +00:00
improvements on cookie options
Former-commit-id: f1d5cfc88a33077a9359eaa25b6a20265f5632b5
This commit is contained in:
76
_examples/cookies/options/main.go
Normal file
76
_examples/cookies/options/main.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080/set/name1/value1
|
||||
// http://localhost:8080/get/name1
|
||||
// http://localhost:8080/remove/name1
|
||||
app.Listen(":8080", iris.WithLogLevel("debug"))
|
||||
}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Use(withCookieOptions)
|
||||
|
||||
app.Get("/set/{name}/{value}", setCookie)
|
||||
app.Get("/get/{name}", getCookie)
|
||||
app.Get("/remove/{name}", removeCookie)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func withCookieOptions(ctx iris.Context) {
|
||||
// Register cookie options for request-lifecycle.
|
||||
// To register per cookie, just add the CookieOption
|
||||
// on the last variadic input argument of
|
||||
// SetCookie, SetCookieKV, UpsertCookie, RemoveCookie
|
||||
// and GetCookie Context methods.
|
||||
//
|
||||
// * CookieAllowReclaim
|
||||
// * CookieAllowSubdomains
|
||||
// * CookieSecure
|
||||
// * CookieHTTPOnly
|
||||
// * CookieSameSite
|
||||
// * CookiePath
|
||||
// * CookieCleanPath
|
||||
// * CookieExpires
|
||||
// * CookieEncoding
|
||||
ctx.AddCookieOptions(iris.CookieAllowReclaim())
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
func setCookie(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
value := ctx.Params().Get("value")
|
||||
|
||||
ctx.SetCookieKV(name, value)
|
||||
|
||||
// By-default net/http does not remove or set the Cookie on the Request object.
|
||||
//
|
||||
// With the `CookieAllowReclaim` option, whenever you set or remove a cookie
|
||||
// it will be also reflected in the Request object immediately (of the same request lifecycle)
|
||||
// therefore, any of the next handlers in the chain are not holding the old value.
|
||||
valueIsAvailableInRequestObject := ctx.GetCookie(name)
|
||||
ctx.Writef("cookie %s=%s", name, valueIsAvailableInRequestObject)
|
||||
}
|
||||
|
||||
func getCookie(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
|
||||
value := ctx.GetCookie(name)
|
||||
ctx.WriteString(value)
|
||||
}
|
||||
|
||||
func removeCookie(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
|
||||
ctx.RemoveCookie(name)
|
||||
|
||||
removedFromRequestObject := ctx.GetCookie(name) // CookieAllowReclaim feature.
|
||||
ctx.Writef("cookie %s removed, value should be empty=%s", name, removedFromRequestObject)
|
||||
}
|
||||
32
_examples/cookies/options/main_test.go
Normal file
32
_examples/cookies/options/main_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
)
|
||||
|
||||
func TestCookieOptions(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app, httptest.URL("http://example.com"))
|
||||
|
||||
cookieName, cookieValue := "my_cookie_name", "my_cookie_value"
|
||||
|
||||
// Test set a Cookie.
|
||||
t1 := e.GET(fmt.Sprintf("/set/%s/%s", cookieName, cookieValue)).Expect().Status(httptest.StatusOK)
|
||||
t1.Cookie(cookieName).Value().Equal(cookieValue)
|
||||
t1.Body().Contains(fmt.Sprintf("%s=%s", cookieName, cookieValue))
|
||||
|
||||
// Test retrieve a Cookie.
|
||||
t2 := e.GET(fmt.Sprintf("/get/%s", cookieName)).Expect().Status(httptest.StatusOK)
|
||||
t2.Body().Equal(cookieValue)
|
||||
|
||||
// Test remove a Cookie.
|
||||
t3 := e.GET(fmt.Sprintf("/remove/%s", cookieName)).Expect().Status(httptest.StatusOK)
|
||||
t3.Body().Contains(fmt.Sprintf("cookie %s removed, value should be empty=%s", cookieName, ""))
|
||||
|
||||
t4 := e.GET(fmt.Sprintf("/get/%s", cookieName)).Expect().Status(httptest.StatusOK)
|
||||
t4.Cookies().Empty()
|
||||
t4.Body().Empty()
|
||||
}
|
||||
Reference in New Issue
Block a user