Skip to content

Commit

Permalink
Added support for SameSite cookie flag (gin-gonic#1615)
Browse files Browse the repository at this point in the history
* Added support for SameSite cookie flag

* fixed tests.

* Update context.go

* Update context_test.go

* Update context_test.go

Co-authored-by: thinkerou <thinkerou@gmail.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
3 people authored and ThomasObenaus committed Feb 19, 2020
1 parent a5e351a commit 9edf526
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ func (c *Context) GetRawData() ([]byte, error) {
// SetCookie adds a Set-Cookie header to the ResponseWriter's headers.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, sameSite http.SameSite, secure, httpOnly bool) {
if path == "" {
path = "/"
}
Expand All @@ -785,6 +785,7 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string,
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: sameSite,
Secure: secure,
HttpOnly: httpOnly,
})
Expand Down
8 changes: 4 additions & 4 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,14 @@ func TestContextPostFormMultipart(t *testing.T) {

func TestContextSetCookie(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.SetCookie("user", "gin", 1, "/", "localhost", true, true)
assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure", c.Writer.Header().Get("Set-Cookie"))
c.SetCookie("user", "gin", 1, "/", "localhost", http.SameSiteLaxMode, true, true)
assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", c.Writer.Header().Get("Set-Cookie"))
}

func TestContextSetCookiePathEmpty(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.SetCookie("user", "gin", 1, "", "localhost", true, true)
assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure", c.Writer.Header().Get("Set-Cookie"))
c.SetCookie("user", "gin", 1, "", "localhost", http.SameSiteLaxMode, true, true)
assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure; SameSite=Lax", c.Writer.Header().Get("Set-Cookie"))
}

func TestContextGetCookie(t *testing.T) {
Expand Down

0 comments on commit 9edf526

Please sign in to comment.