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.
|
// 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.
|
// 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 == "" {
|
if cookie.Path == "" {
|
||||||
cookie.Path = "/"
|
cookie.Path = "/"
|
||||||
}
|
}
|
||||||
|
@ -3124,7 +3124,7 @@ func TestContextNext(t *testing.T) {
|
|||||||
assert.Equal(t, "value3", value)
|
assert.Equal(t, "value3", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextSetCookieStruct(t *testing.T) {
|
func TestContextSetCookieData(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
c.SetSameSite(http.SameSiteLaxMode)
|
c.SetSameSite(http.SameSiteLaxMode)
|
||||||
var setCookie string
|
var setCookie string
|
||||||
@ -3139,7 +3139,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
|||||||
Secure: true,
|
Secure: true,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
}
|
}
|
||||||
c.SetCookieStruct(cookie)
|
c.SetCookieData(cookie)
|
||||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
assert.Contains(t, setCookie, "user=gin")
|
assert.Contains(t, setCookie, "user=gin")
|
||||||
assert.Contains(t, setCookie, "Path=/")
|
assert.Contains(t, setCookie, "Path=/")
|
||||||
@ -3159,7 +3159,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
|||||||
Secure: true,
|
Secure: true,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
}
|
}
|
||||||
c.SetCookieStruct(cookie)
|
c.SetCookieData(cookie)
|
||||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
assert.Contains(t, setCookie, "user=gin")
|
assert.Contains(t, setCookie, "user=gin")
|
||||||
assert.Contains(t, setCookie, "Path=/")
|
assert.Contains(t, setCookie, "Path=/")
|
||||||
@ -3180,7 +3180,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
|||||||
Secure: true,
|
Secure: true,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
}
|
}
|
||||||
c.SetCookieStruct(cookie)
|
c.SetCookieData(cookie)
|
||||||
|
|
||||||
// Since the Expires value varies by time, partially verify with Contains
|
// Since the Expires value varies by time, partially verify with Contains
|
||||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
@ -3201,7 +3201,7 @@ func TestContextSetCookieStruct(t *testing.T) {
|
|||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
Partitioned: true,
|
Partitioned: true,
|
||||||
}
|
}
|
||||||
c.SetCookieStruct(cookie)
|
c.SetCookieData(cookie)
|
||||||
setCookie = c.Writer.Header().Get("Set-Cookie")
|
setCookie = c.Writer.Header().Get("Set-Cookie")
|
||||||
assert.Contains(t, setCookie, "user=gin")
|
assert.Contains(t, setCookie, "user=gin")
|
||||||
assert.Contains(t, setCookie, "Path=/")
|
assert.Contains(t, setCookie, "Path=/")
|
||||||
|
19
docs/doc.md
19
docs/doc.md
@ -2304,12 +2304,23 @@ 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)
|
fmt.Printf("Cookie value: %s \n", cookie)
|
||||||
@ -2319,7 +2330,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
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
|
```go
|
||||||
import (
|
import (
|
||||||
@ -2339,7 +2350,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
cookie = "NotSet"
|
cookie = "NotSet"
|
||||||
// Using http.Cookie struct for more control
|
// Using http.Cookie struct for more control
|
||||||
c.SetCookieStruct(&http.Cookie{
|
c.SetCookieData(&http.Cookie{
|
||||||
Name: "gin_cookie",
|
Name: "gin_cookie",
|
||||||
Value: "test",
|
Value: "test",
|
||||||
Path: "/",
|
Path: "/",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user