-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
58 lines (52 loc) · 1.23 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
// Copyright (c) 2023 William Dode. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package webo
import (
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/gorilla/sessions"
)
func TestSession(t *testing.T) {
store := sessions.NewCookieStore([]byte("abcd"))
r := httptest.NewRequest("GET", "/", nil)
s, _ := store.Get(r, "gos")
ses := &SessionStore{s}
ses.PutInt("ok", 5)
i, e := ses.GetInt("ok")
if i != 5 {
t.Errorf("ok=%d %v", i, e)
}
ses.Alert("ok")
fls := ses.Flashes("alert")
if len(fls) != 1 || fls[0] != "ok" {
t.Errorf("alert=%s", ses.Flashes("alert"))
}
now := time.Now().Truncate(time.Second)
ses.PutDate("d", now)
d, _ := ses.GetDate("d")
if !d.Equal(now) {
t.Errorf("put date : %v %v", now, d)
}
now = time.Now()
ses.PutDate("d", now)
_, e = ses.GetInt("j")
if e == nil {
t.Errorf("should be nil")
}
u := url.Values{}
u.Set("x", "xxx")
u.Set("y", "yyy")
ses.PutForm("fo", u)
res, err := ses.GetForm("fo")
if err != nil {
t.Errorf("GetForm error : %v", err)
}
if res.Get("x") != "xxx" {
t.Errorf("GetForm x should be xxx : %s", res["x"])
}
if res.Get("y") != "yyy" {
t.Errorf("GetForm y should be yyy : %s", res["y"])
}
}