-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
91 lines (71 loc) · 2.8 KB
/
middleware_test.go
File metadata and controls
91 lines (71 loc) · 2.8 KB
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
package httpserver
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/Vonage/gosrvlib/pkg/httputil"
"github.com/Vonage/gosrvlib/pkg/logging"
"github.com/Vonage/gosrvlib/pkg/redact"
"github.com/Vonage/gosrvlib/pkg/testutil"
"github.com/Vonage/gosrvlib/pkg/traceid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)
func TestRequestInjectHandler(t *testing.T) {
t.Parallel()
nextHandler := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
l := logging.FromContext(r.Context())
assert.NotNil(t, l, "logger not found")
l.Info("injected")
// check if the request_time can be retrieved.
reqTime, ok := httputil.GetRequestTime(r)
assert.True(t, ok)
assert.NotEmpty(t, reqTime)
})
ctx, logs := testutil.ContextWithLogObserver(zapcore.DebugLevel)
handler := RequestInjectHandler(logging.FromContext(ctx), traceid.DefaultHeader, redact.HTTPData, nextHandler)
req := httptest.NewRequest(http.MethodGet, "/", nil)
handler.ServeHTTP(nil, req)
logEntries := logs.All()
require.Len(t, logEntries, 1, "expected only 1 log message")
logEntry := logEntries[0]
logContextMap := logEntry.ContextMap()
// check traceid
tiValue, tiExists := logContextMap[traceid.DefaultLogKey]
require.True(t, tiExists, "traceid field missing")
require.NotEmpty(t, tiValue, "expected traceid value not found")
// check request_time
rtValue, rtExists := logContextMap["request_time"]
require.True(t, rtExists, "request_time field missing")
require.NotEmpty(t, rtValue, "expected request_time value not found")
// check request_method
mValue, mExists := logContextMap["request_method"]
require.True(t, mExists, "request_method field missing")
require.Equal(t, http.MethodGet, mValue)
// check request_path
pValue, pExists := logContextMap["request_path"]
require.True(t, pExists, "request_path field missing")
require.Equal(t, "/", pValue)
// check request_query
rqValue, rqExists := logContextMap["request_query"]
require.True(t, rqExists, "request_query field missing")
require.Empty(t, rqValue)
// check request_user_agent
_, ipExists := logContextMap["request_remote_address"]
require.True(t, ipExists, "request_remote_address field missing")
// check request_uri
ruValue, ruExists := logContextMap["request_uri"]
require.True(t, ruExists, "request_uri field missing")
require.Equal(t, "/", ruValue)
// check request_user_agent
uaValue, uaExists := logContextMap["request_user_agent"]
require.True(t, uaExists, "request_user_agent field missing")
require.Empty(t, uaValue)
// check request_x_forwarded_for
rxffValue, rxffExists := logContextMap["request_x_forwarded_for"]
require.True(t, rxffExists, "request_x_forwarded_for field missing")
require.Empty(t, rxffValue)
// message
require.Equal(t, "injected", logEntry.Message)
}