-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchain_test.go
150 lines (125 loc) · 3.59 KB
/
chain_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Apollo provides `net/context`-aware middleware chaining
package apollo
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"context"
"github.com/stretchr/testify/assert"
)
func TestDefaultNew(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
chain := New()
assert.Len(chain.constructors, 0)
assert.Equal(chain.context, context.Background())
})
}
func TestThenNoMiddleware(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
chain := New()
final := chain.Then(HandlerFunc(handlerOne))
assert.Implements((*http.Handler)(nil), final)
})
}
func TestThenFuncNoMiddleware(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
chain := New()
final := chain.ThenFunc(handlerOne)
assert.Implements((*http.Handler)(nil), final)
})
}
func TestThenNil(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
final := New().Then(nil)
assert.Implements((*http.Handler)(nil), final)
})
}
func TestThenFuncNil(t *testing.T) {
assert := assert.New(t)
assert.NotPanics(func() {
final := New().ThenFunc(nil)
assert.Implements((*http.Handler)(nil), final)
})
}
func TestAppend(t *testing.T) {
assert := assert.New(t)
chain := New(middleOne)
newChain := chain.Append(middleTwo)
assert.Len(chain.constructors, 1)
assert.Len(newChain.constructors, 2)
}
func TestAppendContext(t *testing.T) {
assert := assert.New(t)
chain := New(middleOne)
newChain := chain.Append(middleTwo)
assert.Equal(chain.context, context.Background())
assert.Equal(newChain.context, context.Background())
}
func TestAppendAfterWith(t *testing.T) {
assert := assert.New(t)
ctx := NewTestContext(context.Background(), 10)
chain := New(middleOne)
withChain := chain.With(ctx)
newChain := withChain.Append(middleTwo)
assert.Equal(chain.context, context.Background())
assert.Equal(withChain.context, ctx)
assert.Equal(newChain.context, ctx)
}
func TestWithAfterAppend(t *testing.T) {
assert := assert.New(t)
ctx := NewTestContext(context.Background(), 10)
chain := New(middleOne)
newChain := chain.Append(middleTwo)
withChain := newChain.With(ctx)
assert.Equal(chain.context, context.Background())
assert.Equal(newChain.context, context.Background())
assert.Equal(withChain.context, ctx)
}
func TestWithInPlace(t *testing.T) {
assert := assert.New(t)
ctx := NewTestContext(context.Background(), 10)
chain := New(middleOne)
chain.With(ctx)
newChain := chain.Append(middleTwo)
assert.Equal(chain.context, context.Background())
assert.Equal(newChain.context, context.Background())
}
func TestWithReassigned(t *testing.T) {
assert := assert.New(t)
ctx := NewTestContext(context.Background(), 10)
chain := New(middleOne)
chain = chain.With(ctx)
newChain := chain.Append(middleTwo)
assert.Equal(chain.context, ctx)
assert.Equal(newChain.context, ctx)
}
func TestWithMultiples(t *testing.T) {
assert := assert.New(t)
ctx := NewTestContext(context.Background(), 10)
chain := New(middleOne)
assert.Equal(chain.context, context.Background())
chain = chain.With(ctx)
assert.Equal(chain.context, ctx)
chain = chain.With(context.TODO()).With(ctx)
assert.Equal(chain.context, ctx)
}
func TestChains(t *testing.T) {
assert := assert.New(t)
ctx := NewTestContext(context.Background(), 10)
value, _ := FromContext(ctx)
assert.Equal(value, 10)
chain := New(middleOne, middleTwo).With(ctx).ThenFunc(handlerContext)
ts := httptest.NewServer(chain)
defer ts.Close()
res, err := http.Get(ts.URL)
assert.NoError(err)
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
assert.Equal(res.StatusCode, 200)
assert.Equal(string(body), "m1\nm2\n10\n")
}