Skip to content

Commit dc2207b

Browse files
committed
add session.Destroy method.
1 parent 206c373 commit dc2207b

8 files changed

+77
-24
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ go:
66
- 1.8.1
77
- 1.8.2
88
- 1.8.3
9+
- 1.8.4
10+
- 1.8.5
11+
- 1.9
12+
- 1.9.1
13+
- 1.9.2
914
before_install:
1015
- go get -t -v ./...
1116
- go get github.com/mattn/goveralls

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func (s *Session) Save() error {
4444
return s.GetStore().Save(s)
4545
}
4646

47+
func (s *Session) Destroy() error {
48+
return s.GetStore().Destroy(s)
49+
}
50+
4751
func main() {
4852
SessionName := "Sess"
4953
SessionKeys := []string{"keyxxx"}

cookiestore.go

+6
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ func (c *CookieStore) Save(session Sessions) (err error) {
5757
}
5858
return
5959
}
60+
61+
// Destroy destroy the session
62+
func (c *CookieStore) Destroy(session Sessions) (err error) {
63+
session.GetCookie().Remove(session.GetName(), c.opts)
64+
return
65+
}

example/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func (s *Session) Save() error {
2121
return s.GetStore().Save(s)
2222
}
2323

24+
// Destroy ...
25+
func (s *Session) Destroy() error {
26+
return s.GetStore().Destroy(s)
27+
}
28+
2429
func main() {
2530
SessionName := "Sess"
2631
SessionKeys := []string{"keyxxx"}

memorystore.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func NewMemoryStore(options ...*Options) (store *MemoryStore) {
1616
opts := &cookie.Options{
1717
Path: "/",
1818
HTTPOnly: true,
19-
Signed: true,
19+
Signed: false, // not necessary
2020
MaxAge: 24 * 60 * 60,
2121
}
2222
if len(options) > 0 && options[0] != nil {
@@ -90,15 +90,27 @@ func (m *MemoryStore) Save(session Sessions) (err error) {
9090
return
9191
}
9292

93+
// Destroy destroy the session
94+
func (m *MemoryStore) Destroy(session Sessions) (err error) {
95+
sid := session.GetSID()
96+
if sid != "" {
97+
m.lock.Lock()
98+
defer m.lock.Unlock()
99+
delete(m.store, sid)
100+
}
101+
session.GetCookie().Remove(session.GetName(), m.opts)
102+
return
103+
}
104+
93105
// Len ...
94106
func (m *MemoryStore) Len() int {
95107
m.lock.Lock()
96108
defer m.lock.Unlock()
97109
return len(m.store)
98110
}
99111

100-
// Destroy goroutine cleanCache thread
101-
func (m *MemoryStore) Destroy() {
112+
// Close goroutine cleanCache thread
113+
func (m *MemoryStore) Close() {
102114
close(m.done)
103115
}
104116

@@ -113,6 +125,7 @@ func (m *MemoryStore) cleanCache() {
113125
}
114126
}
115127
}
128+
116129
func (m *MemoryStore) clean() {
117130
m.lock.Lock()
118131
defer m.lock.Unlock()

memorystore_test.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,25 @@ func TestMemoryStore(t *testing.T) {
5959
assert.Equal(int64(useage), session.Age)
6060
assert.False(session.IsNew())
6161
assert.True(session.GetSID() != "")
62+
assert.Nil(session.Destroy())
6263
})
64+
recorder = httptest.NewRecorder()
6365
handler.ServeHTTP(recorder, req)
6466

65-
//====== reuse session=====
66-
67+
//====== destroy session=====
6768
req, err = http.NewRequest("GET", "/", nil)
6869
migrateCookies(recorder, req)
6970

7071
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7172
session := &Session{Meta: &sessions.Meta{}}
72-
store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
73-
74-
assert.Equal(username, session.Name)
75-
assert.Equal(useage, session.Age)
76-
assert.False(session.IsNew())
77-
assert.True(session.GetSID() != "")
73+
err := store.Load(SessionName, session, cookie.New(w, r, SessionKeys...))
74+
assert.NotNil(err)
75+
assert.True(session.IsNew())
7876
})
77+
recorder = httptest.NewRecorder()
7978
handler.ServeHTTP(recorder, req)
8079
})
80+
8181
t.Run("Sessions with sign session that should be", func(t *testing.T) {
8282
assert := assert.New(t)
8383
recorder := httptest.NewRecorder()
@@ -117,9 +117,10 @@ func TestMemoryStore(t *testing.T) {
117117
assert.Equal(secondUsage, session.Age)
118118

119119
})
120+
recorder = httptest.NewRecorder()
120121
handler.ServeHTTP(recorder, req)
121-
122122
})
123+
123124
t.Run("Sessions with Name() and Store() that should be", func(t *testing.T) {
124125
assert := assert.New(t)
125126
recorder := httptest.NewRecorder()
@@ -155,6 +156,7 @@ func TestMemoryStore(t *testing.T) {
155156
assert.Equal(true, cookies.Secure)
156157

157158
})
159+
158160
t.Run("Sessions donn't override old value when seting same value that should be", func(t *testing.T) {
159161
assert := assert.New(t)
160162
req, err := http.NewRequest("GET", "/", nil)
@@ -184,6 +186,7 @@ func TestMemoryStore(t *testing.T) {
184186
})
185187
handler.ServeHTTP(recorder, req)
186188
})
189+
187190
t.Run("Sessions with high goroutine should be", func(t *testing.T) {
188191
assert := assert.New(t)
189192
req, err := http.NewRequest("GET", "/", nil)
@@ -237,9 +240,10 @@ func TestMemoryStore(t *testing.T) {
237240
})
238241
handler.ServeHTTP(recorder, req)
239242

240-
store.Destroy()
243+
store.Close()
241244
})
242245
}
246+
243247
func genID() string {
244248
buf := make([]byte, 12)
245249
_, err := rand.Read(buf)

sessions.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ type Store interface {
1818
Load(name string, session Sessions, cookie *cookie.Cookies) error
1919
// Save should persist session to the underlying store implementation.
2020
Save(session Sessions) error
21+
// Destroy should destroy the session.
22+
Destroy(session Sessions) error
2123
}
2224

2325
// Sessions ...
2426
type Sessions interface {
2527
// Init sets current cookie.Cookies and Store to the session instance.
26-
Init(name, sid string, c *cookie.Cookies, store Store, lastvalue string)
28+
Init(name, sid string, c *cookie.Cookies, store Store, lastValue string)
2729
// GetSID returns the session' sid
2830
GetSID() string
2931
// GetName returns the session' name
@@ -41,20 +43,20 @@ type Sessions interface {
4143
// Meta stores the values and optional configuration for a session.
4244
type Meta struct {
4345
// Values map[string]interface{}
44-
sid string
45-
store Store
46-
name string
47-
cookie *cookie.Cookies
48-
lastvale string
46+
sid string
47+
store Store
48+
name string
49+
cookie *cookie.Cookies
50+
lastValue string
4951
}
5052

5153
// Init sets current cookie.Cookies and Store to the session instance.
52-
func (s *Meta) Init(name, sid string, c *cookie.Cookies, store Store, lastvalue string) {
54+
func (s *Meta) Init(name, sid string, c *cookie.Cookies, store Store, lastValue string) {
5355
s.name = name
5456
s.sid = sid
5557
s.cookie = c
5658
s.store = store
57-
s.lastvale = lastvalue
59+
s.lastValue = lastValue
5860
}
5961

6062
// GetSID returns the session' sid
@@ -79,7 +81,7 @@ func (s *Meta) GetCookie() *cookie.Cookies {
7981

8082
// IsChanged to check current session's value whether is changed
8183
func (s *Meta) IsChanged(val string) bool {
82-
return s.lastvale != val
84+
return s.lastValue != val
8385
}
8486

8587
// IsNew to check the current session whether it's new user

sessions_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"net/http/httptest"
66
"testing"
7+
"time"
78

89
"github.com/go-http-utils/cookie"
910
"github.com/go-http-utils/cookie-session"
@@ -22,6 +23,11 @@ type Session struct {
2223
func (s *Session) Save() error {
2324
return s.GetStore().Save(s)
2425
}
26+
27+
func (s *Session) Destroy() error {
28+
return s.GetStore().Destroy(s)
29+
}
30+
2531
func TestSessions(t *testing.T) {
2632

2733
SessionName := "teambition"
@@ -80,6 +86,7 @@ func TestSessions(t *testing.T) {
8086
})
8187
handler.ServeHTTP(recorder, req)
8288
})
89+
8390
t.Run("Sessions with sign session that should be", func(t *testing.T) {
8491
assert := assert.New(t)
8592
recorder := httptest.NewRecorder()
@@ -124,6 +131,7 @@ func TestSessions(t *testing.T) {
124131
handler.ServeHTTP(recorder, req)
125132

126133
})
134+
127135
t.Run("Sessions with Name() and Store() that should be", func(t *testing.T) {
128136
assert := assert.New(t)
129137
recorder := httptest.NewRecorder()
@@ -159,6 +167,7 @@ func TestSessions(t *testing.T) {
159167
assert.Equal(true, cookies.Secure)
160168

161169
})
170+
162171
t.Run("Sessions with Encode() and Decode() that should be", func(t *testing.T) {
163172
assert := assert.New(t)
164173
dt := make(map[string]interface{})
@@ -168,7 +177,8 @@ func TestSessions(t *testing.T) {
168177
_, err = sessions.Encode(make(chan int))
169178
assert.NotNil(err)
170179
})
171-
t.Run("Sessions donn't override old value when seting same value that should be", func(t *testing.T) {
180+
181+
t.Run("Sessions don't override old value when seting same value that should be", func(t *testing.T) {
172182
assert := assert.New(t)
173183
req, err := http.NewRequest("GET", "/", nil)
174184
assert.Nil(err)
@@ -247,6 +257,7 @@ func TestSessionCompatible(t *testing.T) {
247257
handler.ServeHTTP(recorder, req)
248258
})
249259
}
260+
250261
func getCookie(name string, recorder *httptest.ResponseRecorder) (*http.Cookie, error) {
251262
var err error
252263
res := &http.Response{Header: http.Header{"Set-Cookie": recorder.HeaderMap["Set-Cookie"]}}
@@ -257,8 +268,11 @@ func getCookie(name string, recorder *httptest.ResponseRecorder) (*http.Cookie,
257268
}
258269
return nil, err
259270
}
271+
260272
func migrateCookies(recorder *httptest.ResponseRecorder, req *http.Request) {
261273
for _, cookie := range recorder.Result().Cookies() {
262-
req.AddCookie(cookie)
274+
if !cookie.Expires.IsZero() && !cookie.Expires.Before(time.Now()) {
275+
req.AddCookie(cookie)
276+
}
263277
}
264278
}

0 commit comments

Comments
 (0)