-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
155 lines (131 loc) · 5.44 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
package slogctx
import (
"context"
"encoding/json"
"log/slog"
"slices"
"strings"
"testing"
"time"
"github.com/veqryn/slog-context/internal/test"
)
type logLine struct {
Source struct {
Function string `json:"function"`
File string `json:"file"`
Line int `json:"line"`
} `json:"source"`
}
func TestHandler(t *testing.T) {
t.Parallel()
tester := &test.Handler{}
h := NewHandler(tester, nil)
ctx := Prepend(nil, "prepend1", "arg1", slog.String("prepend1", "arg2"))
ctx = Prepend(ctx, "prepend2", "arg1", "prepend2", "arg2")
Prepend(ctx, "prepend3", "arg1", "prepend3", "arg2") // Ensure we aren't overwriting the parent context
ctx = Append(ctx, "append1", "arg1", "append1", "arg2")
ctx = Append(ctx, slog.String("append2", "arg1"), "append2", "arg2")
Append(ctx, "append3", "arg1", "append3", "arg2") // Ensure we aren't overwriting the parent context
Append(nil, "append4", "arg1", "badkey")
Append(ctx, int64(123))
l := slog.New(h)
l = l.With("with1", "arg1", "with1", "arg2").With()
l = l.WithGroup("group1").WithGroup("")
l = l.With("with2", "arg1", "with2", "arg2")
l.InfoContext(ctx, "main message", "main1", "arg1", "main1", "arg2")
expectedText := `time=2023-09-29T13:00:59.000Z level=INFO msg="main message" prepend1=arg1 prepend1=arg2 prepend2=arg1 prepend2=arg2 with1=arg1 with1=arg2 group1.with2=arg1 group1.with2=arg2 group1.main1=arg1 group1.main1=arg2 group1.append1=arg1 group1.append1=arg2 group1.append2=arg1 group1.append2=arg2
`
if s := tester.String(); s != expectedText {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedText, s)
}
b, err := tester.MarshalJSON()
if err != nil {
t.Fatal(err)
}
expectedJSON := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","prepend1":"arg1","prepend1":"arg2","prepend2":"arg1","prepend2":"arg2","with1":"arg1","with1":"arg2","group1":{"with2":"arg1","with2":"arg2","main1":"arg1","main1":"arg2","append1":"arg1","append1":"arg2","append2":"arg1","append2":"arg2"}}
`
if string(b) != expectedJSON {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedText, string(b))
}
// Check the source location fields
tester.Source = true
b, err = tester.MarshalJSON()
if err != nil {
t.Fatal(err)
}
var unmarshalled logLine
err = json.Unmarshal(b, &unmarshalled)
if err != nil {
t.Fatal(err)
}
if unmarshalled.Source.Function != "github.com/veqryn/slog-context.TestHandler" ||
!strings.HasSuffix(unmarshalled.Source.File, "slog-context/handler_test.go") ||
unmarshalled.Source.Line != 44 {
t.Errorf("Expected source fields are incorrect: %#+v\n", unmarshalled)
}
}
func TestHandlerMultipleAttrExtractor(t *testing.T) {
t.Parallel()
tester := &test.Handler{}
h := NewMiddleware(&HandlerOptions{
Prependers: []AttrExtractor{
ExtractPrepended,
func(ctx context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
if v, ok := ctx.Value(prependKey{}).([]slog.Attr); ok {
v = slices.Clone(v)
for i := 0; i < len(v); i++ {
v[i].Key += "^"
}
return v
}
return nil
},
func(_ context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
return []slog.Attr{}
},
},
Appenders: []AttrExtractor{
ExtractAppended,
func(ctx context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
if v, ok := ctx.Value(appendKey{}).([]slog.Attr); ok {
v = slices.Clone(v)
for i := 0; i < len(v); i++ {
v[i].Key += "*"
}
return v
}
return nil
},
func(_ context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr {
return nil
},
},
})(tester)
ctx := Prepend(nil, "prepend1", "arg1", slog.String("prepend1", "arg2"))
ctx = Prepend(ctx, "prepend2", "arg1", "prepend2", "arg2")
Prepend(ctx, "prepend3", "arg1", "prepend3", "arg2") // Ensure we aren't overwriting the parent context
ctx = Append(ctx, "append1", "arg1", "append1", "arg2")
ctx = Append(ctx, slog.String("append2", "arg1"), "append2", "arg2")
Append(ctx, "append3", "arg1", "append3", "arg2") // Ensure we aren't overwriting the parent context
Append(nil, "append4", "arg1", "badkey")
Append(ctx, int64(123))
l := slog.New(h)
l = l.With("with1", "arg1", "with1", "arg2")
l = l.WithGroup("group1")
l = l.With("with2", "arg1", "with2", "arg2")
l.InfoContext(ctx, "main message", "main1", "arg1", "main1", "arg2")
expectedText := `time=2023-09-29T13:00:59.000Z level=INFO msg="main message" prepend1=arg1 prepend1=arg2 prepend2=arg1 prepend2=arg2 prepend1^=arg1 prepend1^=arg2 prepend2^=arg1 prepend2^=arg2 with1=arg1 with1=arg2 group1.with2=arg1 group1.with2=arg2 group1.main1=arg1 group1.main1=arg2 group1.append1=arg1 group1.append1=arg2 group1.append2=arg1 group1.append2=arg2 group1.append1*=arg1 group1.append1*=arg2 group1.append2*=arg1 group1.append2*=arg2
`
if s := tester.String(); s != expectedText {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedText, s)
}
b, err := tester.MarshalJSON()
if err != nil {
t.Fatal(err)
}
expectedJSON := `{"time":"2023-09-29T13:00:59Z","level":"INFO","msg":"main message","prepend1":"arg1","prepend1":"arg2","prepend2":"arg1","prepend2":"arg2","prepend1^":"arg1","prepend1^":"arg2","prepend2^":"arg1","prepend2^":"arg2","with1":"arg1","with1":"arg2","group1":{"with2":"arg1","with2":"arg2","main1":"arg1","main1":"arg2","append1":"arg1","append1":"arg2","append2":"arg1","append2":"arg2","append1*":"arg1","append1*":"arg2","append2*":"arg1","append2*":"arg2"}}
`
if string(b) != expectedJSON {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedText, string(b))
}
}