mirror of https://github.com/gin-gonic/gin.git
Added support for SameSite cookie flag (#1615)
* 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>
This commit is contained in:
parent
982daeb1ec
commit
f94406a087
|
@ -775,7 +775,7 @@ func (c *Context) GetRawData() ([]byte, error) {
|
||||||
// SetCookie adds a Set-Cookie header to the ResponseWriter's headers.
|
// SetCookie adds a Set-Cookie header to the ResponseWriter's headers.
|
||||||
// The provided cookie must have a valid Name. Invalid cookies may be
|
// The provided cookie must have a valid Name. Invalid cookies may be
|
||||||
// silently dropped.
|
// 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 == "" {
|
if path == "" {
|
||||||
path = "/"
|
path = "/"
|
||||||
}
|
}
|
||||||
|
@ -785,6 +785,7 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string,
|
||||||
MaxAge: maxAge,
|
MaxAge: maxAge,
|
||||||
Path: path,
|
Path: path,
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
|
SameSite: sameSite,
|
||||||
Secure: secure,
|
Secure: secure,
|
||||||
HttpOnly: httpOnly,
|
HttpOnly: httpOnly,
|
||||||
})
|
})
|
||||||
|
|
|
@ -602,14 +602,14 @@ func TestContextPostFormMultipart(t *testing.T) {
|
||||||
|
|
||||||
func TestContextSetCookie(t *testing.T) {
|
func TestContextSetCookie(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
c.SetCookie("user", "gin", 1, "/", "localhost", true, true)
|
c.SetCookie("user", "gin", 1, "/", "localhost", http.SameSiteLaxMode, true, true)
|
||||||
assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure", c.Writer.Header().Get("Set-Cookie"))
|
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) {
|
func TestContextSetCookiePathEmpty(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
c.SetCookie("user", "gin", 1, "", "localhost", true, true)
|
c.SetCookie("user", "gin", 1, "", "localhost", http.SameSiteLaxMode, true, true)
|
||||||
assert.Equal(t, "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure", c.Writer.Header().Get("Set-Cookie"))
|
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) {
|
func TestContextGetCookie(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue