forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_sessions_test.go
196 lines (166 loc) · 6.03 KB
/
policy_sessions_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
package iris_test
import (
"testing"
// developers can use any library to add a custom cookie encoder/decoder.
// At this test code we use the gorilla's securecookie library:
"github.com/gorilla/securecookie"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/adaptors/sessions"
"gopkg.in/kataras/iris.v6/httptest"
)
func TestSessions(t *testing.T) {
app := iris.New()
app.Adapt(httprouter.New())
app.Adapt(sessions.New(sessions.Config{Cookie: "mycustomsessionid"}))
testSessions(app, t)
}
func TestSessionsEncodeDecode(t *testing.T) {
// test the sessions encode decode via gorilla.securecookie
app := iris.New()
app.Adapt(httprouter.New())
// IMPORTANT
cookieName := "mycustomsessionid"
// AES only supports key sizes of 16, 24 or 32 bytes.
// You either need to provide exactly that amount or you derive the key from what you type in.
hashKey := []byte("the-big-and-secret-fash-key-here")
blockKey := []byte("lot-secret-of-characters-big-too")
secureCookie := securecookie.New(hashKey, blockKey)
app.Adapt(sessions.New(sessions.Config{
Cookie: cookieName,
Encode: secureCookie.Encode,
Decode: secureCookie.Decode,
}))
testSessions(app, t)
}
func testSessions(app *iris.Framework, t *testing.T) {
values := map[string]interface{}{
"Name": "iris",
"Months": "4",
"Secret": "dsads£2132215£%%Ssdsa",
}
writeValues := func(ctx *iris.Context) {
sessValues := ctx.Session().GetAll()
ctx.JSON(iris.StatusOK, sessValues)
}
if testEnableSubdomain {
app.Party(testSubdomain+".").Get("/get", func(ctx *iris.Context) {
writeValues(ctx)
})
}
app.Post("set", func(ctx *iris.Context) {
vals := make(map[string]interface{}, 0)
if err := ctx.ReadJSON(&vals); err != nil {
t.Fatalf("Cannot readjson. Trace %s", err.Error())
}
for k, v := range vals {
ctx.Session().Set(k, v)
}
})
app.Get("/get", func(ctx *iris.Context) {
writeValues(ctx)
})
app.Get("/clear", func(ctx *iris.Context) {
ctx.Session().Clear()
writeValues(ctx)
})
app.Get("/destroy", func(ctx *iris.Context) {
ctx.SessionDestroy()
writeValues(ctx)
// the cookie and all values should be empty
})
// request cookie should be empty
app.Get("/after_destroy", func(ctx *iris.Context) {
})
app.Config.VHost = "mydomain.com"
e := httptest.New(app, t)
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
if testEnableSubdomain {
es := subdomainTester(e, app)
es.Request("GET", "/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
}
// test destroy which also clears first
d := e.GET("/destroy").Expect().Status(iris.StatusOK)
d.JSON().Object().Empty()
// This removed: d.Cookies().Empty(). Reason:
// httpexpect counts the cookies setted or deleted at the response time, but cookie is not removed, to be really removed needs to SetExpire(now-1second) so,
// test if the cookies removed on the next request, like the browser's behavior.
e.GET("/after_destroy").Expect().Status(iris.StatusOK).Cookies().Empty()
// set and clear again
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
e.GET("/clear").Expect().Status(iris.StatusOK).JSON().Object().Empty()
}
func TestFlashMessages(t *testing.T) {
app := iris.New()
app.Adapt(httprouter.New())
app.Adapt(sessions.New(sessions.Config{Cookie: "mycustomsessionid"}))
valueSingleKey := "Name"
valueSingleValue := "iris-sessions"
values := map[string]interface{}{
valueSingleKey: valueSingleValue,
"Days": "1",
"Secret": "dsads£2132215£%%Ssdsa",
}
writeValues := func(ctx *iris.Context, values map[string]interface{}) error {
return ctx.JSON(iris.StatusOK, values)
}
app.Post("/set", func(ctx *iris.Context) {
vals := make(map[string]interface{}, 0)
if err := ctx.ReadJSON(&vals); err != nil {
t.Fatalf("Cannot readjson. Trace %s", err.Error())
}
sess := ctx.Session()
for k, v := range vals {
sess.SetFlash(k, v)
}
ctx.SetStatusCode(iris.StatusOK)
})
writeFlashValues := func(ctx *iris.Context) {
sess := ctx.Session()
flashes := sess.GetFlashes()
if err := writeValues(ctx, flashes); err != nil {
t.Fatalf("While serialize the flash values: %s", err.Error())
}
}
app.Get("/get_single", func(ctx *iris.Context) {
sess := ctx.Session()
flashMsgString := sess.GetFlashString(valueSingleKey)
ctx.WriteString(flashMsgString)
})
app.Get("/get", func(ctx *iris.Context) {
writeFlashValues(ctx)
})
app.Get("/clear", func(ctx *iris.Context) {
sess := ctx.Session()
sess.ClearFlashes()
writeFlashValues(ctx)
})
app.Get("/destroy", func(ctx *iris.Context) {
ctx.SessionDestroy()
writeFlashValues(ctx)
ctx.SetStatusCode(iris.StatusOK)
// the cookie and all values should be empty
})
// request cookie should be empty
app.Get("/after_destroy", func(ctx *iris.Context) {
ctx.SetStatusCode(iris.StatusOK)
})
e := httptest.New(app, t)
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
// get all
e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
// get the same flash on other request should return nothing because the flash message is removed after fetch once
e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Empty()
// test destory which also clears first
d := e.GET("/destroy").Expect().Status(iris.StatusOK)
d.JSON().Object().Empty()
e.GET("/after_destroy").Expect().Status(iris.StatusOK).Cookies().Empty()
// set and clear again
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
e.GET("/clear").Expect().Status(iris.StatusOK).JSON().Object().Empty()
// set again in order to take the single one ( we don't test Cookies.NotEmpty because httpexpect default conf reads that from the request-only)
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK)
// e.GET("/get/").Expect().Status(http.StatusOK).JSON().Object().Equal(values)
e.GET("/get_single").Expect().Status(iris.StatusOK).Body().Equal(valueSingleValue)
}