forked from volatiletech/authboss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
66 lines (52 loc) · 1.29 KB
/
logger_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
package authboss
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
type (
testLogger struct {
info string
error string
}
testCtxLogger struct{}
)
func (t *testLogger) Info(s string) {
t.info += s
}
func (t *testLogger) Error(s string) {
t.error += s
}
func (t testLogger) FromContext(ctx context.Context) Logger { return testCtxLogger{} }
func (t testLogger) FromRequest(r *http.Request) Logger { return &testLogger{} }
func (t testCtxLogger) Info(string) {}
func (t testCtxLogger) Error(string) {}
func TestLogger(t *testing.T) {
t.Parallel()
ab := New()
logger := &testLogger{}
ab.Config.Core.Logger = logger
if logger != ab.Logger(nil).Logger.(*testLogger) {
t.Error("wanted our logger back")
}
if _, ok := ab.Logger(context.Background()).Logger.(testCtxLogger); !ok {
t.Error("wanted ctx logger back")
}
if _, ok := ab.RequestLogger(httptest.NewRequest("GET", "/", nil)).Logger.(*testLogger); !ok {
t.Error("wanted normal logger back")
}
}
func TestFmtLogger(t *testing.T) {
t.Parallel()
logger := &testLogger{}
fmtlog := FmtLogger{logger}
fmtlog.Errorf("%s %s", "ok", "go")
fmtlog.Infof("%s %s", "go", "ok")
if logger.error != "ok go" {
t.Error("wrong output", logger.error)
}
if logger.info != "go ok" {
t.Error("wrong output", logger.info)
}
}