loopback/test/remoting-coercion.test.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

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
'use strict';
const assert = require('assert');
const loopback = require('../');
const request = require('supertest');
describe('remoting coercion', function() {
it('should coerce arguments based on the type', function(done) {
let called = false;
const app = loopback();
app.use(loopback.rest());
const TestModel = app.registry.createModel('TestModel',
{},
{base: 'Model'});
app.model(TestModel, {public: true});
TestModel.test = function(inst, cb) {
called = true;
assert(inst instanceof TestModel);
assert(inst.foo === 'bar');
cb();
};
TestModel.remoteMethod('test', {
accepts: {arg: 'inst', type: 'TestModel', http: {source: 'body'}},
http: {path: '/test', verb: 'post'},
});
request(app)
.post('/TestModels/test')
.set('Content-Type', 'application/json')
.send({
foo: 'bar',
})
.end(function(err) {
2014-11-21 02:35:36 +00:00
if (err) return done(err);
assert(called);
done();
});
});
});