Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add params.headers to all transports when available #1303

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/express/lib/rest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
24 changes: 24 additions & 0 deletions packages/express/test/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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');
Expand Down
66 changes: 30 additions & 36 deletions packages/express/test/rest/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
});
Expand Down Expand Up @@ -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' }
});
Expand Down Expand 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: {},
Expand Down Expand Up @@ -343,31 +337,31 @@ 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);

return axios.post('http://localhost:4776/todo', { text: 'Do dishes' })
.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());
Expand Down
12 changes: 11 additions & 1 deletion packages/primus/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/primus/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion packages/socketio/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
3 changes: 2 additions & 1 deletion packages/socketio/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down