-
Notifications
You must be signed in to change notification settings - Fork 43
/
interceptor_test.go
117 lines (89 loc) · 2.06 KB
/
interceptor_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package neo
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestBefore(t *testing.T) {
assert.True(t, true)
}
func testMiddleware(ctx *Ctx, next Next) {
counter := ctx.Data.Get("counter").(int)
counter = counter + 1
ctx.Data.Set("counter", counter)
next()
}
func testMiddlewareWithDownstream(ctx *Ctx, next Next) {
counter := ctx.Data.Get("counter-downstream").(int)
counter = counter + 1
ctx.Data.Set("counter-downstream", counter)
next()
counter = counter + 1
ctx.Data.Set("counter-downstream", counter)
}
func testMiddlewareWithoutDownstream(ctx *Ctx, next Next) {
counter := ctx.Data.Get("counter").(int)
counter = counter + 1
ctx.Data.Set("counter", counter)
}
func TestCompose(t *testing.T) {
var mdw Middleware = testMiddleware
middlewares := []appliable{
mdw,
mdw,
mdw,
mdw,
}
fn := compose(middlewares)
assert.NotNil(t, fn)
ctx := &Ctx{
Data: CtxData{},
}
ctx.Data.Set("counter", 0)
fn(ctx)
counter := ctx.Data.Get("counter").(int)
assert.Exactly(t, 4, counter)
}
func TestComposeWithDownstream(t *testing.T) {
var downstream Middleware = testMiddlewareWithDownstream
var normalMdw Middleware = testMiddleware
middlewares := []appliable{
normalMdw,
downstream,
normalMdw,
normalMdw,
}
fn := compose(middlewares)
assert.NotNil(t, fn)
ctx := &Ctx{
Data: CtxData{},
}
ctx.Data.Set("counter", 0)
ctx.Data.Set("counter-downstream", 0)
fn(ctx)
counter := ctx.Data.Get("counter-downstream").(int)
assert.Exactly(t, 2, counter)
}
func TestComposeWithoutDownstream(t *testing.T) {
var noDownstream Middleware = testMiddlewareWithoutDownstream
middlewares := []appliable{
noDownstream,
noDownstream,
noDownstream,
noDownstream,
}
fn := compose(middlewares)
assert.NotNil(t, fn)
ctx := &Ctx{
Data: CtxData{},
}
ctx.Data.Set("counter", 0)
fn(ctx)
assert.Exactly(t, 1, ctx.Data.Get("counter").(int))
}
func TestInterceptorUse(t *testing.T) {
i := &interceptor{[]appliable{}}
i.Use(testMiddleware)
i.Use(testMiddleware)
i.Use(testMiddleware)
assert.Exactly(t, 3, len(i.middlewares))
}