Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2216 from gobuffalo/task-fix-#2211
Browse files Browse the repository at this point in the history
Fix #2211
  • Loading branch information
paganotoni authored Mar 5, 2022
2 parents db7f15c + 8e71596 commit c341dc2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 0 additions & 1 deletion default_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion error_templates.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package buffalo

import (
_ "embed"
_ "embed" // needed to embed the templates.
)

var (
Expand Down
29 changes: 28 additions & 1 deletion flash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package buffalo
import (
"net/http"
"testing"
"text/template"

"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/httptest"
Expand Down Expand Up @@ -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!"))

}

0 comments on commit c341dc2

Please sign in to comment.