From 4735efa41fafd08b62df7c4a6c648246d29faf3c Mon Sep 17 00:00:00 2001 From: Piero Maltese Date: Wed, 26 Apr 2017 13:07:19 +0100 Subject: [PATCH] Support remoting adapters with no ctx.req object Fix `Model.createOptionsFromRemotingContext()` to correctly handle the case where `ctx.req` is not defined, e.g. when using websocket-based adapters. --- lib/model.js | 2 +- test/model.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/model.js b/lib/model.js index f7633b4d..eeecfbc9 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1046,7 +1046,7 @@ module.exports = function(registry) { */ Model.createOptionsFromRemotingContext = function(ctx) { return { - accessToken: ctx.req.accessToken, + accessToken: ctx.req ? ctx.req.accessToken : null, }; }; diff --git a/test/model.test.js b/test/model.test.js index 50d62127..f36557b5 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -964,6 +964,12 @@ describe.onServer('Remote Methods', function() { }); }); + it('sets empty options.accessToken for requests coming from websocket/primus adapters', function() { + const primusContext = {}; + const opts = TestModel.createOptionsFromRemotingContext(primusContext); + expect(opts).to.have.property('accessToken', null); + }); + it('allows apps to add options before remoting hooks', function(done) { TestModel.createOptionsFromRemotingContext = function(ctx) { return {hooks: []};