-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
85 lines (73 loc) · 1.48 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
83
84
85
package gearsession
import (
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"
"github.com/go-session/session"
"github.com/teambition/gear"
)
func TestSession(t *testing.T) {
cookieName := "test_gear_session"
app := gear.New()
app.Use(New(
session.SetCookieName(cookieName),
session.SetSign([]byte("sign")),
))
app.Use(func(ctx *gear.Context) error {
store := FromContext(ctx)
if ctx.Query("login") == "1" {
foo, ok := store.Get("foo")
if !ok || !reflect.DeepEqual(foo, "bar") {
t.Error("Not expected value:", foo)
return nil
}
fmt.Fprint(ctx.Res, "ok")
return nil
}
store.Set("foo", "bar")
err := store.Save()
if err != nil {
return err
}
fmt.Fprint(ctx.Res, "ok")
return nil
})
srv := app.Start()
defer srv.Close()
url := "http://" + srv.Addr().String()
res, err := http.Get(url)
if err != nil {
t.Error(err)
return
}
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", fmt.Sprintf("%s?login=1", url), nil)
if err != nil {
t.Error(err)
return
}
req.AddCookie(cookie)
res, err = http.DefaultClient.Do(req)
if err != nil {
t.Error(err)
return
}
buf, _ = ioutil.ReadAll(res.Body)
res.Body.Close()
if string(buf) != "ok" {
t.Error("Not expected value:", string(buf))
return
}
}