diff --git a/context.go b/context.go index dc9d99c5..45886a4d 100644 --- a/context.go +++ b/context.go @@ -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 = "/" } diff --git a/context_test.go b/context_test.go index 6f8f9021..75ca9a6c 100644 --- a/context_test.go +++ b/context_test.go @@ -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=/") diff --git a/docs/doc.md b/docs/doc.md index d05d3a87..0e882a1b 100644 --- a/docs/doc.md +++ b/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: "/",