mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 04:52:59 +01:00
016e8c0bab
e1fdd2963baab68bb6a77af2ad7a07fcacd4e73e Test batch rpc with params (Gregory Sanders) Pull request description: Useful as an example and test case. ACKs for top commit: laanwj: ACK e1fdd2963baab68bb6a77af2ad7a07fcacd4e73e theStack: ACK e1fdd2963baab68bb6a77af2ad7a07fcacd4e73e Tree-SHA512: 2d2ba8960916342b264a14624857d6dd10005be12efafb3e970b82656f721c8f3700ebc9b8809de1b2f887d482b772043504aeaeebc7f2e1c8203f076a451526
64 lines
2.2 KiB
Python
Executable File
64 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2018 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
"""Tests some generic aspects of the RPC interface."""
|
|
|
|
from test_framework.authproxy import JSONRPCException
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import assert_equal
|
|
|
|
def expect_http_status(expected_http_status, expected_rpc_code,
|
|
fcn, *args):
|
|
try:
|
|
fcn(*args)
|
|
raise AssertionError("Expected RPC error %d, got none" % expected_rpc_code)
|
|
except JSONRPCException as exc:
|
|
assert_equal(exc.error["code"], expected_rpc_code)
|
|
assert_equal(exc.http_status, expected_http_status)
|
|
|
|
class RPCInterfaceTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 1
|
|
self.setup_clean_chain = True
|
|
|
|
def test_batch_request(self):
|
|
self.log.info("Testing basic JSON-RPC batch request...")
|
|
|
|
results = self.nodes[0].batch([
|
|
# A basic request that will work fine.
|
|
{"method": "getblockcount", "id": 1},
|
|
# Request that will fail. The whole batch request should still
|
|
# work fine.
|
|
{"method": "invalidmethod", "id": 2},
|
|
# Another call that should succeed.
|
|
{"method": "getblockhash", "id": 3, "params": [0]},
|
|
])
|
|
|
|
result_by_id = {}
|
|
for res in results:
|
|
result_by_id[res["id"]] = res
|
|
|
|
assert_equal(result_by_id[1]['error'], None)
|
|
assert_equal(result_by_id[1]['result'], 0)
|
|
|
|
assert_equal(result_by_id[2]['error']['code'], -32601)
|
|
assert_equal(result_by_id[2]['result'], None)
|
|
|
|
assert_equal(result_by_id[3]['error'], None)
|
|
assert result_by_id[3]['result'] is not None
|
|
|
|
def test_http_status_codes(self):
|
|
self.log.info("Testing HTTP status codes for JSON-RPC requests...")
|
|
|
|
expect_http_status(404, -32601, self.nodes[0].invalidmethod)
|
|
expect_http_status(500, -8, self.nodes[0].getblockhash, 42)
|
|
|
|
def run_test(self):
|
|
self.test_batch_request()
|
|
self.test_http_status_codes()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
RPCInterfaceTest().main()
|