-
Notifications
You must be signed in to change notification settings - Fork 15
/
apache_test.go
111 lines (103 loc) · 2.9 KB
/
apache_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
package axslogparser
import (
"strings"
"testing"
)
var benchInputs []string
func init() {
for _, v := range parseTests {
if strings.HasPrefix(v.Name, "[Apache]") {
benchInputs = append(benchInputs, v.Input)
}
}
}
func BenchmarkApache_Parse(b *testing.B) {
p := &Apache{}
for i := 0; i < b.N; i++ {
for _, v := range benchInputs {
p.Parse(v)
}
}
}
var parseSuccessTests = []struct {
Name string
Input string
}{
{
Name: "Valid request - many remote host ips",
Input: `192.168.0.1, 192.168.0.2, 192.168.0.3 10.10.10.10 - - [01/Jan/2005:15:04:05 +0000] "GET /home HTTP/1.1" 200 4589 "https://www.google.com/home" "UA"`,
},
}
var parseErrorTests = []struct {
Name string
Input string
ContainsString string
HardError bool
}{
{
Name: "broken",
Input: "hoge",
ContainsString: "(not matched)",
HardError: true,
},
{
Name: "invalid request",
Input: `10.0.0.11 - - [11/Jun/2017:05:56:04 +0900] "GET / hoge HTTP/1.1" 200 741 "-" "mackerel-http-checker/0.0.1"`,
ContainsString: "(invalid request)",
},
{
Name: "invalid request",
Input: `10.0.0.11 - - [11/Jun/2017:05:56:04 +0900] "GET / HTTP/1.1" 200`,
ContainsString: "(invalid status or size)",
HardError: true,
},
{
Name: "invalid request",
Input: `10.0.0.11 - - [11/Jun/2017:05:56:04 +0900] "GET / HTTP/1.1" 2xx 741 "-" "mackerel-http-checker/0.0.1"`,
ContainsString: "(invalid status:",
},
}
func TestParse_error(t *testing.T) {
psr := &Apache{}
for _, tt := range parseErrorTests {
t.Logf("testing: %s", tt.Name)
if _, err := psr.Parse(tt.Input); err == nil {
t.Errorf("%s: error should be occured but nil", tt.Name)
} else if !strings.Contains(err.Error(), tt.ContainsString) {
t.Errorf("%s: error should be contained %q, but: %s", tt.Name, tt.ContainsString, err)
}
}
for _, tt := range parseSuccessTests {
t.Logf("testing: %s", tt.Name)
if _, err := psr.Parse(tt.Input); err != nil {
t.Errorf("%s: error should not be occured but: %s", tt.Name, err.Error())
}
}
}
func TestParse_loose(t *testing.T) {
psr := &Apache{Loose: true}
for _, tt := range parseErrorTests {
t.Logf("testing: %s", tt.Name)
if tt.HardError {
if _, err := psr.Parse(tt.Input); err == nil {
if tt.HardError {
t.Errorf("%s: error should be occured but nil", tt.Name)
}
} else if !strings.Contains(err.Error(), tt.ContainsString) {
t.Errorf("%s: error should be contained %q, but: %s", tt.Name, tt.ContainsString, err)
}
} else {
if _, err := psr.Parse(tt.Input); err != nil {
if err != nil {
t.Errorf("%s: error should not be occured but: %s", tt.Name, err.Error())
}
}
}
}
for _, tt := range parseSuccessTests {
t.Logf("testing: %s", tt.Name)
if _, err := psr.Parse(tt.Input); err != nil {
t.Errorf("%s: error should not be occured but: %s", tt.Name, err.Error())
}
}
}