From 816e989b4e4ba7938ed6ead70d3e4c701e6991b8 Mon Sep 17 00:00:00 2001 From: Kenny Sabir Date: Tue, 19 Sep 2017 09:15:49 +1000 Subject: [PATCH] Add support for configuring remoting options Allow remote-connector users to provide "options" property in the datasource configuration, this "options" object is then passed down to RemoteObjects and allows e.g. configuration of pass-through authorization, where the remoting connector passes the access token used to make the incoming request down to the backend service invoked. --- lib/remote-connector.js | 4 ++-- test/helper.js | 9 +++++++++ test/remote-connector.test.js | 12 ++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/remote-connector.js b/lib/remote-connector.js index 195b1e3..b7dc650 100644 --- a/lib/remote-connector.js +++ b/lib/remote-connector.js @@ -29,14 +29,14 @@ module.exports = RemoteConnector; function RemoteConnector(settings) { assert(typeof settings === 'object', - 'cannot initiaze RemoteConnector without a settings object'); + 'cannot initialize RemoteConnector without a settings object'); this.client = settings.client; this.adapter = settings.adapter || 'rest'; this.protocol = settings.protocol || 'http'; this.root = settings.root || ''; this.host = settings.host || 'localhost'; this.port = settings.port || 3000; - this.remotes = remoting.create(); + this.remotes = remoting.create(settings.options); this.name = 'remote-connector'; if (settings.url) { diff --git a/test/helper.js b/test/helper.js index 962ca8f..9a18d0d 100644 --- a/test/helper.js +++ b/test/helper.js @@ -12,6 +12,7 @@ var remoteConnector = require('..'); exports.createMemoryDataSource = createMemoryDataSource; exports.createModel = createModel; exports.createRemoteDataSource = createRemoteDataSource; +exports.createRemoteDataSourceWithOptions = createRemoteDataSourceWithOptions; exports.createRestAppAndListen = createRestAppAndListen; exports.getUserProperties = getUserProperties; @@ -44,6 +45,14 @@ function createRemoteDataSource(remoteApp) { }); } +function createRemoteDataSourceWithOptions(remoteApp, options) { + return loopback.createDataSource({ + url: 'http://anyURL.com', + connector: remoteConnector, + options: options + }); +} + /** * Used to create models based on a set of options. May associate or link to an * app. diff --git a/test/remote-connector.test.js b/test/remote-connector.test.js index eb002ff..25b57b9 100644 --- a/test/remote-connector.test.js +++ b/test/remote-connector.test.js @@ -134,3 +134,15 @@ describe('Custom Path', function() { }); }); }); + +describe('RemoteConnector with options', () => { + it('should have the remoting options passed to the remote object', () => { + const serverApp = helper.createRestAppAndListen(); + + const datasource = helper.createRemoteDataSourceWithOptions( + serverApp, + {'test': 'abc'}); + + assert.deepEqual(datasource.connector.remotes.options, {test: 'abc'}); + }); +});