-
Notifications
You must be signed in to change notification settings - Fork 88
/
parser_test.go
102 lines (91 loc) · 3.22 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
package gonx
import (
. "github.com/smartystreets/goconvey/convey"
"strings"
"testing"
)
func TestParser(t *testing.T) {
Convey("Test Parser", t, func() {
Convey("Parse custom format", func() {
format := "$remote_addr [$time_local] \"$request\" $status"
parser := NewParser(format)
Convey("Ensure parser format is ok", func() {
So(parser.Format, ShouldEqual, format)
})
Convey("Test format to regexp translation", func() {
So(parser.Regexp.String(), ShouldEqual,
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<request>[^"]*)" (?P<status>[^ ]*)`)
})
Convey("ParseString", func() {
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1" 200`
expected := NewEntry(Fields{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "GET /api/foo/bar HTTP/1.1",
"status": "200",
})
entry, err := parser.ParseString(line)
So(err, ShouldBeNil)
So(entry, ShouldResemble, expected)
})
Convey("Handle empty values", func() {
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] "" 200`
expected := NewEntry(Fields{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "",
"status": "200",
})
entry, err := parser.ParseString(line)
So(err, ShouldBeNil)
So(entry, ShouldResemble, expected)
})
Convey("Parse invalid string", func() {
line := `GET /api/foo/bar HTTP/1.1`
_, err := parser.ParseString(line)
So(err, ShouldNotBeNil)
})
})
Convey("Test multiple fields concatenated toggle", func() {
format := `$remote_addr [$time_local] "$host$request_uri" $status`
parser := NewParser(format)
Convey("Ensure two fields concatenated toggle regexp ok", func() {
So(parser.Format, ShouldEqual, format)
So(
parser.Regexp.String(),
ShouldEqual,
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<host>[^"]*)(?P<request_uri>[^"]*)" (?P<status>[^ ]*)`,
)
})
format = `$remote_addr [$time_local] "$host$request_uri$demo" $status`
parser = NewParser(format)
Convey("Ensure three fields concatenated toggle regexp ok", func() {
So(parser.Format, ShouldEqual, format)
So(
parser.Regexp.String(),
ShouldEqual,
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<host>[^"]*)(?P<request_uri>[^"]*)(?P<demo>[^"]*)" (?P<status>[^ ]*)`,
)
})
})
Convey("Nginx format parser", func() {
expected := "$remote_addr - $remote_user [$time_local] \"$request\" $status \"$http_referer\" \"$http_user_agent\""
conf := strings.NewReader(`
http {
include conf/mime.types;
log_format minimal '$remote_addr [$time_local] "$request"';
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status '
'"$http_referer" "$http_user_agent"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
}
`)
parser, err := NewNginxParser(conf, "main")
So(err, ShouldBeNil)
So(parser.Format, ShouldEqual, expected)
})
})
}