forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_agent_test.go
96 lines (85 loc) · 2.39 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter_test
import (
"reflect"
"strings"
"testing"
. "zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/gctest"
"zgo.at/isbot"
"zgo.at/zdb"
"zgo.at/zstd/ztest"
)
func TestUserAgentGetOrInsert(t *testing.T) {
ctx := gctest.DB(t)
test := func(gotUA, wantUA UserAgent, want string) {
t.Helper()
if !reflect.DeepEqual(gotUA, wantUA) {
t.Fatalf("wrong ua\ngot: %#v\nwant: %#v", gotUA, wantUA)
}
want = strings.ReplaceAll(strings.TrimSpace(strings.ReplaceAll(want, "\t", "")), "@", " ")
out := zdb.DumpString(ctx, `select browsers.name || ' ' || browsers.version as browser from browsers;`) +
zdb.DumpString(ctx, `select systems.name || ' ' || systems.version as system from systems;`)
out = strings.ReplaceAll(out, " \n", "\n") // TODO: fix in zdb
if d := ztest.Diff(out, want); d != "" {
t.Error(d)
}
}
{
ua := UserAgent{UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0"}
err := ua.GetOrInsert(ctx)
if err != nil {
t.Fatal(err)
}
test(ua, UserAgent{UserAgent: ua.UserAgent, BrowserID: 1, SystemID: 1, Isbot: isbot.NoBotNoMatch}, `
browser
Firefox 79
system
Linux
`)
}
{
ua := UserAgent{UserAgent: "Mozilla/5.0 (X11; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0"}
err := ua.GetOrInsert(ctx)
if err != nil {
t.Fatal(err)
}
test(ua, UserAgent{UserAgent: ua.UserAgent, BrowserID: 1, SystemID: 1, Isbot: isbot.NoBotNoMatch}, `
browser
Firefox 79
system
Linux
`)
}
{
ua := UserAgent{UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0"}
err := ua.GetOrInsert(ctx)
if err != nil {
t.Fatal(err)
}
test(ua, UserAgent{UserAgent: ua.UserAgent, BrowserID: 1, SystemID: 2, Isbot: isbot.NoBotNoMatch}, `
browser
Firefox 79
system
Linux
Windows 10
`)
}
{
ua := UserAgent{UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0"}
err := ua.GetOrInsert(ctx)
if err != nil {
t.Fatal(err)
}
test(ua, UserAgent{UserAgent: ua.UserAgent, BrowserID: 2, SystemID: 2, Isbot: isbot.NoBotNoMatch}, `
browser
Firefox 79
Firefox 71
system
Linux
Windows 10
`)
}
}