From aab5b246690f7aa17bb7d6516d896206713d22e6 Mon Sep 17 00:00:00 2001 From: tomonari-t Date: Wed, 10 Jun 2020 22:28:00 +0900 Subject: [PATCH] fix(rpc): handle rpc request with empty params --- lib/rpc/validation.js | 2 +- test/rpc/validation.js | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test/rpc/validation.js diff --git a/lib/rpc/validation.js b/lib/rpc/validation.js index 5075a61..386ffc7 100644 --- a/lib/rpc/validation.js +++ b/lib/rpc/validation.js @@ -8,7 +8,7 @@ module.exports = { * @param {Function[]} validators array of validator */ middleware (method, requiredParamsCount, validators) { - return function (params, cb) { + return function (params = [], cb) { if (params.length < requiredParamsCount) { const err = { code: INVALID_PARAMS, diff --git a/test/rpc/validation.js b/test/rpc/validation.js new file mode 100644 index 0000000..bfed8ec --- /dev/null +++ b/test/rpc/validation.js @@ -0,0 +1,52 @@ + +const test = require('tape') + +const { startRPC } = require('./helpers') +const { middleware } = require('../../lib/rpc/validation') +const { baseRequest } = require('./helpers') +const { checkError } = require('./util') +const { INVALID_PARAMS } = require('../../lib/rpc/error-code') + +const prefix = 'rpc/validation:' + +test(`${prefix} should work without \`params\` when it's optional`, t => { + const mockMethodName = 'mock' + const server = startRPC({ + [mockMethodName]: middleware((params, cb) => { + cb() + }, 0, []) + }) + + const req = { + jsonrpc: '2.0', + method: mockMethodName, + id: 1 + } + const expectRes = res => { + t.equal(res.body.error, undefined, 'should not return an error object') + } + baseRequest(t, server, req, 200, expectRes) +}) + +test(`${prefix} should return error without \`params\` when it's required`, t => { + const mockMethodName = 'mock' + const server = startRPC({ + [mockMethodName]: middleware((params, cb) => { + cb() + }, 1, []) + }) + + const req = { + jsonrpc: '2.0', + method: mockMethodName, + id: 1 + } + + const expectRes = checkError( + t, + INVALID_PARAMS, + 'missing value for required argument 0' + ) + + baseRequest(t, server, req, 200, expectRes) +})