-
Notifications
You must be signed in to change notification settings - Fork 14
/
test_util.go
48 lines (40 loc) · 1.14 KB
/
test_util.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
package testutil
import (
"context"
"net/http"
"github.com/anz-bank/pkg/log"
"github.com/anz-bank/sysl-go/logconfig"
)
type TestHook struct {
Entries []log.LogEntry
}
func (t *TestHook) OnLogged(entry *log.LogEntry) error {
t.Entries = append(t.Entries, *entry)
return nil
}
func (t *TestHook) LastEntry() *log.LogEntry {
i := len(t.Entries) - 1
if i < 0 {
return nil
}
return &t.Entries[i]
}
func NewTestContextWithLoggerHook() (context.Context, *TestHook) {
hook := &TestHook{}
return TestContextWithLoggerHook(context.Background(), hook), hook
}
func TestContextWithLoggerHook(ctx context.Context, hook *TestHook) context.Context {
ctx = logconfig.SetVerboseLogging(ctx, true)
ctx = log.WithConfigs(log.SetVerboseMode(true)).Onto(ctx)
ctx = log.WithConfigs(log.AddHooks(hook)).Onto(ctx)
return ctx
}
func LoggerHookContextMiddleware() (func(next http.Handler) http.Handler, *TestHook) {
hook := &TestHook{}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(TestContextWithLoggerHook(r.Context(), hook))
next.ServeHTTP(w, r)
})
}, hook
}