-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathrest-adapter.test.js
612 lines (501 loc) · 19.3 KB
/
rest-adapter.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// 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 HttpInvocation = require('../lib/http-invocation');
const extend = require('util')._extend;
const inherits = require('util').inherits;
const RemoteObjects = require('../');
const RestAdapter = require('../lib/rest-adapter');
const SharedClass = require('../lib/shared-class');
const SharedMethod = require('../lib/shared-method');
const expect = require('chai').expect;
const factory = require('./helpers/shared-objects-factory.js');
function NOOP() {}
describe('RestAdapter', function() {
let remotes;
beforeEach(function() {
remotes = RemoteObjects.create();
});
describe('getClasses()', function() {
it('fills `name`', function() {
remotes.exports.testClass = factory.createSharedClass();
const classes = getRestClasses();
expect(classes[0]).to.have.property('name', 'testClass');
});
it('fills `routes`', function() {
remotes.exports.testClass = factory.createSharedClass();
remotes.exports.testClass.http = {path: '/test-class', verb: 'any'};
const classes = getRestClasses();
expect(classes[0]).to.have.property('routes')
.eql([{path: '/test-class', verb: 'any'}]);
});
it('fills `sharedClass`', function() {
remotes.exports.testClass = factory.createSharedClass();
const classes = getRestClasses();
expect(classes[0]).to.have.property('sharedClass');
expect(classes[0].sharedClass).to.be.an.instanceOf(SharedClass);
});
it('fills `ctor`', function() {
const testClass = remotes.exports.testClass = factory.createSharedClass();
testClass.sharedCtor.http = {path: '/shared-ctor', verb: 'all'};
const classes = getRestClasses();
expect(classes[0].ctor).to.have.property('routes')
.eql([{path: '/shared-ctor', verb: 'all'}]);
});
it('fills static methods', function() {
const testClass = remotes.exports.testClass = factory.createSharedClass();
testClass.staticMethod = extend(someFunc, {shared: true});
const methods = getRestClasses()[0].methods;
expect(methods).to.have.length(1);
expect(methods[0]).to.have.property('name', 'staticMethod');
expect(methods[0]).to.have.property('fullName', 'testClass.staticMethod');
expect(methods[0])
.to.have.nested.property('routes[0].path', '/staticMethod');
});
it('fills prototype methods', function() {
const testClass = remotes.exports.testClass = factory.createSharedClass();
testClass.prototype.instanceMethod = extend(someFunc, {shared: true});
const methods = getRestClasses()[0].methods;
expect(methods).to.have.length(1);
expect(methods[0])
.to.have.property('fullName', 'testClass.prototype.instanceMethod');
expect(methods[0])
// Note: the `/id:` part is coming from testClass.sharedCtor
.to.have.nested.property('routes[0].path', '/:id/instanceMethod');
});
function getRestClasses() {
return new RestAdapter(remotes).getClasses();
}
});
describe('path normalization', function() {
it('fills `routes`', function() {
remotes.exports.testClass = factory.createSharedClass();
remotes.exports.testClass.http = {path: '/testClass', verb: 'any'};
const classes = getRestClasses();
expect(classes[0]).to.have.property('routes')
.eql([{path: '/test-class', verb: 'any'}]);
});
function getRestClasses() {
return new RestAdapter(remotes, {normalizeHttpPath: true}).getClasses();
}
});
describe('RestClass', function() {
describe('getPath', function() {
it('returns the path of the first route', function() {
const restClass = givenRestClass({http: [
{path: '/a-path'},
{path: '/another-path'},
]});
expect(restClass.getPath()).to.equal('/a-path');
});
});
function givenRestClass(config) {
const ctor = factory.createSharedClass(config);
remotes.testClass = ctor;
const sharedClass = new SharedClass('testClass', ctor);
return new RestAdapter.RestClass(sharedClass);
}
});
describe('RestMethod', function() {
const anArg = {arg: 'an-arg-name', type: String};
it('has `accepts`', function() {
const method = givenRestStaticMethod({accepts: anArg});
expect(method.accepts).to.eql([anArg]);
});
it('has `returns`', function() {
const method = givenRestStaticMethod({returns: anArg});
expect(method.returns).to.eql([anArg]);
});
it('has `errors`', function() {
const method = givenRestStaticMethod({errors: anArg});
expect(method.errors).to.eql([anArg]);
});
it('has `description`', function() {
const method = givenRestStaticMethod({description: 'a-desc'});
expect(method.description).to.equal('a-desc');
});
it('has `notes`', function() {
const method = givenRestStaticMethod({notes: 'some-notes'});
expect(method.notes).to.equal('some-notes');
});
it('has `documented`', function() {
const method = givenRestStaticMethod({documented: false});
expect(method.documented).to.equal(false);
});
it('has `documented:true` by default', function() {
const method = givenRestStaticMethod();
expect(method.documented).to.equal(true);
});
describe('isReturningArray()', function() {
it('returns true when there is single root Array arg', function() {
const method = givenRestStaticMethod({
returns: {root: true, type: Array},
});
expect(method.isReturningArray()).to.equal(true);
});
it('returns true when there is single root "array" arg', function() {
const method = givenRestStaticMethod({
returns: {root: true, type: Array},
});
expect(method.isReturningArray()).to.equal(true);
});
it('returns true when there is single root [Model] arg', function() {
const method = givenRestStaticMethod({
returns: {root: true, type: ['string']},
});
expect(method.isReturningArray()).to.equal(true);
});
it('returns false otherwise', function() {
const method = givenRestStaticMethod({
returns: {arg: 'result', type: Array},
});
expect(method.isReturningArray()).to.equal(false);
});
it('handles invalid type', function() {
const method = givenRestStaticMethod({
returns: {root: true},
});
expect(method.isReturningArray()).to.equal(false);
});
});
describe('getArgByName()', function() {
const acceptsTwoArgs = [
{arg: 'argName1', type: String},
{arg: 'argName2', type: String},
];
it('should find the first arg', function() {
const method = givenRestStaticMethod({accepts: acceptsTwoArgs});
expect(method.getArgByName('argName1', ['firstArg', 'secondArg']))
.to.equal('firstArg');
});
it('should not find the second arg', function() {
const method = givenRestStaticMethod({accepts: acceptsTwoArgs});
expect(method.getArgByName('argName2', ['firstArg', 'secondArg']))
.to.equal('secondArg');
});
it('should not find argument not defined in metadata', function() {
const method = givenRestStaticMethod({accepts: acceptsTwoArgs});
expect(method.getArgByName('unknown-arg', ['firstArg', 'secondArg']))
.to.equal(undefined);
});
});
describe('acceptsSingleBodyArgument()', function() {
it('returns true when the arg is a single Object from body', function() {
const method = givenRestStaticMethod({
accepts: {
arg: 'data',
type: Object,
http: {source: 'body'},
},
});
expect(method.acceptsSingleBodyArgument()).to.equal(true);
});
it('returns false otherwise', function() {
const method = givenRestStaticMethod({
accepts: {arg: 'data', type: Object},
});
expect(method.acceptsSingleBodyArgument()).to.equal(false);
});
});
describe('getHttpMethod', function() {
ignoreDeprecationsInThisBlock();
it('returns POST for `all`', function() {
const method = givenRestStaticMethod({http: {verb: 'all'}});
expect(method.getHttpMethod()).to.equal('POST');
});
it('returns DELETE for `del`', function() {
const method = givenRestStaticMethod({http: {verb: 'del'}});
expect(method.getHttpMethod()).to.equal('DELETE');
});
it('returns upper-case value otherwise', function() {
const method = givenRestStaticMethod({http: {verb: 'get'}});
expect(method.getHttpMethod()).to.equal('GET');
});
});
describe('getPath', function() {
it('returns the path of the first route', function() {
const method = givenRestStaticMethod({http: [
{path: '/a-path'},
{path: '/another-path'},
]});
expect(method.getPath()).to.equal('/a-path');
});
});
describe('getFullPath', function() {
ignoreDeprecationsInThisBlock();
it('returns class path + method path', function() {
const method = givenRestStaticMethod(
{http: {path: '/a-method'}},
{http: {path: '/a-class'}},
);
expect(method.getFullPath()).to.equal('/a-class/a-method');
});
});
describe('getEndpoints', function() {
it('should return verb and fullPath for multiple paths', function() {
const method = givenRestStaticMethod({http: [
{verb: 'DEL', path: '/testMethod1'},
{verb: 'PUT', path: '/testMethod2'},
]});
const expectedEndpoints = [
{
fullPath: '/testClass/testMethod1',
verb: 'DELETE',
}, {
fullPath: '/testClass/testMethod2',
verb: 'PUT',
},
];
expect(method.getEndpoints()).to.eql(expectedEndpoints);
});
it('should return verb and fullPath for single path', function() {
const method = givenRestStaticMethod({http: {verb: 'all'}});
expect(method.getEndpoints()).to.eql([
{
verb: 'POST',
fullPath: '/testClass/testMethod',
},
]);
});
});
function givenRestStaticMethod(methodConfig, classConfig) {
const name = 'testMethod';
methodConfig = extend({shared: true}, methodConfig);
classConfig = extend({shared: true}, classConfig);
remotes.testClass = extend({}, classConfig);
const fn = remotes.testClass[name] = extend(function() {}, methodConfig);
const sharedClass = new SharedClass('testClass', remotes.testClass);
const restClass = new RestAdapter.RestClass(sharedClass);
const sharedMethod = new SharedMethod(fn, name, sharedClass, methodConfig);
return new RestAdapter.RestMethod(restClass, sharedMethod);
}
});
describe('sortRoutes', function() {
it('should sort routes based on verb & path', function() {
const routes = [
{route: {verb: 'get', path: '/'}},
{route: {verb: 'get', path: '/:id'}},
{route: {verb: 'get', path: '/findOne'}},
{route: {verb: 'delete', path: '/'}},
{route: {verb: 'del', path: '/:id'}},
];
routes.sort(RestAdapter.sortRoutes);
expect(routes).to.eql([
{route: {verb: 'get', path: '/findOne'}},
{route: {verb: 'get', path: '/:id'}},
{route: {verb: 'get', path: '/'}},
{route: {verb: 'del', path: '/:id'}},
{route: {verb: 'delete', path: '/'}},
]);
});
it('should sort routes based on path accuracy', function() {
const routes = [
{route: {verb: 'get', path: '/'}},
{route: {verb: 'get', path: '/:id/docs'}},
{route: {verb: 'get', path: '/:id'}},
{route: {verb: 'get', path: '/findOne'}},
];
routes.sort(RestAdapter.sortRoutes);
expect(routes).to.eql([
{route: {verb: 'get', path: '/findOne'}},
{route: {verb: 'get', path: '/:id/docs'}},
{route: {verb: 'get', path: '/:id'}},
{route: {verb: 'get', path: '/'}},
]);
});
it('should sort routes with common parts', function() {
const routes = [
{route: {verb: 'get', path: '/sum'}},
{route: {verb: 'get', path: '/sum/1'}},
];
routes.sort(RestAdapter.sortRoutes);
expect(routes).to.eql([
{route: {verb: 'get', path: '/sum/1'}},
{route: {verb: 'get', path: '/sum'}},
]);
});
it('should sort routes with trailing /', function() {
const routes = [
{route: {verb: 'get', path: '/sum/'}},
{route: {verb: 'get', path: '/sum/1'}},
];
routes.sort(RestAdapter.sortRoutes);
expect(routes).to.eql([
{route: {verb: 'get', path: '/sum/1'}},
{route: {verb: 'get', path: '/sum/'}},
]);
});
});
describe('invoke()', function() {
const oldInvoke = HttpInvocation.prototype.invoke;
let remotes, req, res;
beforeEach(function() {
remotes = RemoteObjects.create();
req = false;
res = false;
HttpInvocation.prototype.invoke = function(callback) {
if (!this.req) {
this.createRequest();
}
req = this.req;
res = this.res = {foo: 'bar'};
this.transformResponse(res, null, callback);
};
});
afterEach(function() {
HttpInvocation.prototype.invoke = oldInvoke;
});
it('should call remote hooks', function(done) {
let beforeCalled = false;
let afterCalled = false;
const name = 'testClass.testMethod';
remotes.before(name, function(ctx, next) {
beforeCalled = true;
next();
});
remotes.after(name, function(ctx, next) {
afterCalled = true;
next();
});
const restAdapter = givenRestStaticMethod({isStatic: true});
restAdapter.connect('foo');
restAdapter.invoke(name, [], [], function() {
assert(beforeCalled);
assert(afterCalled);
done();
});
});
it('should call beforeRemote hook with request object', function(done) {
const name = 'testClass.testMethod';
let _req;
remotes.before(name, function(ctx, next) {
_req = ctx.req;
next();
});
const restAdapter = givenRestStaticMethod({isStatic: true});
restAdapter.connect('foo');
restAdapter.invoke(name, [], [], function() {
expect(_req).to.equal(req);
done();
});
});
it('should call afterRemote hook with response object', function(done) {
const name = 'testClass.testMethod';
let _res;
remotes.after(name, function(ctx, next) {
_res = ctx.res;
next();
});
const restAdapter = givenRestStaticMethod({isStatic: true});
restAdapter.connect('foo');
restAdapter.invoke(name, [], [], function() {
expect(_res).to.equal(res);
done();
});
});
function givenRestStaticMethod(methodConfig, classConfig) {
const name = 'testMethod';
methodConfig = extend({shared: true}, methodConfig);
classConfig = extend({shared: true}, classConfig);
const testClass = extend({}, classConfig);
const fn = testClass[name] = extend(function() {}, methodConfig);
const sharedClass = new SharedClass('testClass', testClass);
const restClass = new RestAdapter.RestClass(sharedClass);
remotes.addClass(sharedClass);
const sharedMethod = new SharedMethod(fn, name, sharedClass, methodConfig);
const restMethod = new RestAdapter.RestMethod(restClass, sharedMethod);
return new RestAdapter(remotes);
}
});
describe('_getInvocationAuth()', function() {
let remotes, restAdapter;
beforeEach(() => {
remotes = RemoteObjects.create({cors: false});
restAdapter = new RestAdapter(remotes, {
passAccessToken: true,
});
});
it('should find the access token in the options from the args', () => {
const accessToken = {id: 'def'};
const options = {accessToken: accessToken};
const auth = restAdapter._getInvocationAuth(options);
expect(auth).to.deep.equal(options);
});
it('should find the auth from the remote', () => {
remotes.auth = {bearer: 'zzz'};
const auth = restAdapter._getInvocationAuth(undefined);
expect(auth).to.deep.equal(remotes.auth);
});
it('should prefer global auth over invocation options', () => {
remotes.auth = {bearer: 'zzz'};
const accessToken = {id: 'def'};
const options = {accessToken: accessToken};
const auth = restAdapter._getInvocationAuth(options);
expect(auth).to.deep.equal(remotes.auth);
});
});
describe('getRestMethodByName()', function() {
const SHARED_CLASS_NAME = 'testClass';
const METHOD_NAME = 'testMethod';
const FULL_NAME = SHARED_CLASS_NAME + '.' + METHOD_NAME;
let sharedClass, restAdapter;
beforeEach(givenSharedClassWithStaticMethod);
beforeEach(givenConnectedRestAdapter);
it('should find method by name', function() {
const restMethod = restAdapter.getRestMethodByName(FULL_NAME);
expect(restMethod).to.have.property('fullName', FULL_NAME);
});
it('should exclude methods disabled after the cache was built', function() {
// Get the rest method to trigger cache rebuild
restAdapter.getRestMethodByName(FULL_NAME);
sharedClass.disableMethodByName(METHOD_NAME);
const restMethod = restAdapter.getRestMethodByName(FULL_NAME);
expect(restAdapter.getRestMethodByName(FULL_NAME)).to.equal(undefined);
});
it('should find methods added after the cache was built', function() {
// Get the rest method to trigger cache rebuild
restAdapter.getRestMethodByName(FULL_NAME);
givenStaticSharedMethod('anotherMethod');
const anotherFullName = SHARED_CLASS_NAME + '.anotherMethod';
const restMethod = restAdapter.getRestMethodByName(anotherFullName);
expect(restMethod).to.have.property('fullName', anotherFullName);
});
function givenSharedClassWithStaticMethod() {
const testClass = {shared: true};
sharedClass = new SharedClass(SHARED_CLASS_NAME, testClass);
remotes.addClass(sharedClass);
givenStaticSharedMethod(METHOD_NAME);
}
function givenStaticSharedMethod(name, config) {
config = extend({shared: true, isStatic: true}, config);
sharedClass.ctor[name] = extend(function() {}, config);
}
function givenConnectedRestAdapter() {
restAdapter = new RestAdapter(remotes);
restAdapter.connect('foo');
}
});
describe('allRoutes', function() {
it('includes http', function() {
const remotes = RemoteObjects.create({cors: false});
remotes.exports.testClass = factory.createSharedClass();
remotes.exports.testClass.http = {path: '/testClass', verb: 'any'};
const restAdapter = new RestAdapter(remotes);
const allRoutes = restAdapter.allRoutes();
expect(allRoutes[0]).to.have.property('http');
});
});
});
function someFunc() {
}
function ignoreDeprecationsInThisBlock() {
before(function() {
process.on('deprecation', NOOP);
});
after(function() {
process.removeListener('deprecation', NOOP);
});
}