2019-05-07 17:34:15 +00:00
|
|
|
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
|
2016-05-06 19:02:21 +00:00
|
|
|
// Node module: loopback-connector-remote
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-09-01 08:12:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const helper = require('./helper');
|
|
|
|
const loopback = require('loopback');
|
2015-11-26 21:17:33 +00:00
|
|
|
|
2014-09-25 21:06:06 +00:00
|
|
|
describe('RemoteConnector', function() {
|
2017-12-11 13:45:43 +00:00
|
|
|
let serverApp, clientApp, ServerModel, ClientModel;
|
2016-01-07 06:45:29 +00:00
|
|
|
|
2016-02-05 12:29:43 +00:00
|
|
|
before(function setupServer(done) {
|
2017-12-11 13:45:43 +00:00
|
|
|
const app = serverApp = helper.createRestAppAndListen();
|
|
|
|
const db = helper.createMemoryDataSource(app);
|
|
|
|
|
|
|
|
ServerModel = app.registry.createModel({
|
|
|
|
name: 'TestModel',
|
2016-01-07 06:45:29 +00:00
|
|
|
});
|
2017-12-11 13:45:43 +00:00
|
|
|
app.model(ServerModel, {dataSource: db});
|
|
|
|
|
|
|
|
app.locals.handler.on('listening', function() { done(); });
|
2016-02-05 12:29:43 +00:00
|
|
|
});
|
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
before(function setupRemoteClient() {
|
|
|
|
const app = clientApp = loopback({localRegistry: true});
|
|
|
|
const remoteDs = helper.createRemoteDataSource(clientApp, serverApp);
|
|
|
|
|
|
|
|
ClientModel = app.registry.createModel({
|
|
|
|
name: 'TestModel',
|
2016-01-07 06:45:29 +00:00
|
|
|
});
|
2017-12-11 13:45:43 +00:00
|
|
|
app.model(ClientModel, {dataSource: remoteDs});
|
2014-09-25 21:06:06 +00:00
|
|
|
});
|
|
|
|
|
2016-01-07 06:45:29 +00:00
|
|
|
after(function() {
|
2017-12-11 13:45:43 +00:00
|
|
|
serverApp.locals.handler.close();
|
|
|
|
ServerModel = null;
|
|
|
|
ClientModel = null;
|
2014-09-25 21:06:06 +00:00
|
|
|
});
|
|
|
|
|
2014-09-25 21:19:39 +00:00
|
|
|
it('should support the save method', function(done) {
|
2017-12-11 13:45:43 +00:00
|
|
|
let calledServerCreate = false;
|
2014-09-25 21:06:06 +00:00
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
ServerModel.create = function(data, options, cb, callback) {
|
2016-12-22 09:56:11 +00:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = cb;
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2014-09-25 21:06:06 +00:00
|
|
|
calledServerCreate = true;
|
|
|
|
data.id = 1;
|
2016-01-07 06:45:29 +00:00
|
|
|
if (callback) callback(null, data);
|
|
|
|
else cb(null, data);
|
|
|
|
};
|
2014-09-25 21:06:06 +00:00
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
const m = new ClientModel({foo: 'bar'});
|
2016-01-07 06:45:29 +00:00
|
|
|
m.save(function(err, instance) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert(instance);
|
2017-12-11 13:45:43 +00:00
|
|
|
assert(instance instanceof ClientModel);
|
2014-09-25 21:06:06 +00:00
|
|
|
assert(calledServerCreate);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-27 00:14:35 +00:00
|
|
|
|
|
|
|
it('should support aliases', function(done) {
|
2017-12-11 13:45:43 +00:00
|
|
|
let calledServerUpsert = false;
|
|
|
|
ServerModel.patchOrCreate =
|
|
|
|
ServerModel.upsert = function(id, options, cb) {
|
2016-12-22 09:56:11 +00:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2016-01-07 06:45:29 +00:00
|
|
|
calledServerUpsert = true;
|
|
|
|
cb();
|
2015-03-27 00:14:35 +00:00
|
|
|
};
|
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
ClientModel.updateOrCreate({}, function(err, instance) {
|
2015-03-27 00:14:35 +00:00
|
|
|
if (err) return done(err);
|
2016-01-07 06:45:29 +00:00
|
|
|
assert(instance);
|
2017-12-11 13:45:43 +00:00
|
|
|
assert(instance instanceof ClientModel);
|
2016-09-01 08:12:12 +00:00
|
|
|
assert(calledServerUpsert, 'server upsert should have been called');
|
2016-01-07 06:45:29 +00:00
|
|
|
done();
|
2015-03-27 00:14:35 +00:00
|
|
|
});
|
|
|
|
});
|
2014-09-25 21:06:06 +00:00
|
|
|
});
|
2015-11-26 21:17:33 +00:00
|
|
|
|
|
|
|
describe('Custom Path', function() {
|
2017-12-11 13:45:43 +00:00
|
|
|
let serverApp, clientApp, ServerModel, ClientModel;
|
2015-11-26 21:17:33 +00:00
|
|
|
|
2016-02-05 12:29:43 +00:00
|
|
|
before(function setupServer(done) {
|
2017-12-11 13:45:43 +00:00
|
|
|
const app = serverApp = helper.createRestAppAndListen();
|
|
|
|
const db = helper.createMemoryDataSource(app);
|
|
|
|
|
|
|
|
ServerModel = app.registry.createModel({
|
|
|
|
name: 'TestModel',
|
2016-01-07 06:45:29 +00:00
|
|
|
options: {
|
2017-12-11 13:51:48 +00:00
|
|
|
http: {path: '/custom'},
|
|
|
|
},
|
2015-11-26 21:17:33 +00:00
|
|
|
});
|
2017-12-11 13:45:43 +00:00
|
|
|
app.model(ServerModel, {dataSource: db});
|
|
|
|
|
|
|
|
serverApp.locals.handler.on('listening', function() { done(); });
|
2016-02-05 12:29:43 +00:00
|
|
|
});
|
2015-11-26 21:17:33 +00:00
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
before(function setupRemoteClient() {
|
|
|
|
const app = clientApp = loopback({localRegistry: true});
|
|
|
|
const remoteDs = helper.createRemoteDataSource(clientApp, serverApp);
|
|
|
|
|
|
|
|
ClientModel = app.registry.createModel({
|
|
|
|
name: 'TestModel',
|
2016-01-07 06:45:29 +00:00
|
|
|
options: {
|
|
|
|
dataSource: 'remote',
|
2017-12-11 13:51:48 +00:00
|
|
|
http: {path: '/custom'},
|
|
|
|
},
|
2015-11-26 21:17:33 +00:00
|
|
|
});
|
2017-12-11 13:45:43 +00:00
|
|
|
app.model(ClientModel, {dataSource: remoteDs});
|
2016-01-07 06:45:29 +00:00
|
|
|
});
|
2015-11-26 21:17:33 +00:00
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
after(function() {
|
|
|
|
serverApp.locals.handler.close();
|
|
|
|
ServerModel = null;
|
|
|
|
ClientModel = null;
|
2015-11-26 21:17:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should support http.path configuration', function(done) {
|
2017-12-11 13:45:43 +00:00
|
|
|
ClientModel.create({}, function(err, instance) {
|
2016-01-07 06:45:29 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
assert(instance);
|
2015-11-26 21:17:33 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2016-01-07 06:45:29 +00:00
|
|
|
});
|
2017-09-18 23:15:49 +00:00
|
|
|
|
|
|
|
describe('RemoteConnector with options', () => {
|
|
|
|
it('should have the remoting options passed to the remote object', () => {
|
2017-12-11 13:45:43 +00:00
|
|
|
const app = loopback();
|
|
|
|
const dataSource = app.dataSource('remote', {
|
|
|
|
url: 'http://example.com',
|
|
|
|
connector: require('..'),
|
|
|
|
options: {'test': 'abc'},
|
|
|
|
});
|
2017-09-18 23:15:49 +00:00
|
|
|
|
2017-12-11 13:45:43 +00:00
|
|
|
assert.deepEqual(dataSource.connector.remotes.options, {test: 'abc'});
|
2017-09-18 23:15:49 +00:00
|
|
|
});
|
|
|
|
});
|