-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththird_party_libraries_test.go
More file actions
80 lines (65 loc) · 1.65 KB
/
third_party_libraries_test.go
File metadata and controls
80 lines (65 loc) · 1.65 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
package logfusc
import (
"bytes"
"fmt"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)
func Test_jsoniter(t *testing.T) {
t.Parallel()
type foo struct {
Secret Secret[string] `json:"secret"`
}
value := "bar"
container := foo{Secret: NewSecret(value)}
b, err := jsoniter.Marshal(container)
assert.NoError(t, err)
assert.JSONEq(t, fmt.Sprintf("{%q: %q}", "secret", fmt.Sprintf(redactionTmpl, value)), string(b))
}
func Test_zerolog(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
logger := zerolog.New(&buf)
value := "bar"
secret := NewSecret(value)
logger.Print(secret)
logged := buf.String()
assert.Contains(t, logged, fmt.Sprintf(redactionTmpl, value))
assert.NotContains(t, logged, value)
}
func Test_zap(t *testing.T) {
t.Parallel()
var buf zaptest.Buffer
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
&buf,
zapcore.DebugLevel,
)
logger := zap.New(core)
sugar := logger.Sugar()
value := "bar"
secret := NewSecret(value)
sugar.Infow("sugar", secret)
_ = logger.Sync()
logged := buf.String()
assert.Contains(t, logged, fmt.Sprintf(redactionTmpl, value))
assert.NotContains(t, logged, value)
}
func Test_logrus(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
logger := logrus.New()
logger.SetOutput(&buf)
value := "bar"
secret := NewSecret(value)
logger.WithField("secret", secret).Info("")
logged := buf.String()
assert.Contains(t, logged, fmt.Sprintf(redactionTmpl, value))
assert.NotContains(t, logged, value)
}