~mna/webparts-sessions

ref: 30542a33f7566f3aae1dfeb112c546f06e067526 webparts-sessions/sessions_test.go -rw-r--r-- 4.2 KiB
30542a33Martin Angers initial commit 1 year, 7 months ago
                                                                                
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
package sessions

import (
	"crypto/rand"
	"io"
	"net/http"
	"net/http/cookiejar"
	"net/http/httptest"
	"strings"
	"testing"
	"time"

	"git.sr.ht/~mna/webparts/http/httpssn"
	"github.com/stretchr/testify/require"
)

func TestSessionStore(t *testing.T) {
	authKey := make([]byte, 32)
	_, err := io.ReadFull(rand.Reader, authKey)
	require.NoError(t, err)

	t.Run("without key pairs", func(t *testing.T) {
		conf := new(Config)
		ss := New(conf)
		req := httptest.NewRequest("GET", "/", nil)
		rw := httptest.NewRecorder()

		// get works and returns a new session
		ssn, err := ss.Get(req, "test")
		require.NoError(t, err)
		require.True(t, ssn.IsNew())

		// save fails, no codec
		err = ss.Save(rw, req, ssn)
		require.Error(t, err)
		require.Contains(t, err.Error(), "no codecs")
	})

	t.Run("with key pair", func(t *testing.T) {
		conf := &Config{KeyPairs: [][]byte{authKey, nil}}
		ss := New(conf)
		req := httptest.NewRequest("GET", "/", nil)
		rw := httptest.NewRecorder()

		// get works and returns a new session
		ssn, err := ss.Get(req, "test")
		require.NoError(t, err)
		require.True(t, ssn.IsNew())

		// set a value
		ssn.Set("a", 1)

		// save works
		err = ss.Save(rw, req, ssn)
		require.NoError(t, err)

		// get returns the session with the value
		ssn, err = ss.Get(req, "test")
		require.NoError(t, err)

		val := ssn.Get("a")
		require.Equal(t, val, 1)

		val = ssn.Get("b")
		require.Nil(t, val)

		ssn.Unset("a")
		err = ss.Save(rw, req, ssn)
		require.NoError(t, err)

		val = ssn.Get("a")
		require.Nil(t, val)
	})

	t.Run("roundtrip", func(t *testing.T) {
		conf := &Config{KeyPairs: [][]byte{authKey, nil}}
		ss := New(conf)

		srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			var ssn httpssn.Session
			var err error

			switch r.URL.Path {
			case "/set":
				ssn, err = ss.Get(r, "test")
				require.NoError(t, err)
				require.True(t, ssn.IsNew())

			case "/get":
				ssn, err = ss.Get(r, "test")
				require.NoError(t, err)
				require.False(t, ssn.IsNew())

			case "/del":
				ssn, err = ss.Get(r, "test")
				require.NoError(t, err)
				require.False(t, ssn.IsNew())
				ssn.Delete()

			case "/postdel":
				ssn, err = ss.Get(r, "test")
				require.NoError(t, err)
				require.True(t, ssn.IsNew())
				return // not saved, so no cookie in response
			}
			require.NoError(t, ss.Save(w, r, ssn))
		}))
		defer srv.Close()

		jar, err := cookiejar.New(nil)
		require.NoError(t, err)
		cli := &http.Client{
			Jar:     jar,
			Timeout: time.Second,
		}

		// set sets the session cookie
		res, err := cli.Get(srv.URL + "/set")
		require.NoError(t, err)
		cks := res.Cookies()
		require.Len(t, cks, 1)

		// get gets the existing cookie
		res, err = cli.Get(srv.URL + "/get")
		require.NoError(t, err)
		cks = res.Cookies()
		require.Len(t, cks, 1)

		// del deletes the cookie, still present in the response but won't be sent anymore
		res, err = cli.Get(srv.URL + "/del")
		require.NoError(t, err)
		cks = res.Cookies()
		require.Len(t, cks, 1)

		// postdel receives no cookie
		res, err = cli.Get(srv.URL + "/postdel")
		require.NoError(t, err)
		cks = res.Cookies()
		require.Len(t, cks, 0)
	})
}

func TestKeyPairs(t *testing.T) {
	const keySize = 4

	cases := []struct {
		in     string
		out    []string
		hasErr bool
	}{
		{"", nil, false},
		{"    ", []string{"", ""}, false},
		{"a", nil, true},
		{"ab", nil, true},
		{"abc", nil, true},
		{"abcd", []string{"abcd", ""}, false},
		{"abcde", nil, true},
		{"abcdef", nil, true},
		{"abcdefg", nil, true},
		{"abcdefgh", []string{"abcd", "efgh"}, false},
		{"abcdefghi", nil, true},
		{"abcdefghij", nil, true},
		{"abcdefghijk", nil, true},
		{"abcdefghijkl", []string{"ijkl", "", "abcd", "efgh"}, false},
		{"abcd    ijkl", []string{"ijkl", "", "abcd", ""}, false},
		{"abcdefghijkl    mnop", []string{"mnop", "", "ijkl", "", "abcd", "efgh"}, false},
		{"abcdefghijkl    mnopqrst", []string{"mnop", "qrst", "ijkl", "", "abcd", "efgh"}, false},
	}
	for _, c := range cases {
		t.Run(c.in, func(t *testing.T) {
			got, err := keyPairs(strings.NewReader(c.in), keySize)
			if c.hasErr {
				require.Error(t, err)
				return
			}

			require.NoError(t, err)

			var res []string
			for _, key := range got {
				res = append(res, string(key))
			}
			require.Equal(t, res, c.out)
		})
	}
}