Files
jiang13-forum/services/friend_link_reciprocal_test.go
freefire 3f50316ad0 fix: 完成 Gitea 目录改组收尾(import、构建与 LICENSE)
同步包路径与路由,去掉 SPA 构建步骤,对齐 Gitea 式 LICENSE,并更新规格/规则与占位 SSR。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-29 04:01:20 +08:00

47 lines
1.2 KiB
Go

package services
import (
"net/url"
"strings"
"testing"
"time"
)
func TestPageContainsLinkToHost(t *testing.T) {
our, err := url.Parse("https://forum.example.com")
if err != nil {
t.Fatal(err)
}
page := "https://friend.example/links.html"
host := "forum.example.com"
if !pageContainsLinkToHost(`<a href="https://forum.example.com/">本站</a>`, page, our, host) {
t.Fatal("应检测到绝对回链")
}
if pageContainsLinkToHost(`<a href="https://other.example/">其他</a>`, page, our, host) {
t.Fatal("不应把外站当成回链")
}
}
func TestPageContainsLinkToHost_LargeHTMLFast(t *testing.T) {
our, err := url.Parse("https://forum.example.com")
if err != nil {
t.Fatal(err)
}
var b strings.Builder
b.Grow(512 * 1024)
for b.Len() < 400*1024 {
b.WriteString(`<a href="https://noise.example/page">x</a>`)
}
b.WriteString(`<a href="https://forum.example.com/">本站</a>`)
html := b.String()
start := time.Now()
if !pageContainsLinkToHost(html, "https://friend.example/", our, "forum.example.com") {
t.Fatal("应在大量无关链接中找到回链")
}
if elapsed := time.Since(start); elapsed > 500*time.Millisecond {
t.Fatalf("解析耗时过长: %s", elapsed)
}
}