|
| 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