-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging_middleware_test.go
213 lines (194 loc) · 6.54 KB
/
logging_middleware_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
package handlers
import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoggingMiddleware_Apply(t *testing.T) {
examples := []struct {
Name string
Filters map[string]logrus.Level
Handler func(t *testing.T) HandlerFunc
Path string
Method string
Host string
Headers map[string]string
Context func(context.Context) context.Context
Env map[string]string
ExpectedLevel logrus.Level
ExpectedFieldValues map[string]string
ExpectedFields []string
}{
{
Name: "HTTP GET / on example.dev without any additional info",
Path: "/",
Method: "GET",
Host: "example.dev",
ExpectedFields: []string{"path", "host", "method"},
}, {
Name: "HTTP GET / on example.dev with X-Forwarded-Proto",
Path: "/",
Method: "GET",
Host: "example.dev",
Headers: map[string]string{"X-Forwarded-For": "10.11.12.13"},
ExpectedFieldValues: map[string]string{"from": "10.11.12.13"},
ExpectedFields: []string{"path", "host", "method", "from"},
}, {
Name: "HTTP GET / on example.dev with X-Forwarded-Proto",
Path: "/",
Method: "GET",
Host: "example.dev",
Headers: map[string]string{"X-Forwarded-Proto": "https"},
ExpectedFieldValues: map[string]string{"protocol": "https"},
ExpectedFields: []string{"path", "host", "method", "protocol"},
}, {
Name: "with user agent",
Path: "/",
Method: "GET",
Host: "example.dev",
Headers: map[string]string{"User-Agent": "MyUserAgent1.0"},
ExpectedFields: []string{"path", "host", "method", "user_agent"},
}, {
Name: "with referer",
Path: "/",
Method: "GET",
Host: "example.dev",
Headers: map[string]string{"Referer": "http://google.com"},
ExpectedFields: []string{"path", "host", "method", "referer"},
}, {
Name: "with request_id in context",
Path: "/",
Method: "GET",
Host: "example.dev",
ExpectedFields: []string{"path", "host", "method", "request_id"},
Context: func(ctx context.Context) context.Context {
return context.WithValue(ctx, "request_id", "0")
},
}, {
Name: "with request_id in context",
Path: "/",
Method: "GET",
Host: "example.dev",
ExpectedFields: []string{"path", "host", "method"},
Handler: func(t *testing.T) HandlerFunc {
return HandlerFunc(func(w http.ResponseWriter, r *http.Request, params map[string]string) error {
logger, ok := r.Context().Value("logger").(logrus.FieldLogger)
assert.True(t, ok)
assert.NotNil(t, logger)
return nil
})
},
}, {
Name: "with filtered path it should not do anything if feature not enabled",
Path: "/res/1/health",
Filters: map[string]logrus.Level{"/res/.+/health": logrus.DebugLevel},
Method: "GET",
Host: "example.dev",
ExpectedLevel: logrus.InfoLevel,
ExpectedFields: []string{"path", "host", "method"},
}, {
Name: "with filtered path it should change the level if the feature is enabled",
Path: "/res/1/health",
Filters: map[string]logrus.Level{"/res/.+/health": logrus.DebugLevel},
Method: "GET",
Host: "example.dev",
Env: map[string]string{"HANDLERS_LOG_FILTERS": "true"},
ExpectedLevel: logrus.DebugLevel,
ExpectedFields: []string{"path", "host", "method"},
}, {
Name: "should not filter unfiltered path",
Path: "/res/health",
Filters: map[string]logrus.Level{"/res/.+/health": logrus.DebugLevel},
Method: "GET",
Host: "example.dev",
ExpectedLevel: logrus.InfoLevel,
ExpectedFields: []string{"path", "host", "method"},
},
}
handler := HandlerFunc(func(w http.ResponseWriter, r *http.Request, params map[string]string) error {
return nil
})
for _, example := range examples {
t.Run(example.Name, func(t *testing.T) {
var err error
envbackup := map[string]string{}
if example.Env != nil {
for k, v := range example.Env {
envbackup[k] = os.Getenv(k)
os.Setenv(k, v)
}
defer func() {
for k, v := range envbackup {
os.Setenv(k, v)
}
}()
}
logger, hook := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
defer hook.Reset()
middleware := NewLoggingMiddleware(logger)
if example.Filters != nil {
middleware, err = NewLoggingMiddlewareWithFilters(logger, example.Filters)
require.NoError(t, err)
}
reqHandler := handler
if example.Handler != nil {
reqHandler = example.Handler(t)
}
reqHandler = middleware.Apply(reqHandler)
w := httptest.NewRecorder()
r, err := http.NewRequest(example.Method, example.Path, nil)
require.NoError(t, err)
if example.Context != nil {
r = r.WithContext(example.Context(r.Context()))
}
r.Host = example.Host
if example.Headers != nil {
for k, v := range example.Headers {
r.Header.Add(k, v)
}
}
err = reqHandler(w, r, map[string]string{})
require.NoError(t, err)
require.Equal(t, 2, len(hook.Entries))
expectedLevel := logrus.InfoLevel
if example.ExpectedLevel != 0 {
expectedLevel = example.ExpectedLevel
}
assert.Equal(t, expectedLevel, hook.Entries[0].Level)
assert.Equal(t, expectedLevel, hook.Entries[1].Level)
log1Keys := []string{}
for k, v := range hook.Entries[0].Data {
if example.ExpectedFieldValues != nil {
for ek, ev := range example.ExpectedFieldValues {
if k == ek {
assert.Equal(t, ev, v)
}
}
}
log1Keys = append(log1Keys, k)
}
log2Keys := []string{}
for k, v := range hook.Entries[1].Data {
if example.ExpectedFieldValues != nil {
for ek, ev := range example.ExpectedFieldValues {
if k == ek {
assert.Equal(t, ev, v)
}
}
}
log2Keys = append(log2Keys, k)
}
assert.Subset(t, log1Keys, example.ExpectedFields)
assert.Subset(t, log1Keys, []string{"protocol"})
assert.Subset(t, log2Keys, example.ExpectedFields)
assert.Subset(t, log2Keys, []string{"protocol", "status", "duration", "bytes"})
})
}
}