2020-01-21 19:19:18 +00:00
|
|
|
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const loopback = require('../');
|
|
|
|
const request = require('supertest');
|
2014-08-28 18:56:43 +00:00
|
|
|
|
|
|
|
describe('remoting coercion', function() {
|
|
|
|
it('should coerce arguments based on the type', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
let called = false;
|
|
|
|
const app = loopback();
|
2014-08-28 18:56:43 +00:00
|
|
|
app.use(loopback.rest());
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const TestModel = app.registry.createModel('TestModel',
|
2016-06-03 20:51:48 +00:00
|
|
|
{},
|
2018-08-08 15:22:20 +00:00
|
|
|
{base: 'Model'});
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(TestModel, {public: true});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
2014-08-28 18:56:43 +00:00
|
|
|
TestModel.test = function(inst, cb) {
|
|
|
|
called = true;
|
|
|
|
assert(inst instanceof TestModel);
|
|
|
|
assert(inst.foo === 'bar');
|
|
|
|
cb();
|
2014-11-21 01:46:21 +00:00
|
|
|
};
|
2014-08-28 18:56:43 +00:00
|
|
|
TestModel.remoteMethod('test', {
|
2016-11-15 21:46:23 +00:00
|
|
|
accepts: {arg: 'inst', type: 'TestModel', http: {source: 'body'}},
|
|
|
|
http: {path: '/test', verb: 'post'},
|
2014-08-28 18:56:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
.post('/TestModels/test')
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send({
|
2016-04-01 09:14:26 +00:00
|
|
|
foo: 'bar',
|
2014-08-28 18:56:43 +00:00
|
|
|
})
|
|
|
|
.end(function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-28 18:56:43 +00:00
|
|
|
assert(called);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-28 18:56:43 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
});
|