-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_options_test.go
79 lines (64 loc) · 2.25 KB
/
middleware_options_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
package chevron
import (
"fmt"
"github.com/aphistic/sweet"
. "github.com/onsi/gomega"
)
type MiddlewareOptionsSuite struct{}
func (s *MiddlewareOptionsSuite) TestWithMiddleware(t sweet.T) {
var (
hm = makeTestHandlerMap()
numCalls = 0
)
middleware := MiddlewareFunc(func(h Handler) (Handler, error) {
numCalls++
return makeEmptyHandler(106), nil
})
// Apply the middleware config
Expect(WithMiddleware(middleware)(hm)).To(BeNil())
Expect(numCalls).To(Equal(6))
Expect(hm[MethodGet](nil, nil, nil).StatusCode()).To(Equal(106))
Expect(hm[MethodOptions](nil, nil, nil).StatusCode()).To(Equal(106))
Expect(hm[MethodPost](nil, nil, nil).StatusCode()).To(Equal(106))
Expect(hm[MethodPut](nil, nil, nil).StatusCode()).To(Equal(106))
Expect(hm[MethodPatch](nil, nil, nil).StatusCode()).To(Equal(106))
Expect(hm[MethodDelete](nil, nil, nil).StatusCode()).To(Equal(106))
}
func (s *MiddlewareOptionsSuite) TestWithMiddlewareError(t sweet.T) {
middleware := MiddlewareFunc(func(h Handler) (Handler, error) {
return nil, fmt.Errorf("utoh")
})
// Apply the middleware config
Expect(WithMiddleware(middleware)(makeTestHandlerMap())).To(MatchError("utoh"))
}
func (s *MiddlewareOptionsSuite) TestWithMiddlewareFor(t sweet.T) {
var (
hm = makeTestHandlerMap()
numCalls = 0
)
middleware := MiddlewareFunc(func(h Handler) (Handler, error) {
numCalls++
return makeEmptyHandler(106), nil
})
// Apply the middleware config
Expect(WithMiddlewareFor(middleware, MethodGet, MethodPatch)(hm)).To(BeNil())
Expect(numCalls).To(Equal(2))
Expect(hm[MethodGet](nil, nil, nil).StatusCode()).To(Equal(106))
Expect(hm[MethodOptions](nil, nil, nil).StatusCode()).To(Equal(101))
Expect(hm[MethodPost](nil, nil, nil).StatusCode()).To(Equal(102))
Expect(hm[MethodPut](nil, nil, nil).StatusCode()).To(Equal(103))
Expect(hm[MethodPatch](nil, nil, nil).StatusCode()).To(Equal(106))
Expect(hm[MethodDelete](nil, nil, nil).StatusCode()).To(Equal(105))
}
//
//
func makeTestHandlerMap() handlerMap {
return handlerMap{
MethodGet: makeEmptyHandler(100),
MethodOptions: makeEmptyHandler(101),
MethodPost: makeEmptyHandler(102),
MethodPut: makeEmptyHandler(103),
MethodPatch: makeEmptyHandler(104),
MethodDelete: makeEmptyHandler(105),
}
}