-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger_test.go
55 lines (45 loc) · 1.28 KB
/
logger_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
package fiberlogrus
import (
"bytes"
"fmt"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestLogger(t *testing.T) {
t.Parallel()
app := fiber.New()
s := ""
buf := bytes.NewBufferString(s)
logger := logrus.New()
logger.SetOutput(buf)
app.Use(New(
Config{
Logger: logger,
Tags: []string{
TagMethod,
TagStatus,
TagReqHeaders,
AttachKeyTag(TagLocals, "loc"),
AttachKeyTag(TagRespHeader, "custom-header"),
},
}))
app.Get("/", func(c *fiber.Ctx) error {
c.Append("custom-header", "custom-header-value")
c.Locals("loc", "val")
return c.SendString("random string")
})
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
req.Header.Add("Multi-Header", "value-1")
req.Header.Add("Multi-Header", "value-2")
resp, err := app.Test(req)
require.NoError(t, err)
require.Equal(t, fiber.StatusOK, resp.StatusCode)
require.Contains(t, buf.String(), "method=GET")
require.Contains(t, buf.String(), "status=200")
require.Contains(t, buf.String(), "respHeader=custom-header-value")
require.Contains(t, buf.String(), "locals=val")
require.Contains(t, buf.String(), fmt.Sprintf(`reqHeaders="%v"`, []byte("Host=example.com&Multi-Header=value-1&Multi-Header=value-2")))
}