-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathreader_test.go
60 lines (49 loc) · 1.4 KB
/
reader_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
package gonx
import (
"io"
"math/rand"
"strings"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func TestReader(t *testing.T) {
Convey("Test Reader", t, func() {
format := "$remote_addr [$time_local] \"$request\""
Convey("Test valid input", func() {
file := strings.NewReader(`89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET /api/foo/bar HTTP/1.1"`)
reader := NewReader(file, format)
So(reader.entries, ShouldBeNil)
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",
})
// Read entry from incoming channel
entry, err := reader.Read()
So(err, ShouldBeNil)
So(entry, ShouldResemble, expected)
// It was only one line, nothing to read
_, err = reader.Read()
So(err, ShouldEqual, io.EOF)
})
Convey("Test long line", func() {
longStr := RandString(64 * 1024)
file := strings.NewReader(`89.234.89.123 [08/Nov/2013:13:39:18 +0000] "GET ` + longStr + ` HTTP/1.1"`)
reader := NewReader(file, format)
_, err := reader.Read()
So(err, ShouldBeNil)
})
})
}