-
Notifications
You must be signed in to change notification settings - Fork 2
/
rememberme_test.go
52 lines (41 loc) · 1.06 KB
/
rememberme_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
package rememberme
import (
"github.com/janekolszak/rememberme/store/sqlstore"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
_ "github.com/mattn/go-sqlite3"
)
const (
testFileName = "/tmp/idp_cookie_test.db3"
)
var (
users = []string{"user1", "user2", "user3", "user4"}
)
func TestSetUpdateCookie(t *testing.T) {
assert := assert.New(t)
store, err := sqlstore.New("sqlite3", testFileName)
assert.Nil(err)
defer store.Close()
c := Rememberme{
Store: store,
MaxAge: time.Minute * 1,
}
for _, user := range users {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/", nil)
err = c.SetCookie(w, r, user)
assert.Nil(err)
// Should check true if this cookie appears
requestToVerify := &http.Request{Header: http.Header{"Cookie": w.HeaderMap["Set-Cookie"]}}
selector, readUser, err := c.Check(requestToVerify)
assert.Nil(err)
assert.Equal(user, readUser)
assert.NotEqual(selector, "")
w2 := httptest.NewRecorder()
err = c.UpdateCookie(w2, r, selector, user)
assert.Nil(err)
}
}