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

Commit

Permalink
task: fixing #221,flash being cleared on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Mar 5, 2022
1 parent db7f15c commit dfd96da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 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
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 dfd96da

Please sign in to comment.