Changeset View
Changeset View
Standalone View
Standalone View
src/test/rpc_server_tests.cpp
- This file was added.
// Copyright (c) 2018-2019 The Bitcoin developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#include "chainparams.h" | |||||
#include "config.h" | |||||
#include "rpc/jsonrpcrequest.h" | |||||
#include "rpc/server.h" | |||||
#include "util.h" | |||||
#include "test/test_bitcoin.h" | |||||
#include <string> | |||||
#include <boost/test/unit_test.hpp> | |||||
class TestRPCCommand : public RPCCommand { | |||||
public: | |||||
TestRPCCommand(std::string nameIn) : RPCCommand(nameIn) {} | |||||
UniValue Execute(const UniValue &args) const override { return UniValue(); } | |||||
}; | |||||
BOOST_FIXTURE_TEST_SUITE(rpc_server_tests, TestingSetup) | |||||
BOOST_AUTO_TEST_CASE(rpc_server_match_command) { | |||||
RPCServer rpcServer; | |||||
const std::string commandName = "testcommand"; | |||||
rpcServer.RegisterCommand(MakeUnique<TestRPCCommand>(commandName)); | |||||
BOOST_CHECK(rpcServer.MatchCommand(commandName)); | |||||
// Checking non-existent command errors as expected | |||||
BOOST_CHECK_EQUAL(rpcServer.MatchCommand("this-command-does-not-exist"), | |||||
NULLPTR(RPCCommand)); | |||||
} | |||||
class ArgsTestRPCCommand : public RPCCommand { | |||||
public: | |||||
ArgsTestRPCCommand(std::string nameIn) : RPCCommand(nameIn) {} | |||||
UniValue Execute(const UniValue &args) const override { | |||||
BOOST_CHECK_EQUAL(args["arg1"].get_str(), "value1"); | |||||
return UniValue(); | |||||
} | |||||
}; | |||||
BOOST_AUTO_TEST_CASE(rpc_server_execute_command) { | |||||
DummyConfig config; | |||||
RPCServer rpcServer; | |||||
const std::string commandName = "testcommand"; | |||||
rpcServer.RegisterCommand(MakeUnique<ArgsTestRPCCommand>(commandName)); | |||||
UniValue args(UniValue::VOBJ); | |||||
args.pushKV("arg1", "value1"); | |||||
JSONRPCRequest request; | |||||
request.strMethod = commandName; | |||||
request.params = args; | |||||
rpcServer.ExecuteCommand(config, request); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() |