|
2 | 2 | 'use strict' |
3 | 3 |
|
4 | 4 | const { strict: assert } = require('assert') |
| 5 | +const { stub } = require('sinon') |
5 | 6 | const RpcEngine = require('../src') |
6 | 7 |
|
7 | | -describe('basic tests', function () { |
| 8 | +describe('RpcEngine tests', function () { |
8 | 9 | it('basic middleware test 1', function (done) { |
9 | 10 | const engine = new RpcEngine() |
10 | 11 |
|
@@ -43,6 +44,21 @@ describe('basic tests', function () { |
43 | 44 | }) |
44 | 45 | }) |
45 | 46 |
|
| 47 | + it('basic middleware test (async)', async function () { |
| 48 | + const engine = new RpcEngine() |
| 49 | + |
| 50 | + engine.push(function (_req, res, _next, end) { |
| 51 | + res.result = 42 |
| 52 | + end() |
| 53 | + }) |
| 54 | + |
| 55 | + const payload = { id: 1, jsonrpc: '2.0', method: 'hello' } |
| 56 | + |
| 57 | + const res = await engine.handle(payload) |
| 58 | + assert.ok(res, 'has res') |
| 59 | + assert.equal(res.result, 42, 'has expected result') |
| 60 | + }) |
| 61 | + |
46 | 62 | it('allow null result', function (done) { |
47 | 63 | const engine = new RpcEngine() |
48 | 64 |
|
@@ -84,6 +100,28 @@ describe('basic tests', function () { |
84 | 100 | }) |
85 | 101 | }) |
86 | 102 |
|
| 103 | + it('middleware ending request before all middlewares applied', function (done) { |
| 104 | + const engine = new RpcEngine() |
| 105 | + |
| 106 | + engine.push(function (_req, res, _next, end) { |
| 107 | + res.result = 42 |
| 108 | + end() |
| 109 | + }) |
| 110 | + |
| 111 | + engine.push(function (_req, _res, _next, _end) { |
| 112 | + assert.fail('should not have called second middleware') |
| 113 | + }) |
| 114 | + |
| 115 | + const payload = { id: 1, jsonrpc: '2.0', method: 'hello' } |
| 116 | + |
| 117 | + engine.handle(payload, function (err, res) { |
| 118 | + assert.ifError(err, 'did not error') |
| 119 | + assert.ok(res, 'has res') |
| 120 | + assert.equal(res.result, 42, 'has expected result') |
| 121 | + done() |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
87 | 125 | it('erroring middleware test: end(error)', function (done) { |
88 | 126 | const engine = new RpcEngine() |
89 | 127 |
|
@@ -185,6 +223,37 @@ describe('basic tests', function () { |
185 | 223 | }) |
186 | 224 | }) |
187 | 225 |
|
| 226 | + it('handle batch payloads (async signature)', async function () { |
| 227 | + const engine = new RpcEngine() |
| 228 | + |
| 229 | + engine.push(function (req, res, _next, end) { |
| 230 | + if (req.id === 4) { |
| 231 | + delete res.result |
| 232 | + res.error = new Error('foobar') |
| 233 | + return end(res.error) |
| 234 | + } |
| 235 | + res.result = req.id |
| 236 | + return end() |
| 237 | + }) |
| 238 | + |
| 239 | + const payloadA = { id: 1, jsonrpc: '2.0', method: 'hello' } |
| 240 | + const payloadB = { id: 2, jsonrpc: '2.0', method: 'hello' } |
| 241 | + const payloadC = { id: 3, jsonrpc: '2.0', method: 'hello' } |
| 242 | + const payloadD = { id: 4, jsonrpc: '2.0', method: 'hello' } |
| 243 | + const payloadE = { id: 5, jsonrpc: '2.0', method: 'hello' } |
| 244 | + const payload = [payloadA, payloadB, payloadC, payloadD, payloadE] |
| 245 | + |
| 246 | + const res = await engine.handle(payload) |
| 247 | + assert.ok(res, 'has res') |
| 248 | + assert.ok(Array.isArray(res), 'res is array') |
| 249 | + assert.equal(res[0].result, 1, 'has expected result') |
| 250 | + assert.equal(res[1].result, 2, 'has expected result') |
| 251 | + assert.equal(res[2].result, 3, 'has expected result') |
| 252 | + assert.ok(!res[3].result, 'has no result') |
| 253 | + assert.equal(res[3].error.code, -32603, 'has expected error') |
| 254 | + assert.equal(res[4].result, 5, 'has expected result') |
| 255 | + }) |
| 256 | + |
188 | 257 | it('basic notifications', function (done) { |
189 | 258 | const engine = new RpcEngine() |
190 | 259 |
|
@@ -286,4 +355,72 @@ describe('basic tests', function () { |
286 | 355 | done() |
287 | 356 | }) |
288 | 357 | }) |
| 358 | + |
| 359 | + it('handles error in next handler', function (done) { |
| 360 | + const engine = new RpcEngine() |
| 361 | + |
| 362 | + engine.push(function (_req, _res, next, _end) { |
| 363 | + next(function (_cb) { |
| 364 | + throw new Error('foo') |
| 365 | + }) |
| 366 | + }) |
| 367 | + |
| 368 | + engine.push(function (_req, res, _next, end) { |
| 369 | + res.result = 42 |
| 370 | + end() |
| 371 | + }) |
| 372 | + |
| 373 | + const payload = { id: 1, jsonrpc: '2.0', method: 'hello' } |
| 374 | + |
| 375 | + engine.handle(payload, (err, _res) => { |
| 376 | + assert.ok(err, 'did error') |
| 377 | + assert.equal(err.message, 'foo', 'error has expected message') |
| 378 | + done() |
| 379 | + }) |
| 380 | + }) |
| 381 | + |
| 382 | + it('handles failure to end request', function (done) { |
| 383 | + const engine = new RpcEngine() |
| 384 | + |
| 385 | + engine.push(function (_req, res, next, _end) { |
| 386 | + res.result = 42 |
| 387 | + next() |
| 388 | + }) |
| 389 | + |
| 390 | + const payload = { id: 1, jsonrpc: '2.0', method: 'hello' } |
| 391 | + |
| 392 | + engine.handle(payload, (err, res) => { |
| 393 | + assert.ok(err, 'should have errored') |
| 394 | + assert.ok( |
| 395 | + err.message.startsWith('JsonRpcEngine: Nothing ended request:'), |
| 396 | + 'should have expected error message', |
| 397 | + ) |
| 398 | + assert.ok(!res.result, 'should not have result') |
| 399 | + done() |
| 400 | + }) |
| 401 | + }) |
| 402 | + |
| 403 | + it('handles batch request processing error', function (done) { |
| 404 | + const engine = new RpcEngine() |
| 405 | + stub(engine, '_promiseHandle').throws(new Error('foo')) |
| 406 | + |
| 407 | + engine.handle([{}], (err) => { |
| 408 | + assert.ok(err, 'did error') |
| 409 | + assert.equal(err.message, 'foo', 'error has expected message') |
| 410 | + done() |
| 411 | + }) |
| 412 | + }) |
| 413 | + |
| 414 | + it('handles batch request processing error (async)', async function () { |
| 415 | + const engine = new RpcEngine() |
| 416 | + stub(engine, '_promiseHandle').throws(new Error('foo')) |
| 417 | + |
| 418 | + try { |
| 419 | + await engine.handle([{}]) |
| 420 | + assert.fail('should have errored') |
| 421 | + } catch (err) { |
| 422 | + assert.ok(err, 'did error') |
| 423 | + assert.equal(err.message, 'foo', 'error has expected message') |
| 424 | + } |
| 425 | + }) |
289 | 426 | }) |
0 commit comments