diff --git a/packages/express/lib/rest/index.js b/packages/express/lib/rest/index.js index ca690aaf00..e84e1c474a 100644 --- a/packages/express/lib/rest/index.js +++ b/packages/express/lib/rest/index.js @@ -108,7 +108,10 @@ function rest (handler = formatter) { }; app.use(function (req, res, next) { - req.feathers = Object.assign({ provider: 'rest' }, req.feathers); + req.feathers = Object.assign({ + provider: 'rest', + headers: req.headers + }, req.feathers); next(); }); diff --git a/packages/express/test/authentication.test.js b/packages/express/test/authentication.test.js index 624dbf127f..4d2f3ab525 100644 --- a/packages/express/test/authentication.test.js +++ b/packages/express/test/authentication.test.js @@ -116,6 +116,21 @@ describe('@feathersjs/express/authentication', () => { }); }); + it('errors when there are no httpStrategies', () => { + const { accessToken } = authResult; + app.get('authentication').httpStrategies = []; + + return axios.get('/dummy/dave', { + headers: { + Authorization: accessToken + } + }).then(() => assert.fail('Should never get here')) + .catch(error => { + assert.strictEqual(error.response.data.name, 'NotAuthenticated'); + app.get('authentication').httpStrategies = [ 'jwt' ]; + }); + }); + it('can make a protected request with Authorization header and bearer scheme', () => { const { accessToken } = authResult; @@ -134,6 +149,15 @@ describe('@feathersjs/express/authentication', () => { }); describe('authenticate middleware', () => { + it('errors without valid strategies', () => { + try { + authenticate(); + assert.fail('Should never get here'); + } catch (error) { + assert.strictEqual(error.message, 'The authenticate hook needs at least one allowed strategy'); + } + }); + it('protected endpoint fails when JWT is not present', () => { return axios.get('/protected').then(() => { assert.fail('Should never get here'); diff --git a/packages/express/test/rest/index.test.js b/packages/express/test/rest/index.test.js index cb4cdae4f3..308ff227ed 100644 --- a/packages/express/test/rest/index.test.js +++ b/packages/express/test/rest/index.test.js @@ -152,26 +152,22 @@ describe('@feathersjs/express/rest provider', () => { return axios.get('http://localhost:4777/hook/dishes?test=param') .then(res => { + const paramsWithHeaders = { + ...params, + headers: res.data.params.headers + }; + assert.deepStrictEqual(res.data, { id: 'dishes', - params, + params: paramsWithHeaders, arguments: [ - 'dishes', - params + 'dishes', paramsWithHeaders ], type: 'after', method: 'get', path: 'hook', result: { description: 'You have to do dishes' }, - addedProperty: true, - arguments: [ - 'dishes', - { - route: {}, - query: { test: 'param' }, - provider: 'rest' - } - ] + addedProperty: true }); }); }); @@ -241,22 +237,19 @@ describe('@feathersjs/express/rest provider', () => { return axios('http://localhost:4777/hook-error/dishes') .catch(error => { + const { data } = error.response; + const paramsWithHeaders = { + ...params, + headers: data.hook.params.headers + }; assert.deepStrictEqual(error.response.data, { hook: { id: 'dishes', - params, - arguments: [ 'dishes', params ], + params: paramsWithHeaders, + arguments: ['dishes', paramsWithHeaders ], type: 'error', method: 'get', - path: 'hook-error', - arguments: [ - 'dishes', - { - route: {}, - query: {}, - provider: 'rest' - } - ] + path: 'hook-error' }, error: { message: 'I blew up' } }); @@ -286,6 +279,7 @@ describe('@feathersjs/express/rest provider', () => { return axios.get('http://localhost:4778/service/bla?some=param&another=thing') .then(res => { let expected = { + headers: res.data.headers, test: 'Happy', provider: 'rest', route: {}, @@ -343,22 +337,22 @@ describe('@feathersjs/express/rest provider', () => { app.configure(rest()) .use(expressify.json()) .use('/todo', function (req, res, next) { - req.body.before = [ 'before first' ]; + req.body.before = ['before first']; next(); }, function (req, res, next) { req.body.before.push('before second'); next(); }, { - create (data) { - return Promise.resolve(data); - } - }, function (req, res, next) { - res.data.after = [ 'after first' ]; - next(); - }, function (req, res, next) { - res.data.after.push('after second'); - next(); - }); + create (data) { + return Promise.resolve(data); + } + }, function (req, res, next) { + res.data.after = ['after first']; + next(); + }, function (req, res, next) { + res.data.after.push('after second'); + next(); + }); const server = app.listen(4776); @@ -366,8 +360,8 @@ describe('@feathersjs/express/rest provider', () => { .then(res => { assert.deepStrictEqual(res.data, { text: 'Do dishes', - before: [ 'before first', 'before second' ], - after: [ 'after first', 'after second' ] + before: ['before first', 'before second'], + after: ['after first', 'after second'] }); }) .then(() => server.close()); diff --git a/packages/primus/lib/index.js b/packages/primus/lib/index.js index 66cd8bbf59..70957979e5 100644 --- a/packages/primus/lib/index.js +++ b/packages/primus/lib/index.js @@ -42,7 +42,17 @@ function configurePrimus (config, configurer) { primus.plugin('emitter', Emitter); primus.use('feathers', function (req, res, next) { - req.feathers = { provider: 'primus' }; + req.feathers = { + headers: Object.keys(req.headers).reduce((key, result) => { + const value = req.headers[key]; + + return typeof value === 'object' ? result : { + ...result, + [key]: value + }; + }, {}), + provider: 'primus' + }; next(); }, 0); diff --git a/packages/primus/test/index.test.js b/packages/primus/test/index.test.js index 707e3c4196..4ca3f694d8 100644 --- a/packages/primus/test/index.test.js +++ b/packages/primus/test/index.test.js @@ -29,6 +29,7 @@ describe('@feathersjs/primus', () => { }, function (primus) { primus.authorize(function (req, done) { req.feathers.user = { name: 'David' }; + options.socketParams.headers = req.feathers.headers; const { channel } = req.query; diff --git a/packages/socketio/lib/index.js b/packages/socketio/lib/index.js index 2a96eedeab..52ea46e16e 100644 --- a/packages/socketio/lib/index.js +++ b/packages/socketio/lib/index.js @@ -51,7 +51,8 @@ function configureSocketio (port, options, config) { io.use((socket, next) => { const connection = { - provider: 'socketio' + provider: 'socketio', + headers: socket.handshake.headers }; Object.defineProperty(connection, socketKey, { diff --git a/packages/socketio/test/index.test.js b/packages/socketio/test/index.test.js index c2a3dc590f..b009c02261 100644 --- a/packages/socketio/test/index.test.js +++ b/packages/socketio/test/index.test.js @@ -10,7 +10,7 @@ const methodTests = require('./methods.js'); const eventTests = require('./events'); const socketio = require('../lib'); -describe('@feathersjs/socketio', () => { +describe.only('@feathersjs/socketio', () => { let app; let server; let socket; @@ -40,6 +40,7 @@ describe('@feathersjs/socketio', () => { .configure(socketio(function (io) { io.use(function (socket, next) { socket.feathers.user = { name: 'David' }; + socketParams.headers = socket.feathers.headers; const { channel } = socket.handshake.query;