-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
247 lines (201 loc) · 6.53 KB
/
handler_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
package ctxslog_test
import (
"context"
"log/slog"
"os"
"testing"
"github.com/felttrip/ctxslog"
)
func TestNewHandler(t *testing.T) {
mockHandler := &MockHandler{}
handler := ctxslog.NewHandler(mockHandler)
if handler == nil {
t.Error("NewHandler returned nil")
}
}
func TestHandlerEnabled(t *testing.T) {
mockHandler := &MockHandler{}
handler := ctxslog.NewHandler(mockHandler)
ctx := context.Background()
// Test with different log levels should just default down to the mock handler which is true for all
levels := []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError}
for _, level := range levels {
enabled := handler.Enabled(ctx, level)
if !enabled {
t.Errorf("Handler.Enabled returned false for level %s", level)
}
}
}
func TestHandlerHandle(t *testing.T) {
t.Run("Will Handle when nothing has been added to context", func(t *testing.T) {
mockHandler := &MockHandler{}
handler := ctxslog.NewHandler(mockHandler)
ctx := context.Background()
record := slog.Record{
Level: slog.LevelInfo,
Message: "Test message",
}
err := handler.Handle(ctx, record)
if err != nil {
t.Errorf("Handler.Handle returned an error: %v", err)
}
if !mockHandler.HandleCalled {
t.Error("MockHandler.Handle was not called")
}
if mockHandler.HandleRecord.Message != record.Message || mockHandler.HandleRecord.Level != record.Level {
t.Error("MockHandler.Handle did not receive the correct record")
}
})
t.Run("Will Handle when values have been added to context and add them to attrs", func(t *testing.T) {
mockHandler := &MockHandler{}
handler := ctxslog.NewHandler(mockHandler)
ctx := context.Background()
ctx = ctxslog.WithValue(ctx, "key1", "value1")
ctx = ctxslog.WithValue(ctx, "key2", "value2")
expectedAttrs := []slog.Attr{
{Key: "key1", Value: slog.StringValue("value1")},
{Key: "key2", Value: slog.StringValue("value2")},
}
record := slog.Record{
Level: slog.LevelInfo,
Message: "Test message",
}
err := handler.Handle(ctx, record)
if err != nil {
t.Errorf("Handler.Handle returned an error: %v", err)
}
if !mockHandler.HandleCalled {
t.Error("MockHandler.Handle was not called")
}
if mockHandler.HandleRecord.Message != record.Message || mockHandler.HandleRecord.Level != record.Level {
t.Error("MockHandler.Handle did not receive the correct record")
}
if mockHandler.HandleRecord.NumAttrs() != len(expectedAttrs) {
t.Error("MockHandler.WithAttrs did not receive the correct number of attributes")
}
})
}
func TestHandlerWithAttrs(t *testing.T) {
mockHandler := &MockHandler{}
handler := ctxslog.NewHandler(mockHandler)
attrs := []slog.Attr{
{Key: "attr1", Value: slog.StringValue("value1")},
{Key: "attr2", Value: slog.StringValue("value2")},
}
newHandler := handler.WithAttrs(attrs)
if newHandler == nil {
t.Error("Handler.WithAttrs returned nil")
}
if len(mockHandler.WithAttrsAttrs) != len(attrs) {
t.Error("MockHandler.WithAttrs did not receive the correct number of attributes")
}
for i, attr := range attrs {
if !mockHandler.WithAttrsAttrs[i].Equal(attr) {
t.Errorf("MockHandler.WithAttrs did not receive the correct attribute at index %d", i)
}
}
}
func TestHandlerWithGroup(t *testing.T) {
mockHandler := &MockHandler{WithGroupCalled: false}
handler := ctxslog.NewHandler(mockHandler)
groupName := "test-group"
newHandler := handler.WithGroup(groupName)
if newHandler == nil {
t.Error("Handler.WithGroup returned nil")
}
if mockHandler.WithGroupCalled != true {
t.Error("MockHandler.WithGroup was not called")
}
if mockHandler.WithGroupName != groupName {
t.Error("MockHandler.WithGroup did not receive the correct group name")
}
}
// Examples
func ExampleNewHandler() {
slog.SetDefault(slog.New(ctxslog.NewHandler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}))))
ctx := ctxslog.WithValue(context.Background(), "AccountID", 123456789)
slog.InfoContext(ctx, "Info With Context")
// Output:
//{"level":"INFO","msg":"Info With Context","AccountID":123456789}
}
func ExampleWithValue() {
slog.SetDefault(slog.New(ctxslog.NewHandler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}))))
ctx := ctxslog.WithValue(context.Background(), "AccountID", 123456789)
ctx = ctxslog.WithValue(ctx, "email", "noone@felttrip.com")
ctx = ctxslog.WithValue(ctx, "sender", "greg@BailysInAShoe.lake")
slog.InfoContext(ctx, "Info With Context")
// Output:
//{"level":"INFO","msg":"Info With Context","AccountID":123456789,"email":"noone@felttrip.com","sender":"greg@BailysInAShoe.lake"}
}
func ExampleWithValues() {
slog.SetDefault(slog.New(ctxslog.NewHandler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
}))))
ctx := ctxslog.WithValues(context.Background(), map[string]interface{}{
"AccountID": 987654321,
"email": "bob@TheBuilder.fake",
"complexData": ComplexData{
IntField: 123,
StrField: "DEADBEEF",
BoolField: true,
SliceField: []string{"one", "two", "three"},
},
})
slog.ErrorContext(ctx, "Error With Context")
// Output:
//{"level":"INFO","msg":"Info With Context","AccountID":123456789,"email":"noone@felttrip.com","sender":"greg@BailysInAShoe.lake"}
}
// ComplexData is a struct used in the examples.
type ComplexData struct {
IntField int
StrField string
BoolField bool
SliceField []string
}
// MockHandler is a mock implementation of slog.Handler for testing purposes.
type MockHandler struct {
HandleCalled bool
HandleRecord slog.Record
WithAttrsAttrs []slog.Attr
WithGroupCalled bool
WithGroupName string
}
func (m *MockHandler) Enabled(ctx context.Context, lvl slog.Level) bool {
return true
}
func (m *MockHandler) Handle(ctx context.Context, r slog.Record) error {
m.HandleCalled = true
m.HandleRecord = r
return nil
}
func (m *MockHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
m.WithAttrsAttrs = attrs
return m
}
func (m *MockHandler) WithGroup(name string) slog.Handler {
m.WithGroupCalled = true
m.WithGroupName = name
return m
}