github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_session_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/v2/frame/g" 15 "github.com/gogf/gf/v2/net/ghttp" 16 "github.com/gogf/gf/v2/test/gtest" 17 "github.com/gogf/gf/v2/util/guid" 18 ) 19 20 func Test_Session_Cookie(t *testing.T) { 21 s := g.Server(guid.S()) 22 s.BindHandler("/set", func(r *ghttp.Request) { 23 r.Session.Set(r.Get("k").String(), r.Get("v").String()) 24 }) 25 s.BindHandler("/get", func(r *ghttp.Request) { 26 r.Response.Write(r.Session.Get(r.Get("k").String())) 27 }) 28 s.BindHandler("/remove", func(r *ghttp.Request) { 29 r.Session.Remove(r.Get("k").String()) 30 }) 31 s.BindHandler("/clear", func(r *ghttp.Request) { 32 r.Session.RemoveAll() 33 }) 34 s.SetDumpRouterMap(false) 35 s.Start() 36 defer s.Shutdown() 37 38 time.Sleep(100 * time.Millisecond) 39 gtest.C(t, func(t *gtest.T) { 40 client := g.Client() 41 client.SetBrowserMode(true) 42 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 43 r1, e1 := client.Get(ctx, "/set?k=key1&v=100") 44 if r1 != nil { 45 defer r1.Close() 46 } 47 t.Assert(e1, nil) 48 t.Assert(r1.ReadAllString(), "") 49 50 t.Assert(client.GetContent(ctx, "/set?k=key2&v=200"), "") 51 52 t.Assert(client.GetContent(ctx, "/get?k=key1"), "100") 53 t.Assert(client.GetContent(ctx, "/get?k=key2"), "200") 54 t.Assert(client.GetContent(ctx, "/get?k=key3"), "") 55 t.Assert(client.GetContent(ctx, "/remove?k=key1"), "") 56 t.Assert(client.GetContent(ctx, "/remove?k=key3"), "") 57 t.Assert(client.GetContent(ctx, "/remove?k=key4"), "") 58 t.Assert(client.GetContent(ctx, "/get?k=key1"), "") 59 t.Assert(client.GetContent(ctx, "/get?k=key2"), "200") 60 t.Assert(client.GetContent(ctx, "/clear"), "") 61 t.Assert(client.GetContent(ctx, "/get?k=key2"), "") 62 }) 63 } 64 65 func Test_Session_Header(t *testing.T) { 66 s := g.Server(guid.S()) 67 s.BindHandler("/set", func(r *ghttp.Request) { 68 r.Session.Set(r.Get("k").String(), r.Get("v").String()) 69 }) 70 s.BindHandler("/get", func(r *ghttp.Request) { 71 r.Response.Write(r.Session.Get(r.Get("k").String())) 72 }) 73 s.BindHandler("/remove", func(r *ghttp.Request) { 74 r.Session.Remove(r.Get("k").String()) 75 }) 76 s.BindHandler("/clear", func(r *ghttp.Request) { 77 r.Session.RemoveAll() 78 }) 79 s.SetDumpRouterMap(false) 80 s.Start() 81 defer s.Shutdown() 82 83 time.Sleep(100 * time.Millisecond) 84 gtest.C(t, func(t *gtest.T) { 85 client := g.Client() 86 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 87 response, e1 := client.Get(ctx, "/set?k=key1&v=100") 88 if response != nil { 89 defer response.Close() 90 } 91 sessionId := response.GetCookie(s.GetSessionIdName()) 92 t.Assert(e1, nil) 93 t.AssertNE(sessionId, nil) 94 t.Assert(response.ReadAllString(), "") 95 96 client.SetHeader(s.GetSessionIdName(), sessionId) 97 98 t.Assert(client.GetContent(ctx, "/set?k=key2&v=200"), "") 99 100 t.Assert(client.GetContent(ctx, "/get?k=key1"), "100") 101 t.Assert(client.GetContent(ctx, "/get?k=key2"), "200") 102 t.Assert(client.GetContent(ctx, "/get?k=key3"), "") 103 t.Assert(client.GetContent(ctx, "/remove?k=key1"), "") 104 t.Assert(client.GetContent(ctx, "/remove?k=key3"), "") 105 t.Assert(client.GetContent(ctx, "/remove?k=key4"), "") 106 t.Assert(client.GetContent(ctx, "/get?k=key1"), "") 107 t.Assert(client.GetContent(ctx, "/get?k=key2"), "200") 108 t.Assert(client.GetContent(ctx, "/clear"), "") 109 t.Assert(client.GetContent(ctx, "/get?k=key2"), "") 110 }) 111 } 112 113 func Test_Session_StorageFile(t *testing.T) { 114 sessionId := "" 115 s := g.Server(guid.S()) 116 s.BindHandler("/set", func(r *ghttp.Request) { 117 r.Session.Set(r.Get("k").String(), r.Get("v").String()) 118 r.Response.Write(r.Get("k").String(), "=", r.Get("v").String()) 119 }) 120 s.BindHandler("/get", func(r *ghttp.Request) { 121 r.Response.Write(r.Session.Get(r.Get("k").String())) 122 }) 123 s.SetDumpRouterMap(false) 124 s.Start() 125 defer s.Shutdown() 126 127 time.Sleep(100 * time.Millisecond) 128 129 gtest.C(t, func(t *gtest.T) { 130 client := g.Client() 131 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 132 response, e1 := client.Get(ctx, "/set?k=key&v=100") 133 if response != nil { 134 defer response.Close() 135 } 136 sessionId = response.GetCookie(s.GetSessionIdName()) 137 t.Assert(e1, nil) 138 t.AssertNE(sessionId, nil) 139 t.Assert(response.ReadAllString(), "key=100") 140 }) 141 time.Sleep(time.Second) 142 gtest.C(t, func(t *gtest.T) { 143 client := g.Client() 144 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 145 client.SetHeader(s.GetSessionIdName(), sessionId) 146 t.Assert(client.GetContent(ctx, "/get?k=key"), "100") 147 t.Assert(client.GetContent(ctx, "/get?k=key1"), "") 148 }) 149 } 150 151 func Test_Session_Custom_Id(t *testing.T) { 152 var ( 153 sessionId = "1234567890" 154 key = "key" 155 value = "value" 156 s = g.Server(guid.S()) 157 ) 158 s.BindHandler("/id", func(r *ghttp.Request) { 159 if err := r.Session.SetId(sessionId); err != nil { 160 r.Response.WriteExit(err.Error()) 161 } 162 if err := r.Session.Set(key, value); err != nil { 163 r.Response.WriteExit(err.Error()) 164 } 165 r.Response.WriteExit(r.Session.Id()) 166 }) 167 s.BindHandler("/value", func(r *ghttp.Request) { 168 r.Response.WriteExit(r.Session.Get(key)) 169 }) 170 s.SetDumpRouterMap(false) 171 s.Start() 172 defer s.Shutdown() 173 174 time.Sleep(100 * time.Millisecond) 175 176 gtest.C(t, func(t *gtest.T) { 177 client := g.Client() 178 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 179 r, err := client.Get(ctx, "/id") 180 t.AssertNil(err) 181 defer r.Close() 182 t.Assert(r.ReadAllString(), sessionId) 183 t.Assert(r.GetCookie(s.GetSessionIdName()), sessionId) 184 }) 185 gtest.C(t, func(t *gtest.T) { 186 client := g.Client() 187 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 188 client.SetHeader(s.GetSessionIdName(), sessionId) 189 t.Assert(client.GetContent(ctx, "/value"), value) 190 }) 191 } 192 193 func Test_Session_New_Id(t *testing.T) { 194 var ( 195 sessionId = "1234567890" 196 newSessionId = "0987654321" 197 newSessionId2 = "abcdefghij" 198 key = "key" 199 value = "value" 200 s = g.Server(guid.S()) 201 ) 202 s.BindHandler("/id", func(r *ghttp.Request) { 203 if err := r.Session.SetId(sessionId); err != nil { 204 r.Response.WriteExit(err.Error()) 205 } 206 if err := r.Session.Set(key, value); err != nil { 207 r.Response.WriteExit(err.Error()) 208 } 209 r.Response.WriteExit(r.Session.Id()) 210 }) 211 212 s.BindHandler("/newIdBySession", func(r *ghttp.Request) { 213 // Use before session init 214 if err := r.Session.SetId(newSessionId); err != nil { 215 r.Response.WriteExit(err.Error()) 216 } 217 if err := r.Session.Set(key, value); err != nil { 218 r.Response.WriteExit(err.Error()) 219 } 220 r.Response.WriteExit(r.Session.Id()) 221 }) 222 223 s.BindHandler("/newIdByCookie", func(r *ghttp.Request) { 224 if err := r.Session.Remove("someKey"); err != nil { 225 r.Response.WriteExit(err.Error()) 226 } 227 228 r.Cookie.SetSessionId(newSessionId2) 229 //r.Response.WriteExit(r.Session.Id()) // only change in cookie 230 231 r.Response.WriteExit(newSessionId2) 232 }) 233 234 s.BindHandler("/value", func(r *ghttp.Request) { 235 r.Response.WriteExit(r.Session.Get(key)) 236 }) 237 s.SetDumpRouterMap(false) 238 s.Start() 239 defer s.Shutdown() 240 241 time.Sleep(100 * time.Millisecond) 242 243 gtest.C(t, func(t *gtest.T) { 244 client := g.Client() 245 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 246 r, err := client.Get(ctx, "/id") 247 t.AssertNil(err) 248 defer r.Close() 249 t.Assert(r.ReadAllString(), sessionId) 250 t.Assert(r.GetCookie(s.GetSessionIdName()), sessionId) 251 }) 252 gtest.C(t, func(t *gtest.T) { 253 client := g.Client() 254 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 255 client.SetHeader(s.GetSessionIdName(), sessionId) 256 t.Assert(client.GetContent(ctx, "/value"), value) 257 }) 258 259 gtest.C(t, func(t *gtest.T) { 260 client := g.Client() 261 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 262 client.SetHeader(s.GetSessionIdName(), sessionId) 263 r, err := client.Get(ctx, "/newIdBySession") 264 t.AssertNil(err) 265 defer r.Close() 266 t.Assert(r.ReadAllString(), newSessionId) 267 t.Assert(r.GetCookie(s.GetSessionIdName()), newSessionId) 268 }) 269 270 gtest.C(t, func(t *gtest.T) { 271 client := g.Client() 272 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 273 r, err := client.Get(ctx, "/newIdByCookie") 274 client.SetHeader(s.GetSessionIdName(), sessionId) 275 t.AssertNil(err) 276 defer r.Close() 277 t.Assert(r.ReadAllString(), newSessionId2) 278 t.Assert(r.GetCookie(s.GetSessionIdName()), newSessionId2) 279 }) 280 }