-
Notifications
You must be signed in to change notification settings - Fork 4
/
logrus_test.go
136 lines (107 loc) · 3.2 KB
/
logrus_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
/*
* Copyright (c) Fabio da Silva Ribeiro <[email protected]>
* SPDX-License-Identifier: MIT
*/
package middleware
import (
"bytes"
"strings"
"testing"
"github.com/labstack/echo/v4"
emw "github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
)
func TestLogrusWithConfig(t *testing.T) {
ec := postCtx(t)
b := new(bytes.Buffer)
logger := logrus.StandardLogger()
logger.Out = b
config := LogrusConfig{
Logger: logger,
FieldMap: testFields,
}
_ = LogrusWithConfig(config)(testHandler)(ec)
tests := []struct {
str string
err string
}{
{"handle request", "invalid log: handle request info not found"},
{"id=123", "invalid log: request id not found"},
{`remote_ip="http://foo.bar"`, "invalid log: remote ip not found"},
{`uri="http://some/foo/456?name=john"`, "invalid log: uri not found"},
{"host=some", "invalid log: host not found"},
{"method=POST", "invalid log: method not found"},
{"status=200", "invalid log: status not found"},
{"latency=", "invalid log: latency not found"},
{"latency_human=", "invalid log: latency_human not found"},
{"bytes_in=0", "invalid log: bytes_in not found"},
{"bytes_out=4", "invalid log: bytes_out not found"},
{"path=/foo/456", "invalid log: path not found"},
{`route="/foo/:id"`, "invalid log: route not found"},
{"protocol=HTTP/1.1", "invalid log: protocol not found"},
{`referer="http://foo.bar"`, "invalid log: referer not found"},
{"user_agent=cli-agent", "invalid log: user_agent not found"},
{"user=admin", "invalid log: header user not found"},
{"filter_name=john", "invalid log: query filter_name not found"},
{"username=doejohn", "invalid log: form field username not found"},
{"session=A1B2C3", "invalid log: cookie session not found"},
}
for _, test := range tests {
if !strings.Contains(b.String(), test.str) {
t.Error(test.err)
}
}
}
func TestLogrus(t *testing.T) {
ec := reqCtx(t)
_ = Logrus()(testHandler)(ec)
}
func TestLogrusWithEmptyConfig(t *testing.T) {
ec := reqCtx(t)
_ = LogrusWithConfig(LogrusConfig{})(testHandler)(ec)
}
func TestLogrusWithSkipper(t *testing.T) {
ec := reqCtx(t)
config := DefaultLogrusConfig
config.Skipper = func(echo.Context) bool {
return true
}
_ = LogrusWithConfig(config)(testHandler)(ec)
}
func TestLogrusRetrievesAnError(t *testing.T) {
ec := errCtx(t)
b := new(bytes.Buffer)
logger := logrus.StandardLogger()
logger.Out = b
config := LogrusConfig{
Logger: logger,
}
_ = LogrusWithConfig(config)(testHandler)(ec)
res := b.String()
if !strings.Contains(res, "status=500") {
t.Errorf("invalid log: wrong status code")
}
if !strings.Contains(res, `error=error`) {
t.Errorf("invalid log: error not found")
}
}
func TestLogrusRecoverFn(t *testing.T) {
ec := panicCtx(t)
b := new(bytes.Buffer)
logger := logrus.StandardLogger()
logger.Out = b
rec := emw.RecoverWithConfig(emw.RecoverConfig{
LogErrorFunc: LogrusRecoverFn(logger),
})
config := LogrusConfig{
Logger: logger,
}
_ = LogrusWithConfig(config)(rec(testHandler))(ec)
res := b.String()
if !strings.Contains(res, "status=500") {
t.Errorf("invalid log: wrong status code")
}
if !strings.Contains(res, `error="unable to call"`) {
t.Errorf("invalid log: error not found")
}
}