-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_agent_test.go
39 lines (32 loc) · 1.29 KB
/
user_agent_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
_ "embed"
"fmt"
"testing"
)
func TestUserAgentParser1(t *testing.T) {
uaStrings := []string{
"Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-A500FU Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/3.3 Chrome/38.0.2125.102 Mobile Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
"Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/30.0.1599.12 Mobile/11A465 Safari/8536.25 (3B92C18B-D9DE-4CB7-A02A-22FD2AF17C8F)",
"curl/7.68.0",
"AdsBot-Google (+http://www.google.com/adsbot.html)",
"Wget/1.20.3 (linux-gnu)",
"<script>alert(1);</script>Wget/1.20.3 (linux-gnu)",
}
userAgentParser := newUserAgentParser()
for _, uaString := range uaStrings {
parsed := userAgentParser.parse(uaString)
fmt.Printf("%v\n", parsed)
if parsed.UaBrowserName == "" {
t.Errorf("invalid parsed data")
}
}
}
func BenchmarkNewUserAgentParser(b *testing.B) {
uaString := "Mozilla/5.0 (Linux; Android 5.0.2; SAMSUNG SM-A500FU Build/LRX22G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/3.3 Chrome/38.0.2125.102 Mobile Safari/537.36"
userAgentParser := newUserAgentParser()
for n := 0; n < b.N; n++ {
userAgentParser.parse(uaString)
}
}