-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock.go
53 lines (43 loc) · 1.26 KB
/
mock.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
package ginsession
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/boxgo/redisstore/v2"
"github.com/boxgo/redisstore/v2/serializer"
"github.com/gorilla/sessions"
)
// MockSet set cookie and return redis sid key and cookie string
func (s *GinSession) MockSet(value interface{}) (sid string, cookie string, err error) {
var (
store *redisstore.RedisStore
session *sessions.Session
req *http.Request
rsp = httptest.NewRecorder()
)
if store, err = redisstore.NewStoreWithUniversalClient(
s.client.Client(),
redisstore.WithMaxLength(s.cfg.MaxLen),
redisstore.WithKeyPrefix(s.cfg.KeyPrefix),
redisstore.WithKeyPairs([]byte(s.cfg.KeyPair)),
redisstore.WithSerializer(serializer.JSONSerializer{}),
); err != nil {
return
}
if req, err = http.NewRequest("GET", "http://ginsession_mock", nil); err != nil {
return
}
if session, err = store.New(req, s.CookieName()); err != nil {
return
}
session.Values["user"] = value
if err = session.Save(req, rsp); err != nil {
return
}
return fmt.Sprintf("%s%s", s.cfg.KeyPrefix, session.ID), rsp.Header().Get("Set-Cookie"), nil
}
// MockDel del session by sid
func (s *GinSession) MockDel(sid string) (err error) {
return s.client.Client().Del(context.TODO(), sid).Err()
}