-
Notifications
You must be signed in to change notification settings - Fork 70
/
logging_test.go
45 lines (39 loc) · 1.21 KB
/
logging_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
package ginoauth2
import (
"bytes"
"fmt"
"strings"
"testing"
)
type mockLogger struct{ buffer bytes.Buffer }
func (m *mockLogger) Errorf(format string, args ...interface{}) {
m.buffer.WriteString(fmt.Sprintf("ERROR: "+format, args...))
}
func (m *mockLogger) Infof(format string, args ...interface{}) {
m.buffer.WriteString(fmt.Sprintf("INFO: "+format, args...))
}
func (m *mockLogger) Debugf(format string, args ...interface{}) {
m.buffer.WriteString(fmt.Sprintf("DEBUG: "+format, args...))
}
func TestLogWithMaskedAccessToken(t *testing.T) {
mockLog := &mockLogger{}
DefaultLogger = mockLog
tests := []struct{ name, input, expected string }{
{"With access token", "&access_token=abcdefghijklmnop&", "INFO: <MASK>&"},
{"Without access token", "no_token_here", "INFO: no_token_here"},
{"Empty string", "", "INFO: "},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockLog.buffer.Reset()
infof("%s", tt.input)
logOutput := mockLog.buffer.String()
if logOutput != tt.expected {
t.Errorf("Expected log to contain %q, got %q", tt.expected, logOutput)
}
if strings.Contains(logOutput, "abcdefghijklmnop") {
t.Errorf("Log should not contain the original token")
}
})
}
}