forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_agent_test.go
134 lines (119 loc) · 4.25 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright © 2019 Martin Tournoij <[email protected]>
// This file is part of GoatCounter and published under the terms of the EUPL
// v1.2, which can be found in the LICENSE file or at http://eupl12.zgo.at
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) {
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
user_agents.user_agent_id as id,
user_agents.system_id as bid,
user_agents.browser_id as sid,
user_agents.isbot,
browsers.name || ' ' || browsers.version as browser,
systems.name || ' ' || systems.version as system,
user_agents.ua
from user_agents
join browsers using (browser_id)
join systems using (system_id)`)
if d := ztest.Diff(out, want); d != "" {
t.Errorf(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, ID: 1, BrowserID: 1, SystemID: 1, Isbot: isbot.NoBotNoMatch}, `
id bid sid isbot browser system ua
1 1 1 1 Firefox 79 Linux ~Z (X11; ~L x86_64; rv:79.0) ~g20100101 ~f79.0
`)
}
{
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, ID: 1, BrowserID: 1, SystemID: 1, Isbot: isbot.NoBotNoMatch}, `
id bid sid isbot browser system ua
1 1 1 1 Firefox 79 Linux ~Z (X11; ~L x86_64; rv:79.0) ~g20100101 ~f79.0
`)
}
{
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, ID: 2, BrowserID: 1, SystemID: 2, Isbot: isbot.NoBotNoMatch}, `
id bid sid isbot browser system ua
1 1 1 1 Firefox 79 Linux ~Z (X11; ~L x86_64; rv:79.0) ~g20100101 ~f79.0
2 2 1 1 Firefox 79 Windows 10 ~Z (~W NT 10.0; Win64; x64; rv:79.0) ~g20100101 ~f79.0
`)
}
{
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, ID: 3, BrowserID: 2, SystemID: 2, Isbot: isbot.NoBotNoMatch}, `
id bid sid isbot browser system ua
1 1 1 1 Firefox 79 Linux ~Z (X11; ~L x86_64; rv:79.0) ~g20100101 ~f79.0
2 2 1 1 Firefox 79 Windows 10 ~Z (~W NT 10.0; Win64; x64; rv:79.0) ~g20100101 ~f79.0
3 2 2 1 Firefox 71 Windows 10 ~Z (~W NT 10.0; Win64; x64; rv:71.0) ~g20100101 ~f71.0
`)
}
}
func TestUserAgentUpdate(t *testing.T) {
ctx := gctest.DB(t)
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)
}
oldB := ua.BrowserID
oldS := ua.SystemID
{
err = ua.Update(ctx)
if err != nil {
t.Fatal(err)
}
if ua.BrowserID != oldB || ua.SystemID != oldS {
t.Errorf("browser %d == %d; system %d == %d", oldB, ua.BrowserID, oldS, ua.SystemID)
}
}
{
ua.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0"
err = zdb.Exec(ctx, `update user_agents set ua=? where user_agent_id=?`,
ua.UserAgent, ua.ID)
if err != nil {
t.Fatal(err)
}
err = ua.Update(ctx)
if err != nil {
t.Fatal(err)
}
if ua.BrowserID == oldB || ua.SystemID == oldS {
t.Errorf("browser %d == %d; system %d == %d", oldB, ua.BrowserID, oldS, ua.SystemID)
}
}
}