forked from gofiber/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap_test.go
443 lines (367 loc) · 11.6 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package fiberzap
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strconv"
"sync"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
func setupLogsCapture() (*zap.Logger, *observer.ObservedLogs) {
core, logs := observer.New(zap.InfoLevel)
return zap.New(core), logs
}
func Test_GetResBody(t *testing.T) {
var readableResBody = "this is readable response body"
var app = fiber.New()
var logger, logs = setupLogsCapture()
app.Use(New(Config{
Logger: logger,
GetResBody: func(c *fiber.Ctx) []byte {
return []byte(readableResBody)
},
Fields: []string{"resBody"},
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("------this is unreadable resp------")
})
_, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, readableResBody, logs.All()[0].ContextMap()["resBody"])
}
// go test -run Test_SkipBody
func Test_SkipBody(t *testing.T) {
logger, logs := setupLogsCapture()
app := fiber.New()
app.Use(New(Config{
SkipBody: func(_ *fiber.Ctx) bool {
return true
},
Logger: logger,
Fields: []string{"pid", "body"},
}))
body := bytes.NewReader([]byte("this is test"))
resp, err := app.Test(httptest.NewRequest("GET", "/", body))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
_, ok := logs.All()[0].ContextMap()["body"]
utils.AssertEqual(t, false, ok)
}
// go test -run Test_SkipResBody
func Test_SkipResBody(t *testing.T) {
logger, logs := setupLogsCapture()
app := fiber.New()
app.Use(New(Config{
SkipResBody: func(_ *fiber.Ctx) bool {
return true
},
Logger: logger,
Fields: []string{"pid", "resBody"},
}))
body := bytes.NewReader([]byte("this is test"))
resp, err := app.Test(httptest.NewRequest("GET", "/", body))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
_, ok := logs.All()[0].ContextMap()["resBody"]
utils.AssertEqual(t, false, ok)
}
// go test -run Test_Logger
func Test_Logger(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
Fields: []string{"pid", "latency", "error"},
}))
app.Get("/", func(c *fiber.Ctx) error {
return errors.New("some random error")
})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
utils.AssertEqual(t, "some random error", logs.All()[0].Context[3].String)
}
// go test -run Test_Logger_Next
func Test_Logger_Next(t *testing.T) {
app := fiber.New()
app.Use(New(Config{
Next: func(_ *fiber.Ctx) bool {
return true
},
}))
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
}
// go test -run Test_Logger_All
func Test_Logger_All(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
Fields: []string{"protocol", "pid", "body", "ip", "host", "url", "route", "method", "resBody", "queryParams", "bytesReceived", "bytesSent"},
}))
resp, err := app.Test(httptest.NewRequest("GET", "/?foo=bar", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
expected := map[string]interface{}{
"body": "",
"ip": "0.0.0.0",
"host": "example.com",
"url": "/?foo=bar",
"method": "GET",
"route": "/",
"protocol": "http",
"pid": strconv.Itoa(os.Getpid()),
"queryParams": "foo=bar",
"resBody": "Cannot GET /",
"bytesReceived": int64(0),
"bytesSent": int64(12),
}
utils.AssertEqual(t, expected, logs.All()[0].ContextMap())
}
// go test -run Test_Query_Params
func Test_Query_Params(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
Fields: []string{"queryParams"},
}))
resp, err := app.Test(httptest.NewRequest("GET", "/?foo=bar&baz=moz", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
expected := "foo=bar&baz=moz"
utils.AssertEqual(t, expected, logs.All()[0].Context[1].String)
}
// go test -run Test_Response_Body
func Test_Response_Body(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
Fields: []string{"resBody"},
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Sample response body")
})
app.Post("/test", func(c *fiber.Ctx) error {
return c.Send([]byte("Post in test"))
})
_, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
expectedGetResponse := "Sample response body"
utils.AssertEqual(t, expectedGetResponse, logs.All()[0].ContextMap()["resBody"])
_, err = app.Test(httptest.NewRequest("POST", "/test", nil))
utils.AssertEqual(t, nil, err)
expectedPostResponse := "Post in test"
t.Log(logs.All())
utils.AssertEqual(t, expectedPostResponse, logs.All()[1].ContextMap()["resBody"])
}
// go test -run Test_Logger_AppendUint
func Test_Logger_AppendUint(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
Fields: []string{"bytesReceived", "bytesSent", "status"},
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("hello")
})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
output := logs.All()[0].ContextMap()
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "0 5 200", fmt.Sprintf("%d %d %d", output["bytesReceived"], output["bytesSent"], output["status"]))
}
// go test -run Test_Logger_Data_Race -race
func Test_Logger_Data_Race(t *testing.T) {
app := fiber.New()
logger := zap.NewExample()
app.Use(New(Config{
Logger: logger,
Fields: []string{"bytesReceived", "bytesSent", "status"},
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("hello")
})
var (
resp1, resp2 *http.Response
err1, err2 error
)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
resp1, err1 = app.Test(httptest.NewRequest("GET", "/", nil))
wg.Done()
}()
resp2, err2 = app.Test(httptest.NewRequest("GET", "/", nil))
wg.Wait()
utils.AssertEqual(t, nil, err1)
utils.AssertEqual(t, fiber.StatusOK, resp1.StatusCode)
utils.AssertEqual(t, nil, err2)
utils.AssertEqual(t, fiber.StatusOK, resp2.StatusCode)
}
// go test -v -run=^$ -bench=Benchmark_Logger -benchmem -count=4
func Benchmark_Logger(b *testing.B) {
app := fiber.New()
app.Use(New())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
h := app.Handler()
fctx := &fasthttp.RequestCtx{}
fctx.Request.Header.SetMethod("GET")
fctx.Request.SetRequestURI("/")
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
h(fctx)
}
utils.AssertEqual(b, 200, fctx.Response.Header.StatusCode())
}
// go test -run Test_Request_Id
func Test_Request_Id(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
Fields: []string{"requestId"},
}))
app.Get("/", func(c *fiber.Ctx) error {
c.Response().Header.Add(fiber.HeaderXRequestID, "bf985e8e-6a32-42ec-8e50-05a21db8f0e4")
return c.SendString("hello")
})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, "bf985e8e-6a32-42ec-8e50-05a21db8f0e4", logs.All()[0].Context[1].String)
}
// go test -run Test_Skip_URIs
func Test_Skip_URIs(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
SkipURIs: []string{"/ignore_logging"},
}))
app.Get("/ignore_logging", func(c *fiber.Ctx) error {
return errors.New("no log")
})
resp, err := app.Test(httptest.NewRequest("GET", "/ignore_logging", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
utils.AssertEqual(t, 0, len(logs.All()))
}
// go test -run Test_Req_Headers
func Test_Req_Headers(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
app.Use(New(Config{
Logger: logger,
Fields: []string{"reqHeaders"},
}))
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("hello")
})
expected := map[string]interface{}{
"Host": "example.com",
"Baz": "foo",
"Foo": "bar",
}
req := httptest.NewRequest("GET", "/", nil)
req.Header.Add("foo", "bar")
req.Header.Add("baz", "foo")
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, expected, logs.All()[0].ContextMap())
}
// go test -run Test_LoggerLevelsAndMessages
func Test_LoggerLevelsAndMessages(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
levels := []zapcore.Level{zapcore.ErrorLevel, zapcore.WarnLevel, zapcore.InfoLevel}
messages := []string{"server error", "client error", "success"}
app.Use(New(Config{
Logger: logger,
Messages: messages,
Levels: levels,
}))
app.Get("/200", func(c *fiber.Ctx) error {
c.Status(fiber.StatusOK)
return nil
})
app.Get("/400", func(c *fiber.Ctx) error {
c.Status(fiber.StatusBadRequest)
return nil
})
app.Get("/500", func(c *fiber.Ctx) error {
c.Status(fiber.StatusInternalServerError)
return nil
})
resp, err := app.Test(httptest.NewRequest("GET", "/500", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
utils.AssertEqual(t, levels[0], logs.All()[0].Level)
utils.AssertEqual(t, messages[0], logs.All()[0].Message)
resp, err = app.Test(httptest.NewRequest("GET", "/400", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusBadRequest, resp.StatusCode)
utils.AssertEqual(t, levels[1], logs.All()[1].Level)
utils.AssertEqual(t, messages[1], logs.All()[1].Message)
resp, err = app.Test(httptest.NewRequest("GET", "/200", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, levels[2], logs.All()[2].Level)
utils.AssertEqual(t, messages[2], logs.All()[2].Message)
}
// go test -run Test_LoggerLevelsAndMessagesSingle
func Test_LoggerLevelsAndMessagesSingle(t *testing.T) {
app := fiber.New()
logger, logs := setupLogsCapture()
levels := []zapcore.Level{zapcore.ErrorLevel}
messages := []string{"server error"}
app.Use(New(Config{
Logger: logger,
Messages: messages,
Levels: levels,
}))
app.Get("/200", func(c *fiber.Ctx) error {
c.Status(fiber.StatusOK)
return nil
})
app.Get("/400", func(c *fiber.Ctx) error {
c.Status(fiber.StatusBadRequest)
return nil
})
app.Get("/500", func(c *fiber.Ctx) error {
c.Status(fiber.StatusInternalServerError)
return nil
})
resp, err := app.Test(httptest.NewRequest("GET", "/500", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)
utils.AssertEqual(t, levels[0], logs.All()[0].Level)
utils.AssertEqual(t, messages[0], logs.All()[0].Message)
resp, err = app.Test(httptest.NewRequest("GET", "/400", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusBadRequest, resp.StatusCode)
utils.AssertEqual(t, levels[0], logs.All()[1].Level)
utils.AssertEqual(t, messages[0], logs.All()[1].Message)
resp, err = app.Test(httptest.NewRequest("GET", "/200", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
utils.AssertEqual(t, levels[0], logs.All()[2].Level)
utils.AssertEqual(t, messages[0], logs.All()[2].Message)
}