-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslog_test.go
69 lines (57 loc) · 1.62 KB
/
slog_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
package gopii
import (
"log/slog"
"strconv"
"strings"
"testing"
"github.com/matryer/is"
)
func TestSlogReplaceAttr_LogValue(t *testing.T) {
obscureFuncs := map[PartType]map[Level]ObscureFunc{
typePlain: {
levelMax: Plain(),
},
typeLogin: {
levelDevelopment: Plain(), // user123456789 -> user123456789
levelLog: KeepFirstLast(4, 3), // user123456789 -> user***789
levelAuditLog: KeepFirst(3), // user123456789 -> use***
levelInternet: KeepFirst(1), // user123456789 -> u***
},
typePassword: {
levelDevelopment: Plain(), // pass123456789 -> pass123456789
},
}
str := NewString(obscureFuncs,
"user123456789", typeLogin,
":", typePlain,
"pass123456789", typePassword,
)
tests := []struct {
outLevel Level
want string
}{
{0, "user123456789:pass123456789"},
{levelDevelopment, "user123456789:pass123456789"},
{levelLog - 2, "user***789:***"},
{levelLog, "user***789:***"},
{levelAuditLog - 2, "use***:***"},
{levelAuditLog, "use***:***"},
{levelInternet - 2, "u***:***"},
{levelInternet, "u***:***"},
{100, "***:***"},
}
for _, test := range tests {
t.Run("level "+strconv.Itoa(int(test.outLevel)), func(t *testing.T) {
is := is.New(t)
buf := strings.Builder{}
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelInfo,
ReplaceAttr: SlogReplaceAttr(test.outLevel),
})
logger := slog.New(handler)
logger.Info("msg", slog.Any("s", str), slog.String("x", "y"))
is.True(strings.Contains(buf.String(), "s="+test.want))
is.True(strings.Contains(buf.String(), "x=y"))
})
}
}