-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
82 lines (68 loc) · 1.52 KB
/
session_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package beegosession
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/go-session/session"
)
func TestSession(t *testing.T) {
cookieName := "test_beego_session"
app := beego.NewApp()
app.Handlers.InsertFilter("*", beego.BeforeRouter, New(
session.SetCookieName(cookieName),
session.SetSign([]byte("sign")),
))
app.Handlers.Get("/", func(ctx *context.Context) {
store := FromContext(ctx)
if ctx.Input.Query("login") == "1" {
foo, ok := store.Get("foo")
fmt.Fprintf(ctx.ResponseWriter, "%s:%v", foo, ok)
return
}
store.Set("foo", "bar")
err := store.Save()
if err != nil {
t.Error(err)
return
}
fmt.Fprint(ctx.ResponseWriter, "ok")
})
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Error(err)
return
}
app.Handlers.ServeHTTP(w, req)
res := w.Result()
cookie := res.Cookies()[0]
if cookie.Name != cookieName {
t.Error("Not expected value:", cookie.Name)
return
}
buf, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
if string(buf) != "ok" {
t.Error("Not expected value:", string(buf))
return
}
req, err = http.NewRequest("GET", "/?login=1", nil)
if err != nil {
t.Error(err)
return
}
req.AddCookie(cookie)
w = httptest.NewRecorder()
app.Handlers.ServeHTTP(w, req)
res = w.Result()
buf, _ = ioutil.ReadAll(res.Body)
res.Body.Close()
if string(buf) != "bar:true" {
t.Error("Not expected value:", string(buf))
return
}
}