From 1655e52436e447613f3d373538687fb6c3d5ce19 Mon Sep 17 00:00:00 2001 From: Camel Aissani Date: Tue, 28 Jun 2016 14:15:11 +0200 Subject: [PATCH] forgot a feature: must call middleware function when request fails --- lib/application.js | 2 ++ test/application-test.js | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/application.js b/lib/application.js index f651898..a2cb3d1 100755 --- a/lib/application.js +++ b/lib/application.js @@ -104,6 +104,8 @@ export default class Application { for (const route of currentRoutes) { if (route.middleware.failed) { route.middleware.failed(request, response); + } else { + route.middleware(request, response); } } if (reject) { diff --git a/test/application-test.js b/test/application-test.js index 4bff350..9da6561 100755 --- a/test/application-test.js +++ b/test/application-test.js @@ -105,6 +105,28 @@ describe('Application', () => { }); }); + it('middleware as function on path / request fails', (done) => { + requester = new Requester(); + sinon.stub(requester, 'fetch', ({uri, method, headers, data}, resolve, reject) => { + reject( + {uri, method, headers, data}, + {status: 401, statusText: 'not logged'} + ); + }); + + const spy = sinon.spy(); + + const app = frontexpress(); + app.set('http-requester', requester); + app.use((request, response) => {spy()}); + + app.httpGet('/', null, (request, response) => { + assert(spy.callCount === 1); + assert(response.status === 401); + done(); + }); + }); + it('middleware as object on path /', (done) => { const middleware = new frontexpress.Middleware('on path /'); const spy = sinon.spy(middleware, 'updated'); @@ -141,6 +163,29 @@ describe('Application', () => { }); }); + it('middleware as object on path / request fails', (done) => { + requester = new Requester(); + sinon.stub(requester, 'fetch', ({uri, method, headers, data}, resolve, reject) => { + reject( + {uri, method, headers, data}, + {status: 401, statusText: 'not logged'} + ); + }); + + const middleware = new frontexpress.Middleware('on path /'); + const spy = sinon.spy(middleware, 'failed'); + + const app = frontexpress(); + app.set('http-requester', requester); + app.use(middleware); + + app.httpGet('/', null, (request, response) => { + assert(spy.callCount === 1); + assert(response.status === 401); + done(); + }); + }); + it('router on path /', (done) => { const spy = sinon.spy(); const router = new frontexpress.Router();