-
Notifications
You must be signed in to change notification settings - Fork 5
/
logger_test.go
130 lines (98 loc) · 3.22 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
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
package logger
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/getsentry/sentry-go"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type TestLoggerSuite struct {
suite.Suite
ctrl *gomock.Controller
sendEventMock *gomock.Call
logger *zap.Logger
}
func (s *TestLoggerSuite) SetupTest() {
s.logger = zap.New(zapcore.NewNopCore())
s.ctrl = gomock.NewController(s.T())
transportMock := NewMockTransport(s.ctrl)
transportMock.EXPECT().
Configure(gomock.AssignableToTypeOf(sentry.ClientOptions{})).
Return()
s.sendEventMock = transportMock.EXPECT().
SendEvent(gomock.AssignableToTypeOf(&sentry.Event{})).
Return().
MinTimes(0)
transportMock.EXPECT().
Flush(gomock.Any()).
Return(true).
MinTimes(0)
_ = sentry.Init(sentry.ClientOptions{
Transport: transportMock,
})
}
func (s *TestLoggerSuite) TearDownTest() {
s.ctrl.Finish()
// reset sentry client to default
_ = sentry.Init(sentry.ClientOptions{})
}
func (s *TestLoggerSuite) wrapHandler(handler http.HandlerFunc) http.Handler {
return RequestLogger(s.logger)(handler)
}
func (s *TestLoggerSuite) TestLoggerShouldSendEventToSentryAndReturnEventID() {
s.logger = zap.New(NewSentryCoreWrapper(zapcore.NewNopCore(), sentry.CurrentHub()))
eventID := sentry.EventID("<not valid>")
s.sendEventMock.Do(func(event *sentry.Event) {
eventID = event.EventID
})
wrappedHandler := s.wrapHandler(func(_ http.ResponseWriter, r *http.Request) {
Ctx(r.Context()).Error("test error")
})
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
wrappedHandler.ServeHTTP(w, req)
s.EqualValues(w.Header().Get("X-Sentry-Id"), eventID)
}
func (s *TestLoggerSuite) TestLoggerWithInjectedExtraFields() {
s.logger = zap.New(NewSentryCoreWrapper(zapcore.NewNopCore(), sentry.CurrentHub()))
handlerFunc := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
Ctx(r.Context()).Error("test error")
})
wrappedHandler := WithExtraFields(func(r *http.Request) []zap.Field {
return []zap.Field{
zap.String("key", r.URL.String()),
}
})(handlerFunc)
wrappedHandler = RequestLogger(s.logger)(wrappedHandler)
called := false
s.sendEventMock.Do(func(event *sentry.Event) {
called = true
s.EqualValues("test error", event.Message)
s.EqualValues("http://example.com/foo", event.Extra["key"])
})
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
wrappedHandler.ServeHTTP(w, req)
s.True(called)
}
func (s *TestLoggerSuite) TestForkedLoggerShouldOnlyLogRelatedEvents() {
s.logger = zap.New(NewSentryCoreWrapper(zapcore.NewNopCore(), sentry.CurrentHub()))
called := false
s.sendEventMock.Do(func(event *sentry.Event) {
called = true
s.Require().Len(event.Breadcrumbs, 1)
s.EqualValues("debug breadcrumb", event.Breadcrumbs[0].Message)
s.EqualValues("error message to create event in sentry", event.Message)
})
s.logger.Debug("should not be sent")
forkedLogger := ForkedLogger(s.logger)
forkedLogger.Debug("debug breadcrumb")
forkedLogger.Error("error message to create event in sentry")
s.True(called)
}
func TestRequestLogger(t *testing.T) {
suite.Run(t, new(TestLoggerSuite))
}