-
Notifications
You must be signed in to change notification settings - Fork 7
/
session_test.go
59 lines (41 loc) · 1.38 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
package session
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/alexedwards/scs/v2"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
var sessionManager = scs.New()
func TestSession(t *testing.T) {
sessionManager.Lifetime = 24 * time.Hour
e := echo.New()
// Call /put to set the message in the session manager
req := httptest.NewRequest(http.MethodGet, "/put", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
session := LoadAndSave(sessionManager)
h := session(func(c echo.Context) error {
sessionManager.Put(c.Request().Context(), "message", "Hello from a session!")
return c.String(http.StatusOK, "")
})
h(c)
assert.Equal(t, rec.Result().StatusCode, 200)
assert.Equal(t, len(rec.Result().Cookies()), 1)
sessionCookie := rec.Result().Cookies()[0]
assert.Equal(t, sessionCookie.Name, "session")
// Make a request to /get to see if the message is still there
req = httptest.NewRequest(http.MethodGet, "/get", nil)
req.Header.Set("Cookie", sessionCookie.Name+"="+sessionCookie.Value)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
h = session(func(c echo.Context) error {
msg := sessionManager.GetString(c.Request().Context(), "message")
return c.String(http.StatusOK, msg)
})
h(c)
assert.Equal(t, rec.Result().StatusCode, 200)
assert.Equal(t, rec.Body.String(), "Hello from a session!")
}