mirror of
https://github.com/gin-gonic/gin.git
synced 2025-05-22 12:19:16 +08:00
feat(context): add SetCookieData (#4240)
* feat(context): add SetCookieStruct (#4215)# This is a combination of 2 commits. feat(context): add SetCookieStruct (#4215) feat(context): add SetCookieStruct (#4215) * feat(context): add SetCookieStruct (#4215) * feat(context): fix SetCookieStruct→SetCookieData (gin-gonic#4215) * fix(context): respect caller-specified SameSite value in SetCookieData
This commit is contained in:
parent
3d8e288c64
commit
fb09c825e8
13
context.go
13
context.go
@ -1027,6 +1027,19 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetCookieData adds a Set-Cookie header to the ResponseWriter's headers.
|
||||||
|
// It accepts a pointer to http.Cookie structure for more flexibility in setting cookie attributes.
|
||||||
|
// The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
|
||||||
|
func (c *Context) SetCookieData(cookie *http.Cookie) {
|
||||||
|
if cookie.Path == "" {
|
||||||
|
cookie.Path = "/"
|
||||||
|
}
|
||||||
|
if cookie.SameSite == http.SameSiteDefaultMode {
|
||||||
|
cookie.SameSite = c.sameSite
|
||||||
|
}
|
||||||
|
http.SetCookie(c.Writer, cookie)
|
||||||
|
}
|
||||||
|
|
||||||
// Cookie returns the named cookie provided in the request or
|
// Cookie returns the named cookie provided in the request or
|
||||||
// ErrNoCookie if not found. And return the named cookie is unescaped.
|
// ErrNoCookie if not found. And return the named cookie is unescaped.
|
||||||
// If multiple cookies match the given name, only one cookie will
|
// If multiple cookies match the given name, only one cookie will
|
||||||
|
126
context_test.go
126
context_test.go
@ -3123,3 +3123,129 @@ func TestContextNext(t *testing.T) {
|
|||||||
assert.True(t, exists)
|
assert.True(t, exists)
|
||||||
assert.Equal(t, "value3", value)
|
assert.Equal(t, "value3", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextSetCookieData(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
c.SetSameSite(http.SameSiteLaxMode)
|
||||||
|
var setCookie string
|
||||||
|
|
||||||
|
// Basic cookie settings
|
||||||
|
cookie := &http.Cookie{
|
||||||
|
Name: "user",
|
||||||
|
Value: "gin",
|
||||||
|
MaxAge: 1,
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
}
|
||||||
|
c.SetCookieData(cookie)
|
||||||
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
|
assert.Contains(t, setCookie, "user=gin")
|
||||||
|
assert.Contains(t, setCookie, "Path=/")
|
||||||
|
assert.Contains(t, setCookie, "Domain=localhost")
|
||||||
|
assert.Contains(t, setCookie, "Max-Age=1")
|
||||||
|
assert.Contains(t, setCookie, "HttpOnly")
|
||||||
|
assert.Contains(t, setCookie, "Secure")
|
||||||
|
// SameSite=Lax might be omitted in Go 1.23+ as it's the default
|
||||||
|
// assert.Contains(t, setCookie, "SameSite=Lax")
|
||||||
|
|
||||||
|
// Test that when Path is empty, "/" is automatically set
|
||||||
|
cookie = &http.Cookie{
|
||||||
|
Name: "user",
|
||||||
|
Value: "gin",
|
||||||
|
MaxAge: 1,
|
||||||
|
Path: "",
|
||||||
|
Domain: "localhost",
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
}
|
||||||
|
c.SetCookieData(cookie)
|
||||||
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
|
assert.Contains(t, setCookie, "user=gin")
|
||||||
|
assert.Contains(t, setCookie, "Path=/")
|
||||||
|
assert.Contains(t, setCookie, "Domain=localhost")
|
||||||
|
assert.Contains(t, setCookie, "Max-Age=1")
|
||||||
|
assert.Contains(t, setCookie, "HttpOnly")
|
||||||
|
assert.Contains(t, setCookie, "Secure")
|
||||||
|
// SameSite=Lax might be omitted in Go 1.23+ as it's the default
|
||||||
|
// assert.Contains(t, setCookie, "SameSite=Lax")
|
||||||
|
|
||||||
|
// Test additional cookie attributes (Expires)
|
||||||
|
expireTime := time.Now().Add(24 * time.Hour)
|
||||||
|
cookie = &http.Cookie{
|
||||||
|
Name: "user",
|
||||||
|
Value: "gin",
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
Expires: expireTime,
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
}
|
||||||
|
c.SetCookieData(cookie)
|
||||||
|
|
||||||
|
// Since the Expires value varies by time, partially verify with Contains
|
||||||
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
|
assert.Contains(t, setCookie, "user=gin")
|
||||||
|
assert.Contains(t, setCookie, "Path=/")
|
||||||
|
assert.Contains(t, setCookie, "Domain=localhost")
|
||||||
|
assert.Contains(t, setCookie, "HttpOnly")
|
||||||
|
assert.Contains(t, setCookie, "Secure")
|
||||||
|
// SameSite=Lax might be omitted in Go 1.23+ as it's the default
|
||||||
|
// assert.Contains(t, setCookie, "SameSite=Lax")
|
||||||
|
|
||||||
|
// Test for Partitioned attribute (Go 1.18+)
|
||||||
|
cookie = &http.Cookie{
|
||||||
|
Name: "user",
|
||||||
|
Value: "gin",
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
Partitioned: true,
|
||||||
|
}
|
||||||
|
c.SetCookieData(cookie)
|
||||||
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
|
assert.Contains(t, setCookie, "user=gin")
|
||||||
|
assert.Contains(t, setCookie, "Path=/")
|
||||||
|
assert.Contains(t, setCookie, "Domain=localhost")
|
||||||
|
assert.Contains(t, setCookie, "HttpOnly")
|
||||||
|
assert.Contains(t, setCookie, "Secure")
|
||||||
|
// SameSite=Lax might be omitted in Go 1.23+ as it's the default
|
||||||
|
// assert.Contains(t, setCookie, "SameSite=Lax")
|
||||||
|
// Not testing for Partitioned attribute as it may not be supported in all Go versions
|
||||||
|
|
||||||
|
// Test that SameSiteStrictMode is explicitly included in the header
|
||||||
|
t.Run("SameSite=Strict is included", func(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
cookie := &http.Cookie{
|
||||||
|
Name: "user",
|
||||||
|
Value: "gin",
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteStrictMode,
|
||||||
|
}
|
||||||
|
c.SetCookieData(cookie)
|
||||||
|
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||||
|
assert.Contains(t, setCookie, "SameSite=Strict")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test that SameSiteNoneMode is explicitly included in the header
|
||||||
|
t.Run("SameSite=None is included", func(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
cookie := &http.Cookie{
|
||||||
|
Name: "user",
|
||||||
|
Value: "gin",
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteNoneMode,
|
||||||
|
}
|
||||||
|
c.SetCookieData(cookie)
|
||||||
|
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||||
|
assert.Contains(t, setCookie, "SameSite=None")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
56
docs/doc.md
56
docs/doc.md
@ -2304,12 +2304,64 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.GET("/cookie", func(c *gin.Context) {
|
router.GET("/cookie", func(c *gin.Context) {
|
||||||
|
|
||||||
cookie, err := c.Cookie("gin_cookie")
|
cookie, err := c.Cookie("gin_cookie")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cookie = "NotSet"
|
cookie = "NotSet"
|
||||||
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
|
// Using http.Cookie struct for more control
|
||||||
|
c.SetCookieData(&http.Cookie{
|
||||||
|
Name: "gin_cookie",
|
||||||
|
Value: "test",
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
MaxAge: 3600,
|
||||||
|
Secure: false,
|
||||||
|
HttpOnly: true,
|
||||||
|
// Additional fields available in http.Cookie
|
||||||
|
Expires: time.Now().Add(24 * time.Hour),
|
||||||
|
// Partitioned: true, // Available in newer Go versions
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Cookie value: %s \n", cookie)
|
||||||
|
})
|
||||||
|
|
||||||
|
router.Run()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also use the `SetCookieData` method, which accepts a `*http.Cookie` directly for more flexibility:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
router := gin.Default()
|
||||||
|
|
||||||
|
router.GET("/cookie", func(c *gin.Context) {
|
||||||
|
cookie, err := c.Cookie("gin_cookie")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
cookie = "NotSet"
|
||||||
|
// Using http.Cookie struct for more control
|
||||||
|
c.SetCookieData(&http.Cookie{
|
||||||
|
Name: "gin_cookie",
|
||||||
|
Value: "test",
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
MaxAge: 3600,
|
||||||
|
Secure: false,
|
||||||
|
HttpOnly: true,
|
||||||
|
// Additional fields available in http.Cookie
|
||||||
|
Expires: time.Now().Add(24 * time.Hour),
|
||||||
|
// Partitioned: true, // Available in newer Go versions
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Cookie value: %s \n", cookie)
|
fmt.Printf("Cookie value: %s \n", cookie)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user