@@ -0,0 +1,53 @@
|
||||
package useragent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildXUserAgent_iPhone(t *testing.T) {
|
||||
ua := "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1"
|
||||
got := BuildXUserAgent(BuildOptions{RawUA: ua, Ver: "1.0.0", SID: "abc"})
|
||||
wantParts := []string{
|
||||
"DevType=Apple iPhone mobile",
|
||||
"SysType=h5_ios",
|
||||
"Ver=1.0.0",
|
||||
"BuildID=Safari 17.2",
|
||||
"device_brand=Apple",
|
||||
"device_model=iPhone",
|
||||
"system_name=iOS",
|
||||
"system_version=17.2",
|
||||
"sid=abc",
|
||||
}
|
||||
for _, p := range wantParts {
|
||||
if !strings.Contains(got, p) {
|
||||
t.Fatalf("missing %q in %s", p, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildXUserAgent_Android(t *testing.T) {
|
||||
ua := "Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"
|
||||
got := BuildXUserAgent(BuildOptions{RawUA: ua, Ver: "1.0.0", SID: "s1"})
|
||||
wantParts := []string{
|
||||
"SysType=h5_android",
|
||||
"device_brand=Google",
|
||||
"device_model=Pixel 7",
|
||||
"system_name=Android",
|
||||
"system_version=13",
|
||||
"BuildID=Chrome 120.0.0.0",
|
||||
"sid=s1",
|
||||
}
|
||||
for _, p := range wantParts {
|
||||
if !strings.Contains(got, p) {
|
||||
t.Fatalf("missing %q in %s", p, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildXUserAgent_EmptyFallsBackDevType(t *testing.T) {
|
||||
got := BuildXUserAgent(BuildOptions{RawUA: "", Ver: "1.0.0"})
|
||||
if !strings.Contains(got, "DevType=Apple iPhone mobile") || !strings.Contains(got, "SysType=h5_pc") {
|
||||
t.Fatalf("unexpected fallback: %s", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user