109 lines
4.8 KiB
Go
109 lines
4.8 KiB
Go
package browser
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"xianyu-go/internal/xianyu/cookierefresh"
|
|
)
|
|
|
|
// TestCredentialCookieSnapshotUsesCurrentFlatCookieAsAuthority 封装TestCredential登录凭证SnapshotUsesCurrentFlat登录凭证AsAuthority业务协调。
|
|
func TestCredentialCookieSnapshotUsesCurrentFlatCookieAsAuthority(t *testing.T) {
|
|
// existing 用于本次流程后续判断的existing
|
|
existing := []cookierefresh.BrowserCookie{
|
|
{Name: "session", Value: "stale", Domain: ".goofish.com", Path: "/", Expires: 123, HTTPOnly: true, Secure: true, SameSite: "Lax"},
|
|
{Name: "removed", Value: "old", Domain: ".goofish.com", Path: "/", Secure: true},
|
|
{Name: "passport", Value: "keep", Domain: ".taobao.com", Path: "/", Secure: true},
|
|
}
|
|
// got 用于本次流程后续判断的got
|
|
got := credentialCookieSnapshot(existing, map[string]string{
|
|
"session": "fresh",
|
|
"new": "value",
|
|
})
|
|
if len(got) != 3 {
|
|
t.Fatalf("snapshot len=%d, want 3: %+v", len(got), got)
|
|
}
|
|
// byName 用于本次流程后续判断的by名称
|
|
byName := make(map[string]cookierefresh.BrowserCookie, len(got))
|
|
// cookie 表示当前遍历过程中的登录凭证
|
|
for _, cookie := range got {
|
|
byName[cookie.Name] = cookie
|
|
}
|
|
// session 用于本次流程后续判断的会话
|
|
session := byName["session"]
|
|
if session.Value != "fresh" || session.Expires != 123 || !session.HTTPOnly || session.SameSite != "Lax" {
|
|
t.Fatalf("existing attributes or fresh value were lost: %+v", session)
|
|
}
|
|
if // exists 用于本次流程后续判断的exists
|
|
_, exists := byName["removed"]; exists {
|
|
t.Fatalf("cookie absent from current flat value was resurrected: %+v", got)
|
|
}
|
|
if // passport 用于本次流程后续判断的passport
|
|
passport := byName["passport"]; passport.Value != "keep" || passport.Domain != ".taobao.com" {
|
|
t.Fatalf("out-of-scope cookie was lost: %+v", passport)
|
|
}
|
|
// added 用于本次流程后续判断的added
|
|
added := byName["new"]
|
|
if added.Value != "value" || added.Domain != goofishDot || added.Path != "/" || !added.Secure {
|
|
t.Fatalf("new cookie defaults mismatch: %+v", added)
|
|
}
|
|
}
|
|
|
|
// TestCredentialCookieSnapshotPreservesAmbiguousSameNameScopes 封装TestCredential登录凭证SnapshotPreservesAmbiguousSame名称Scopes业务协调。
|
|
func TestCredentialCookieSnapshotPreservesAmbiguousSameNameScopes(t *testing.T) {
|
|
// existing 用于本次流程后续判断的existing
|
|
existing := []cookierefresh.BrowserCookie{
|
|
{Name: "token", Value: "old-root", Domain: ".goofish.com", Path: "/"},
|
|
{Name: "token", Value: "old-im", Domain: "www.goofish.com", Path: "/im", HTTPOnly: true},
|
|
}
|
|
// got 用于本次流程后续判断的got
|
|
got := credentialCookieSnapshot(existing, map[string]string{"token": "fresh"})
|
|
if len(got) != 2 {
|
|
t.Fatalf("same-name scoped cookies collapsed: %+v", got)
|
|
}
|
|
// values 用于本次流程后续判断的values
|
|
values := map[string]string{}
|
|
// cookie 表示当前遍历过程中的登录凭证
|
|
for _, cookie := range got {
|
|
values[cookie.Path] = cookie.Value
|
|
}
|
|
if values["/"] != "old-root" || values["/im"] != "old-im" {
|
|
t.Fatalf("ambiguous flat value corrupted scoped cookies: %+v", got)
|
|
}
|
|
}
|
|
|
|
// TestReconcileSnapshotWithCurrentCookie 封装TestReconcileSnapshotWithCurrent登录凭证业务协调。
|
|
func TestReconcileSnapshotWithCurrentCookie(t *testing.T) {
|
|
// snapshot 用于本次流程后续判断的snapshot
|
|
snapshot := []cookierefresh.BrowserCookie{{Name: "session", Value: "old", Domain: ".goofish.com", Path: "/", Expires: 456}}
|
|
|
|
// got 用于本次流程后续判断的got
|
|
got := reconcileSnapshotWithCurrentCookie(snapshot, "session=new", goofishIMURL)
|
|
if len(got) != 1 || got[0].Value != "new" || got[0].Expires != 456 {
|
|
t.Fatalf("flat cookie did not update snapshot without losing attributes: %+v", got)
|
|
}
|
|
|
|
got = reconcileSnapshotWithCurrentCookie(snapshot, "", goofishIMURL)
|
|
if len(got) != 1 || got[0].Value != "old" {
|
|
t.Fatalf("snapshot-only renewal input was discarded: %+v", got)
|
|
}
|
|
|
|
got = reconcileSnapshotWithCurrentCookie([]cookierefresh.BrowserCookie{}, "session=stale", goofishIMURL)
|
|
if got == nil || len(got) != 0 {
|
|
t.Fatalf("authoritative empty snapshot must not fall back to flat cookies: %#v", got)
|
|
}
|
|
if // got 用于本次流程后续判断的got
|
|
got := reconcileSnapshotWithCurrentCookie(nil, "session=legacy", goofishIMURL); got != nil {
|
|
t.Fatalf("missing snapshot should preserve the legacy flat-cookie fallback: %#v", got)
|
|
}
|
|
}
|
|
|
|
// TestSilentHasLoginURLMatcher 封装TestSilentHas登录URLMatcher业务协调。
|
|
func TestSilentHasLoginURLMatcher(t *testing.T) {
|
|
if !isSilentHasLoginURL("https://passport.goofish.com/newlogin/silentHasLogin.do?appName=xianyu") {
|
|
t.Fatal("official silentHasLogin request was not recognized")
|
|
}
|
|
if isSilentHasLoginURL("https://passport.goofish.com/newlogin/hasLogin.do") {
|
|
t.Fatal("ordinary hasLogin request must not be treated as silentHasLogin")
|
|
}
|
|
}
|