bilifeed_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "net/http"
  4. "testing"
  5. )
  6. func TestBiliCookieValue(t *testing.T) {
  7. cookie := "SESSDATA=abc123; bili_jct=def456; buvid3=xyz"
  8. cases := []struct {
  9. key string
  10. want string
  11. }{
  12. {"SESSDATA", "abc123"},
  13. {"bili_jct", "def456"},
  14. {"buvid3", "xyz"},
  15. {"missing", ""},
  16. }
  17. for _, c := range cases {
  18. got := biliCookieValue(cookie, c.key)
  19. if got != c.want {
  20. t.Errorf("biliCookieValue(%q, %q) = %q, want %q", cookie, c.key, got, c.want)
  21. }
  22. }
  23. }
  24. func TestMergeCookie(t *testing.T) {
  25. oldCookie := "SESSDATA=old; bili_jct=oldjct; buvid3=buvid"
  26. newCookies := []*http.Cookie{
  27. {Name: "SESSDATA", Value: "new"},
  28. {Name: "bili_jct", Value: "newjct"},
  29. }
  30. merged := mergeCookie(oldCookie, newCookies)
  31. // 被覆盖的键应为新值
  32. if got := biliCookieValue(merged, "SESSDATA"); got != "new" {
  33. t.Errorf("merged SESSDATA = %q, want new", got)
  34. }
  35. if got := biliCookieValue(merged, "bili_jct"); got != "newjct" {
  36. t.Errorf("merged bili_jct = %q, want newjct", got)
  37. }
  38. // 未覆盖的键应保留
  39. if got := biliCookieValue(merged, "buvid3"); got != "buvid" {
  40. t.Errorf("merged buvid3 = %q, want buvid (preserved)", got)
  41. }
  42. // 空新 cookie 返回原串
  43. if merged := mergeCookie(oldCookie, nil); merged != oldCookie {
  44. t.Errorf("mergeCookie(nil) = %q, want oldCookie", merged)
  45. }
  46. }