| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package main
- import (
- "net/http"
- "testing"
- )
- func TestBiliCookieValue(t *testing.T) {
- cookie := "SESSDATA=abc123; bili_jct=def456; buvid3=xyz"
- cases := []struct {
- key string
- want string
- }{
- {"SESSDATA", "abc123"},
- {"bili_jct", "def456"},
- {"buvid3", "xyz"},
- {"missing", ""},
- }
- for _, c := range cases {
- got := biliCookieValue(cookie, c.key)
- if got != c.want {
- t.Errorf("biliCookieValue(%q, %q) = %q, want %q", cookie, c.key, got, c.want)
- }
- }
- }
- func TestMergeCookie(t *testing.T) {
- oldCookie := "SESSDATA=old; bili_jct=oldjct; buvid3=buvid"
- newCookies := []*http.Cookie{
- {Name: "SESSDATA", Value: "new"},
- {Name: "bili_jct", Value: "newjct"},
- }
- merged := mergeCookie(oldCookie, newCookies)
- // 被覆盖的键应为新值
- if got := biliCookieValue(merged, "SESSDATA"); got != "new" {
- t.Errorf("merged SESSDATA = %q, want new", got)
- }
- if got := biliCookieValue(merged, "bili_jct"); got != "newjct" {
- t.Errorf("merged bili_jct = %q, want newjct", got)
- }
- // 未覆盖的键应保留
- if got := biliCookieValue(merged, "buvid3"); got != "buvid" {
- t.Errorf("merged buvid3 = %q, want buvid (preserved)", got)
- }
- // 空新 cookie 返回原串
- if merged := mergeCookie(oldCookie, nil); merged != oldCookie {
- t.Errorf("mergeCookie(nil) = %q, want oldCookie", merged)
- }
- }
|