Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Partitioned Parameter to SetCookie #1048

Merged
merged 3 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,10 @@ func (ctx *RequestContext) Cookie(key string) []byte {
// 4. ctx.SetCookie("user", "", 10, "/", "localhost",protocol.CookieSameSiteLaxMode, false, false)
// add response header ---> Set-Cookie: user=; max-age=10; domain=localhost; path=/; SameSite=Lax;
func (ctx *RequestContext) SetCookie(name, value string, maxAge int, path, domain string, sameSite protocol.CookieSameSite, secure, httpOnly bool) {
ctx.setCookie(name, value, maxAge, path, domain, sameSite, secure, httpOnly, false)
}

func (ctx *RequestContext) setCookie(name, value string, maxAge int, path, domain string, sameSite protocol.CookieSameSite, secure, httpOnly, partitioned bool) {
if path == "" {
path = "/"
}
Expand All @@ -1240,9 +1244,20 @@ func (ctx *RequestContext) SetCookie(name, value string, maxAge int, path, domai
cookie.SetSecure(secure)
cookie.SetHTTPOnly(httpOnly)
cookie.SetSameSite(sameSite)
cookie.SetPartitioned(partitioned)
ctx.Response.Header.SetCookie(cookie)
}

// SetPartitionedCookie adds a partitioned cookie to the Response's headers.
// Use protocol.CookieSameSiteNoneMode for cross-site cookies to work.
//
// Usage: ctx.SetPartitionedCookie("user", "name", 10, "/", "localhost", protocol.CookieSameSiteNoneMode, true, true)
//
// This adds the response header: Set-Cookie: user=name; Max-Age=10; Domain=localhost; Path=/; HttpOnly; Secure; SameSite=None; Partitioned
func (ctx *RequestContext) SetPartitionedCookie(name, value string, maxAge int, path, domain string, sameSite protocol.CookieSameSite, secure, httpOnly bool) {
ctx.setCookie(name, value, maxAge, path, domain, sameSite, secure, httpOnly, true)
}

// UserAgent returns the value of the request user_agent.
func (ctx *RequestContext) UserAgent() []byte {
return ctx.Request.Header.UserAgent()
Expand Down
10 changes: 8 additions & 2 deletions pkg/app/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,8 +1615,14 @@ func TestSetBinder(t *testing.T) {

func TestRequestContext_SetCookie(t *testing.T) {
c := NewContext(0)
c.SetCookie("user", "hertz", 1, "/", "localhost", protocol.CookieSameSiteLaxMode, true, true)
assert.DeepEqual(t, "user=hertz; max-age=1; domain=localhost; path=/; HttpOnly; secure; SameSite=Lax", c.Response.Header.Get("Set-Cookie"))
c.SetCookie("user", "hertz", 1, "/", "localhost", protocol.CookieSameSiteNoneMode, true, true)
assert.DeepEqual(t, "user=hertz; max-age=1; domain=localhost; path=/; HttpOnly; secure; SameSite=None", c.Response.Header.Get("Set-Cookie"))
}

func TestRequestContext_SetPartitionedCookie(t *testing.T) {
c := NewContext(0)
c.SetPartitionedCookie("user", "hertz", 1, "/", "localhost", protocol.CookieSameSiteNoneMode, true, true)
assert.DeepEqual(t, "user=hertz; max-age=1; domain=localhost; path=/; HttpOnly; secure; SameSite=None; Partitioned", c.Response.Header.Get("Set-Cookie"))
}

func TestRequestContext_SetCookiePathEmpty(t *testing.T) {
Expand Down
Loading