This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
64 lines (49 loc) · 1.47 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
package buffalo
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/httptest"
"github.com/stretchr/testify/require"
)
func Test_Session_SingleCookie(t *testing.T) {
r := require.New(t)
sessionName := "_test_session"
a := New(Options{SessionName: sessionName})
rr := render.New(render.Options{})
a.GET("/", func(c Context) error {
return c.Render(http.StatusCreated, rr.String(""))
})
w := httptest.New(a)
res := w.HTML("/").Get()
var sessionCookies []string
for _, c := range res.Header().Values("Set-Cookie") {
if strings.HasPrefix(c, sessionName) {
sessionCookies = append(sessionCookies, c)
}
}
r.Equal(1, len(sessionCookies))
}
func Test_Session_CustomValue(t *testing.T) {
r := require.New(t)
a := New(Options{})
rr := render.New(render.Options{})
// Root path sets a custom session value
a.GET("/", func(c Context) error {
c.Session().Set("example", "test")
return c.Render(http.StatusCreated, rr.String(""))
})
// /session path prints custom session value as response
a.GET("/session", func(c Context) error {
sessionValue := c.Session().Get("example")
return c.Render(http.StatusCreated, rr.String(fmt.Sprintf("%s", sessionValue)))
})
w := httptest.New(a)
_ = w.HTML("/").Get()
// Create second request that should contain the cookie from the first response
reqGetSession := w.HTML("/session")
resGetSession := reqGetSession.Get()
r.Equal(resGetSession.Body.String(), "test")
}