-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware_test.go
111 lines (84 loc) · 3.25 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
// Copyright 2020 Lauris BH. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package logger
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
)
func testRequest(t *testing.T, req *http.Request, middleware ...func(h http.Handler) http.Handler) chi.Router {
w := httptest.NewRecorder()
r := chi.NewRouter()
r.Use(middleware...)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "Response status code")
return r
}
func TestLogger(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Host = "localhost:8080"
logger, hook := test.NewNullLogger()
testRequest(t, req, Logger("router", logger))
assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Nil(t, hook.LastEntry().Data["request_id"])
assert.Equal(t, "GET", hook.LastEntry().Data["method"])
assert.Equal(t, 11, hook.LastEntry().Data["bytes"])
assert.Greater(t, hook.LastEntry().Data["duration"], int64(0))
assert.Equal(t, "http://localhost:8080/", hook.LastEntry().Message)
}
func TestLoggerWithLevelDebug(t *testing.T) {
testLoggerWithLevel(t, logrus.DebugLevel)
}
func TestLoggerWithLevelWarning(t *testing.T) {
testLoggerWithLevel(t, logrus.WarnLevel)
}
func TestLoggerWithLevelTrace(t *testing.T) {
testLoggerWithLevel(t, logrus.TraceLevel)
}
func testLoggerWithLevel(t *testing.T, level logrus.Level) {
req := httptest.NewRequest("GET", "/", nil)
req.Host = "localhost:8080"
logger, hook := test.NewNullLogger()
logger.SetLevel(logrus.TraceLevel)
testRequest(t, req, LoggerWithLevel("router", logger, level))
assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, level, hook.LastEntry().Level)
assert.Nil(t, hook.LastEntry().Data["request_id"])
assert.Equal(t, "GET", hook.LastEntry().Data["method"])
assert.Equal(t, 11, hook.LastEntry().Data["bytes"])
assert.Greater(t, hook.LastEntry().Data["duration"], int64(0))
assert.Equal(t, "http://localhost:8080/", hook.LastEntry().Message)
}
func TestEntry(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Host = "localhost:8080"
logger, hook := test.NewNullLogger()
entry := logger.WithField("entry", "exists")
testRequest(t, req, Logger("router", entry))
assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "exists", hook.LastEntry().Data["entry"])
assert.Nil(t, hook.LastEntry().Data["request_id"])
assert.Equal(t, "GET", hook.LastEntry().Data["method"])
assert.Equal(t, 11, hook.LastEntry().Data["bytes"])
assert.Greater(t, hook.LastEntry().Data["duration"], int64(0))
assert.Equal(t, "http://localhost:8080/", hook.LastEntry().Message)
}
func TestLoggerRequestID(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Host = "localhost:8080"
logger, hook := test.NewNullLogger()
testRequest(t, req, middleware.RequestID, Logger("router", logger))
assert.Equal(t, 1, len(hook.Entries))
assert.NotNil(t, hook.LastEntry().Data["request_id"])
}