-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathshared-method.test.js
440 lines (390 loc) · 14.3 KB
/
shared-method.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
// Node module: strong-remoting
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
const assert = require('assert');
const extend = require('util')._extend;
const expect = require('./helpers/expect');
const Context = require('../lib/context-base');
const SharedMethod = require('../lib/shared-method');
const TypeRegistry = require('../lib/type-registry');
const factory = require('./helpers/shared-objects-factory.js');
const Promise = global.Promise || require('bluebird');
describe('SharedMethod', function() {
const STUB_CLASS = {};
const STUB_METHOD = function(cb) { cb(); };
describe('constructor', function() {
it('normalizes "array" type in "accepts" arguments', function() {
const sharedMethod = new SharedMethod(STUB_METHOD, 'a-name', STUB_CLASS, {
accepts: {arg: 'data', type: 'array'},
});
expect(sharedMethod.accepts).to.eql([
{arg: 'data', type: ['any']},
]);
});
it('normalizes "array" type in "returns" arguments', function() {
const sharedMethod = new SharedMethod(STUB_METHOD, 'a-name', STUB_CLASS, {
returns: {arg: 'data', type: 'array'},
});
expect(sharedMethod.returns).to.eql([
{arg: 'data', type: ['any']},
]);
});
it('passes along `documented` flag correctly', function() {
const sharedMethod = new SharedMethod(STUB_METHOD, 'a-name', STUB_CLASS, {
documented: false,
});
expect(sharedMethod.documented).to.eql(false);
});
});
describe('sharedMethod.isDelegateFor(suspect, [isStatic])', function() {
// stub function
function myFunction() {}
it('checks if the given function is going to be invoked', function() {
const mockSharedClass = {};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass);
assert.equal(sharedMethod.isDelegateFor(myFunction), true);
});
it('checks by name if a function is going to be invoked', function() {
const mockSharedClass = {prototype: {myName: myFunction}};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass);
assert.equal(sharedMethod.isDelegateFor('myName', false), true);
assert.equal(sharedMethod.isDelegateFor('myName', true), false);
assert.equal(sharedMethod.isDelegateFor('myName'), true);
});
it('checks by name if static function is going to be invoked', function() {
const mockSharedClass = {myName: myFunction};
const opts = {isStatic: true};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass, opts);
assert.equal(sharedMethod.isDelegateFor('myName', true), true);
assert.equal(sharedMethod.isDelegateFor('myName', false), false);
});
it('checks by alias if static function is going to be invoked', function() {
const mockSharedClass = {myName: myFunction};
const opts = {isStatic: true, aliases: ['myAlias']};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass, opts);
assert.equal(sharedMethod.isDelegateFor('myAlias', true), true);
assert.equal(sharedMethod.isDelegateFor('myAlias', false), false);
});
it('checks if the given name is a string', function() {
const mockSharedClass = {};
let err;
try {
const sharedMethod = new SharedMethod(myFunction, Number, mockSharedClass);
} catch (e) {
err = e;
}
assert(err);
});
});
describe('sharedMethod.isDelegateForName(suspect)', function() {
// stub function
function myFunction() {}
it('checks by name if static function is going to be invoked', function() {
const mockSharedClass = {myName: myFunction};
const opts = {isStatic: true};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass, opts);
assert.equal(sharedMethod.isDelegateForName('myName'), true);
});
it('checks by alias if static function is going to be invoked', function() {
const mockSharedClass = {myName: myFunction};
const opts = {isStatic: true, aliases: ['myAlias']};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass, opts);
assert.equal(sharedMethod.isDelegateForName('myAlias'), true);
});
it('checks by name if prototype function is going to be invoked', function() {
const mockSharedClass = {myName: myFunction};
const opts = {isStatic: false};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass, opts);
assert.equal(sharedMethod.isDelegateForName('prototype.myName'), true);
});
it('checks by alias if prototype function is going to be invoked', function() {
const mockSharedClass = {myName: myFunction};
const opts = {isStatic: false, aliases: ['myAlias']};
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass, opts);
assert.equal(sharedMethod.isDelegateForName('prototype.myAlias'), true);
});
it('checks if the given name is a string', function() {
const mockSharedClass = {};
let err;
const sharedMethod = new SharedMethod(myFunction, 'myName', mockSharedClass);
expect(function() { sharedMethod.isDelegateForName(myFunction); }).to.throw(/argument.*string/);
});
});
describe('sharedMethod.invoke', function() {
it('returns 400 when number argument is `NaN`', function(done) {
const method = givenSharedMethod({
accepts: {arg: 'num', type: 'number'},
});
method.invoke('ctx', {num: NaN}, {}, ctx(method), function(err) {
setImmediate(function() {
expect(err).to.exist();
expect(err.message).to.contain('not a number');
expect(err.statusCode).to.equal(400);
done();
});
});
});
describe('data type: integer', function() {
describe('SharedMethod.getType - determine actual type based on value', function() {
it('returns type: number for decimal value & integer target type',
function() {
expect(SharedMethod.getType(15.2, 'integer')).to.equal('number');
});
it('returns type:integer for intiger value & integer target type',
function() {
expect(SharedMethod.getType(14, 'integer')).to.equal('integer');
});
});
it('returns 400 when integer argument is a decimal number',
function(done) {
const method = givenSharedMethod({
accepts: {arg: 'num', type: 'integer'},
});
method.invoke('ctx', {num: 2.5}, {}, ctx(method), function(err) {
setImmediate(function() {
expect(err).to.exist();
expect(err.message).to.match(/not a safe integer/);
expect(err.statusCode).to.equal(400);
done();
});
});
});
it('returns 400 when integer argument is `NaN`', function(done) {
const method = givenSharedMethod({
accepts: {arg: 'num', type: 'integer'},
});
method.invoke('ctx', {num: NaN}, {}, ctx(method), function(err) {
setImmediate(function() {
expect(err).to.exist();
expect(err.message).to.match(/not a number/i);
expect(err.statusCode).to.equal(400);
done();
});
});
});
it('returns 400 when integer argument is not a safe integer',
function(done) {
const method = givenSharedMethod(
function(arg, cb) {
return cb({'num': arg});
},
{
accepts: {arg: 'num', type: 'integer'},
},
);
method.invoke('ctx', {num: 2343546576878989879789}, {}, ctx(method),
function(err) {
setImmediate(function() {
expect(err).to.exist();
expect(err.message).to.match(/integer/i);
expect(err.statusCode).to.equal(400);
done();
});
});
});
it('treats integer argument of type x.0 as integer', function(done) {
const method = givenSharedMethod(
function(arg, cb) {
return cb({'num': arg});
},
{
accepts: {arg: 'num', type: 'integer'},
},
);
method.invoke('ctx', {num: 12.0}, {}, ctx(method), function(result) {
setImmediate(function() {
expect(result.num).to.equal(12);
done();
});
});
});
it('returns 500 for non-integer return value if type: `integer`',
function(done) {
const method = givenSharedMethod(
function(cb) {
cb(null, 3.141);
},
{
returns: {arg: 'value', type: 'integer'},
},
);
method.invoke('ctx', {}, {}, ctx(method), function(err, result) {
setImmediate(function() {
expect(err).to.exist();
expect(err.message).to.match(/integer/i);
expect(err.statusCode).to.equal(500);
done();
});
});
});
it('returns 500 if returned value is not a safe integer', function(done) {
const method = givenSharedMethod(
function(cb) {
cb(null, -2343546576878989879789);
},
{
returns: {arg: 'value', type: 'integer'},
},
);
method.invoke('ctx', {}, {}, ctx(method), function(err, result) {
setImmediate(function() {
expect(err).to.exist();
expect(err.message).to.match(/integer/i);
expect(err.statusCode).to.equal(500);
done();
});
});
});
});
describe('data type: Date', function() {
it('converts return values to GMT timezone', function(done) {
const method = givenSharedMethod(
function(cb) {
cb(null, new Date(0));
},
{
returns: {arg: 'value', type: 'date'},
},
);
method.invoke('ctx', {}, {}, ctx(method), function(err, result) {
setImmediate(function() {
if (err) return done(err);
expect(result).to.eql({
value: {
$type: 'date',
$data: '1970-01-01T00:00:00.000Z',
},
});
done();
});
});
});
});
it('returns 400 and doesn\'t crash with unparsable object', function(done) {
const method = givenSharedMethod({
accepts: [{arg: 'obj', type: 'object'}],
});
method.invoke('ctx', {obj: 'test'}, {}, ctx(method), function(err) {
setImmediate(function() {
expect(err).to.exist();
expect(err.message).to.contain('not an object');
expect(err.statusCode).to.equal(400);
done();
});
});
});
it('resolves promise returned from the method', function(done) {
const method = givenSharedMethod(
function() {
return new Promise(function(resolve, reject) {
resolve(['one', 'two']);
});
},
{
returns: [
{arg: 'first', type: 'string'},
{arg: 'second', type: 'string'},
],
},
);
method.invoke('ctx', {}, {}, ctx(method), function(err, result) {
setImmediate(function() {
expect(result).to.eql({first: 'one', second: 'two'});
done();
});
});
});
it('handles promise resolved with a single arg', function(done) {
const method = givenSharedMethod(
function() {
return new Promise(function(resolve, reject) {
resolve('data');
});
},
{
returns: [
{arg: 'value', type: 'string'},
],
},
);
method.invoke('ctx', {}, {}, ctx(method), function(err, result) {
setImmediate(function() {
expect(result).to.eql({value: 'data'});
done();
});
});
});
it('handles promise resolved with a single array arg', function(done) {
const method = givenSharedMethod(
function() {
return new Promise(function(resolve, reject) {
resolve(['a', 'b']);
});
},
{
returns: [
{arg: 'value', type: ['string']},
],
},
);
method.invoke('ctx', {}, {}, ctx(method), function(err, result) {
setImmediate(function() {
expect(result).to.eql({value: ['a', 'b']});
done();
});
});
});
it('handles rejected promise returned from the method', function(done) {
const testError = new Error('expected test error');
const method = givenSharedMethod(function() {
return new Promise(function(resolve, reject) {
reject(testError);
});
});
method.invoke('ctx', {}, {}, ctx(method), function(err, result) {
setImmediate(function() {
expect(err).to.equal(testError);
done();
});
});
});
it('should remove from result the targeted value from promise', function(done) {
const body = {everything: 'ok'};
const method = givenSharedMethod(function() {
return Promise.resolve([201, body]);
}, {
returns: [
{arg: 'statusResult', type: 'number', http: {target: 'status'}},
{arg: 'result', type: 'object', root: true},
],
});
const context = ctx(method);
// override function that should be provided in HttpContext
context.setReturnArgByName = function(name) {
return name === 'statusResult';
};
method.invoke('ctx', {}, {}, context, function(err, result) {
setImmediate(function() {
expect(result).to.not.have.property('statusResult');
expect(result).to.eql(body);
done();
});
});
});
});
function givenSharedMethod(fn, options) {
if (options === undefined && typeof fn === 'object') {
options = fn;
fn = function() {
arguments[arguments.length - 1]();
};
}
const mockSharedClass = {fn: fn};
return new SharedMethod(fn, 'fn', mockSharedClass, options);
}
function ctx(method) {
return new Context(method, new TypeRegistry({warnOnUnknownType: false}));
}
});