Skip to content

Commit

Permalink
Mark session as written when updating options (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfrench authored Dec 21, 2021
1 parent 9ca5251 commit 202bf0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (s *session) Flashes(vars ...string) []interface{} {
}

func (s *session) Options(options Options) {
s.written = true
s.Session().Options = options.ToGorillaOptions()
}

Expand Down
34 changes: 34 additions & 0 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ func Options(t *testing.T, newStore storeFactory) {
_ = session.Save()
c.String(http.StatusOK, ok)
})
r.GET("/set", func(c *gin.Context) {
session := sessions.Default(c)
session.Set("key", ok)
_ = session.Save()
c.String(http.StatusOK, ok)
})
r.GET("/expire", func(c *gin.Context) {
session := sessions.Default(c)
session.Options(sessions.Options{
MaxAge: -1,
})
_ = session.Save()
c.String(http.StatusOK, ok)
})
r.GET("/check", func(c *gin.Context) {
session := sessions.Default(c)
val := session.Get("key")
if val != nil {
t.Fatal("Session expiration failed")
}
c.String(http.StatusOK, ok)
})

testOptionSameSitego(t, r)

Expand All @@ -216,6 +238,18 @@ func Options(t *testing.T, newStore storeFactory) {
req2, _ := http.NewRequest("GET", "/path", nil)
r.ServeHTTP(res2, req2)

res3 := httptest.NewRecorder()
req3, _ := http.NewRequest("GET", "/set", nil)
r.ServeHTTP(res3, req3)

res4 := httptest.NewRecorder()
req4, _ := http.NewRequest("GET", "/expire", nil)
r.ServeHTTP(res4, req4)

res5 := httptest.NewRecorder()
req5, _ := http.NewRequest("GET", "/check", nil)
r.ServeHTTP(res5, req5)

s := strings.Split(res1.Header().Get("Set-Cookie"), ";")
if s[1] != " Path=/foo/bar/bat" {
t.Error("Error writing path with options:", s[1])
Expand Down

0 comments on commit 202bf0f

Please sign in to comment.