-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser_test.go
116 lines (105 loc) · 3.09 KB
/
parser_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
package main
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
var (
msgsGood = []string{
`10.84.2.10|http|static.site.com|GET|HTTP/1.1|/banners/etc/sirtaki/july_2018/1/fonts/Candara-Bold.ttf|200|0.142|123456|229673`,
`10.84.2.10|https|static.site.com|GET|HTTP/1.1|/api/foo?a=b|200|0.142|123456|229673`,
`10.84.2.10|https|static.site.com|GET|HTTP/1.1|/api/bar?a=b|200|0.142|123456|229673`,
`10.84.2.10|https|static.site.com|GET|HTTP/1.1|/api/bar?a=b|200|0.142|123456|229673`,
}
msgsGoodParsed = []*logEntry{
{
server: "1.1.1.1",
scheme: "http",
method: "GET",
hostname: "static.site.com",
status: "200",
protocol: "HTTP/1.1",
clientIP: net.ParseIP("10.84.2.10"),
uri: `/banners/etc/sirtaki/july_2018/1/fonts/Candara-Bold.ttf`,
duration: 0.142,
bytesSent: 229673,
bytesRcvd: 123456,
clientCountry: "Unknown",
}, {
server: "1.1.1.1",
scheme: "https",
method: "GET",
hostname: "static.site.com",
status: "200",
protocol: "HTTP/1.1",
clientIP: net.ParseIP("10.84.2.10"),
uri: `/api/foo`,
duration: 0.142,
bytesSent: 229673,
bytesRcvd: 123456,
clientCountry: "Unknown",
}, {
server: "1.1.1.1",
scheme: "https",
method: "GET",
hostname: "static.site.com",
status: "200",
protocol: "HTTP/1.1",
clientIP: net.ParseIP("10.84.2.10"),
uri: `/api/bar`,
duration: 0.142,
bytesSent: 229673,
bytesRcvd: 123456,
clientCountry: "Unknown",
}, {
server: "1.1.1.1",
scheme: "https",
method: "GET",
hostname: "static.site.com",
status: "200",
protocol: "HTTP/1.1",
clientIP: net.ParseIP("10.84.2.10"),
uri: `/api/bar`,
duration: 0.142,
bytesSent: 229673,
bytesRcvd: 123456,
clientCountry: "Unknown",
},
}
msgsBad = []string{
`10.84.2.10|https|static.site.com|GET|HTTP/1.1|/api/bar?a=b|200|0.142a|123456|229673|someshit`,
`10.84.2.10|https|static.site.com|GET|HTTP/1.1|/api/bar?a=b|200|0.142a|123456|229673`,
`10.84.2.10|https|static.site.com|GET|HTTP/1.1|/api/bar?a=b|200|0.142|123456a|229673`,
`10.84.2.10|https|static.site.com|GET|HTTP/1.1|/api/bar?a=b|200|0.142|123456|229673a`,
`10.84.2.10a|https|static.site.com|GET|HTTP/1.1|/api/bar?a=b|200|0.142|123456|229673`,
}
)
func Test_parseSyslogMessage(t *testing.T) {
for k, v := range msgsGood {
m := format.LogParts{
"hostname": "1.1.1.1",
"content": v,
}
l, err := parseSyslogMessage(m)
assert.Nil(t, err)
assert.Equal(t, msgsGoodParsed[k], l)
}
for _, v := range msgsBad {
m := format.LogParts{
"hostname": "1.1.1.1",
"content": v,
}
_, err := parseSyslogMessage(m)
assert.NotNil(t, err)
}
}
func Benchmark_parseSyslogMessage(b *testing.B) {
msg1 := format.LogParts{
"hostname": "1.1.1.1",
"content": msgsGood[0],
}
for i := 0; i < b.N; i++ {
parseSyslogMessage(msg1)
}
}