Skip to content

Commit a49fe49

Browse files
authored
Fix ".use" middlewares registration (#24)
* adding cross-env module for cross OS testing * fixing .use middlewares registration
1 parent ab4c42d commit a49fe49

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

lib/router/sequential.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = (config = {}) => {
3434

3535
router.use = (prefix, ...middlewares) => {
3636
if (typeof prefix === 'function') {
37-
middlewares = [prefix]
37+
middlewares = [prefix, ...middlewares]
3838
prefix = '/'
3939
}
4040
_use.call(router, prefix, middlewares)

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"lint": "npx standard",
88
"format": "npx standard --fix",
9-
"test": "PORT=3000 NODE_ENV=testing npx nyc --check-coverage --lines 85 node ./node_modules/mocha/bin/mocha tests/*.test.js"
9+
"test": "cross-env PORT=3000 NODE_ENV=testing npx nyc --check-coverage --lines 85 node ./node_modules/mocha/bin/mocha tests/*.test.js"
1010
},
1111
"repository": {
1212
"type": "git",
@@ -30,6 +30,7 @@
3030
"devDependencies": {
3131
"body-parser": "^1.19.1",
3232
"chai": "^4.3.6",
33+
"cross-env": "^7.0.3",
3334
"find-my-way": "^5.2.0",
3435
"mocha": "^9.2.0",
3536
"nyc": "^15.1.0",
@@ -46,4 +47,4 @@
4647
"regexparam": "^2.0.0",
4748
"trouter": "^3.2.0"
4849
}
49-
}
50+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* global describe, it */
2+
const expect = require('chai').expect
3+
const request = require('supertest')
4+
5+
describe('0http Web Framework - Middlewares Registration', () => {
6+
const baseUrl = 'http://localhost:' + process.env.PORT
7+
8+
const { router, server } = require('../index')({
9+
router: require('../lib/router/sequential')()
10+
})
11+
12+
const m0 = (req, res, next) => {
13+
res.body = []
14+
15+
return next()
16+
}
17+
18+
const m1 = (req, res, next) => {
19+
res.body.push('m1')
20+
21+
return next()
22+
}
23+
24+
const m2 = (req, res, next) => {
25+
res.body.push('m2')
26+
27+
return next()
28+
}
29+
30+
const m3 = (req, res, next) => {
31+
res.body.push('m3')
32+
33+
return next()
34+
}
35+
36+
it('should successfully register middlewares', (done) => {
37+
router.use(m0, m1)
38+
router.use('/v1', m2, m3)
39+
40+
router.get('/v1/hello', (req, res, next) => {
41+
res.end(JSON.stringify(res.body))
42+
})
43+
44+
server.listen(~~process.env.PORT, err => {
45+
if (!err) done()
46+
})
47+
})
48+
49+
it('should hit middlewares', async () => {
50+
await request(baseUrl)
51+
.get('/v1/hello')
52+
.expect(200)
53+
.then((response) => {
54+
const payload = JSON.parse(response.text)
55+
56+
expect(payload[0]).to.equal('m1')
57+
expect(payload[1]).to.equal('m2')
58+
expect(payload[2]).to.equal('m3')
59+
})
60+
})
61+
62+
it('should successfully terminate the service', async () => {
63+
server.close()
64+
})
65+
})

0 commit comments

Comments
 (0)