-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmiddleware_wrapper_test.go
87 lines (67 loc) · 1.8 KB
/
middleware_wrapper_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
package gimlet
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMergeMiddleware(t *testing.T) {
t.Run("ValidatesInput", func(t *testing.T) {
assert.Panics(t, func() {
MergeMiddleware()
})
})
t.Run("ValidateCalling", func(t *testing.T) {
legacyCalls := 0
legacyFunc := func(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
legacyCalls++
h(w, r)
}
}
noop := func(w http.ResponseWriter, r *http.Request) {}
MergeMiddleware(WrapperMiddleware(legacyFunc), WrapperMiddleware(legacyFunc)).ServeHTTP(nil, nil, noop)
assert.Equal(t, 2, legacyCalls)
})
}
func TestMiddlewareFuncWrapper(t *testing.T) {
assert := assert.New(t)
legacyCalls := 0
legacyFunc := func(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
legacyCalls++
h(w, r)
}
}
nextCalls := 0
next := func(w http.ResponseWriter, r *http.Request) {
nextCalls++
}
wrapped := WrapperMiddleware(legacyFunc)
assert.Implements((*Middleware)(nil), wrapped)
assert.Equal(0, legacyCalls)
assert.Equal(0, nextCalls)
wrapped.ServeHTTP(nil, nil, next)
assert.Equal(1, legacyCalls)
assert.Equal(1, nextCalls)
}
func TestMiddlewareWrapper(t *testing.T) {
assert := assert.New(t)
legacyCalls := 0
legacyFunc := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
legacyCalls++
h.ServeHTTP(w, r)
})
}
nextCalls := 0
next := func(w http.ResponseWriter, r *http.Request) {
nextCalls++
}
wrapped := WrapperHandlerMiddleware(legacyFunc)
assert.Implements((*Middleware)(nil), wrapped)
assert.Equal(0, legacyCalls)
assert.Equal(0, nextCalls)
wrapped.ServeHTTP(nil, nil, next)
assert.Equal(1, legacyCalls)
assert.Equal(1, nextCalls)
}