-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession_test.go
212 lines (167 loc) · 5.14 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package session
import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/flamego/flamego"
)
func TestSessioner(t *testing.T) {
f := flamego.NewWithLogger(&bytes.Buffer{})
f.Use(Sessioner())
f.Get("/", func(c flamego.Context, session Session, store Store) string {
_ = store.GC(c.Request().Context())
return session.ID()
})
f.Get("/regenerate", func(w http.ResponseWriter, r *http.Request, session Session) string {
err := session.RegenerateID(w, r)
require.NoError(t, err)
return "something in the response body"
})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
f.ServeHTTP(resp, req)
want := fmt.Sprintf("flamego_session=%s; Path=/; HttpOnly; SameSite=Lax", resp.Body.String())
cookie := resp.Header().Get("Set-Cookie")
assert.Equal(t, want, cookie)
// Make a request again using the same session ID
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.Header.Set("Cookie", cookie)
f.ServeHTTP(resp, req)
got := fmt.Sprintf("flamego_session=%s; Path=/; HttpOnly; SameSite=Lax", resp.Body.String())
assert.Equal(t, cookie, got)
// Force-regenerate the session ID even if the session ID exists.
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/regenerate", nil)
require.NoError(t, err)
req.Header.Set("Cookie", cookie)
f.ServeHTTP(resp, req)
got = resp.Header().Get("Set-Cookie")
assert.NotEmpty(t, got)
assert.NotEqual(t, cookie, got)
}
func TestSessioner_Header(t *testing.T) {
f := flamego.NewWithLogger(&bytes.Buffer{})
f.Use(Sessioner(
Options{
ReadIDFunc: func(r *http.Request) string {
return r.Header.Get("Session-Id")
},
WriteIDFunc: func(w http.ResponseWriter, r *http.Request, sid string, created bool) {
if created {
r.Header.Set("Session-Id", sid)
}
w.Header().Set("Session-Id", sid)
},
},
))
f.Get("/", func(c flamego.Context, session Session, store Store) string {
_ = store.GC(c.Request().Context())
return session.ID()
})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
f.ServeHTTP(resp, req)
sid := resp.Header().Get("Session-Id")
assert.Equal(t, resp.Body.String(), sid)
// Make a request again using the same session ID
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.Header.Set("Session-Id", sid)
f.ServeHTTP(resp, req)
assert.Equal(t, sid, resp.Body.String())
}
type noopStore struct{}
func (s *noopStore) Exist(context.Context, string) bool {
return false
}
func (s *noopStore) Read(_ context.Context, sid string) (Session, error) {
return newMemorySession(sid, nil), nil
}
func (s *noopStore) Destroy(context.Context, string) error {
return nil
}
func (s *noopStore) Touch(context.Context, string) error {
return nil
}
func (s *noopStore) Save(ctx context.Context, _ Session) error {
if ctx.Err() != nil {
return errors.Wrap(ctx.Err(), "something went wrong")
}
return nil
}
func (s *noopStore) GC(context.Context) error {
return nil
}
func TestSessioner_ContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
f := flamego.NewWithLogger(&bytes.Buffer{})
f.Use(Sessioner(
Options{
Initer: func(context.Context, ...interface{}) (Store, error) {
return &noopStore{}, nil
},
},
))
f.Get("/", func() {
cancel()
})
resp := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/", nil)
require.NoError(t, err)
f.ServeHTTP(resp, req)
}
func TestSession_Flash(t *testing.T) {
f := flamego.NewWithLogger(&bytes.Buffer{})
f.Use(Sessioner())
f.Get("/", func(c flamego.Context, f Flash) string {
s, ok := f.(string)
if !ok {
return "no flash"
}
return s
})
f.Post("/set-flash", func(s Session) {
s.SetFlash("This is a flash message")
})
// No flash in the initial request
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, "no flash", resp.Body.String())
cookie := resp.Header().Get("Set-Cookie")
// Send a request to set flash
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodPost, "/set-flash", nil)
require.NoError(t, err)
req.Header.Set("Cookie", cookie)
f.ServeHTTP(resp, req)
// Flash should be returned
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.Header.Set("Cookie", cookie)
f.ServeHTTP(resp, req)
assert.Equal(t, "This is a flash message", resp.Body.String())
// Flash has gone now if we try again
resp = httptest.NewRecorder()
req, err = http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.Header.Set("Cookie", cookie)
f.ServeHTTP(resp, req)
assert.Equal(t, "no flash", resp.Body.String())
}