|
| 1 | +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) |
| 2 | +// go-aah/essentials source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package ess |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "aahframework.org/test.v0/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestFmtParseFlagLog(t *testing.T) { |
| 14 | + const ( |
| 15 | + FmtFlagLevel FmtFlag = iota |
| 16 | + FmtFlagTime |
| 17 | + FmtFlagUTCTime |
| 18 | + FmtFlagLongfile |
| 19 | + FmtFlagShortfile |
| 20 | + FmtFlagLine |
| 21 | + FmtFlagMessage |
| 22 | + FmtFlagCustom |
| 23 | + ) |
| 24 | + |
| 25 | + logFmtFlags := map[string]FmtFlag{ |
| 26 | + "level": FmtFlagLevel, |
| 27 | + "time": FmtFlagTime, |
| 28 | + "utctime": FmtFlagUTCTime, |
| 29 | + "longfile": FmtFlagLongfile, |
| 30 | + "shortfile": FmtFlagShortfile, |
| 31 | + "line": FmtFlagLine, |
| 32 | + "message": FmtFlagMessage, |
| 33 | + "custom": FmtFlagCustom, |
| 34 | + } |
| 35 | + |
| 36 | + flagParts, err := ParseFmtFlag("%time:2006-01-02 15:04:05.000 %level %custom:- %message", logFmtFlags) |
| 37 | + assert.Nil(t, err) |
| 38 | + |
| 39 | + assertFlagPart(t, "time", "2006-01-02 15:04:05.000", FmtFlag(1), flagParts[0]) |
| 40 | + assertFlagPart(t, "level", "%v", FmtFlag(0), flagParts[1]) |
| 41 | + assertFlagPart(t, "custom", "-", FmtFlag(7), flagParts[2]) |
| 42 | + assertFlagPart(t, "message", "%v", FmtFlag(6), flagParts[3]) |
| 43 | + |
| 44 | + // Unknown flag |
| 45 | + flagParts, err = ParseFmtFlag("%myflag", logFmtFlags) |
| 46 | + assert.NotNil(t, err) |
| 47 | + assert.Equal(t, "fmtflag: unknown flag 'myflag'", err.Error()) |
| 48 | + assert.True(t, len(flagParts) == 0) |
| 49 | +} |
| 50 | + |
| 51 | +func TestFmtParseFlagAccessLog(t *testing.T) { |
| 52 | + const ( |
| 53 | + fmtFlagClientIP FmtFlag = iota |
| 54 | + fmtFlagRequestTime |
| 55 | + fmtFlagRequestURL |
| 56 | + fmtFlagRequestMethod |
| 57 | + fmtFlagRequestID |
| 58 | + fmtFlagRequestHeader |
| 59 | + fmtFlagQueryString |
| 60 | + fmtFlagResponseStatus |
| 61 | + fmtFlagResponseSize |
| 62 | + fmtFlagResponseHeader |
| 63 | + fmtFlagResponseTime |
| 64 | + ) |
| 65 | + |
| 66 | + accessLogFmtFlags := map[string]FmtFlag{ |
| 67 | + "clientip": fmtFlagClientIP, |
| 68 | + "reqtime": fmtFlagRequestTime, |
| 69 | + "requrl": fmtFlagRequestURL, |
| 70 | + "reqmethod": fmtFlagRequestMethod, |
| 71 | + "reqid": fmtFlagRequestID, |
| 72 | + "reqhdr": fmtFlagRequestHeader, |
| 73 | + "querystr": fmtFlagQueryString, |
| 74 | + "resstatus": fmtFlagResponseStatus, |
| 75 | + "ressize": fmtFlagResponseSize, |
| 76 | + "reshdr": fmtFlagResponseHeader, |
| 77 | + "restime": fmtFlagResponseTime, |
| 78 | + } |
| 79 | + |
| 80 | + flagParts, err := ParseFmtFlag("%clientip %reqid %reqtime %restime %resstatus %ressize %reqmethod %requrl %reqhdr:Referer %reshdr:Server", accessLogFmtFlags) |
| 81 | + assert.Nil(t, err) |
| 82 | + |
| 83 | + assertFlagPart(t, "clientip", "%v", FmtFlag(0), flagParts[0]) |
| 84 | + assertFlagPart(t, "reqid", "%v", FmtFlag(4), flagParts[1]) |
| 85 | + assertFlagPart(t, "reqtime", "%v", FmtFlag(1), flagParts[2]) |
| 86 | + assertFlagPart(t, "restime", "%v", FmtFlag(10), flagParts[3]) |
| 87 | + assertFlagPart(t, "resstatus", "%v", FmtFlag(7), flagParts[4]) |
| 88 | + assertFlagPart(t, "ressize", "%v", FmtFlag(8), flagParts[5]) |
| 89 | + assertFlagPart(t, "reqmethod", "%v", FmtFlag(3), flagParts[6]) |
| 90 | + assertFlagPart(t, "requrl", "%v", FmtFlag(2), flagParts[7]) |
| 91 | + assertFlagPart(t, "reqhdr", "Referer", FmtFlag(5), flagParts[8]) |
| 92 | + assertFlagPart(t, "reshdr", "Server", FmtFlag(9), flagParts[9]) |
| 93 | +} |
| 94 | + |
| 95 | +func assertFlagPart(t *testing.T, name, format string, fflag FmtFlag, flagPart FmtFlagPart) { |
| 96 | + t.Logf("Fmt Flag: %v", format) |
| 97 | + assert.Equal(t, name, flagPart.Name) |
| 98 | + assert.Equal(t, format, flagPart.Format) |
| 99 | + assert.Equal(t, fflag, flagPart.Flag) |
| 100 | +} |
0 commit comments