1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-04 02:37:14 +00:00

Embrace https://github.com/kataras/iris/issues/250 - Flash messages available inside request life

This commit is contained in:
Makis Maropoulos
2016-07-02 15:02:33 +02:00
parent 1f1ed4cc36
commit 9100560e00
4 changed files with 88 additions and 30 deletions

View File

@@ -60,27 +60,27 @@ func TestRenderRest(t *testing.T) {
})
e := tester(api, t)
dataT := e.GET("/data").Expect()
dataT := e.GET("/data").Expect().Status(iris.StatusOK)
dataT.Header("Content-Type").Equal("application/octet-stream")
dataT.Body().Equal(string(dataContents))
textT := e.GET("/text").Expect()
textT := e.GET("/text").Expect().Status(iris.StatusOK)
textT.Header("Content-Type").Equal("text/plain; charset=UTF-8")
textT.Body().Equal(textContents)
JSONPT := e.GET("/jsonp").Expect()
JSONPT := e.GET("/jsonp").Expect().Status(iris.StatusOK)
JSONPT.Header("Content-Type").Equal("application/javascript; charset=UTF-8")
JSONPT.Body().Equal(JSONPCallback + `({"hello":"jsonp"});`)
JSONT := e.GET("/json").Expect()
JSONT := e.GET("/json").Expect().Status(iris.StatusOK)
JSONT.Header("Content-Type").Equal("application/json; charset=UTF-8")
JSONT.JSON().Object().Equal(JSONXMLContents)
XMLT := e.GET("/xml").Expect()
XMLT := e.GET("/xml").Expect().Status(iris.StatusOK)
XMLT.Header("Content-Type").Equal("text/xml; charset=UTF-8")
XMLT.Body().Equal(`<` + JSONXMLContents.XMLName.Local + ` first="` + JSONXMLContents.FirstAttr + `" second="` + JSONXMLContents.SecondAttr + `"><name>` + JSONXMLContents.Name + `</name><birth>` + JSONXMLContents.Birth + `</birth><stars>` + strconv.Itoa(JSONXMLContents.Stars) + `</stars></info>`)
markdownT := e.GET("/markdown").Expect()
markdownT := e.GET("/markdown").Expect().Status(iris.StatusOK)
markdownT.Header("Content-Type").Equal("text/html; charset=UTF-8")
markdownT.Body().Equal("<h1>" + markdownContents[2:] + "</h1>\n")

View File

@@ -72,3 +72,43 @@ func TestSessions(t *testing.T) {
e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
e.GET("/clear").Expect().Status(iris.StatusOK).JSON().Object().Empty()
}
func FlashMessagesTest(t *testing.T) {
api := iris.New()
values := map[string]string{"name": "kataras", "package": "iris"}
api.Put("/set", func(ctx *iris.Context) {
for k, v := range values {
ctx.SetFlash(k, v)
}
})
api.Get("/get", func(ctx *iris.Context) {
// one time one handler
kv := make(map[string]string)
for k := range values {
kv[k], _ = ctx.GetFlash(k)
}
//second time on the same handler
for k := range values {
kv[k], _ = ctx.GetFlash(k)
}
}, func(ctx *iris.Context) {
// third time on a next handler
// test the if next handler has access to them(must) because flash are request lifetime now.
kv := make(map[string]string)
for k := range values {
kv[k], _ = ctx.GetFlash(k)
}
// print them to the client for test the response also
ctx.JSON(iris.StatusOK, kv)
})
e := tester(api, t)
e.PUT("/set").Expect().Status(iris.StatusOK)
e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
// secnd request lifetime ,the flash messages here should be not available
e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Empty()
}