-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmiddleware-registration.test.js
More file actions
84 lines (64 loc) · 1.82 KB
/
middleware-registration.test.js
File metadata and controls
84 lines (64 loc) · 1.82 KB
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
/* global describe, it */
const expect = require('chai').expect
const request = require('supertest')
describe('0http - Middlewares Registration', () => {
const baseUrl = `http://localhost:${process.env.PORT}`
const { router, server } = require('../index')({
router: require('../lib/router/sequential')()
})
const m0 = (req, res, next) => {
res.body = []
return next()
}
const m1 = (req, res, next) => {
res.body.push('m1')
return next()
}
const m2 = (req, res, next) => {
res.body.push('m2')
return next()
}
const m3 = (req, res, next) => {
res.body.push('m3')
return next()
}
it('should successfully register middlewares', (done) => {
router.use(m0, m1)
router.use('/v1', m2, m3)
router.get('/v1/hello', (req, res, next) => {
res.end(JSON.stringify(res.body))
})
server.listen(~~process.env.PORT, (err) => {
if (!err) done()
})
})
it('should hit middlewares', async () => {
await request(baseUrl)
.get('/v1/hello')
.expect(200)
.then((response) => {
const payload = JSON.parse(response.text)
expect(payload[0]).to.equal('m1')
expect(payload[1]).to.equal('m2')
expect(payload[2]).to.equal('m3')
})
})
it('should support array-based middleware registration', async () => {
router.use('/arr', [m2, m3])
router.get('/arr/hello', (req, res, next) => {
res.end(JSON.stringify(res.body))
})
await request(baseUrl)
.get('/arr/hello')
.expect(200)
.then((response) => {
const payload = JSON.parse(response.text)
expect(payload[0]).to.equal('m1')
expect(payload[1]).to.equal('m2')
expect(payload[2]).to.equal('m3')
})
})
it('should successfully terminate the service', async () => {
server.close()
})
})