-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
130 lines (105 loc) · 3.16 KB
/
router_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
package chevron
import (
"context"
"net/http"
"net/http/httptest"
"github.com/aphistic/sweet"
"github.com/efritz/response"
"github.com/go-nacelle/nacelle"
. "github.com/onsi/gomega"
)
type RouterSuite struct{}
func (s *RouterSuite) TestRegisterAddsToMux(t sweet.T) {
var (
container = nacelle.NewServiceContainer()
logger = nacelle.NewNilLogger()
router = NewRouter(container, logger)
)
// Register resource
Expect(router.Register("/foo", &SimpleGetSpec{})).To(BeNil())
// Matched route
req, _ := http.NewRequest("GET", "/foo", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)
Expect(recorder.Code).To(Equal(http.StatusNoContent))
// Unmatched route
req, _ = http.NewRequest("GET", "/bar", nil)
recorder = httptest.NewRecorder()
router.ServeHTTP(recorder, req)
Expect(recorder.Code).To(Equal(http.StatusNotFound))
}
func (s *RouterSuite) TestRegisterInjectsServices(t sweet.T) {
var (
container = nacelle.NewServiceContainer()
logger = nacelle.NewNilLogger()
router = NewRouter(container, logger)
)
for _, val := range []string{"a", "b", "c"} {
container.MustSet(val, val)
}
spec := &ServiceSpec{}
Expect(router.Register("/users", spec)).To(BeNil())
Expect(spec.A).To(Equal("a"))
Expect(spec.B).To(Equal("b"))
Expect(spec.C).To(Equal("c"))
}
func (s *RouterSuite) TestRegisterDuplicateURL(t sweet.T) {
var (
container = nacelle.NewServiceContainer()
logger = nacelle.NewNilLogger()
router = NewRouter(container, logger)
)
err1 := router.Register("/users", &EmptySpec{})
err2 := router.Register("/users", &EmptySpec{})
Expect(err1).To(BeNil())
Expect(err2).NotTo(BeNil())
Expect(err2.Error()).To(ContainSubstring("resource already registered"))
}
func (s *RouterSuite) TestRegisterWithMiddleware(t sweet.T) {
var (
container = nacelle.NewServiceContainer()
logger = nacelle.NewNilLogger()
router = NewRouter(container, logger)
calls = []string{}
)
middlewareFactory := func(name string) Middleware {
return MiddlewareFunc(func(h Handler) (Handler, error) {
handler := func(ctx context.Context, r *http.Request, logger nacelle.Logger) response.Response {
calls = append(calls, name)
return h(ctx, r, logger)
}
return handler, nil
})
}
// Add global middleware
router.AddMiddleware(middlewareFactory("a"))
router.AddMiddleware(middlewareFactory("b"))
router.AddMiddleware(middlewareFactory("c"))
err := router.Register(
"/foo",
&SimpleGetSpec{},
WithMiddleware(middlewareFactory("d")),
WithMiddlewareFor(middlewareFactory("e"), MethodGet),
WithMiddleware(middlewareFactory("f")),
)
Expect(err).To(BeNil())
req, _ := http.NewRequest("GET", "/foo", nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)
Expect(recorder.Code).To(Equal(http.StatusNoContent))
Expect(calls).To(Equal([]string{"a", "b", "c", "d", "e", "f"}))
}
//
//
type ServiceSpec struct {
*EmptySpec
A string `service:"a"`
B string `service:"b"`
C string `service:"c"`
}
type SimpleGetSpec struct {
*EmptySpec
}
func (s *SimpleGetSpec) Get(ctx context.Context, r *http.Request, logger nacelle.Logger) response.Response {
return response.Empty(http.StatusNoContent)
}