成交 GMV 排除已关闭订单,避免未支付关单被计入;点击按访客 5 秒、IP 1 秒双重去重;生产禁止 localhost 落地页,失效短码改用 QueryEscape。 Co-authored-by: Cursor <cursoragent@cursor.com>
132 lines
4.0 KiB
Go
132 lines
4.0 KiB
Go
package adtracking
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestAppendTrackCode(t *testing.T) {
|
|
tests := []struct {
|
|
target string
|
|
code string
|
|
expected string
|
|
}{
|
|
{
|
|
target: "https://hao858.com",
|
|
code: "wb01",
|
|
expected: "https://hao858.com?track_code=wb01",
|
|
},
|
|
{
|
|
target: "https://hao858.com/",
|
|
code: "campus99",
|
|
expected: "https://hao858.com/?track_code=campus99",
|
|
},
|
|
{
|
|
target: "https://hao858.com/listings?game=delta",
|
|
code: "poster01",
|
|
expected: "https://hao858.com/listings?game=delta&track_code=poster01",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := appendTrackCode(tt.target, tt.code)
|
|
if got != tt.expected {
|
|
t.Errorf("appendTrackCode(%q, %q) = %q; want %q", tt.target, tt.code, got, tt.expected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseUserAgent(t *testing.T) {
|
|
tests := []struct {
|
|
ua string
|
|
wantDeviceType string
|
|
wantOS string
|
|
wantBrowser string
|
|
}{
|
|
{
|
|
ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
wantDeviceType: "pc",
|
|
wantOS: "macOS",
|
|
wantBrowser: "Chrome",
|
|
},
|
|
{
|
|
ua: "Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.38",
|
|
wantDeviceType: "mobile",
|
|
wantOS: "iOS",
|
|
wantBrowser: "WeChat",
|
|
},
|
|
{
|
|
ua: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
|
|
wantDeviceType: "pc",
|
|
wantOS: "Windows",
|
|
wantBrowser: "Edge",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
deviceType, _, browser := parseUserAgent(tt.ua)
|
|
if deviceType != tt.wantDeviceType {
|
|
t.Errorf("parseUserAgent(%q) deviceType = %q; want %q", tt.ua, deviceType, tt.wantDeviceType)
|
|
}
|
|
if browser != tt.wantBrowser {
|
|
t.Errorf("parseUserAgent(%q) browser = %q; want %q", tt.ua, browser, tt.wantBrowser)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCodePattern(t *testing.T) {
|
|
validCodes := []string{"wb01", "campus_2026", "poster-hefei", "NetBar99"}
|
|
for _, c := range validCodes {
|
|
if !codePattern.MatchString(c) {
|
|
t.Errorf("expected valid code %q", c)
|
|
}
|
|
}
|
|
|
|
invalidCodes := []string{"a", "has space", "bad@code", "中文短码", "a!b"}
|
|
for _, c := range invalidCodes {
|
|
if codePattern.MatchString(c) {
|
|
t.Errorf("expected invalid code %q", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateTargetURL(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
allowLocal bool
|
|
wantURL string
|
|
wantErr bool
|
|
errExpected error
|
|
}{
|
|
{"", false, "https://hao858.com/", false, nil},
|
|
{"/listings", false, "https://hao858.com/listings", false, nil},
|
|
{"https://hao858.com", false, "https://hao858.com", false, nil},
|
|
{"https://h5.hao858.com/m", false, "https://h5.hao858.com/m", false, nil},
|
|
{"http://localhost:5189", true, "http://localhost:5189", false, nil},
|
|
{"http://127.0.0.1:18088", true, "http://127.0.0.1:18088", false, nil},
|
|
{"http://localhost:5189", false, "", true, ErrDisallowedTargetDomain},
|
|
{"http://127.0.0.1:18088", false, "", true, ErrDisallowedTargetDomain},
|
|
{"https://evil-phishing.com", false, "", true, ErrDisallowedTargetDomain},
|
|
{"https://hao858.com.fake.com", false, "", true, ErrDisallowedTargetDomain},
|
|
{"https://sub.evil.com/hao858.com", false, "", true, ErrDisallowedTargetDomain},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got, err := validateTargetURL(tt.input, tt.allowLocal)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("validateTargetURL(%q, %v) expected error, got nil", tt.input, tt.allowLocal)
|
|
}
|
|
if tt.errExpected != nil && err != tt.errExpected {
|
|
t.Errorf("validateTargetURL(%q, %v) err = %v; want %v", tt.input, tt.allowLocal, err, tt.errExpected)
|
|
}
|
|
} else {
|
|
if err != nil {
|
|
t.Errorf("validateTargetURL(%q, %v) unexpected error: %v", tt.input, tt.allowLocal, err)
|
|
}
|
|
if got != tt.wantURL {
|
|
t.Errorf("validateTargetURL(%q, %v) = %q; want %q", tt.input, tt.allowLocal, got, tt.wantURL)
|
|
}
|
|
}
|
|
}
|
|
}
|