-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.js
302 lines (263 loc) · 10.1 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
/*!
* koa-rest-router <https://github.com/tunnckoCore/koa-rest-router>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
const request = require('supertest')
const test = require('mukla')
const Koa = require('koa')
const Router = require('./index')
const router = Router()
test('should expose constructor', function (done) {
test.strictEqual(typeof Router, 'function')
test.strictEqual(typeof Router(), 'object')
test.strictEqual(typeof (new Router()), 'object')
test.strictEqual(typeof router, 'object')
done()
})
test('should have `koa-better-router` methods', function (done) {
test.strictEqual(typeof router.createRoute, 'function')
test.strictEqual(typeof router.addRoute, 'function')
test.strictEqual(typeof router.getRoute, 'function')
test.strictEqual(typeof router.addRoutes, 'function')
test.strictEqual(typeof router.getRoutes, 'function')
test.strictEqual(typeof router.groupRoutes, 'function')
test.strictEqual(typeof router.loadMethods, 'function')
test.strictEqual(typeof router.middleware, 'function')
test.strictEqual(typeof router.createResource, 'function')
test.strictEqual(typeof router.addResource, 'function')
test.strictEqual(typeof router.addResources, 'function')
test.strictEqual(typeof router.getResource, 'function')
test.strictEqual(typeof router.legacyMiddleware, 'function')
done()
})
test('should not have the HTTP verbs as methods if not `.loadMethods` called', function (done) {
test.strictEqual(router.get, undefined)
test.strictEqual(router.put, undefined)
test.strictEqual(router.del, undefined)
test.strictEqual(router.post, undefined)
test.strictEqual(router.patch, undefined)
test.strictEqual(router.delete, undefined)
done()
})
test('should have HTTP verbs as methods when `.loadMethods` is called', function (done) {
let api = Router({ prefix: '/api' })
api.loadMethods()
test.strictEqual(typeof api.put, 'function')
test.strictEqual(typeof api.get, 'function')
test.strictEqual(typeof api.post, 'function')
test.strictEqual(typeof api.patch, 'function')
done()
})
test('should get single resource by plural name - `.getResource(name)`', function (done) {
let r = (new Router()).resource('foobar')
let resource = r.getResource('foobars')
test.strictEqual(typeof resource, 'object')
test.strictEqual(Array.isArray(resource), true)
test.strictEqual(resource.length, 7) // 7 Route Objects
test.deepStrictEqual(resource[1].route, '/foobars/new')
done()
})
test('should `.getResource` return null if not found', function (done) {
let ro = Router().resource('foo')
test.strictEqual(ro.getResources().length, 1)
test.strictEqual(ro.getResource('bar'), null)
done()
})
test('should get all resources using `.getResources`', function (done) {
let ruter = Router({ prefix: '/api' })
ruter.resource('dogs').createResource()
let resources = ruter.getResources()
test.strictEqual(Array.isArray(resources), true)
test.strictEqual(resources.length, 2)
test.strictEqual(resources[0].length, 7)
test.strictEqual(resources[1].length, 7)
test.strictEqual(resources[0][1].path, '/api/dogs/new')
test.strictEqual(resources[0][1].route, '/dogs/new')
test.strictEqual(resources[1][1].path, '/api/new')
test.strictEqual(resources[1][1].route, '/new')
test.strictEqual(ruter.resources[0][1].path, '/api/dogs/new')
test.strictEqual(ruter.resources[1][1].path, '/api/new')
done()
})
test('should have `.route` method (path-match matcher) on instance', function (done) {
test.strictEqual(typeof router.route, 'function')
done()
})
test('should have empty `.routes` array on initialization', function (done) {
test.strictEqual(Array.isArray(router.routes), true)
test.strictEqual(router.routes.length, 0)
done()
})
test('should have empty array `.resources` on init', function (done) {
let r = new Router()
let isArr = Array.isArray(r.resources)
test.strictEqual(isArr, true)
test.strictEqual(r.resources.length, 0)
done()
})
test('should `.addRoute` throw TypeError if `method` a string', function (done) {
function fixture () {
router.addRoute(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `method` to be a string/)
done()
})
test('should `.addRoute` throw TypeError route not a string, array or function', function (done) {
function fixture () {
router.addRoute('GET', 123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `route` be string, array or function/)
done()
})
test('should create REST 7 routes and 1 resource using `.resource` method', function (done) {
let routerSelf = Router().resource('users')
test.strictEqual(routerSelf.resources.length, 1)
test.strictEqual(routerSelf.routes.length, 7)
routerSelf.resource('cats')
test.strictEqual(routerSelf.resources.length, 2)
test.strictEqual(routerSelf.routes.length, 14)
done()
})
test('should got `501 Not Implemented` if ctrl method not implemented', function (done) {
const r = new Router()
const serv = new Koa()
r.resource('fool', {
index: (ctx, next) => {}
})
serv.use(r.middleware())
request(serv.callback()).get('/fools/new').expect(501, /Not Implemented/)
.end(done)
})
test('should group resources using `.groupResources`', function (done) {
let server = new Koa()
let apiRouter = new Router({
prefix: '/api'
})
let companies = apiRouter.createResource('companies', {
show: function * (next) {
this.body = `companies: path is ${this.route.path}, haha`
this.body = `${this.body}!! :company is ${this.params.company}, yea`
yield next
}
})
let profiles = apiRouter.createResource('profiles', {
show: function (ctx, next) {
return next()
}
})
let cats = apiRouter.createResource('cats', {
show: function (ctx, next) {
ctx.body = `catsCtrl: path is ${ctx.route.path}, hoho`
ctx.body = `${ctx.body} :company is ${ctx.params.company}`
ctx.body = `${ctx.body} :profile is ${ctx.params.profile}`
ctx.body = `${ctx.body} :cat is ${ctx.params.cat}`
return next()
}
})
test.strictEqual(apiRouter.resources.length, 3)
test.strictEqual(apiRouter.routes.length, 0)
test.strictEqual(companies.length, 7)
test.strictEqual(profiles.length, 7)
test.strictEqual(cats.length, 7)
let resource = apiRouter.groupResources(companies, profiles, cats)
test.strictEqual(apiRouter.resources.length, 3)
test.strictEqual(apiRouter.routes.length, 0)
test.strictEqual(resource.length, 7)
// method `.addResource` works too
apiRouter.addResources(resource)
test.strictEqual(apiRouter.resources.length, 3)
test.strictEqual(apiRouter.routes.length, 7)
server.use(apiRouter.middleware())
request(server.callback())
.get('/api/companies/foo')
// we don't have `/api/companies` routes
// we don't have `/api/comapnies/:company/profiles/:profile` routes
// but we have `/companies/:company/profiles/:profile/cats` routes
.expect(404, function (err) {
test.ifError(err)
request(server.callback())
.get('/api/companies/foo/profiles/bar')
.expect(404).end(function () {
request(server.callback())
.get('/api/companies/foo/profiles/bar/cats/qux')
.expect(/:company is foo/)
.expect(/:profile is bar/)
.expect(/:cat is qux/)
.expect(200, /catsCtrl: path is/)
.end(done)
})
})
})
test('should generate correct routes using `.groupResources` for a default prefix', function () {
let apiRouter = new Router()
let companies = apiRouter.createResource('companies')
let profiles = apiRouter.createResource('profiles')
let resource = apiRouter.groupResources(companies, profiles)
test.strictEqual(resource[0].path, '/companies/:company/profiles')
test.strictEqual(resource[1].path, '/companies/:company/profiles/new')
test.strictEqual(resource[2].path, '/companies/:company/profiles')
test.strictEqual(resource[3].path, '/companies/:company/profiles/:profile')
test.strictEqual(resource[4].path, '/companies/:company/profiles/:profile/edit')
test.strictEqual(resource[5].path, '/companies/:company/profiles/:profile')
})
test('should generate correct routes using `.groupResources` for a custom prefix', function () {
let apiRouter = new Router({ prefix: '/api' })
let companies = apiRouter.createResource('companies')
let profiles = apiRouter.createResource('profiles')
let resource = apiRouter.groupResources(companies, profiles)
test.strictEqual(resource[0].path, '/api/companies/:company/profiles')
test.strictEqual(resource[1].path, '/api/companies/:company/profiles/new')
test.strictEqual(resource[2].path, '/api/companies/:company/profiles')
test.strictEqual(resource[3].path, '/api/companies/:company/profiles/:profile')
test.strictEqual(resource[4].path, '/api/companies/:company/profiles/:profile/edit')
test.strictEqual(resource[5].path, '/api/companies/:company/profiles/:profile')
})
test('should be able to re-map controller methods through opitons', function (done) {
let options = {
map: {
edit: 'foo'
}
}
let api = Router({ prefix: '/api' })
api.resource('companies', {
foo: function (ctx, next) {
ctx.body = `Hello world! Edit company ${ctx.params.company}.`
ctx.body = `${ctx.body} Path: ${ctx.route.path}`
return next()
}
}, options)
let app = new Koa()
app.use(api.middleware())
request(app.callback()).get('/api/companies/123/edit').expect(/Hello world!/)
.expect(200, /Edit company 123/)
.end(done)
})
test('should be able to re-map request methods through options', function (done) {
let options = {
methods: {
get: 'post'
}
}
let kkk = new Koa()
let api = new Router()
api.resource({
new: function * (next) {
this.body = `Normally this is called with GET request`
this.body = `${this.body}, but now it is called with POST request`
yield next
}
}, options)
kkk.use(api.middleware())
request(kkk.callback())
.post('/new')
.send({ foo: 'bar' })
.expect(/Normally this is called with GET request/)
.expect(/but now it is called with POST request/)
.expect(200, done)
})