mirror of
https://github.com/gin-gonic/gin.git
synced 2025-05-24 22:09:46 +08:00
feat(context): fix SetCookieStruct→SetCookieData (gin-gonic#4215)
This commit is contained in:
parent
cc76014ea3
commit
0bee089a02
@ -1027,10 +1027,10 @@ func (c *Context) SetCookie(name, value string, maxAge int, path, domain string,
|
||||
})
|
||||
}
|
||||
|
||||
// SetCookieStruct adds a Set-Cookie header to the ResponseWriter's headers.
|
||||
// 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) SetCookieStruct(cookie *http.Cookie) {
|
||||
func (c *Context) SetCookieData(cookie *http.Cookie) {
|
||||
if cookie.Path == "" {
|
||||
cookie.Path = "/"
|
||||
}
|
||||
|
@ -3124,7 +3124,7 @@ func TestContextNext(t *testing.T) {
|
||||
assert.Equal(t, "value3", value)
|
||||
}
|
||||
|
||||
func TestContextSetCookieStruct(t *testing.T) {
|
||||
func TestContextSetCookieData(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.SetSameSite(http.SameSiteLaxMode)
|
||||
var setCookie string
|
||||
@ -3139,7 +3139,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
}
|
||||
c.SetCookieStruct(cookie)
|
||||
c.SetCookieData(cookie)
|
||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "user=gin")
|
||||
assert.Contains(t, setCookie, "Path=/")
|
||||
@ -3159,7 +3159,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
}
|
||||
c.SetCookieStruct(cookie)
|
||||
c.SetCookieData(cookie)
|
||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "user=gin")
|
||||
assert.Contains(t, setCookie, "Path=/")
|
||||
@ -3180,7 +3180,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
}
|
||||
c.SetCookieStruct(cookie)
|
||||
c.SetCookieData(cookie)
|
||||
|
||||
// Since the Expires value varies by time, partially verify with Contains
|
||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||
@ -3201,7 +3201,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
||||
HttpOnly: true,
|
||||
Partitioned: true,
|
||||
}
|
||||
c.SetCookieStruct(cookie)
|
||||
c.SetCookieData(cookie)
|
||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "user=gin")
|
||||
assert.Contains(t, setCookie, "Path=/")
|
||||
|
29
docs/doc.md
29
docs/doc.md
@ -2304,22 +2304,33 @@ func main() {
|
||||
router := gin.Default()
|
||||
|
||||
router.GET("/cookie", func(c *gin.Context) {
|
||||
cookie, err := c.Cookie("gin_cookie")
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
cookie = "NotSet"
|
||||
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
|
||||
}
|
||||
|
||||
fmt.Printf("Cookie value: %s \n", cookie)
|
||||
fmt.Printf("Cookie value: %s \n", cookie)
|
||||
})
|
||||
|
||||
router.Run()
|
||||
}
|
||||
```
|
||||
|
||||
You can also use the `SetCookieStruct` method, which accepts a `*http.Cookie` directly for more flexibility:
|
||||
You can also use the `SetCookieData` method, which accepts a `*http.Cookie` directly for more flexibility:
|
||||
|
||||
```go
|
||||
import (
|
||||
@ -2339,7 +2350,7 @@ func main() {
|
||||
if err != nil {
|
||||
cookie = "NotSet"
|
||||
// Using http.Cookie struct for more control
|
||||
c.SetCookieStruct(&http.Cookie{
|
||||
c.SetCookieData(&http.Cookie{
|
||||
Name: "gin_cookie",
|
||||
Value: "test",
|
||||
Path: "/",
|
||||
|
Loading…
x
Reference in New Issue
Block a user