Files
Ydisks-Xianyu-Helper/internal/browser/cookies_test.go
T
ChristandClaude Opus 4.8 980de41b53 chore: 重构配套改动 + 包文档 + 构建产物清理
automation 重构期间的配套修改:
- browser:订单抓取/密码登录/二维码刷新/stealth 等适配新流程,删 search.go
- db:cookies/orders_ext/extras/sessions/users/password/repository 等调整
- xianyu:mtop client、protocol(sign/decrypt/msgpack/cookies)、qrlogin、ws、ua
- server:admin/analytics/auth/cookie/helpers/item/item_publish_batch handlers
- frontend:App/Dashboard/ItemList/Rules/Sidebar/api/types,删 Keywords/TestPage
- auth、notify、webui、go.mod 等

本次会话补充:
- cmd/server、cmd/init-admin 改为 // Package main 开头的标准包文档
- .gitignore 加 internal/webui/static/assets/(hash 化构建产物不入库)
- internal/webui/static/index.html 重新构建

全量 go build/vet/test + race + 前端 test/build 通过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 21:41:03 +08:00

83 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package browser
import (
"strings"
"testing"
)
func TestParseCookieStrRoundTrip(t *testing.T) {
in := "unb=999; _m_h5_tk=abc_1; cookie2=xyz"
m := parseCookieStr(in)
if m["unb"] != "999" || m["_m_h5_tk"] != "abc_1" || m["cookie2"] != "xyz" {
t.Fatalf("解析异常: %+v", m)
}
out := cookieMarshal(m)
// 顺序不保证,逐项检查。
for _, kv := range []string{"unb=999", "_m_h5_tk=abc_1", "cookie2=xyz"} {
if !strings.Contains(out, kv) {
t.Fatalf("marshal 缺少 %q: %q", kv, out)
}
}
}
func TestParseCookieStrToPlaywright(t *testing.T) {
cookies := parseCookieStrToPlaywright("a=1; b=2")
if len(cookies) != 2 {
t.Fatalf("应解析 2 个 cookie,got %d", len(cookies))
}
for _, c := range cookies {
if c.Domain == nil || *c.Domain != goofishDot {
t.Fatalf("domain 应为 %s: %+v", goofishDot, c.Domain)
}
if c.Path == nil || *c.Path != "/" {
t.Fatalf("path 应为 /: %+v", c.Path)
}
}
}
func TestParseCookieStrEmpty(t *testing.T) {
if got := parseCookieStr(""); len(got) != 0 {
t.Fatalf("空串应返回空 map, got %+v", got)
}
if got := parseCookieStrToPlaywright(",,, ;"); len(got) != 0 {
t.Fatalf("无效串应返回空, got %+v", got)
}
}
func TestCookiesToMapAndStr(t *testing.T) {
// 用 cookiesToMap/cookiesToStr 覆盖(构造 Cookie 不导出字段,借 parse 间接)。
m := map[string]string{"unb": "1", "cna": "xx"}
s := cookieMarshal(m)
m2 := parseCookieStr(s)
if m2["unb"] != "1" || m2["cna"] != "xx" {
t.Fatalf("往返异常: %+v", m2)
}
}
func TestStealthScriptReplaced(t *testing.T) {
s := stealthScript()
if strings.Contains(s, "{{") {
t.Fatalf("stealth 脚本仍有未替换占位符: %q", s[:min(200, len(s))])
}
for _, key := range []string{"webdriver", "chrome", "RTCPeerConnection", "toDataURL"} {
if !strings.Contains(s, key) {
t.Fatalf("stealth 脚本缺少关键段 %q", key)
}
}
}
func TestStealthScriptUnique(t *testing.T) {
// 多次生成应有时不同(随机参数)。
a := stealthScript()
differ := false
for i := 0; i < 10; i++ {
if stealthScript() != a {
differ = true
break
}
}
if !differ {
t.Fatal("stealth 脚本多次生成应出现随机差异")
}
}