Configurable path for sticky cookies
This commit is contained in:
parent
552bd8f180
commit
ec00c4aa42
28 changed files with 530 additions and 26 deletions
|
@ -26,6 +26,7 @@ type stickyCookie struct {
|
|||
httpOnly bool
|
||||
sameSite string
|
||||
maxAge int
|
||||
path string
|
||||
}
|
||||
|
||||
func convertSameSite(sameSite string) http.SameSite {
|
||||
|
@ -79,6 +80,10 @@ func New(sticky *dynamic.Sticky, wantHealthCheck bool) *Balancer {
|
|||
httpOnly: sticky.Cookie.HTTPOnly,
|
||||
sameSite: sticky.Cookie.SameSite,
|
||||
maxAge: sticky.Cookie.MaxAge,
|
||||
path: "/",
|
||||
}
|
||||
if sticky.Cookie.Path != nil {
|
||||
balancer.stickyCookie.path = *sticky.Cookie.Path
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -236,7 +241,7 @@ func (b *Balancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||
cookie := &http.Cookie{
|
||||
Name: b.stickyCookie.name,
|
||||
Value: hash(server.name),
|
||||
Path: "/",
|
||||
Path: b.stickyCookie.path,
|
||||
HttpOnly: b.stickyCookie.httpOnly,
|
||||
Secure: b.stickyCookie.secure,
|
||||
SameSite: convertSameSite(b.stickyCookie.sameSite),
|
||||
|
|
|
@ -226,6 +226,7 @@ func TestSticky(t *testing.T) {
|
|||
HTTPOnly: true,
|
||||
SameSite: "none",
|
||||
MaxAge: 42,
|
||||
Path: func(v string) *string { return &v }("/foo"),
|
||||
},
|
||||
}, false)
|
||||
|
||||
|
@ -263,6 +264,7 @@ func TestSticky(t *testing.T) {
|
|||
assert.True(t, recorder.cookies["test"].Secure)
|
||||
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)
|
||||
}
|
||||
|
||||
func TestSticky_FallBack(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue