/
router_test.go
322 lines (287 loc) · 12.7 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
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
package auwfg
import (
"github.com/karlseguin/gspec"
"net/http/httptest"
"testing"
)
func TestNotFoundOnShortUrls(t *testing.T) {
req := gspec.Request().Url("/x").Req
res := httptest.NewRecorder()
newRouter(Configure()).ServeHTTP(res, req)
assertResponse(t, res, 404, `{"error":"not found","code":404}`)
}
func TestNotFoundOnUnknownVersions(t *testing.T) {
req := gspec.Request().Url("/v3/sessions.json").Req
res := httptest.NewRecorder()
newRouter(Configure()).ServeHTTP(res, req)
assertResponse(t, res, 404, `{"error":"not found","code":404}`)
}
func TestNotFoundOnUnknownControllers(t *testing.T) {
req := gspec.Request().Url("/v4/cats.json").Req
res := httptest.NewRecorder()
newRouter(Configure()).ServeHTTP(res, req)
assertResponse(t, res, 404, `{"error":"not found","code":404}`)
}
func TestNotFoundOnUnknownActions(t *testing.T) {
req := gspec.Request().Url("/v4/sessions.json").Req
res := httptest.NewRecorder()
newRouter(Configure()).ServeHTTP(res, req)
assertResponse(t, res, 404, `{"error":"not found","code":404}`)
}
func TestExecutesAListAction(t *testing.T) {
f := func(context interface{}) Response { return Json(`{"spice":"mustflow"}`).Response }
req := gspec.Request().Url("/v4/sessions.json").Req
res := httptest.NewRecorder()
router := newRouter(Configure().Route(R("LIST", "v4", "sessions", f)))
router.ServeHTTP(res, req)
assertResponse(t, res, 200, `{"spice":"mustflow"}`)
}
func TestExecutesAGetAction(t *testing.T) {
f := func(context interface{}) Response { return Json(`{"name":"duncan"}`).Response }
req := gspec.Request().Url("/v2/gholas/123g.json").Req
res := httptest.NewRecorder()
router := newRouter(Configure().Route(R("GET", "v2", "gholas", f)))
router.ServeHTTP(res, req)
assertResponse(t, res, 200, `{"name":"duncan"}`)
}
func TestExecutesAnActionRegardlessOfCasing(t *testing.T) {
f := func(context interface{}) Response { return Json(`{"name":"duncan"}`).Response }
req := gspec.Request().Url("/v2/GHOLAS/123g.json").Req
res := httptest.NewRecorder()
router := newRouter(Configure().Route(R("GET", "v2", "gholas", f)))
router.ServeHTTP(res, req)
assertResponse(t, res, 200, `{"name":"duncan"}`)
}
func TestExecutesAPostAction(t *testing.T) {
f := func(context interface{}) Response { return Json(`{"name":"shaihulud"}`).Response }
req := gspec.Request().Url("/v1/worms.json").Method("POST").Req
res := httptest.NewRecorder()
router := newRouter(Configure().Route(R("POST", "v1", "worms", f)))
router.ServeHTTP(res, req)
assertResponse(t, res, 200, `{"name":"shaihulud"}`)
}
func TestExecutesAPutAction(t *testing.T) {
f := func(context interface{}) Response { return Json(`{"name":"shaihulud"}`).Response }
req := gspec.Request().Url("/v1/worms/22w.json").Method("PUT").Req
res := httptest.NewRecorder()
router := newRouter(Configure().Route(R("PUT", "v1", "worms", f)))
router.ServeHTTP(res, req)
assertResponse(t, res, 200, `{"name":"shaihulud"}`)
}
func TestRoutesToANestedResource(t *testing.T) {
spec := gspec.New(t)
f := func(context interface{}) Response {
spec.Expect(context.(*BaseContext).Params.ParentResource).ToEqual("gholas")
spec.Expect(context.(*BaseContext).Params.ParentId).ToEqual("123g")
spec.Expect(context.(*BaseContext).Params.Resource).ToEqual("history")
spec.Expect(context.(*BaseContext).Params.Id).ToEqual("")
return Json(`{"name":"history"}`).Response
}
req := gspec.Request().Url("/v2/gholas/123g/history.json").Req
res := httptest.NewRecorder()
router := newRouter(Configure().Route(R("LIST", "v2", "gholas/history", f)))
router.ServeHTTP(res, req)
assertResponse(t, res, 200, `{"name":"history"}`)
}
func TestCreatingAndDispatchingThroughCustomTypes(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(context.Name).ToEqual("leto")
spec.Expect(context.Params.Version).ToEqual("v1")
spec.Expect(context.Params.Resource).ToEqual("worms")
spec.Expect(context.Params.Id).ToEqual("22w")
return Json(`{"name":"bigjohn"}`).Status(203).Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
res := httptest.NewRecorder()
newRouter(c).ServeHTTP(res, req)
assertResponse(t, res, 203, `{"name":"bigjohn"}`)
}
func TestDefaulsTheBodyValueWhenNoBodyIsPresent(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
input := context.Body.(*TestBody)
spec.Expect(input.Hello).ToEqual("")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f).BodyFactory(testBodyFactory)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestParsesABody(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
input := context.Body.(*TestBody)
spec.Expect(input.Hello).ToEqual("World")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f).BodyFactory(testBodyFactory)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").BodyString(`{"hello":"World"}`).Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestDoesNotStoreRawBodyWhenNotConfigured(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(string(context.RawBody)).ToEqual("")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").BodyString(`{"hello":"World"}`).Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestStoresRawBody(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(string(context.RawBody)).ToEqual(`{"hello":"World"}`)
return Json("").Response
}
c := Configure().LoadRawBody().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").BodyString(`{"hello":"World"}`).Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestHandlesANilResponse(t *testing.T) {
f := func(context interface{}) Response { return nil }
c := Configure().Route(R("GET", "v1", "worms", f))
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
res := httptest.NewRecorder()
newRouter(c).ServeHTTP(res, req)
assertResponse(t, res, 500, `{"error":"internal server error","code":500}`)
}
func TestHandlesBodiesLargerThanAllowed(t *testing.T) {
f := func(context *TestContext) Response { return Json("").Response }
c := Configure().Route(R("GET", "v1", "worms", f).BodyFactory(testBodyFactory)).ContextFactory(testContextFactory).Dispatcher(testDispatcher).BodyPool(3, 1)
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").BodyString(`{"hello":"World"}`).Req
res := httptest.NewRecorder()
newRouter(c).ServeHTTP(res, req)
assertResponse(t, res, 413, `{"error":"body too large","code":413}`)
}
func TestParsesQueryString(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(context.Query["app"]).ToEqual("6003")
spec.Expect(context.Query["t"]).ToEqual("1 2")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json?APP=6003&t=1%202&").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestParsesQueryStirngWithEmptyPairAtTheStart(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(context.Query["app"]).ToEqual("100004a")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json?&app=100004a").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestParsesQueryStirngWithEmptyPairInTheMiddle(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(context.Query["app"]).ToEqual("100004a")
spec.Expect(context.Query["t"]).ToEqual("1")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json?app=100004a&&t=1").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestHandlesMultipleQuestionMarksInQueryString(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(context.Query["app"]).ToEqual("100005a")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json?app=100002a?app=100005a").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestParsesAQueryStringWithAMissingValue(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(len(context.Query)).ToEqual(0)
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json?a").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestParsesAQueryStringWithAMissingValue2(t *testing.T) {
spec := gspec.New(t)
f := func(context *TestContext) Response {
spec.Expect(context.Query["b"]).ToEqual("")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json?b=").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestNoIpPresent(t *testing.T) {
f := func(context *TestContext) Response {
gspec.New(t).Expect(context.RemoteIp).ToBeNil()
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestInvalidIp(t *testing.T) {
f := func(context *TestContext) Response {
gspec.New(t).Expect(context.RemoteIp).ToBeNil()
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Url("/v1/worms/22w.json").Header("x-forwarded-for", "12.12").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestLoadIpFromXForwardedIp(t *testing.T) {
f := func(context *TestContext) Response {
gspec.New(t).Expect(context.RemoteIp.String()).ToEqual("12.12.12.12")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Header("x-forwarded-for", "12.12.12.12,13.13.13.13").Header("client-ip", "11.11.11.11")
req = req.Header("remote-addr", "10.10.10.10").Url("/v1/worms/22w.json").Method("GET")
newRouter(c).ServeHTTP(httptest.NewRecorder(), req.Req)
}
func TestLoadIpFromClientIp(t *testing.T) {
f := func(context *TestContext) Response {
gspec.New(t).Expect(context.RemoteIp.String()).ToEqual("10.10.10.10")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Header("client-ip", "10.10.10.10").Header("remote-addr", "12.12.10.10").Url("/v1/worms/22w.json").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func TestLoadIpFromRemoteAddr(t *testing.T) {
f := func(context *TestContext) Response {
gspec.New(t).Expect(context.RemoteIp.String()).ToEqual("13.10.10.10")
return Json("").Response
}
c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
req := gspec.Request().Header("remote-addr", "13.10.10.10").Url("/v1/worms/22w.json").Method("GET").Req
newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
func assertResponse(t *testing.T, res *httptest.ResponseRecorder, status int, raw string) {
spec := gspec.New(t)
spec.Expect(res.Code).ToEqual(status)
spec.Expect(string(res.Body.Bytes())).ToEqual(raw)
}
type TestContext struct {
Name string
*BaseContext
}
type TestBody struct {
Hello string
}
func testContextFactory(c *BaseContext) interface{} {
return &TestContext{"leto", c}
}
func testDispatcher(route *Route, context interface{}) Response {
return route.Action.(func(*TestContext) Response)(context.(*TestContext))
}
func testBodyFactory() interface{} {
return new(TestBody)
}