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> | |||||
deadalnix: This is included last | |||||
#include <boost/test/unit_test.hpp> | |||||
BOOST_FIXTURE_TEST_SUITE(rpc_server_tests, TestingSetup) | |||||
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"); | |||||
// Registered commands execute properly | |||||
JSONRPCRequest request; | |||||
request.strMethod = commandName; | |||||
request.params = args; | |||||
BOOST_CHECK_NO_THROW(rpcServer.ExecuteCommand(config, request)); | |||||
deadalnixUnsubmitted Done Inline ActionsYou probably want to return something specific and check that it is returned. That would at least prove that the code gets executed. Right now the only thing that's tested is that it doesn't throw, which is not putting the bar very high. deadalnix: You probably want to return something specific and check that it is returned. That would at… | |||||
// Not-registered commands throw an exception as expected | |||||
JSONRPCRequest badCommandRequest; | |||||
badCommandRequest.strMethod = "this-command-does-not-exist"; | |||||
BOOST_CHECK_THROW(rpcServer.ExecuteCommand(config, badCommandRequest), | |||||
UniValue); | |||||
deadalnixUnsubmitted Done Inline ActionsAlways check that the error yo get is the one you expect. For all you know the test can be fubared and you are testing nothing. deadalnix: Always check that the error yo get is the one you expect. For all you know the test can be… | |||||
jasonbcoxAuthorUnsubmitted Done Inline ActionsI was having trouble finding something like BOOST_CHECK_EXCEPTION (until I did). My fallback was similar behavior that others tests are using. I've fixed this now. jasonbcox: I was having trouble finding something like BOOST_CHECK_EXCEPTION (until I did). My fallback… | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() |
This is included last