forked from plimble/ace
/
session_test.go
44 lines (35 loc) · 897 Bytes
/
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
package ace
import (
"github.com/plimble/sessions/store/cookie"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestSession(t *testing.T) {
a := New()
store := cookie.NewCookieStore()
a.Use(Session(store, nil))
a.GET("/", func(c *C) {
session := c.Sessions("test")
session.Set("test1", "123")
session.Set("test2", 123)
c.String(200, "")
})
a.GET("/test", func(c *C) {
session := c.Sessions("test")
test1 := session.GetString("test1", "")
test2 := session.GetInt("test2", 0)
assert.Equal(t, "123", test1)
assert.Equal(t, 123, test2)
c.String(200, "")
})
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
a.ServeHTTP(w, req)
cookie := w.Header().Get("Set-Cookie")
req, _ = http.NewRequest("GET", "/test", nil)
req.Header.Set("Cookie", cookie)
w = httptest.NewRecorder()
a.ServeHTTP(w, req)
}