-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_test.go
89 lines (71 loc) · 1.79 KB
/
context_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package zapmw
import (
"net/http"
"net/http/httptest"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
func withLogger(t testing.TB, e zapcore.LevelEnabler, opts []zap.Option, f func(*zap.Logger, *observer.ObservedLogs)) {
fac, logs := observer.New(e)
log := zap.New(fac, opts...)
f(log, logs)
}
type middleware func(next http.Handler) http.Handler
type mwStack struct {
middlewares []middleware
}
func newMws(mws ...middleware) mwStack {
return mwStack{append([]middleware(nil), mws...)}
}
func (m mwStack) then(h func(http.ResponseWriter, *http.Request)) http.Handler {
var f http.Handler
f = http.HandlerFunc(h)
for i := range m.middlewares {
f = m.middlewares[len(m.middlewares)-1-i](f)
}
return f
}
func TestWithZap(t *testing.T) {
logger, _ := zap.NewDevelopment()
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
mw := newMws(
WithZap(logger),
)
r := http.NewServeMux()
var l interface{}
r.Handle("/", mw.then(func(w http.ResponseWriter, r *http.Request) {
l = r.Context().Value(ZapKey)
w.Write([]byte("Hello World"))
}))
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatal("Response Code should be 200")
}
if l == nil {
t.Fatal("Logger in context should'nt be Nil")
}
if _, ok := l.(*zap.Logger); !ok {
t.Fatal("Logger in context should be *zap.Logger instance")
}
}
func TestGetZap(t *testing.T) {
logger, _ := zap.NewDevelopment()
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
mw := newMws(
WithZap(logger),
)
r := http.NewServeMux()
r.Handle("/", mw.then(func(w http.ResponseWriter, r *http.Request) {
req = r
w.Write([]byte("Hello World"))
}))
r.ServeHTTP(w, req)
logger = GetZap(req)
if logger == nil {
t.Fatal("Logger in context should'nt be Nil")
}
}