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

RPC test method cleanup #126

Merged
merged 1 commit into from
Jun 9, 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
189 changes: 47 additions & 142 deletions test/rpc/eth/getBlockByHash.js
Original file line number Diff line number Diff line change
@@ -1,176 +1,81 @@
const test = require('tape')

const request = require('supertest')
const { INVALID_PARAMS } = require('../../../lib/rpc/error-code')
const { startRPC, closeRPC, createManager, createNode } = require('../helpers')
const { baseSetup, params, baseRequest } = require('../helpers')
const { checkError } = require('../util')

test('call eth_getBlockByHash with valid arguments', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())
const method = 'eth_getBlockByHash'

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockByHash',
params: [
'0x910abca1728c53e8d6df870dd7af5352e974357dc58205dea1676be17ba6becf',
true
],
id: 1
}
test(`${method}: call with valid arguments`, t => {
const server = baseSetup()

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
if (res.body.result.number !== 444444) {
throw new Error('number is not 444444')
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
const req = params(method, [
'0x910abca1728c53e8d6df870dd7af5352e974357dc58205dea1676be17ba6becf',
true
])
const expectRes = res => {
let msg = 'should return the correct number'
t.equal(res.body.result.number, 444444, msg)
}
baseRequest(t, server, req, 200, expectRes)
})

test('call eth_getBlockByHash with false for second argument', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockByHash',
params: [
'0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae',
false
],
id: 1
test(`${method}: call with false for second argument`, t => {
const server = baseSetup()

const req = params(method, [
'0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae',
false
])
const expectRes = res => {
let msg = 'should return the correct number'
t.equal(res.body.result.number, 444444, msg)
msg = 'should return only the hashes of the transactions'
t.equal(typeof (res.body.result.transactions[0]), 'string', msg)
}

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(res => {
if (res.body.result.number !== 444444) {
throw new Error('number is not 444444')
}
if (typeof res.body.result.transactions[0] !== 'string') {
throw new Error('only the hashes of the transactions')
}
})
.end((err, res) => {
closeRPC(server)
t.end(err)
})
baseRequest(t, server, req, 200, expectRes)
})

test('call eth_getBlockByHash with invalid block hash without 0x', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())
test(`${method}: call with invalid block hash without 0x`, t => {
const server = baseSetup()

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockByHash',
params: ['WRONG BLOCK NUMBER', true],
id: 1
}
const checkInvalidParams = checkError(
const req = params(method, ['WRONG BLOCK NUMBER', true])
const expectRes = checkError(
t,
INVALID_PARAMS,
'invalid argument 0: hex string without 0x prefix'
)

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
baseRequest(t, server, req, 200, expectRes)
})

test('call eth_getBlockByHash with invalid hex string as block hash', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())
test(`${method}: call with invalid hex string as block hash`, t => {
const server = baseSetup()

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockByHash',
params: ['0xWRONG BLOCK NUMBER', true],
id: 1
}
const checkInvalidParams = checkError(
const req = params(method, ['0xWRONG BLOCK NUMBER', true])
const expectRes = checkError(
t,
INVALID_PARAMS,
'invalid argument 0: invalid block hash'
)

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
baseRequest(t, server, req, 200, expectRes)
})

test('call eth_getBlockByHash without second parameter', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockByHash',
params: ['0x0'],
id: 1
}
const server = baseSetup()

const checkInvalidParams = checkError(
const req = params(method, ['0x0'])
const expectRes = checkError(
t,
INVALID_PARAMS,
'missing value for required argument 1'
)

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
baseRequest(t, server, req, 200, expectRes)
})

test('call eth_getBlockByHash with invalid second parameter', t => {
const manager = createManager(createNode())
const server = startRPC(manager.getMethods())

const req = {
jsonrpc: '2.0',
method: 'eth_getBlockByHash',
params: ['0x0', 'INVALID PARAMETER'],
id: 1
}

const checkInvalidParams = checkError(INVALID_PARAMS)
const server = baseSetup()

request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(req)
.expect(200)
.expect(checkInvalidParams)
.end(err => {
closeRPC(server)
t.end(err)
})
const req = params(method, ['0x0', 'INVALID PARAMETER'])
const expectRes = checkError(t, INVALID_PARAMS)
baseRequest(t, server, req, 200, expectRes)
})
Loading