mirror of
https://github.com/zkitter/json-rpc-engine.git
synced 2026-01-10 07:37:54 -05:00
124 lines
3.1 KiB
JavaScript
124 lines
3.1 KiB
JavaScript
/* eslint-env mocha */
|
|
/* eslint require-await: off */
|
|
'use strict';
|
|
|
|
const { strict: assert } = require('assert');
|
|
const { JsonRpcEngine, createAsyncMiddleware } = require('../dist');
|
|
|
|
describe('createAsyncMiddleware', function () {
|
|
it('basic middleware test', function (done) {
|
|
const engine = new JsonRpcEngine();
|
|
|
|
engine.push(
|
|
createAsyncMiddleware(async (_req, res, _next) => {
|
|
res.result = 42;
|
|
}),
|
|
);
|
|
|
|
const payload = { id: 1, jsonrpc: '2.0', method: 'hello' };
|
|
|
|
engine.handle(payload, function (err, res) {
|
|
assert.ifError(err, 'did not error');
|
|
assert.ok(res, 'has res');
|
|
assert.equal(res.result, 42, 'has expected result');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('next middleware test', function (done) {
|
|
const engine = new JsonRpcEngine();
|
|
|
|
engine.push(
|
|
createAsyncMiddleware(async (_req, res, next) => {
|
|
assert.ifError(res.result, 'does not have result');
|
|
await next(); // eslint-disable-line node/callback-return
|
|
assert.equal(res.result, 1234, 'value was set as expected');
|
|
// override value
|
|
res.result = 42; // eslint-disable-line require-atomic-updates
|
|
}),
|
|
);
|
|
|
|
engine.push(function (_req, res, _next, end) {
|
|
res.result = 1234;
|
|
end();
|
|
});
|
|
|
|
const payload = { id: 1, jsonrpc: '2.0', method: 'hello' };
|
|
|
|
engine.handle(payload, function (err, res) {
|
|
assert.ifError(err, 'did not error');
|
|
assert.ok(res, 'has res');
|
|
assert.equal(res.result, 42, 'has expected result');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('basic throw test', function (done) {
|
|
const engine = new JsonRpcEngine();
|
|
|
|
const error = new Error('bad boy');
|
|
|
|
engine.push(
|
|
createAsyncMiddleware(async (_req, _res, _next) => {
|
|
throw error;
|
|
}),
|
|
);
|
|
|
|
const payload = { id: 1, jsonrpc: '2.0', method: 'hello' };
|
|
|
|
engine.handle(payload, function (err, _res) {
|
|
assert.ok(err, 'has err');
|
|
assert.equal(err, error, 'has expected result');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('throw after next test', function (done) {
|
|
const engine = new JsonRpcEngine();
|
|
|
|
const error = new Error('bad boy');
|
|
|
|
engine.push(
|
|
createAsyncMiddleware(async (_req, _res, next) => {
|
|
await next(); // eslint-disable-line node/callback-return
|
|
throw error;
|
|
}),
|
|
);
|
|
|
|
engine.push(function (_req, res, _next, end) {
|
|
res.result = 1234;
|
|
end();
|
|
});
|
|
|
|
const payload = { id: 1, jsonrpc: '2.0', method: 'hello' };
|
|
|
|
engine.handle(payload, function (err, _res) {
|
|
assert.ok(err, 'has err');
|
|
assert.equal(err, error, 'has expected result');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("doesn't await next", function (done) {
|
|
const engine = new JsonRpcEngine();
|
|
|
|
engine.push(
|
|
createAsyncMiddleware(async (_req, _res, next) => {
|
|
next();
|
|
}),
|
|
);
|
|
|
|
engine.push(function (_req, res, _next, end) {
|
|
res.result = 1234;
|
|
end();
|
|
});
|
|
|
|
const payload = { id: 1, jsonrpc: '2.0', method: 'hello' };
|
|
|
|
engine.handle(payload, function (err, _res) {
|
|
assert.ifError(err, 'has err');
|
|
done();
|
|
});
|
|
});
|
|
});
|