express-middleware
Advanced tools
Comparing version 2.1.0 to 3.0.0
{ | ||
"name": "express-middleware", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Set of middlewares for Chauffeur-Privé", | ||
@@ -33,3 +33,3 @@ "keywords": [ | ||
"mocha-lcov-reporter": "~1.0.0", | ||
"sinon": "~1.17.3", | ||
"sinon": "4.4.2", | ||
"supertest": "^2.0.1" | ||
@@ -36,0 +36,0 @@ }, |
@@ -27,5 +27,8 @@ 'use strict'; | ||
return wrap(function* middleware(req, res, next) { | ||
if (res.body && i18n) { | ||
if (!i18n) { | ||
throw new Error('Missing i18n dependency, i18n middleware should be initialized with a configured instance of i18n'); | ||
} | ||
if (res.body) { | ||
res.body = yield i18n.translate(res.body, req.language); | ||
return res.json(res.body); | ||
@@ -32,0 +35,0 @@ } |
@@ -40,3 +40,3 @@ 'use strict'; | ||
it('should accept a non-blacklisted ip', function test(done) { | ||
sandbox.stub(process.env, 'IP_BLACKLIST', blacklistedIP); | ||
sandbox.stub(process.env, 'IP_BLACKLIST').value(blacklistedIP); | ||
const req = { get: () => otherIP, connection: { remoteAddress: otherIP } }; | ||
@@ -56,3 +56,3 @@ const res = { set: () => { } }; | ||
it('should reject a blacklisted ip from x-forwarded-for', function test(done) { | ||
sandbox.stub(process.env, 'IP_BLACKLIST', blacklistedIP); | ||
sandbox.stub(process.env, 'IP_BLACKLIST').value(blacklistedIP); | ||
const req = { get: () => otherIP, connection: { remoteAddress: blacklistedIP } }; | ||
@@ -70,3 +70,3 @@ const res = { set: () => { } }; | ||
it('should reject a blacklisted ip from remoteAddress', function test(done) { | ||
sandbox.stub(process.env, 'IP_BLACKLIST', blacklistedIP); | ||
sandbox.stub(process.env, 'IP_BLACKLIST').value(blacklistedIP); | ||
const req = { get: () => otherIP, connection: { remoteAddress: blacklistedIP } }; | ||
@@ -84,3 +84,3 @@ const res = { set: () => { } }; | ||
it('should work with comma-separated lists as well', function test(done) { | ||
sandbox.stub(process.env, 'IP_BLACKLIST', `127.127.127.0,${blacklistedIP}`); | ||
sandbox.stub(process.env, 'IP_BLACKLIST').value(`127.127.127.0,${blacklistedIP}`); | ||
const req = { get: () => '', connection: { remoteAddress: `127.0.0.1,${blacklistedIP}` } }; | ||
@@ -98,3 +98,3 @@ const res = { set: () => { } }; | ||
it('should ignore empty values from lists', function test(done) { | ||
sandbox.stub(process.env, 'IP_BLACKLIST', `,127.127.127.0,,${blacklistedIP}`); | ||
sandbox.stub(process.env, 'IP_BLACKLIST').value(`,127.127.127.0,,${blacklistedIP}`); | ||
const req = { get: () => ',,', connection: { remoteAddress: `,127.0.0.1,,${blacklistedIP},` } }; | ||
@@ -101,0 +101,0 @@ const res = { set: () => { } }; |
@@ -14,4 +14,9 @@ 'use strict'; | ||
}; | ||
const sandbox = sinon.sandbox.create(); | ||
it('should do nothing if i18n is falsey', function* test() { | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
it('should throw an exception if i18n is not injected', function* test() { | ||
const req = { | ||
@@ -32,4 +37,6 @@ language: 'en-US' | ||
sinon.assert.called(next); | ||
sinon.assert.notCalled(res.json); | ||
expect(next.args[0][0].message) | ||
.to.deep.equal('Missing i18n dependency, i18n middleware should be initialized with a configured instance of i18n'); | ||
expect(next.callCount).to.equal(1); | ||
expect(res.json.callCount).to.equal(0); | ||
expect(res.body).to.deep.equal(body); | ||
@@ -49,3 +56,8 @@ }); | ||
const middleware = i18nMW(); | ||
const i18n = { | ||
translate: () => {} | ||
}; | ||
sandbox.stub(i18n, 'translate').resolves('translated value'); | ||
const middleware = i18nMW(i18n); | ||
expect(middleware).to.be.instanceof(Function); | ||
@@ -55,4 +67,4 @@ | ||
sinon.assert.called(next); | ||
sinon.assert.notCalled(res.json); | ||
expect(next.callCount).to.equal(1); | ||
expect(res.json.callCount).to.equal(0); | ||
expect(res.body2).to.deep.equal(body); | ||
@@ -92,3 +104,3 @@ }); | ||
]); | ||
sinon.assert.notCalled(next); | ||
expect(next.callCount).to.equal(0); | ||
expect(res.body).to.deep.equal(body); | ||
@@ -104,2 +116,29 @@ expect(i18nObj).to.deep.equal({ | ||
}); | ||
it('should call translate successfully if i18n is passed as a promise and body exists', function* test() { | ||
const req = { | ||
language: 'fr-FR' | ||
}; | ||
const res = { | ||
body, | ||
json: sinon.spy() | ||
}; | ||
const next = sinon.spy(); | ||
const i18n = { | ||
translate: () => {} | ||
}; | ||
sandbox.stub(i18n, 'translate').resolves('translated value'); | ||
const middleware = i18nMW(i18n); | ||
expect(middleware).to.be.instanceof(Function); | ||
yield middleware(req, res); | ||
expect(res.json.args).to.deep.equal([ | ||
['translated value'] | ||
]); | ||
expect(next.callCount).to.equal(0); | ||
expect(res.body).to.deep.equal('translated value'); | ||
}); | ||
}); |
48424
1094