forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_storage_test.go
executable file
·55 lines (49 loc) · 1.47 KB
/
session_storage_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
package session_test
import (
"context"
"github.com/go-chassis/go-chassis/core/lager"
"net/http"
"testing"
"time"
"github.com/go-chassis/go-chassis/session"
"github.com/stretchr/testify/assert"
)
func init() {
lager.Init(&lager.Options{
LoggerLevel: "INFO",
RollingPolicy: "size",
})
}
func TestSessionStorage(t *testing.T) {
t.Run("session option", func(t *testing.T) {
session.Save("abc", "127.0.0.1:8080", time.Second)
addr, ok := session.Get("abc")
assert.Equal(t, true, ok)
assert.Equal(t, "127.0.0.1:8080", addr)
session.Delete("abc")
addr, ok = session.Get("abc")
assert.Equal(t, false, ok)
assert.Equal(t, nil, addr)
})
t.Run("get session for cookie", func(t *testing.T) {
cookieValue := session.GetSessionCookie(nil, nil)
assert.Equal(t, "", cookieValue)
resp := &http.Response{
Header: http.Header{},
}
cookieValue = session.GetSessionCookie(nil, resp)
assert.Equal(t, "", cookieValue)
session.DeletingKeySuccessiveFailure(nil)
session.DeletingKeySuccessiveFailure(resp)
cookieValue = session.GetSessionFromResp("abc", resp)
assert.Equal(t, "", cookieValue)
session.SaveSessionIDFromHTTP("", 1, resp, new(http.Request))
ctx := context.Background()
ctx = session.SetContextMetadata(ctx, "key", "value")
val := session.GetContextMetadata(ctx, "key")
assert.Equal(t, val, "value")
ctx = session.SaveSessionIDFromContext(ctx, "", 1)
val = session.GetContextMetadata(ctx, "go-chassisLB")
assert.NotEqual(t, val, "")
})
}