-
Notifications
You must be signed in to change notification settings - Fork 11
/
middleware_test.go
225 lines (181 loc) Β· 5.63 KB
/
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
214
215
216
217
218
219
220
221
222
223
224
225
package lecho_test
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/ziflex/lecho/v3"
)
func TestMiddleware(t *testing.T) {
t.Run("should not trigger error handler when HandleError is false", func(t *testing.T) {
var called bool
e := echo.New()
e.HTTPErrorHandler = func(err error, c echo.Context) {
called = true
c.JSON(http.StatusInternalServerError, err.Error())
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
m := lecho.Middleware(lecho.Config{})
next := func(c echo.Context) error {
return errors.New("error")
}
handler := m(next)
err := handler(c)
assert.Error(t, err, "should return error")
assert.False(t, called, "should not call error handler")
})
t.Run("should trigger error handler when HandleError is true", func(t *testing.T) {
var called bool
e := echo.New()
e.HTTPErrorHandler = func(err error, c echo.Context) {
called = true
c.JSON(http.StatusInternalServerError, err.Error())
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
m := lecho.Middleware(lecho.Config{
HandleError: true,
})
next := func(c echo.Context) error {
return errors.New("error")
}
handler := m(next)
err := handler(c)
assert.Error(t, err, "should return error")
assert.Truef(t, called, "should call error handler")
})
t.Run("should use enricher", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
b := &bytes.Buffer{}
l := lecho.New(b)
m := lecho.Middleware(lecho.Config{
Logger: l,
Enricher: func(c echo.Context, logger zerolog.Context) zerolog.Context {
return logger.Str("test", "test")
},
})
next := func(c echo.Context) error {
return nil
}
handler := m(next)
err := handler(c)
assert.NoError(t, err, "should not return error")
str := b.String()
assert.Contains(t, str, `"test":"test"`)
})
t.Run("should escalate log level for slow requests", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
b := &bytes.Buffer{}
l := lecho.New(b)
l.SetLevel(log.INFO)
m := lecho.Middleware(lecho.Config{
Logger: l,
RequestLatencyLimit: 5 * time.Millisecond,
RequestLatencyLevel: zerolog.WarnLevel,
})
// Slow request should be logged at the escalated level
next := func(c echo.Context) error {
time.Sleep(5 * time.Millisecond)
return nil
}
handler := m(next)
err := handler(c)
assert.NoError(t, err, "should not return error")
str := b.String()
assert.Contains(t, str, `"level":"warn"`)
assert.NotContains(t, str, `"level":"info"`)
})
t.Run("shouldn't escalate log level for fast requests", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
b := &bytes.Buffer{}
l := lecho.New(b)
l.SetLevel(log.INFO)
m := lecho.Middleware(lecho.Config{
Logger: l,
RequestLatencyLimit: 5 * time.Millisecond,
RequestLatencyLevel: zerolog.WarnLevel,
})
// Fast request should be logged at the default level
next := func(c echo.Context) error {
time.Sleep(1 * time.Millisecond)
return nil
}
handler := m(next)
err := handler(c)
assert.NoError(t, err, "should not return error")
str := b.String()
assert.Contains(t, str, `"level":"info"`)
assert.NotContains(t, str, `"level":"warn"`)
})
t.Run("should skip middleware before calling next handler when Skipper func returns true", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/skip", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
b := &bytes.Buffer{}
l := lecho.New(b)
l.SetLevel(log.INFO)
m := lecho.Middleware(lecho.Config{
Logger: l,
Skipper: func(c echo.Context) bool {
return c.Request().URL.Path == "/skip"
},
})
next := func(c echo.Context) error {
return nil
}
handler := m(next)
err := handler(c)
assert.NoError(t, err, "should not return error")
str := b.String()
assert.Empty(t, str, "should not log anything")
})
t.Run("should skip middleware after calling next handler when AfterNextSkipper func returns true", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
b := &bytes.Buffer{}
l := lecho.New(b)
l.SetLevel(log.INFO)
m := lecho.Middleware(lecho.Config{
Logger: l,
AfterNextSkipper: func(c echo.Context) bool {
return c.Response().Status == http.StatusMovedPermanently
},
})
next := func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "/other")
}
handler := m(next)
err := handler(c)
assert.NoError(t, err, "should not return error")
str := b.String()
assert.Empty(t, str, "should not log anything")
})
}