-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfmt_flag_test.go
100 lines (87 loc) · 3.06 KB
/
fmt_flag_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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/essentials source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ess
import (
"testing"
"aahframework.org/test.v0/assert"
)
func TestFmtParseFlagLog(t *testing.T) {
const (
FmtFlagLevel FmtFlag = iota
FmtFlagTime
FmtFlagUTCTime
FmtFlagLongfile
FmtFlagShortfile
FmtFlagLine
FmtFlagMessage
FmtFlagCustom
)
logFmtFlags := map[string]FmtFlag{
"level": FmtFlagLevel,
"time": FmtFlagTime,
"utctime": FmtFlagUTCTime,
"longfile": FmtFlagLongfile,
"shortfile": FmtFlagShortfile,
"line": FmtFlagLine,
"message": FmtFlagMessage,
"custom": FmtFlagCustom,
}
flagParts, err := ParseFmtFlag("%time:2006-01-02 15:04:05.000 %level %custom:- %message", logFmtFlags)
assert.Nil(t, err)
assertFlagPart(t, "time", "2006-01-02 15:04:05.000", FmtFlag(1), flagParts[0])
assertFlagPart(t, "level", "%v", FmtFlag(0), flagParts[1])
assertFlagPart(t, "custom", "-", FmtFlag(7), flagParts[2])
assertFlagPart(t, "message", "%v", FmtFlag(6), flagParts[3])
// Unknown flag
flagParts, err = ParseFmtFlag("%myflag", logFmtFlags)
assert.NotNil(t, err)
assert.Equal(t, "fmtflag: unknown flag 'myflag'", err.Error())
assert.True(t, len(flagParts) == 0)
}
func TestFmtParseFlagAccessLog(t *testing.T) {
const (
fmtFlagClientIP FmtFlag = iota
fmtFlagRequestTime
fmtFlagRequestURL
fmtFlagRequestMethod
fmtFlagRequestID
fmtFlagRequestHeader
fmtFlagQueryString
fmtFlagResponseStatus
fmtFlagResponseSize
fmtFlagResponseHeader
fmtFlagResponseTime
)
accessLogFmtFlags := map[string]FmtFlag{
"clientip": fmtFlagClientIP,
"reqtime": fmtFlagRequestTime,
"requrl": fmtFlagRequestURL,
"reqmethod": fmtFlagRequestMethod,
"reqid": fmtFlagRequestID,
"reqhdr": fmtFlagRequestHeader,
"querystr": fmtFlagQueryString,
"resstatus": fmtFlagResponseStatus,
"ressize": fmtFlagResponseSize,
"reshdr": fmtFlagResponseHeader,
"restime": fmtFlagResponseTime,
}
flagParts, err := ParseFmtFlag("%clientip %reqid %reqtime %restime %resstatus %ressize %reqmethod %requrl %reqhdr:Referer %reshdr:Server", accessLogFmtFlags)
assert.Nil(t, err)
assertFlagPart(t, "clientip", "%v", FmtFlag(0), flagParts[0])
assertFlagPart(t, "reqid", "%v", FmtFlag(4), flagParts[1])
assertFlagPart(t, "reqtime", "%v", FmtFlag(1), flagParts[2])
assertFlagPart(t, "restime", "%v", FmtFlag(10), flagParts[3])
assertFlagPart(t, "resstatus", "%v", FmtFlag(7), flagParts[4])
assertFlagPart(t, "ressize", "%v", FmtFlag(8), flagParts[5])
assertFlagPart(t, "reqmethod", "%v", FmtFlag(3), flagParts[6])
assertFlagPart(t, "requrl", "%v", FmtFlag(2), flagParts[7])
assertFlagPart(t, "reqhdr", "Referer", FmtFlag(5), flagParts[8])
assertFlagPart(t, "reshdr", "Server", FmtFlag(9), flagParts[9])
}
func assertFlagPart(t *testing.T, name, format string, fflag FmtFlag, flagPart FmtFlagPart) {
t.Logf("Fmt Flag: %v", format)
assert.Equal(t, name, flagPart.Name)
assert.Equal(t, format, flagPart.Format)
assert.Equal(t, fflag, flagPart.Flag)
}