1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

fix https://github.com/kataras/iris/issues/1485 by adding and using the new 'context.UpsertCookie' instead of 'context.SetCookie'

Former-commit-id: 31a50e580929616504b9bbbb1d602b0e9274a568
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-04-13 09:27:49 +03:00
parent e1d3cad905
commit 989ac436e8
4 changed files with 56 additions and 7 deletions

View File

@@ -36,7 +36,8 @@ func AddCookie(ctx context.Context, cookie *http.Cookie, reclaim bool) {
if reclaim {
ctx.Request().AddCookie(cookie)
}
ctx.SetCookie(cookie)
ctx.UpsertCookie(cookie)
}
// RemoveCookie deletes a cookie by it's name/key

View File

@@ -205,8 +205,9 @@ func TestSessionsUpdateExpiration(t *testing.T) {
cookieName := "mycustomsessionid"
sess := sessions.New(sessions.Config{
Cookie: cookieName,
Expires: 30 * time.Minute,
Cookie: cookieName,
Expires: 30 * time.Minute,
AllowReclaim: true,
})
app.Use(sess.Handler())
@@ -233,13 +234,17 @@ func TestSessionsUpdateExpiration(t *testing.T) {
writeResponse(ctx)
})
app.Get("/remember_me", func(ctx iris.Context) {
app.Post("/remember_me", func(ctx iris.Context) {
// re-sends the cookie with the new Expires and MaxAge fields,
// test checks that on same session id too.
sess.UpdateExpiration(ctx, 24*time.Hour)
writeResponse(ctx)
})
app.Get("/destroy", func(ctx iris.Context) {
sess.Destroy(ctx) // this will delete the cookie too.
})
e := httptest.New(t, app, httptest.URL("http://example.com"))
tt := e.GET("/set").Expect().Status(httptest.StatusOK)
@@ -250,7 +255,12 @@ func TestSessionsUpdateExpiration(t *testing.T) {
e.GET("/get").Expect().Status(httptest.StatusOK).
JSON().Equal(expectedResponse)
tt = e.GET("/remember_me").Expect().Status(httptest.StatusOK)
tt = e.POST("/remember_me").Expect().Status(httptest.StatusOK)
tt.Cookie(cookieName).MaxAge().Equal(24 * time.Hour)
tt.JSON().Equal(expectedResponse)
// Test call `UpdateExpiration` when cookie is firstly created.
e.GET("/destroy").Expect().Status(httptest.StatusOK)
e.POST("/remember_me").Expect().Status(httptest.StatusOK).
Cookie(cookieName).MaxAge().Equal(24 * time.Hour)
}