Support domain configuration for sticky cookies

This commit is contained in:
Jorge 2025-03-06 09:38:04 +01:00 committed by GitHub
parent fa76ed57d3
commit 740b4cfd25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 184 additions and 0 deletions

View file

@ -30,6 +30,7 @@ type stickyCookie struct {
sameSite string
maxAge int
path string
domain string
}
func convertSameSite(sameSite string) http.SameSite {
@ -87,6 +88,7 @@ func New(sticky *dynamic.Sticky, wantHealthCheck bool) *Balancer {
sameSite: sticky.Cookie.SameSite,
maxAge: sticky.Cookie.MaxAge,
path: "/",
domain: sticky.Cookie.Domain,
}
if sticky.Cookie.Path != nil {
balancer.stickyCookie.path = *sticky.Cookie.Path
@ -281,6 +283,7 @@ func (b *Balancer) writeStickyCookie(w http.ResponseWriter, handler *namedHandle
Secure: b.stickyCookie.secure,
SameSite: convertSameSite(b.stickyCookie.sameSite),
MaxAge: b.stickyCookie.maxAge,
Domain: b.stickyCookie.domain,
}
http.SetCookie(w, cookie)
}

View file

@ -239,6 +239,7 @@ func TestSticky(t *testing.T) {
Secure: true,
HTTPOnly: true,
SameSite: "none",
Domain: "foo.com",
MaxAge: 42,
Path: func(v string) *string { return &v }("/foo"),
},
@ -276,6 +277,7 @@ func TestSticky(t *testing.T) {
assert.Equal(t, 3, recorder.save["second"])
assert.True(t, recorder.cookies["test"].HttpOnly)
assert.True(t, recorder.cookies["test"].Secure)
assert.Equal(t, "foo.com", recorder.cookies["test"].Domain)
assert.Equal(t, http.SameSiteNoneMode, recorder.cookies["test"].SameSite)
assert.Equal(t, 42, recorder.cookies["test"].MaxAge)
assert.Equal(t, "/foo", recorder.cookies["test"].Path)