-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcliware_test.go
339 lines (312 loc) · 8.43 KB
/
cliware_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cliware_test
import (
"context"
"errors"
"net/http"
"reflect"
"testing"
c "github.com/delicb/cliware"
)
func TestRequestHandlerFunc(t *testing.T) {
var called bool
handlerFunc := func(req *http.Request) (resp *http.Response, err error) {
called = true
return nil, nil
}
var handler c.Handler = c.HandlerFunc(handlerFunc)
_, err := handler.Handle(nil)
if err != nil {
t.Error("Handle returned error: ", err)
}
if !called {
t.Error("Expected request handler function to be called.")
}
}
func TestMiddlewareFunc(t *testing.T) {
var called bool
middlewareFunc := func(next c.Handler) c.Handler {
called = true
return nil
}
var middleware c.Middleware = c.MiddlewareFunc(middlewareFunc)
middleware.Exec(nil)
if !called {
t.Error("Expected middleware func to be called.")
}
}
func TestChainCreation(t *testing.T) {
m1, _ := createMiddleware()
m2, _ := createMiddleware()
chain := c.NewChain(m1, m2)
if len(chain.Middlewares()) != 2 {
t.Error("Expected 2 middlewares in chain, found: ", len(chain.Middlewares()))
}
}
func TestMiddlewareUse(t *testing.T) {
m1, _ := createMiddleware()
m2, _ := createMiddleware()
chain := c.NewChain()
chain.Use(m1)
chain.Use(m2)
if len(chain.Middlewares()) != 2 {
t.Error("Expected 2 middlewares in chain, found: ", len(chain.Middlewares()))
}
}
func TestMiddlewareUseMultiple(t *testing.T) {
m1, _ := createMiddleware()
m2, _ := createMiddleware()
chain := c.NewChain()
chain.Use(m1, m2)
if len(chain.Middlewares()) != 2 {
t.Error("Expected 2 middlewares in chain, found: ", len(chain.Middlewares()))
}
}
func TestMiddlewareUseFunc(t *testing.T) {
chain := c.NewChain()
chain.UseFunc(func(next c.Handler) c.Handler {
return nil
})
if len(chain.Middlewares()) != 1 {
t.Error("Expected 1 middleware in chain, found: ", len(chain.Middlewares()))
}
}
func TestUseRequest(t *testing.T) {
chain := c.NewChain()
var called bool
var validRequest bool
templateReq, _ := http.NewRequest("GET", "http://localhost", nil)
chain.UseRequest(func(req *http.Request) error {
called = true
validRequest = req == templateReq
return nil
})
handler, handlerCalled := createHandler()
_, err := chain.Exec(handler).Handle(templateReq)
if err != nil {
t.Error("Handle returned error: ", err)
}
if !called {
t.Error("Request middleware not called.")
}
if !*handlerCalled {
t.Error("Final handler not called.")
}
if !validRequest {
t.Error("Request handler did not receive expected request.")
}
}
func TestUseResponse(t *testing.T) {
chain := c.NewChain()
var called bool
chain.UseResponse(func(resp *http.Response, err error) error {
called = true
return nil
})
handler, handlerCalled := createHandler()
_, err := chain.Exec(handler).Handle(nil)
if err != nil {
t.Error("Handle returned error: ", err)
}
if !called {
t.Error("Response middleware not called.")
}
if !*handlerCalled {
t.Error("Final handler not called.")
}
}
func TestMiddlewareCalled(t *testing.T) {
m1, m1Called := createMiddleware()
m2, m2Called := createMiddleware()
handler, handlerCalled := createHandler()
chain := c.NewChain(m1, m2)
_, err := chain.Exec(handler).Handle(nil)
if err != nil {
t.Error("Handle returned error: ", err)
}
if !*m1Called {
t.Error("m1 middleware not called.")
}
if !*m2Called {
t.Error("m2 middleware not called.")
}
if !*handlerCalled {
t.Error("Final handler not called.")
}
}
func TestMiddlewareCalledWithParent(t *testing.T) {
m1, m1Called := createMiddleware()
m2, m2Called := createMiddleware()
handler, handlerCalled := createHandler()
chain := c.NewChain(m1)
childChain := chain.ChildChain(m2)
_, err := childChain.Exec(handler).Handle(nil)
if err != nil {
t.Error("Handle returned error: ", err)
}
if !*m1Called {
t.Error("m1 middleware not called.")
}
if !*m2Called {
t.Error("m2 middleware not called.")
}
if !*handlerCalled {
t.Error("Final handler not called.")
}
}
func TestGetParent(t *testing.T) {
chain := c.NewChain()
childChain := chain.ChildChain()
if childChain.Parent() != chain {
t.Error("Parent middleware not set properly.")
}
}
func TestRequestProcessorNoError(t *testing.T) {
var processorCalled bool
processor := c.RequestProcessor(func(req *http.Request) error {
processorCalled = true
return nil
})
chain := c.NewChain(processor)
handler, handlerCalled := createHandler()
_, err := chain.Exec(handler).Handle(nil)
if err != nil {
t.Error("Handle returned error: ", err)
}
if !processorCalled {
t.Error("Request processor not called.")
}
if !*handlerCalled {
t.Error("Handler was not called.")
}
}
func TestRequestProcessorWithError(t *testing.T) {
var processorCalled bool
myErr := errors.New("custom error")
processor := c.RequestProcessor(func(req *http.Request) error {
processorCalled = true
return myErr
})
chain := c.NewChain(processor)
handler, handlerCalled := createHandler()
_, err := chain.Exec(handler).Handle(nil)
if err != myErr {
t.Errorf("Expected error: \"%s\", got: \"%s\"", myErr, err)
}
if !processorCalled {
t.Error("Request processor not called.")
}
if *handlerCalled {
t.Error("Handler called even when middleware returned error.")
}
}
func TestResponseProcessorNoError(t *testing.T) {
var processorCalled bool
processor := c.ResponseProcessor(func(resp *http.Response, err error) error {
processorCalled = true
return nil
})
chain := c.NewChain(processor)
handler, handlerCalled := createHandler()
_, err := chain.Exec(handler).Handle(nil)
if err != nil {
t.Error("Handle returned error: ", err)
}
if !processorCalled {
t.Error("Response processor not called.")
}
if !*handlerCalled {
t.Error("Handler was not called.")
}
}
func TestResponseProcessorWithError(t *testing.T) {
var processorCalled bool
myErr := errors.New("custom error")
processor := c.ResponseProcessor(func(resp *http.Response, err error) error {
processorCalled = true
return myErr
})
chain := c.NewChain(processor)
handler, handlerCalled := createHandler()
_, err := chain.Exec(handler).Handle(nil)
if err != myErr {
t.Errorf("Expected error: \"%s\", got: \"%s\"", myErr, err)
}
if !processorCalled {
t.Error("Response processor not called.")
}
if !*handlerCalled {
t.Error("Handler not called.")
}
}
func TestContextProcessor_Exec(t *testing.T) {
var processorCalled bool
processor := c.ContextProcessor(func(ctx context.Context) context.Context {
processorCalled = true
return ctx
})
chain := c.NewChain(processor)
handler, handlerCalled := createHandler()
_, err := chain.Exec(handler).Handle(c.EmptyRequest())
if err != nil {
t.Error("Handle returned error: ", err)
}
if !processorCalled {
t.Error("Context processor not called.")
}
if !*handlerCalled {
t.Error("Handler was not called.")
}
}
func TestCopy(t *testing.T) {
processor := c.RequestProcessor(func(req *http.Request) error {
return nil
})
originalProcessor := reflect.ValueOf(processor)
chain := c.NewChain(processor).Copy()
if len(chain.Middlewares()) != 1 {
t.Fatal("Wrong number of middlewares in copied chain.")
}
copiedProcessor := reflect.ValueOf(chain.Middlewares()[0])
if originalProcessor != copiedProcessor {
t.Error("Got wrong middleware in copied chain.")
}
}
func TestEmptyRequest(t *testing.T) {
req := c.EmptyRequest()
if req.Method != "GET" {
t.Errorf("Empty request method wrong. Got: %s, expected: GET", req.Method)
}
if req.URL.Host != "" || req.URL.Scheme != "" || req.URL.Path != "" {
t.Errorf("Empty request URL wrong. Got: %s, expected: <empty>", req.URL)
}
if req.Host != "" {
t.Errorf("Empty request host wrong. Got %s, expected: <empty>", req.Host)
}
if req.ProtoMajor != 1 {
t.Errorf("Empty request ProtoMajor wrong. Got: %d, expected: 1", req.ProtoMajor)
}
if req.ProtoMinor != 1 {
t.Errorf("Empty request ProtoMinor wrong. Got: %d, expected: 1", req.ProtoMajor)
}
if req.Proto != "HTTP/1.1" {
t.Errorf("Empty request Proto wrong. Got: %s, expected HTTP/1.1", req.Proto)
}
}
func createMiddleware() (middleware c.Middleware, called *bool) {
var middlewareCalled bool
middleware = c.MiddlewareFunc(func(next c.Handler) c.Handler {
middlewareCalled = true
return c.HandlerFunc(func(req *http.Request) (resp *http.Response, err error) {
return next.Handle(req)
})
})
return middleware, &middlewareCalled
}
func createHandler() (handler c.Handler, called *bool) {
var handlerCalled bool
handler = c.HandlerFunc(func(req *http.Request) (resp *http.Response, err error) {
handlerCalled = true
return nil, nil
})
return handler, &handlerCalled
}