server.respond.js
23.6 KB
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
'use strict';
var test = require('tape');
var Server = require('../').Server;
var sinon = require('sinon');
test('Server.respond', function (t) {
var errors = {
PARSE_ERROR: -32700,
INVALID_REQUEST: -32600,
METHOD_NOT_FOUND: -32601,
INVALID_PARAMS: -32602,
INTERNAL_ERROR: -32603
};
function expectValidResponse(t, response, id) {
t.equal(response.jsonrpc, '2.0');
t.equal(response.id, id);
}
function expectValidResult(t, response, id) {
expectValidResponse(t, response, id);
t.equal(response.error, void undefined);
}
function expectValidError(t, response, id) {
expectValidResponse(t, response, id);
t.equal(response.result, void undefined);
t.equal(typeof response.error, 'object');
t.equal(typeof response.error.code, 'number');
t.equal(typeof response.error.message, 'string');
}
t.test('returns an error when', function (t) {
t.test('request is not valid JSON', function (t) {
var server = new Server();
var requests = [
void undefined,
{
id: 1,
method: 'foo'
},
[],
'[',
'{ foo:',
'{ foo }',
'{ foo: "bar" }'
];
var callbackCount = 0;
var response;
server.provide(function foo() { });
requests.forEach(function (request) {
server.respond(request, function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, null);
t.equal(response.error.code, errors.PARSE_ERROR);
callbackCount++;
if (callbackCount === requests.length) {
t.end();
}
});
});
});
t.test('request.jsonrpc !== "2.0"', function (t) {
var server = new Server();
var request = {
id: 1,
method: 'foo'
};
var versions = ['2.1', '2', 2.0, {}, [], null, false, true];
server.provide(function foo() { });
server.respond(JSON.stringify(request), function (error, response) {
var callbackCount = 0;
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.INVALID_REQUEST);
versions.forEach(function (version) {
request.jsonrpc = version;
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.INVALID_REQUEST);
callbackCount++;
if (callbackCount === versions.length) {
t.end();
}
});
});
});
});
t.test('request.method is missing', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1
};
server.provide(function foo() { });
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.INVALID_REQUEST);
t.end();
});
});
t.test('request.method is not a string', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
};
var methods = [null, {}, [], 23, false];
var callbackCount = 0;
var response;
server.provide(function foo() { });
methods.forEach(function (method) {
request.method = method;
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.INVALID_REQUEST);
callbackCount++;
if (callbackCount === methods.length) {
t.end();
}
});
});
});
t.test('request.method is not provided by the server', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'fiz'
};
var response;
server.provide(function foo() { });
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.METHOD_NOT_FOUND);
t.end();
});
});
t.test('request.id is present, but not a string, number, or null',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
};
var ids = [{}, [], false];
var callbackCount = 0;
var response;
server.provide(function foo() { });
ids.forEach(function (id) {
request.id = id;
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, null);
t.equal(response.error.code, errors.INVALID_REQUEST);
callbackCount++;
if (callbackCount === ids.length) {
t.end();
}
});
});
});
t.test('request.params is present, but not an object or array',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
};
var paramses = ['', false, true, null, 0, 42];
var callbackCount = 0;
var response;
server.provide(function foo() { });
paramses.forEach(function (params) {
request.params = params;
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.INVALID_REQUEST);
callbackCount++;
if (callbackCount === paramses.length) {
t.end();
}
});
});
});
t.end();
});
t.test('upon a valid request', function (t) {
t.test('calls the named method', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var spy = sinon.spy();
server.provide(function foo () { spy(); });
server.respond(JSON.stringify(request));
sinon.assert.calledOnce(spy);
t.end();
});
t.test('calls the method in a context that provides an `async` method',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var spy = sinon.spy();
server.provide(function foo () {
t.equal(typeof this.async, 'function', 'this.async is a function');
});
server.respond(JSON.stringify(request), function (error, response) {
error = error = response.error;
if (error) {
throw new Error(error.message);
}
t.end();
});
});
t.test('`async` method', function (t) {
t.test('returns a callback', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var spy = sinon.spy();
server.provide(function foo () {
var callback = this.async();
t.equal(
typeof callback, 'function', 'this.async returns a function');
t.end();
});
server.respond(JSON.stringify(request));
});
t.test('if called', function (t) {
t.test(
'`respond` waits for the returned callback to be invoked',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var spy = sinon.spy();
server.provide(function foo () {
var callback = this.async();
setTimeout(callback, 0);
});
server.respond(
JSON.stringify(request),
function (error, response) {
t.end();
});
});
t.test(
'`respond` passes thru errors thrown during method invocation',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var expected = 'doh!';
var spy = sinon.spy();
server.provide(function foo () {
var callback = this.async();
throw new Error(expected);
});
server.respond(
JSON.stringify(request),
function (error, response) {
response = JSON.parse(response);
t.equal(response.error.message, expected);
t.end();
});
});
t.test(
'`respond` passes thru errors passed to the callback',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var expected = 'doh!';
var spy = sinon.spy();
server.provide(function foo () {
var callback = this.async();
setTimeout(function () {
callback(new Error(expected));
}, 0);
});
server.respond(
JSON.stringify(request),
function (error, response) {
response = JSON.parse(response);
t.equal(response.error.message, expected);
t.end();
});
});
t.test(
'`respond` passes thru results from the callback',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var expected = 'bar';
var spy = sinon.spy();
server.provide(function foo () {
var callback = this.async();
setTimeout(function () {
callback(null, expected);
}, 0);
});
server.respond(
JSON.stringify(request),
function (error, response) {
response = JSON.parse(response);
t.equal(response.result, expected);
t.end();
});
});
t.end();
});
t.test('if not called', function (t) {
t.test(
'invokes `respond` callback with the returned result synchronously',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var expected = 'bar';
var actual;
var spy = sinon.spy();
server.provide(function foo () {
return expected;
});
server.respond(
JSON.stringify(request),
function (error, response) {
actual = JSON.parse(response).result;
});
t.equal(actual, expected);
t.end();
});
t.test(
'`respond` passes thru errors thrown during method invocation',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var expected = 'doh!';
var actual;
var spy = sinon.spy();
server.provide(function foo () {
throw new Error(expected);
});
server.respond(
JSON.stringify(request),
function (error, response) {
actual = JSON.parse(response).error.message;
});
t.equal(actual, expected);
t.end();
});
});
t.end();
});
t.test('passes positional arguments in order', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var args = [
[], [1], [1, 2], [1, 2, 3]
];
var spy = sinon.spy();
server.provide(function foo (first, second) {
spy.apply(null, Array.prototype.slice.call(arguments));
});
for (var i = 0; i < args.length; i++) {
request.params = args[i];
server.respond(JSON.stringify(request));
sinon.assert.calledOnce(spy);
t.deepEqual(spy.lastCall.args, args[i]);
spy.reset();
}
t.end();
});
t.test('passes named arguments in appropriately', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var args = [
{}, { first: 23 }, { second: 42 }, { first: 23, second: 42 },
{ second: 42, first: 23 },
{ first: 23, second: 42, third: 23251 }
];
var spy = sinon.spy();
server.provide(function foo (first, second) {
spy.apply(null, Array.prototype.slice.call(arguments));
});
for (var i = 0; i < args.length; i++) {
request.params = args[i];
server.respond(JSON.stringify(request));
sinon.assert.calledOnce(spy);
t.equal(spy.lastCall.args[0], args[i].first);
t.equal(spy.lastCall.args[1], args[i].second);
spy.reset();
}
t.end();
});
t.test('returns a valid response with the method result', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var results = [
undefined, null, false, true, 0, 42, [1, 2, 3], { foo: 'bar' }
];
var callbackCount = 0;
var response, currentResult;
server.provide(function foo () {
return currentResult;
});
results.forEach(function (result) {
currentResult = result;
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidResult(t, response, request.id);
t.deepEqual(response.result, result);
callbackCount++;
if (callbackCount === results.length) {
t.end();
}
});
});
});
t.test('returns null when passed a notification', function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
method: 'foo'
};
var results = [
undefined, null, false, true, 0, 42, [1, 2, 3], { foo: 'bar' }
];
var callbackCount = 0;
var response, currentResult;
server.provide(function foo () {
return currentResult;
});
results.forEach(function (result) {
currentResult = result;
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
t.equal(response, null);
callbackCount++;
if (callbackCount === results.length) {
t.end();
}
});
});
});
t.test(
'returns an instance of Error if a method throws on a notification',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
method: 'foo'
};
var message = 'OHNOES';
server.provide(function foo () {
throw message;
});
server.respond(JSON.stringify(request), function (error, response) {
// FIXME: there are no assertions
t.end();
});
});
t.test('returns an INTERNAL_ERROR if the method throws a string',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var message = 'OHNOES';
server.provide(function foo () {
throw message;
});
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.INTERNAL_ERROR);
t.equal(response.error.message, message);
t.end();
});
});
t.test(
'returns an INTERNAL_ERROR if the method throws an Error with no code',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var message = 'OHNOES';
server.provide(function foo () {
throw new Error(message);
});
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, errors.INTERNAL_ERROR);
t.equal(response.error.message, message);
t.end();
});
});
t.test(
'returns an error with correct code if the method throws an Error ' +
'with a code',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var message = 'OHNOES';
var code = -32001;
server.provide(function foo () {
var e = new Error(message);
e.code = code;
throw e;
});
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.equal(response.error.code, code);
t.end();
});
});
t.test('returns an error with correct data if the method throws an ' +
'Error with data',
function (t) {
var server = new Server();
var request = {
jsonrpc: '2.0',
id: 1,
method: 'foo'
};
var message = 'OHNOES';
var data = { foo: 'bar' };
server.provide(function foo () {
var e = new Error(message);
e.data = data;
throw e;
});
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, request.id);
t.same(response.error.data, data);
t.end();
});
});
t.end();
});
t.test('upon a batch request', function (t) {
t.test('returns INVALID_REQUEST if the batch is empty', function (t) {
var server = new Server();
var request = [];
server.provide(function foo() { });
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
expectValidError(t, response, null);
t.equal(response.error.code, errors.INVALID_REQUEST);
t.end();
});
});
t.test('returns an array of responses', function (t) {
var server = new Server();
var request = [
1,
{ 'foo': 'bar' },
{ jsonrpc: 2, method: 'foo', id: 1 },
{ jsonrpc: '2.0', id: 2 },
{ jsonrpc: '2.0', method: 2, id: 3 },
{ jsonrpc: '2.0', method: 'fiz', id: 4 },
{ jsonrpc: '2.0', method: 'foo', id: [] },
{ jsonrpc: '2.0', method: 'foo', id: 5, params: 23 },
{ jsonrpc: '2.0', method: 'foo', id: 6 },
{ jsonrpc: '2.0', method: 'foo', id: 7, params: [1, 2] },
{
jsonrpc: '2.0', method: 'foo', id: 8,
params: { one: 1, two: 2 }
},
{ jsonrpc: '2.0', method: 'foo', id: 9, params: [-1] },
{
jsonrpc: '2.0', method: 'foo', id: 10,
params: [-1, true, 123]
},
{
jsonrpc: '2.0', method: 'foo', id: 11,
params: [-1, true, void undefined, { foo: 'bar' }]
}
];
server.provide(function foo(one, two, three, four) {
var e;
if (one < 0) {
if (two) {
e = new Error('OHNOES');
e.code = three;
e.data = four;
} else {
e = 'OHNOES';
}
throw e;
}
if (one && two) {
return one + two;
} else if (one) {
return one;
} else {
return null;
}
});
server.respond(JSON.stringify(request), function (error, response) {
response = JSON.parse(response);
// invalid requests
expectValidError(t, response[0], null);
t.equal(response[0].error.code, errors.INVALID_REQUEST);
expectValidError(t, response[1], null);
t.equal(response[0].error.code, errors.INVALID_REQUEST);
// bad version
expectValidError(t, response[2], 1);
t.equal(response[2].error.code, errors.INVALID_REQUEST);
// missing method
expectValidError(t, response[3], 2);
t.equal(response[3].error.code, errors.INVALID_REQUEST);
// invalid method
expectValidError(t, response[4], 3);
t.equal(response[4].error.code, errors.INVALID_REQUEST);
// unprovided method
expectValidError(t, response[5], 4);
t.equal(response[5].error.code, errors.METHOD_NOT_FOUND);
// invalid id
expectValidError(t, response[6], null);
t.equal(response[6].error.code, errors.INVALID_REQUEST);
// invalid params
expectValidError(t, response[7], 5);
t.equal(response[7].error.code, errors.INVALID_REQUEST);
// no params
expectValidResult(t, response[8], 6);
t.equal(response[8].result, null);
// positional params
expectValidResult(t, response[9], 7);
t.equal(response[9].result, 3);
// named params
expectValidResult(t, response[10], 8);
t.equal(response[10].result, 3);
// throws a string
expectValidError(t, response[11], 9);
t.equal(response[11].error.code, errors.INTERNAL_ERROR);
t.equal(response[11].error.message, 'OHNOES');
// throws an Error with code
expectValidError(t, response[12], 10);
t.equal(response[12].error.code, 123);
t.equal(response[12].error.message, 'OHNOES');
// throws an Error with data
expectValidError(t, response[13], 11);
t.equal(response[13].error.code, errors.INTERNAL_ERROR);
t.equal(response[13].error.message, 'OHNOES');
t.deepEqual(response[13].error.data, { foo: 'bar' });
t.end();
});
});
t.end();
});
});