-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookie_test.go
87 lines (73 loc) · 1.49 KB
/
cookie_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
86
87
package cookie
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-session/session"
)
var (
hashKey = []byte("FF51A553-72FC-478B-9AEF-93D6F506DE91")
)
func TestCookie(t *testing.T) {
sess := session.NewManager(
session.SetCookieName("test_cookie"),
session.SetSign([]byte("sign")),
session.SetStore(NewCookieStore(
SetCookieName("test_cookie_store"),
SetHashKey(hashKey),
)),
)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
store, err := sess.Start(context.Background(), w, r)
if err != nil {
t.Error(err)
return
}
if r.URL.Query().Get("login") == "1" {
foo, ok := store.Get("foo")
fmt.Fprintf(w, "%s:%v", foo, ok)
return
}
store.Set("foo", "bar")
err = store.Save()
if err != nil {
t.Error(err)
return
}
fmt.Fprint(w, "ok")
}))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
t.Error(err)
return
}
buf, _ := ioutil.ReadAll(res.Body)
if string(buf) != "ok" {
t.Error("Not expected value:", string(buf))
return
}
res.Body.Close()
req, err := http.NewRequest("GET", fmt.Sprintf("%s?login=1", ts.URL), nil)
if err != nil {
t.Error(err)
return
}
for _, c := range res.Cookies() {
req.AddCookie(c)
}
res, err = http.DefaultClient.Do(req)
if err != nil {
t.Error(err)
return
}
buf, _ = ioutil.ReadAll(res.Body)
res.Body.Close()
if string(buf) != "bar:true" {
t.Error("Not expected value:", string(buf))
return
}
}