forked from vjeantet/grok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grok_test.go
539 lines (470 loc) · 17.6 KB
/
grok_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package grok
import "testing"
func TestNew(t *testing.T) {
g := New()
if len(g.Patterns()) == 0 {
t.Fatal("the Grok object should have some patterns pre loaded")
}
g = NewWithConfig(&Config{NamedCapturesOnly: true})
if len(g.Patterns()) == 0 {
t.Fatal("the Grok object should have some patterns pre loaded")
}
}
func TestParseWithDefaultCaptureMode(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != "" {
t.Fatalf("%s should be '%s' have '%s'", "TIME", "", captures["TIME"])
}
}
g = New()
if captures, err := g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != "22:58:32" {
t.Fatalf("%s should be '%s' have '%s'", "TIME", "22:58:32", captures["TIME"])
}
}
}
func TestMultiParseWithDefaultCaptureMode(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
res, _ := g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:23:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["TIME"]) != 0 {
t.Fatalf("DAY should be an array of 0 elements, but is '%s'", res["TIME"])
}
g = New()
res, _ = g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:23:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["TIME"]) != 2 {
t.Fatalf("TIME should be an array of 2 elements, but is '%s'", res["TIME"])
}
if len(res["timestamp"]) != 2 {
t.Fatalf("timestamp should be an array of 2 elements, but is '%s'", res["timestamp"])
}
}
func TestNewWithNoDefaultPatterns(t *testing.T) {
g := NewWithConfig(&Config{SkipDefaultPatterns: true})
if len(g.Patterns()) != 0 {
t.Fatal("Using SkipDefaultPatterns the Grok object should not have any patterns pre loaded")
}
}
func TestAddPatternsFromPath(t *testing.T) {
g := New()
err := g.AddPatternsFromPath("./Lorem ipsum Minim qui in.")
if err == nil {
t.Fatalf("AddPatternsFromPath should returns an error when path is invalid")
}
}
func TestAddPatternsFromPathFileOpenErr(t *testing.T) {
t.Skipped()
}
func TestAddPatternsFromPathFile(t *testing.T) {
g := New()
err := g.AddPatternsFromPath("./patterns/base")
if err != nil {
t.Fatalf("err %#v", err)
}
}
func TestAddPatternErr(t *testing.T) {
name := "Error"
pattern := "%{ERR}"
g := New()
err := g.AddPattern(name, pattern)
if err == nil {
t.Fatalf("AddPattern should returns an error when path is invalid")
}
}
func TestAddPattern(t *testing.T) {
name := "DAYO"
pattern := "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)"
g := New()
cPatterns := len(g.patterns)
g.AddPattern(name, pattern)
g.AddPattern(name+"2", pattern)
if len(g.patterns) != cPatterns+2 {
t.Fatalf("%d Default patterns should be available, have %d", cPatterns+2, len(g.patterns))
}
g = NewWithConfig(&Config{NamedCapturesOnly: true})
cPatterns = len(g.patterns)
g.AddPattern(name, pattern)
g.AddPattern(name+"2", pattern)
if len(g.patterns) != cPatterns+2 {
t.Fatalf("%d NamedCapture patterns should be available, have %d", cPatterns+2, len(g.patterns))
}
}
func TestMatch(t *testing.T) {
g := New()
g.AddPatternsFromPath("./patterns")
if r, err := g.Match("%{MONTH}", "June"); !r {
t.Fatalf("June should match %s: err=%s", "%{MONTH}", err.Error())
}
}
func TestDoesNotMatch(t *testing.T) {
g := New()
g.AddPatternsFromPath("./patterns")
if r, _ := g.Match("%{MONTH}", "13"); r {
t.Fatalf("13 should not match %s", "%{MONTH}")
}
}
func TestErrorMatch(t *testing.T) {
g := New()
if _, err := g.Match("(", "13"); err == nil {
t.Fatal("Error expected")
}
}
func TestDayCompile(t *testing.T) {
g := New()
g.AddPattern("DAY", "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)")
pattern := "%{DAY}"
_, err := g.compile(pattern)
if err != nil {
t.Fatal("Error:", err)
}
}
func TestErrorCompile(t *testing.T) {
g := New()
_, err := g.compile("(")
if err == nil {
t.Fatal("Error:", err)
}
}
func BenchmarkCaptures(t *testing.B) {
g := New()
g.AddPatternsFromPath("./patterns/base")
check := func(key, value, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
// run the check function b.N times
for n := 0; n < t.N; n++ {
check("verb", "GET",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
}
}
func TestNamedCaptures(t *testing.T) {
g := New()
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
captures, _ := g.Parse(pattern, text)
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
check("jour", "Tue",
"%{DAY:jour}",
"Tue May 15 11:21:42 [conn1047685] moveChunk deleted: 7157",
)
}
func TestErrorCaptureUnknowPattern(t *testing.T) {
g := New()
pattern := "%{UNKNOWPATTERN}"
_, err := g.Parse(pattern, "")
if err == nil {
t.Fatal("Expected error not set")
}
}
func TestParse(t *testing.T) {
g := New()
g.AddPatternsFromPath("./patterns")
res, _ := g.Parse("%{DAY}", "Tue qds")
if res["DAY"] != "Tue" {
t.Fatalf("DAY should be 'Tue' have '%s'", res["DAY"])
}
}
func TestErrorParseToMultiMap(t *testing.T) {
g := New()
pattern := "%{UNKNOWPATTERN}"
_, err := g.ParseToMultiMap(pattern, "")
if err == nil {
t.Fatal("Expected error not set")
}
}
func TestParseToMultiMap(t *testing.T) {
g := New()
g.AddPatternsFromPath("./patterns")
res, _ := g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:23:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["TIME"]) != 2 {
t.Fatalf("DAY should be an array of 3 elements, but is '%s'", res["TIME"])
}
if res["TIME"][0] != "23:58:32" {
t.Fatalf("TIME[0] should be '23:58:32' have '%s'", res["TIME"][0])
}
if res["TIME"][1] != "22:58:32" {
t.Fatalf("TIME[1] should be '22:58:32' have '%s'", res["TIME"][1])
}
}
func TestParseToMultiMapOnlyNamedCaptures(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
g.AddPatternsFromPath("./patterns")
res, _ := g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["timestamp"]) != 2 {
t.Fatalf("timestamp should be an array of 2 elements, but is '%s'", res["timestamp"])
}
if res["timestamp"][0] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("timestamp[0] should be '23/Apr/2014:22:58:32 +0200' have '%s'", res["DAY"][0])
}
if res["timestamp"][1] != "24/Apr/2014:22:58:32 +0200" {
t.Fatalf("timestamp[1] should be '24/Apr/2014:22:58:32 +0200' have '%s'", res["DAY"][1])
}
}
func TestCaptureAll(t *testing.T) {
g := New()
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
check("timestamp", "23/Apr/2014:22:58:32 +0200",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("TIME", "22:58:32",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("SECOND", `17,1599`, "%{TIMESTAMP_ISO8601}", `s d9fq4999s ../ sdf 2013-11-06 04:50:17,1599sd`)
check("HOSTNAME", `google.com`, "%{HOSTPORT}", `google.com:8080`)
//HOSTPORT
check("POSINT", `8080`, "%{HOSTPORT}", `google.com:8080`)
}
func TestNamedCapture(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
check("timestamp", "23/Apr/2014:22:58:32 +0200",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("TIME", "",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("SECOND", ``, "%{TIMESTAMP_ISO8601}", `s d9fq4999s ../ sdf 2013-11-06 04:50:17,1599sd`)
check("HOSTNAME", ``, "%{HOSTPORT}", `google.com:8080`)
//HOSTPORT
check("POSINT", ``, "%{HOSTPORT}", `google.com:8080`)
}
func TestCapturesAndNamedCapture(t *testing.T) {
check := func(key, value, pattern, text string) {
g := New()
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
checkNamed := func(key, value, pattern, text string) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
check("DAY", "Tue",
"%{DAY}",
"Tue May 15 11:21:42 [conn1047685] moveChunk deleted: 7157",
)
checkNamed("jour", "Tue",
"%{DAY:jour}",
"Tue May 15 11:21:42 [conn1047685] moveChunk deleted: 7157",
)
check("clientip", "127.0.0.1",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("verb", "GET",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("timestamp", "23/Apr/2014:22:58:32 +0200",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("bytes", "207",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
//PATH
check("WINPATH", `c:\winfows\sdf.txt`, "%{WINPATH}", `s dfqs c:\winfows\sdf.txt`)
check("WINPATH", `\\sdf\winfows\sdf.txt`, "%{WINPATH}", `s dfqs \\sdf\winfows\sdf.txt`)
check("UNIXPATH", `/usr/lib/`, "%{UNIXPATH}", `s dfqs /usr/lib/ sqfd`)
check("UNIXPATH", `/usr/lib`, "%{UNIXPATH}", `s dfqs /usr/lib sqfd`)
check("UNIXPATH", `/usr/`, "%{UNIXPATH}", `s dfqs /usr/ sqfd`)
check("UNIXPATH", `/usr`, "%{UNIXPATH}", `s dfqs /usr sqfd`)
check("UNIXPATH", `/`, "%{UNIXPATH}", `s dfqs / sqfd`)
//YEAR
check("YEAR", `4999`, "%{YEAR}", `s d9fq4999s ../ sdf`)
check("YEAR", `79`, "%{YEAR}", `s d79fq4999s ../ sdf`)
check("TIMESTAMP_ISO8601", `2013-11-06 04:50:17,1599`, "%{TIMESTAMP_ISO8601}", `s d9fq4999s ../ sdf 2013-11-06 04:50:17,1599sd`)
//MAC
check("MAC", `01:02:03:04:ab:cf`, "%{MAC}", `s d9fq4999s ../ sdf 2013- 01:02:03:04:ab:cf 11-06 04:50:17,1599sd`)
check("MAC", `01-02-03-04-ab-cd`, "%{MAC}", `s d9fq4999s ../ sdf 2013- 01-02-03-04-ab-cd 11-06 04:50:17,1599sd`)
//QUOTEDSTRING
check("QUOTEDSTRING", `"lkj"`, "%{QUOTEDSTRING}", `qsdklfjqsd fk"lkj"mkj`)
check("QUOTEDSTRING", `'lkj'`, "%{QUOTEDSTRING}", `qsdklfjqsd fk'lkj'mkj`)
check("QUOTEDSTRING", `"fk'lkj'm"`, "%{QUOTEDSTRING}", `qsdklfjqsd "fk'lkj'm"kj`)
check("QUOTEDSTRING", `'fk"lkj"m'`, "%{QUOTEDSTRING}", `qsdklfjqsd 'fk"lkj"m'kj`)
//BASE10NUM
check("BASE10NUM", `1`, "%{BASE10NUM}", `1`) // this is a nice one
check("BASE10NUM", `8080`, "%{BASE10NUM}", `qsfd8080qsfd`)
}
// Should be run with -race
func TestConcurentParse(t *testing.T) {
g := New()
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
go check("QUOTEDSTRING", `"lkj"`, "%{QUOTEDSTRING}", `qsdklfjqsd fk"lkj"mkj`)
go check("QUOTEDSTRING", `'lkj'`, "%{QUOTEDSTRING}", `qsdklfjqsd fk'lkj'mkj`)
go check("QUOTEDSTRING", `'lkj'`, "%{QUOTEDSTRING}", `qsdklfjqsd fk'lkj'mkj`)
go check("QUOTEDSTRING", `"fk'lkj'm"`, "%{QUOTEDSTRING}", `qsdklfjqsd "fk'lkj'm"kj`)
go check("QUOTEDSTRING", `'fk"lkj"m'`, "%{QUOTEDSTRING}", `qsdklfjqsd 'fk"lkj"m'kj`)
}
func TestPatterns(t *testing.T) {
g := NewWithConfig(&Config{SkipDefaultPatterns: true})
if len(g.Patterns()) != 0 {
t.Fatalf("Patterns should return 0, have '%d'", len(g.Patterns()))
}
name := "DAY0"
pattern := "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)"
g.AddPattern(name, pattern)
g.AddPattern(name+"1", pattern)
if len(g.Patterns()) != 2 {
t.Fatalf("Patterns should return 2, have '%d'", len(g.Patterns()))
}
}
func TestParseTypedWithDefaultCaptureMode(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.ParseTyped("%{IPV4:ip:string} %{NUMBER:status:int} %{NUMBER:duration:float}", `127.0.0.1 200 0.8`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["ip"] != "127.0.0.1" {
t.Fatalf("%s should be '%s' have '%s'", "ip", "127.0.0.1", captures["ip"])
} else {
if captures["status"] != 200 {
t.Fatalf("%s should be '%d' have '%d'", "status", 200, captures["status"])
} else {
if captures["duration"] != 0.8 {
t.Fatalf("%s should be '%d' have '%d'", "duration", 0.8, captures["duration"])
}
}
}
}
}
func TestParseTypedWithNoTypeInfo(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.ParseTyped("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != nil {
t.Fatalf("%s should be '%s' have '%s'", "TIME", nil, captures["TIME"])
}
}
g = New()
if captures, err := g.ParseTyped("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != "22:58:32" {
t.Fatalf("%s should be '%s' have '%s'", "TIME", "22:58:32", captures["TIME"])
}
}
}
func TestParseTypedWithIntegerTypeCoercion(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.ParseTyped("%{WORD:coerced:int}", `5.75`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["coerced"] != 5 {
t.Fatalf("%s should be '%s' have '%s'", "coerced", "5", captures["coerced"])
}
}
}
func TestParseTypedWithUnknownType(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
if _, err := g.ParseTyped("%{WORD:word:unknown}", `hello`); err == nil {
t.Fatalf("parsing an unknown type must result in a conversion error")
}
}
func TestParseTypedErrorCaptureUnknowPattern(t *testing.T) {
g := New()
pattern := "%{UNKNOWPATTERN}"
_, err := g.ParseTyped(pattern, "")
if err == nil {
t.Fatal("Expected error not set")
}
}
func TestParseTypedWithTypedParents(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
g.AddPattern("TESTCOMMON", `%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes:int}|-)`)
if captures, err := g.ParseTyped("%{TESTCOMMON}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["bytes"] != 207 {
t.Fatalf("%s should be '%s' have '%s'", "bytes", "207", captures["bytes"])
}
}
}
func TestParseTypedWithSemanticHomonyms(t *testing.T) {
g := NewWithConfig(&Config{NamedCapturesOnly: true})
g.AddPattern("MYNUM", `%{NUMBER:bytes:int}`)
g.AddPattern("MYSTR", `%{NUMBER:bytes:string}`)
if captures, err := g.ParseTyped("%{MYNUM}", `207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["bytes"] != 207 {
t.Fatalf("%s should be %#v have %#v", "bytes", 207, captures["bytes"])
}
}
if captures, err := g.ParseTyped("%{MYSTR}", `207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["bytes"] != "207" {
t.Fatalf("%s should be %#v have %#v", "bytes", "207", captures["bytes"])
}
}
}