fix: harden cluster key rate limit cookie parsing (#4012)

Signed-off-by: EndlessSeeker <1766508902@qq.com>
This commit is contained in:
EndlessSeeker
2026-06-24 15:03:05 +08:00
committed by GitHub
parent 10498a2c86
commit 04389fd0c6
2 changed files with 41 additions and 2 deletions

View File

@@ -0,0 +1,39 @@
package util
import "testing"
func TestExtractCookieValueByKey(t *testing.T) {
tests := []struct {
name string
cookie string
key string
want string
}{
{
name: "extracts matching cookie value",
cookie: "user=alice; other=value",
key: "user",
want: "alice",
},
{
name: "skips segment without equals sign",
cookie: "user; other=value",
key: "user",
want: "",
},
{
name: "keeps equals signs in cookie value",
cookie: "user=alice=admin; other=value",
key: "user",
want: "alice=admin",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ExtractCookieValueByKey(tt.cookie, tt.key); got != tt.want {
t.Fatalf("ExtractCookieValueByKey() = %q, want %q", got, tt.want)
}
})
}
}