Skip to content

Commit

Permalink
Middleware system added
Browse files Browse the repository at this point in the history
  • Loading branch information
Shudrum committed Mar 16, 2020
1 parent cdd8646 commit 47363c5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions services/nodegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ const nodegate = () => {
});
};

app.use = (middlewares) => {
toArray(middlewares).forEach((middleware) => {
expressApp.use(middleware);
});
};

app.listen = expressApp.listen;

return app;
Expand Down
53 changes: 53 additions & 0 deletions test/services/nodegate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,59 @@ describe('services/nodegate', () => {
expect(text).toEqual('Multiline\nContent');
});
});
describe('#use', () => {
it('should work with express middlewares', async () => {
expect.assertions(1);
const gate = nodegate();
gate.use((req, _, next) => {
req.testValue = 'Hello';
next();
});
gate.route({
method: 'get',
path: '/route1',
workflow: [(_, req) => {
expect(req.testValue).toEqual('Hello');
}],
});
await request(gate).get('/route1').expect(200);
});
it('should work with multiple middlewares on the right order', async () => {
expect.assertions(1);
const gate = nodegate();
gate.use([(req, _, next) => {
req.testValue = 'Hello';
next();
}, (req, _, next) => {
req.testValue += ' world';
next();
}]);
gate.route({
method: 'get',
path: '/route1',
workflow: [(_, req) => {
expect(req.testValue).toEqual('Hello world');
}],
});
await request(gate).get('/route1').expect(200);
});
it('should ignore routes added before the middleware', async () => {
expect.assertions(1);
const gate = nodegate();
gate.route({
method: 'get',
path: '/route1',
workflow: [(_, req) => {
expect(req.testValue).toBeUndefined();
}],
});
gate.use((req, _, next) => {
req.testValue = 'Hello';
next();
});
await request(gate).get('/route1').expect(200);
});
});
describe('HTTP status codes', () => {
it('should respond a 500 error in case of error', async () => {
const gate = nodegate();
Expand Down

0 comments on commit 47363c5

Please sign in to comment.