-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
executable file
·197 lines (160 loc) · 4.94 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
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
package scaffold
import (
"errors"
"fmt"
"net/http"
"testing"
"golang.org/x/net/context"
. "github.com/smartystreets/goconvey/convey"
)
func TestRouter(t *testing.T) {
Convey("Given a router", t, func() {
h := NewMockHandler()
d := NewMockDispatcher()
r := New(d)
methods := map[string]func(pattern string, handlers ...interface{}) *Router{
"OPTIONS": r.Options,
"GET": r.Get,
"HEAD": r.Head,
"POST": r.Post,
"PUT": r.Put,
"DELETE": r.Delete,
"": r.Handle,
}
Convey("When a handler function is added", func() {
r = r.Handle("somepath", h.CtxServeHTTP)
Convey("Then handler should be added", func() {
So(d.HandlerHasBeenCalled, ShouldBeTrue)
So(d.Route, ShouldNotBeNil)
})
})
Convey("When a http handler is added", func() {
r = r.Handle("somepath", http.NotFoundHandler())
Convey("Then handler should be added", func() {
So(d.HandlerHasBeenCalled, ShouldBeTrue)
So(d.Route, ShouldNotBeNil)
})
})
Convey("When a http handler function is added", func() {
r = r.Handle("somepath", http.NotFound)
Convey("Then handler should be added", func() {
So(d.HandlerHasBeenCalled, ShouldBeTrue)
So(d.Route, ShouldNotBeNil)
})
})
for method, handler := range methods {
when := fmt.Sprintf("When a route with method `%s` is added", method)
Convey(when, func() {
r = handler("somepath", h)
then := fmt.Sprintf("Then the route method should be `%s`", method)
Convey(then, func() {
So(d.HandlerHasBeenCalled, ShouldBeTrue)
So(d.Route, ShouldNotBeNil)
So(d.Route.Method, ShouldEqual, method)
})
Convey("Then the route pattern should be correct", func() {
So(d.HandlerHasBeenCalled, ShouldBeTrue)
So(d.Route, ShouldNotBeNil)
So(d.Route.Pattern, ShouldEqual, "somepath")
})
})
}
Convey("When a host is set", func() {
r = r.Host("example.com", "www.example.com")
Convey("Then the route should contain the hosts", func() {
So(r.route.Hosts, ShouldContain, "example.com")
So(r.route.Hosts, ShouldContain, "www.example.com")
})
})
Convey("When a route pattern is set", func() {
r = r.Route("somepath")
Convey("Then the route pattern should be correct", func() {
So(r.route.Pattern, ShouldEqual, "somepath")
})
})
Convey("When a group is used", func() {
r.Group("somepath", func(r *Router) {
Convey("Then the route pattern should be correct", func() {
So(r.route.Pattern, ShouldEqual, "somepath")
})
})
})
Convey("When a plaform is added", func() {
platform := NewMockPlatform()
r.Platform("somepath", platform)
Convey("Then the route pattern should be correct", func() {
So(platform.Route, ShouldNotBeNil)
So(platform.Route.Pattern, ShouldEqual, "somepath")
})
})
Convey("When middleware is added", func() {
m := NewMockMiddleware()
middleware := m.Middleware()
r.Use(middleware)
Convey("Then the middleware should be added", func() {
So(d.MiddlewareHasBeenCalled, ShouldBeTrue)
So(d.MiddlewareItems, ShouldHaveLength, 1)
})
})
Convey("When http middlware is added", func() {
middleware := func(next http.Handler) http.Handler {
return next
}
r.Use(middleware)
Convey("Then the middleware should be added", func() {
So(d.MiddlewareHasBeenCalled, ShouldBeTrue)
So(d.MiddlewareItems, ShouldHaveLength, 1)
})
})
Convey("When middleware function is added", func() {
m := NewMockMiddleware()
middleware := (func(Handler) Handler)(m.Middleware())
r.Use(middleware)
Convey("Then the middleware should be added", func() {
So(d.MiddlewareHasBeenCalled, ShouldBeTrue)
})
})
Convey("When a not found handler is added", func() {
h := NewMockHandler()
r.NotFound(h)
Convey("Then the not found handler should be set", func() {
So(d.NotFound, ShouldEqual, h)
})
})
})
Convey("Given a handler builder", t, func() {
b := func(i interface{}) (Handler, error) {
if s, ok := i.(string); ok {
return HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(s))
}), nil
}
return nil, errors.New("Not a string")
}
d := NewMockDispatcher()
r := New(d)
r.AddHandlerBuilder(b)
Convey("When the a var that can be built into a handler is routed", func() {
r.Handle("somepath", "hello")
Convey("Then the generated handler should be added", func() {
So(d.HandlerHasBeenCalled, ShouldBeTrue)
So(d.Route, ShouldNotBeNil)
})
})
Convey("When the a var that cannot be built into a handler is routed", func() {
Convey("Then router should panic", func() {
So(func() { r.Handle("somepath", 123) }, ShouldPanic)
})
})
})
Convey("Given a platform", t, func() {
d := NewMockDispatcher()
platform := NewMockPlatform()
Convey("When the platform is scaffolded", func() {
Scaffold(d, platform)
Convey("The platform should be routed", func() {
So(platform.Route, ShouldNotBeNil)
})
})
})
}