This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2193 from saurori/dupe-session
Only save Session once to avoid duplicate session cookie
- Loading branch information
Showing
7 changed files
with
74 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,6 @@ func New(opts Options) *App { | |
} | ||
a.Use(a.PanicHandler) | ||
a.Use(RequestLogger) | ||
a.Use(sessionSaver) | ||
|
||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package buffalo | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gobuffalo/buffalo/render" | ||
"github.com/gobuffalo/httptest" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Session_SingleCookie(t *testing.T) { | ||
r := require.New(t) | ||
|
||
sessionName := "_test_session" | ||
a := New(Options{SessionName: sessionName}) | ||
rr := render.New(render.Options{}) | ||
|
||
a.GET("/", func(c Context) error { | ||
return c.Render(http.StatusCreated, rr.String("")) | ||
}) | ||
|
||
w := httptest.New(a) | ||
res := w.HTML("/").Get() | ||
|
||
var sessionCookies []string | ||
for _, c := range res.Header().Values("Set-Cookie") { | ||
if strings.HasPrefix(c, sessionName) { | ||
sessionCookies = append(sessionCookies, c) | ||
} | ||
} | ||
|
||
r.Equal(1, len(sessionCookies)) | ||
} | ||
|
||
func Test_Session_CustomValue(t *testing.T) { | ||
r := require.New(t) | ||
|
||
a := New(Options{}) | ||
rr := render.New(render.Options{}) | ||
|
||
// Root path sets a custom session value | ||
a.GET("/", func(c Context) error { | ||
c.Session().Set("example", "test") | ||
return c.Render(http.StatusCreated, rr.String("")) | ||
}) | ||
// /session path prints custom session value as response | ||
a.GET("/session", func(c Context) error { | ||
sessionValue := c.Session().Get("example") | ||
return c.Render(http.StatusCreated, rr.String(fmt.Sprintf("%s", sessionValue))) | ||
}) | ||
|
||
w := httptest.New(a) | ||
_ = w.HTML("/").Get() | ||
|
||
// Create second request that should contain the cookie from the first response | ||
reqGetSession := w.HTML("/session") | ||
resGetSession := reqGetSession.Get() | ||
|
||
r.Equal(resGetSession.Body.String(), "test") | ||
} |