mirror of https://github.com/gofiber/fiber.git
🧨 introduce destroy
parent
e5cffc9bc9
commit
27baab0ea0
|
@ -8,6 +8,7 @@ import (
|
|||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -358,3 +359,49 @@ func Test_Utils_lnMetadata(t *testing.T) {
|
|||
utils.AssertEqual(t, true, config != nil)
|
||||
})
|
||||
}
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_SlashRecognition -benchmem -count=4
|
||||
func Benchmark_SlashRecognition(b *testing.B) {
|
||||
search := "wtf/1234"
|
||||
var result bool
|
||||
b.Run("indexBytes", func(b *testing.B) {
|
||||
result = false
|
||||
for i := 0; i < b.N; i++ {
|
||||
if strings.IndexByte(search, slashDelimiter) != -1 {
|
||||
result = true
|
||||
}
|
||||
}
|
||||
utils.AssertEqual(b, true, result)
|
||||
})
|
||||
b.Run("forEach", func(b *testing.B) {
|
||||
result = false
|
||||
c := int32(slashDelimiter)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, b := range search {
|
||||
if b == c {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
utils.AssertEqual(b, true, result)
|
||||
})
|
||||
b.Run("IndexRune", func(b *testing.B) {
|
||||
result = false
|
||||
c := int32(slashDelimiter)
|
||||
for i := 0; i < b.N; i++ {
|
||||
result = IndexRune(search, c)
|
||||
|
||||
}
|
||||
utils.AssertEqual(b, true, result)
|
||||
})
|
||||
}
|
||||
|
||||
func IndexRune(str string, needle int32) bool {
|
||||
for _, b := range str {
|
||||
if b == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ func configDefault(config ...Config) Config {
|
|||
cfg := config[0]
|
||||
|
||||
// Set default values
|
||||
if cfg.Expiration <= 0 {
|
||||
if int(cfg.Expiration.Seconds()) <= 0 {
|
||||
cfg.Expiration = ConfigDefault.Expiration
|
||||
}
|
||||
if cfg.KeyGenerator == nil {
|
||||
|
|
|
@ -66,18 +66,38 @@ func (s *Session) Delete(key string) {
|
|||
s.db.Delete(key)
|
||||
}
|
||||
|
||||
// Reset will clear the session and remove from storage
|
||||
func (s *Session) Reset() error {
|
||||
// Destroy will delete the session from Storage and expire session cookie
|
||||
func (s *Session) Destroy() error {
|
||||
// Reset local data
|
||||
s.db.Reset()
|
||||
return s.config.Storage.Delete(s.id)
|
||||
|
||||
// Delete data from storage
|
||||
if err := s.config.Storage.Delete(s.id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Expire cookie
|
||||
s.delCookie()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Regenerate generates a new session id and delete the old one from Storage
|
||||
func (s *Session) Regenerate() error {
|
||||
|
||||
// Delete old id from storage
|
||||
if err := s.config.Storage.Delete(s.id); err != nil {
|
||||
return err
|
||||
}
|
||||
// Create new ID
|
||||
s.id = s.config.KeyGenerator()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save will update the storage and client cookie
|
||||
func (s *Session) Save() error {
|
||||
// Expire session if no data is present ( aka reset )
|
||||
// Don't save to Storage if no data is available
|
||||
if s.db.Len() <= 0 {
|
||||
// Delete cookie
|
||||
s.deleteCookie()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -107,7 +127,7 @@ func (s *Session) setCookie() {
|
|||
fcookie.SetValue(s.id)
|
||||
fcookie.SetPath(s.config.CookiePath)
|
||||
fcookie.SetDomain(s.config.CookieDomain)
|
||||
fcookie.SetMaxAge(int(s.config.Expiration))
|
||||
fcookie.SetMaxAge(int(s.config.Expiration.Seconds()))
|
||||
fcookie.SetExpire(time.Now().Add(s.config.Expiration))
|
||||
fcookie.SetSecure(s.config.CookieSecure)
|
||||
fcookie.SetHTTPOnly(s.config.CookieHTTPOnly)
|
||||
|
@ -125,7 +145,7 @@ func (s *Session) setCookie() {
|
|||
fasthttp.ReleaseCookie(fcookie)
|
||||
}
|
||||
|
||||
func (s *Session) deleteCookie() {
|
||||
func (s *Session) delCookie() {
|
||||
s.ctx.Request().Header.DelCookie(s.config.CookieName)
|
||||
s.ctx.Response().Header.DelCookie(s.config.CookieName)
|
||||
|
||||
|
@ -133,7 +153,7 @@ func (s *Session) deleteCookie() {
|
|||
fcookie.SetKey(s.config.CookieName)
|
||||
fcookie.SetPath(s.config.CookiePath)
|
||||
fcookie.SetDomain(s.config.CookieDomain)
|
||||
fcookie.SetMaxAge(int(s.config.Expiration))
|
||||
fcookie.SetMaxAge(-1)
|
||||
fcookie.SetExpire(time.Now().Add(-1 * time.Minute))
|
||||
fcookie.SetSecure(s.config.CookieSecure)
|
||||
fcookie.SetHTTPOnly(s.config.CookieHTTPOnly)
|
||||
|
|
|
@ -135,7 +135,7 @@ func Test_Session_Reset(t *testing.T) {
|
|||
sess, _ := store.Get(ctx)
|
||||
|
||||
sess.Set("name", "fenny")
|
||||
sess.Reset()
|
||||
sess.Destroy()
|
||||
name := sess.Get("name")
|
||||
utils.AssertEqual(t, nil, name)
|
||||
}
|
||||
|
@ -150,31 +150,23 @@ func Test_Session_Custom_Config(t *testing.T) {
|
|||
|
||||
store = New(Config{Expiration: 0})
|
||||
utils.AssertEqual(t, ConfigDefault.Expiration, store.Expiration)
|
||||
// fmt.Println(ConfigDefault.KeyGenerator == store.KeyGenerator)
|
||||
// utils.AssertEqual(t, ConfigDefault.KeyGenerator, store.KeyGenerator)
|
||||
}
|
||||
|
||||
// TODO
|
||||
// func Test_Session_Cookie(t *testing.T) {
|
||||
// t.Parallel()
|
||||
// // session store
|
||||
// store := New()
|
||||
// // fiber instance
|
||||
// app := fiber.New()
|
||||
// // fiber context
|
||||
// ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
// defer app.ReleaseCtx(ctx)
|
||||
// go test -run Test_Session_Cookie
|
||||
func Test_Session_Cookie(t *testing.T) {
|
||||
t.Parallel()
|
||||
// session store
|
||||
store := New()
|
||||
// fiber instance
|
||||
app := fiber.New()
|
||||
// fiber context
|
||||
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
defer app.ReleaseCtx(ctx)
|
||||
|
||||
// // get session
|
||||
// sess, _ := store.Get(ctx)
|
||||
// sess.Save()
|
||||
// get session
|
||||
sess, _ := store.Get(ctx)
|
||||
sess.Save()
|
||||
|
||||
// // TODO make sure cookie exists
|
||||
// // fmt.Println(string(ctx.Response().Header.PeekCookie("session_id")))
|
||||
|
||||
// // delete cookie
|
||||
// // sess.deleteCookie()
|
||||
// // sess.Save()
|
||||
|
||||
// // TODO make sure cookie does not exist
|
||||
// }
|
||||
// cookie should not be set if empty data
|
||||
utils.AssertEqual(t, 0, len(ctx.Response().Header.PeekCookie(store.CookieName)))
|
||||
}
|
||||
|
|
|
@ -61,6 +61,7 @@ func (s *Store) Get(c *fiber.Ctx) (*Session, error) {
|
|||
return sess, nil
|
||||
}
|
||||
|
||||
// Reset will delete all session from the storage
|
||||
func (s *Store) Reset() error {
|
||||
return s.Storage.Reset()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue