Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

fix(rpc): handle rpc request with empty params #130

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rpc/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 52 additions & 0 deletions test/rpc/validation.js
Original file line number Diff line number Diff line change
@@ -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)
})