From dfd96dae86612a12cdbd74c50b997ccf32d998ee Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Sat, 5 Mar 2022 12:46:12 -0500 Subject: [PATCH 1/2] task: fixing #221,flash being cleared on redirect --- default_context.go | 1 - flash_test.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/default_context.go b/default_context.go index 0aad92404..3f2eec99d 100644 --- a/default_context.go +++ b/default_context.go @@ -195,7 +195,6 @@ var mapType = reflect.ValueOf(map[string]interface{}{}).Type() // Redirect a request with the given status to the given URL. func (d *DefaultContext) Redirect(status int, url string, args ...interface{}) error { if d.Session() != nil { - d.Flash().Clear() d.Flash().persist(d.Session()) if err := d.Session().Save(); err != nil { return HTTPError{Status: http.StatusInternalServerError, Cause: err} diff --git a/flash_test.go b/flash_test.go index 25f863f47..bd3edb86a 100644 --- a/flash_test.go +++ b/flash_test.go @@ -3,6 +3,7 @@ package buffalo import ( "net/http" "testing" + "text/template" "github.com/gobuffalo/buffalo/render" "github.com/gobuffalo/httptest" @@ -120,4 +121,30 @@ func Test_FlashRenderCustomKeyNotDefined(t *testing.T) { const customKeyTPL = ` {{#each flash.other as |k value|}} {{value}} - {{/each}}` + {{/each}} +` + +func Test_FlashNotClearedOnRedirect(t *testing.T) { + r := require.New(t) + a := New(Options{}) + rr := render.New(render.Options{}) + + a.GET("/flash", func(c Context) error { + c.Flash().Add("success", "Antonio, you're welcome!") + return c.Redirect(http.StatusSeeOther, "/") + }) + + a.GET("/", func(c Context) error { + template := `Message: <%= flash["success"] %>` + return c.Render(http.StatusCreated, rr.String(template)) + }) + + w := httptest.New(a) + res := w.HTML("/flash").Get() + r.Equal(res.Code, http.StatusSeeOther) + r.Equal(res.Location(), "/") + + res = w.HTML("/").Get() + r.Contains(res.Body.String(), template.HTMLEscapeString("Antonio, you're welcome!")) + +} From 8e715969fdafcb02f520572410699a3a4e88d9d5 Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Sat, 5 Mar 2022 12:47:40 -0500 Subject: [PATCH 2/2] adding comment for the embed underscore import --- error_templates.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error_templates.go b/error_templates.go index 2fb4d13d9..6ebbb3983 100644 --- a/error_templates.go +++ b/error_templates.go @@ -1,7 +1,7 @@ package buffalo import ( - _ "embed" + _ "embed" // needed to embed the templates. ) var (