-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathfilteruseragent_test.go
104 lines (89 loc) · 2.24 KB
/
filteruseragent_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
package filteruseragent
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
)
var (
fileName = "regexes.yaml"
fileData = []byte(strings.TrimSpace(`
user_agent_parsers:
# Chrome/Chromium/major_version.minor_version
- regex: '(Chromium|Chrome)/(\d+)\.(\d+)(?:\.(\d+)|)(?:\.(\d+)|)'
os_parsers:
- regex: '(Windows NT 6\.1)'
os_replacement: 'Windows'
os_v1_replacement: '7'
`))
)
func init() {
goglog.Logger.SetLevel(logrus.DebugLevel)
config.RegistFilterHandler(ModuleName, InitHandler)
err := os.WriteFile(fileName, fileData, 0o644)
if err != nil {
panic(err)
}
}
func Test_filter_useragent_module_default(t *testing.T) {
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
filter:
- type: useragent
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
}
func Test_filter_useragent_module_parse(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
filter:
- type: useragent
source: agent
target: user_agent
regexes: "./regexes.yaml"
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
uagent := "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
expectedEvent := logevent.LogEvent{
Extra: map[string]any{
"agent": uagent,
"user_agent": map[string]any{
"device": "Other",
"major": "71",
"minor": "0",
"name": "Chrome",
"os": "Windows",
"os_name": "Windows",
"os_major": "7",
"patch": "3578",
},
},
}
conf.TestInputEvent(logevent.LogEvent{
Extra: map[string]any{
"agent": uagent,
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
err = os.Remove(fileName)
require.NoError(err)
}