2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-02-26 17:17:11 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2022-02-22 14:12:09 +08:00
|
|
|
"github.com/gogf/gf/v2/util/guid"
|
2019-02-26 17:17:11 +08:00
|
|
|
)
|
|
|
|
|
2019-08-10 16:39:46 +08:00
|
|
|
func Test_Session_Cookie(t *testing.T) {
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2019-06-19 09:06:52 +08:00
|
|
|
s.BindHandler("/set", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Session.Set(r.Get("k").String(), r.Get("v").String())
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/get", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Response.Write(r.Session.Get(r.Get("k").String()))
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/remove", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Session.Remove(r.Get("k").String())
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/clear", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Session.RemoveAll()
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-02-28 23:57:20 +08:00
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetBrowserMode(true)
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2021-09-30 14:06:46 +08:00
|
|
|
r1, e1 := client.Get(ctx, "/set?k=key1&v=100")
|
2019-06-19 09:06:52 +08:00
|
|
|
if r1 != nil {
|
|
|
|
defer r1.Close()
|
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e1, nil)
|
|
|
|
t.Assert(r1.ReadAllString(), "")
|
2019-02-26 17:17:11 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/set?k=key2&v=200"), "")
|
2019-02-26 17:17:11 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key1"), "100")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key3"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/remove?k=key1"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/remove?k=key3"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/remove?k=key4"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key1"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
|
|
|
|
t.Assert(client.GetContent(ctx, "/clear"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key2"), "")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-02-26 17:17:11 +08:00
|
|
|
}
|
2019-08-10 16:39:46 +08:00
|
|
|
|
|
|
|
func Test_Session_Header(t *testing.T) {
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2019-08-10 16:39:46 +08:00
|
|
|
s.BindHandler("/set", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Session.Set(r.Get("k").String(), r.Get("v").String())
|
2019-08-10 16:39:46 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/get", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Response.Write(r.Session.Get(r.Get("k").String()))
|
2019-08-10 16:39:46 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/remove", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Session.Remove(r.Get("k").String())
|
2019-08-10 16:39:46 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/clear", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Session.RemoveAll()
|
2019-08-10 16:39:46 +08:00
|
|
|
})
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-10 16:39:46 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2021-09-30 14:06:46 +08:00
|
|
|
response, e1 := client.Get(ctx, "/set?k=key1&v=100")
|
2019-08-10 16:39:46 +08:00
|
|
|
if response != nil {
|
|
|
|
defer response.Close()
|
|
|
|
}
|
2019-09-12 00:00:59 +08:00
|
|
|
sessionId := response.GetCookie(s.GetSessionIdName())
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e1, nil)
|
|
|
|
t.AssertNE(sessionId, nil)
|
|
|
|
t.Assert(response.ReadAllString(), "")
|
2019-08-10 16:39:46 +08:00
|
|
|
|
|
|
|
client.SetHeader(s.GetSessionIdName(), sessionId)
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/set?k=key2&v=200"), "")
|
2019-08-10 16:39:46 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key1"), "100")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key3"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/remove?k=key1"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/remove?k=key3"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/remove?k=key4"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key1"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
|
|
|
|
t.Assert(client.GetContent(ctx, "/clear"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key2"), "")
|
2019-08-10 16:39:46 +08:00
|
|
|
})
|
|
|
|
}
|
2019-09-12 00:00:59 +08:00
|
|
|
|
|
|
|
func Test_Session_StorageFile(t *testing.T) {
|
|
|
|
sessionId := ""
|
2022-02-22 14:12:09 +08:00
|
|
|
s := g.Server(guid.S())
|
2019-11-08 09:06:01 +08:00
|
|
|
s.BindHandler("/set", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Session.Set(r.Get("k").String(), r.Get("v").String())
|
|
|
|
r.Response.Write(r.Get("k").String(), "=", r.Get("v").String())
|
2019-11-08 09:06:01 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/get", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Response.Write(r.Session.Get(r.Get("k").String()))
|
2019-11-08 09:06:01 +08:00
|
|
|
})
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-11-08 09:06:01 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-09-12 00:00:59 +08:00
|
|
|
|
2019-11-08 09:06:01 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2021-09-30 14:06:46 +08:00
|
|
|
response, e1 := client.Get(ctx, "/set?k=key&v=100")
|
2019-09-12 00:00:59 +08:00
|
|
|
if response != nil {
|
|
|
|
defer response.Close()
|
|
|
|
}
|
|
|
|
sessionId = response.GetCookie(s.GetSessionIdName())
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e1, nil)
|
|
|
|
t.AssertNE(sessionId, nil)
|
|
|
|
t.Assert(response.ReadAllString(), "key=100")
|
2019-09-12 00:00:59 +08:00
|
|
|
})
|
|
|
|
time.Sleep(time.Second)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2019-09-12 00:00:59 +08:00
|
|
|
client.SetHeader(s.GetSessionIdName(), sessionId)
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key"), "100")
|
|
|
|
t.Assert(client.GetContent(ctx, "/get?k=key1"), "")
|
2019-09-12 00:00:59 +08:00
|
|
|
})
|
|
|
|
}
|
2020-10-13 20:34:31 +08:00
|
|
|
|
|
|
|
func Test_Session_Custom_Id(t *testing.T) {
|
|
|
|
var (
|
|
|
|
sessionId = "1234567890"
|
|
|
|
key = "key"
|
|
|
|
value = "value"
|
2022-10-08 21:45:21 +08:00
|
|
|
s = g.Server(guid.S())
|
2020-10-13 20:34:31 +08:00
|
|
|
)
|
|
|
|
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.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
client := g.Client()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2021-09-30 14:06:46 +08:00
|
|
|
r, err := client.Get(ctx, "/id")
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-13 20:34:31 +08:00
|
|
|
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()
|
2022-02-22 14:12:09 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
2020-10-13 20:34:31 +08:00
|
|
|
client.SetHeader(s.GetSessionIdName(), sessionId)
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/value"), value)
|
2020-10-13 20:34:31 +08:00
|
|
|
})
|
|
|
|
}
|