forked from mulesoft-labs/osprey-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
329 lines (273 loc) · 7.83 KB
/
test.js
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
/* global describe, it */
require('es6-promise').polyfill()
var methods = require('methods')
var expect = require('chai').expect
var Router = require('./')
/* Helps using popsicle-server with popsicle version 12+.
*
* Inspired by popsicle 12.0+ code.
*/
function makeFetcher (app) {
var compose = require('throwback').compose
var Request = require('servie').Request
var popsicle = require('popsicle')
var popsicleServer = require('popsicle-server')
var finalhandler = require('finalhandler')
// Set response text to "body" property to mimic popsicle v10
// response interface.
function responseBodyMiddleware (req, next) {
return next().then(res => {
return res.text().then(body => {
res.body = body
return res
})
})
}
function createServer (router) {
return function (req, res) {
router(req, res, finalhandler(req, res))
}
}
var popsicleServerMiddleware = popsicleServer(createServer(app))
var middleware = compose([
responseBodyMiddleware,
popsicleServerMiddleware,
popsicle.middleware
])
return {
fetch: popsicle.toFetch(middleware, Request)
}
}
describe('Router', function () {
it('should be a function', function () {
expect(Router).to.be.a('function')
})
it('should return a function', function () {
expect(new Router()).to.be.a('function')
})
it('should reject missing callback', function () {
var router = new Router()
expect(Function.prototype.bind.call(router, router, {}, {}))
.to.throw(/argument callback is required/)
})
it('should not crash with invalid decode', function () {
var router = new Router()
router.get('/{id}', { id: { type: 'string' } }, function (req, res, next) {
return next()
}, helloWorld)
return makeFetcher(router).fetch('/foo%d', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(400)
})
})
describe('Router#all(path, fn)', function () {
it('should be chainable', function () {
var router = new Router()
expect(router.all('/', helloWorld)).to.equal(router)
})
it('should respond to all methods', function () {
var router = new Router()
router.all('/', helloWorld)
return Promise.all(methods.map(function (method) {
// This is tricky.
if (method === 'connect') {
return
}
return makeFetcher(router).fetch('/', {
method
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.body).to.equal(
method === 'head' ? '' : 'hello, world'
)
})
}))
})
it('should accept arrays', function () {
var router = new Router()
router.all('/', [helloWorld])
return makeFetcher(router).fetch('/', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.body).to.equal('hello, world')
})
})
it('should not stack overflow with many registered routes', function () {
var router = new Router()
for (var i = 0; i < 6000; i++) {
router.get('/thing' + i, helloWorld)
}
router.get('/', helloWorld)
return makeFetcher(router).fetch('/', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.body).to.equal('hello, world')
})
})
})
describe('methods', function () {
methods.forEach(function (method) {
if (method === 'connect') {
return
}
it('Router#' + method, function () {
var router = new Router()
router[method]('/foo', helloWorld)
return makeFetcher(router).fetch('/foo', {
method
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.body).to.equal(
method === 'head' ? '' : 'hello, world'
)
})
})
it('Router#' + method + ' should accept an array', function () {
var router = new Router()
router[method]('/foo', [helloWorld])
return makeFetcher(router).fetch('/foo', {
method
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.body).to.equal(
method === 'head' ? '' : 'hello, world'
)
})
})
})
})
describe('schemas', function () {
methods.forEach(function (method) {
if (method === 'connect') {
return
}
it('Router#' + method, function () {
var router = new Router()
router[method]('/{id}', {
id: {
type: 'number'
}
}, function (req, res) {
res.setHeader('x-typeof', typeof req.params.id)
res.end()
})
return makeFetcher(router).fetch('/123', {
method
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.headers.get('x-typeof')).to.equal('number')
})
})
})
})
describe('Router#use(path, fn)', function () {
it('should be able to use a middleware function', function () {
var router = new Router()
router.use(function (req, res) {
res.setHeader('x-url', req.url)
res.end()
})
return makeFetcher(router).fetch('/foo', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.headers.get('x-url')).to.equal('/foo')
})
})
it('should accept arrays', function () {
var router = new Router()
router.use([function (req, res) {
res.setHeader('x-url', req.url)
res.end()
}])
return makeFetcher(router).fetch('/foo', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.headers.get('x-url')).to.equal('/foo')
})
})
it('should accept a path', function () {
var router = new Router()
router.use('/foo', function (req, res) {
res.setHeader('x-url', req.url)
res.end()
})
return makeFetcher(router).fetch('/foo', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.headers.get('x-url')).to.equal('/')
})
})
it('should accept a path and schema', function () {
var router = new Router()
router.use('/{path}', {
path: {
type: 'string',
enum: [
'foo',
'bar'
]
}
}, function (req, res) {
res.setHeader('x-url', req.url)
res.end()
})
return makeFetcher(router).fetch('/bar', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(res.headers.get('x-url')).to.equal('/')
})
})
})
it('should allow re-use of uri parameters', function () {
var router = new Router()
router.use('/{id}', {
id: {
type: 'number'
}
}, function (req, res, next) {
next()
})
router.use('/{id}', function (req, res) {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(req.params))
})
return makeFetcher(router).fetch('/12345', {
method: 'GET'
})
.then(function (res) {
expect(res.status).to.equal(200)
expect(JSON.parse(res.body)).to.deep.equal({ id: 12345 })
})
.then(function () {
return makeFetcher(router).fetch('/abc', {
method: 'GET'
})
})
.then(function (res) {
expect(res.status).to.equal(404)
})
})
})
function helloWorld (req, res) {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
}