-
Notifications
You must be signed in to change notification settings - Fork 4
/
zap_test.go
138 lines (110 loc) · 3.08 KB
/
zap_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
/*
* Copyright (c) Fabio da Silva Ribeiro <[email protected]>
* SPDX-License-Identifier: MIT
*/
package middleware
import (
"fmt"
"net/http"
"testing"
"github.com/labstack/echo/v4"
emw "github.com/labstack/echo/v4/middleware"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)
func TestZapLogWithConfig(t *testing.T) {
ec := postCtx(t)
logger, logs := observer.New(zap.InfoLevel)
config := ZapLogConfig{
Logger: zap.New(logger),
FieldMap: testFields,
}
_ = ZapLogWithConfig(config)(testHandler)(ec)
entry := logs.All()[0]
ectx := entry.ContextMap()
tests := []struct {
name string
got interface{}
want interface{}
}{
{"msg", entry.Message, "handle request"},
{"id", ectx["id"], "123"},
{"remote_ip", ectx["remote_ip"], "http://foo.bar"},
{"uri", ectx["uri"], "http://some/foo/456?name=john"},
{"host", ectx["host"], "some"},
{"method", ectx["method"], "POST"},
{"status", ectx["status"], int64(http.StatusOK)},
{"bytes_in", ectx["bytes_in"], "0"},
{"bytes_out", ectx["bytes_out"], "4"},
{"path", ectx["path"], "/foo/456"},
{"route", ectx["route"], "/foo/:id"},
{"protocol", ectx["protocol"], "HTTP/1.1"},
{"referer", ectx["referer"], "http://foo.bar"},
{"user_agent", ectx["user_agent"], "cli-agent"},
{"user", ectx["user"], "admin"},
{"filter_name", ectx["filter_name"], "john"},
{"username", ectx["username"], "doejohn"},
{"session", ectx["session"], "A1B2C3"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("entry_%s", tt.name), func(t *testing.T) {
if tt.got != tt.want {
t.Errorf("expect '%s' as '%v', got '%v'", tt.name, tt.want, tt.got)
}
})
}
}
func TestZapLog(t *testing.T) {
ec := reqCtx(t)
_ = ZapLog()(testHandler)(ec)
}
func TestZapLogWithEmptyConfig(t *testing.T) {
ec := reqCtx(t)
_ = ZapLogWithConfig(ZapLogConfig{})(testHandler)(ec)
}
func TestZapLogWithSkipper(t *testing.T) {
ec := reqCtx(t)
config := DefaultZapLogConfig
config.Skipper = func(echo.Context) bool {
return true
}
_ = ZapLogWithConfig(config)(testHandler)(ec)
}
func TestZapLogRetrievesAnError(t *testing.T) {
ec := errCtx(t)
logger, logs := observer.New(zap.InfoLevel)
config := ZapLogConfig{
Logger: zap.New(logger),
}
_ = ZapLogWithConfig(config)(testHandler)(ec)
entry := logs.All()[0]
ectx := entry.ContextMap()
if ectx["status"] != int64(http.StatusInternalServerError) {
t.Errorf("invalid log: wrong status code")
}
if _, ok := ectx["error"]; !ok {
t.Errorf("invalid log: error not found")
}
}
func TestZapLogRecoverFn(t *testing.T) {
ec := panicCtx(t)
obsLog, logs := observer.New(zap.InfoLevel)
logger := zap.New(obsLog)
rec := emw.RecoverWithConfig(emw.RecoverConfig{
LogErrorFunc: ZapLogRecoverFn(logger),
})
config := ZapLogConfig{
Logger: logger,
}
_ = ZapLogWithConfig(config)(rec(testHandler))(ec)
entry := logs.All()[0]
ectx := entry.ContextMap()
if _, ok := ectx["error"]; !ok {
t.Errorf("invalid log: error not found")
}
entry = logs.All()[1]
ectx = entry.ContextMap()
if ectx["status"] != int64(http.StatusInternalServerError) {
t.Errorf("invalid log: wrong status code")
}
}