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 "test/test_bitcoin.h" | |||||
#include <string> | |||||
#include <boost/test/unit_test.hpp> | |||||
struct TestRPCCommand : public RPCCommand { | |||||
deadalnix: class | |||||
jasonbcoxAuthorUnsubmitted Done Inline ActionsI used struct since everything is public in this definition, but I'm fine with either. jasonbcox: I used struct since everything is public in this definition, but I'm fine with either. | |||||
UniValue Execute(const UniValue &args) const { return UniValue(); } | |||||
static const std::string commandName; | |||||
std::string GetName() const { return commandName; } | |||||
}; | |||||
const std::string TestRPCCommand::commandName = "testcommand"; | |||||
BOOST_FIXTURE_TEST_SUITE(rpc_server_tests, TestingSetup) | |||||
BOOST_AUTO_TEST_CASE(rpc_server_match_command) { | |||||
RPCServer rpcServer; | |||||
rpcServer.RegisterCommand(new TestRPCCommand()); | |||||
deadalnixUnsubmitted Done Inline ActionsYou are clearly leaking here deadalnix: You are clearly leaking here | |||||
BOOST_CHECK(rpcServer.MatchCommand(TestRPCCommand::commandName)); | |||||
// Checking non-existent command errors as expected | |||||
BOOST_CHECK(rpcServer.MatchCommand("this-command-does-not-exist") == | |||||
nullptr); | |||||
} | |||||
struct ArgsTestRPCCommand : public RPCCommand { | |||||
UniValue Execute(const UniValue &args) const { | |||||
BOOST_CHECK_EQUAL(args["arg1"].get_str(), "value1"); | |||||
return UniValue(); | |||||
} | |||||
static const std::string commandName; | |||||
std::string GetName() const { return commandName; } | |||||
deadalnixUnsubmitted Done Inline ActionsYou have this oddly recurring pattern in all your commands. The code is trying to tell you something, you just have to listen to it. If you do not, you'll end up iterating forever. deadalnix: You have this oddly recurring pattern in all your commands. The code is trying to tell you… | |||||
jasonbcoxAuthorUnsubmitted Done Inline ActionsI only did this for testing purposes. I realized it looked odd, but I wanted the string value to be accessible to the test. Alternatively, I can do: request.strMethod = testCommand->GetName(); I just felt it did not test ExecuteCommand as well. jasonbcox: I only did this for testing purposes. I realized it looked odd, but I wanted the string value… | |||||
}; | |||||
const std::string ArgsTestRPCCommand::commandName = "testcommand"; | |||||
BOOST_AUTO_TEST_CASE(rpc_server_execute_command) { | |||||
DummyConfig config; | |||||
RPCServer rpcServer; | |||||
rpcServer.RegisterCommand(new ArgsTestRPCCommand()); | |||||
UniValue args(UniValue::VOBJ); | |||||
args.pushKV("arg1", "value1"); | |||||
JSONRPCRequest request; | |||||
request.strMethod = ArgsTestRPCCommand::commandName; | |||||
request.params = args; | |||||
rpcServer.ExecuteCommand(config, request); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() |
class