mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
fix issue in custom session id lost for ghttp.Request
This commit is contained in:
@ -152,3 +152,47 @@ func Test_Session_StorageFile(t *testing.T) {
|
||||
t.Assert(client.GetContent("/get?k=key1"), "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Session_Custom_Id(t *testing.T) {
|
||||
var (
|
||||
sessionId = "1234567890"
|
||||
key = "key"
|
||||
value = "value"
|
||||
p, _ = ports.PopRand()
|
||||
s = g.Server(p)
|
||||
)
|
||||
s.BindHandler("/id", func(r *ghttp.Request) {
|
||||
if err := r.Session.SetId(sessionId); err != nil {
|
||||
r.Response.WriteExit(err.Error())
|
||||
}
|
||||
if err := r.Session.Set(key, value); err != nil {
|
||||
r.Response.WriteExit(err.Error())
|
||||
}
|
||||
r.Response.WriteExit(r.Session.Id())
|
||||
})
|
||||
s.BindHandler("/value", func(r *ghttp.Request) {
|
||||
r.Response.WriteExit(r.Session.Get(key))
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
r, err := client.Get("/id")
|
||||
t.Assert(err, nil)
|
||||
defer r.Close()
|
||||
t.Assert(r.ReadAllString(), sessionId)
|
||||
t.Assert(r.GetCookie(s.GetSessionIdName()), sessionId)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
client.SetHeader(s.GetSessionIdName(), sessionId)
|
||||
t.Assert(client.GetContent("/value"), value)
|
||||
})
|
||||
}
|
||||
|
||||
@ -50,11 +50,6 @@ func (s *Session) init() {
|
||||
intlog.Errorf("session restoring failed for id '%s': %v", s.id, err)
|
||||
}
|
||||
}
|
||||
// If it's an invalid or expired session id,
|
||||
// it should create a new session id.
|
||||
if s.data == nil {
|
||||
s.id = ""
|
||||
}
|
||||
}
|
||||
// Use custom session id creating function.
|
||||
if s.id == "" && s.idFunc != nil {
|
||||
|
||||
Reference in New Issue
Block a user