2019-05-04 21:57:55 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
2015-03-09 22:50:20 +00:00
|
|
|
#include <fc/api.hpp>
|
|
|
|
|
#include <fc/log/logger.hpp>
|
2015-03-11 14:49:30 +00:00
|
|
|
#include <fc/rpc/api_connection.hpp>
|
2015-03-27 20:29:33 +00:00
|
|
|
#include <fc/rpc/websocket_api.hpp>
|
2015-03-09 22:50:20 +00:00
|
|
|
|
|
|
|
|
class calculator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
int32_t add( int32_t a, int32_t b ); // not implemented
|
|
|
|
|
int32_t sub( int32_t a, int32_t b ); // not implemented
|
2015-03-31 15:31:56 +00:00
|
|
|
void on_result( const std::function<void(int32_t)>& cb );
|
2015-05-04 18:07:22 +00:00
|
|
|
void on_result2( const std::function<void(int32_t)>& cb, int test );
|
2015-03-09 22:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
2015-05-04 18:07:22 +00:00
|
|
|
FC_API( calculator, (add)(sub)(on_result)(on_result2) )
|
2015-03-09 22:50:20 +00:00
|
|
|
|
2015-03-10 22:52:27 +00:00
|
|
|
|
2015-03-27 20:29:33 +00:00
|
|
|
class login_api
|
2015-03-10 22:52:27 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
fc::api<calculator> get_calc()const
|
|
|
|
|
{
|
|
|
|
|
FC_ASSERT( calc );
|
|
|
|
|
return *calc;
|
|
|
|
|
}
|
|
|
|
|
fc::optional<fc::api<calculator>> calc;
|
2015-04-01 21:06:13 +00:00
|
|
|
std::set<std::string> test( const std::string&, const std::string& ) { return std::set<std::string>(); }
|
2015-03-10 22:52:27 +00:00
|
|
|
};
|
2015-03-30 18:05:21 +00:00
|
|
|
FC_API( login_api, (get_calc)(test) );
|
2015-03-10 22:52:27 +00:00
|
|
|
|
2019-05-04 02:51:15 +00:00
|
|
|
|
|
|
|
|
class optionals_api
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
std::string foo( const std::string& first, const fc::optional<std::string>& second,
|
|
|
|
|
const fc::optional<std::string>& third ) {
|
|
|
|
|
return fc::json::to_string(fc::variants{first, {second, 2}, {third, 2}});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
FC_API( optionals_api, (foo) );
|
|
|
|
|
|
2015-03-10 13:14:33 +00:00
|
|
|
using namespace fc;
|
|
|
|
|
|
2015-03-09 22:50:20 +00:00
|
|
|
class some_calculator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-03-31 15:31:56 +00:00
|
|
|
int32_t add( int32_t a, int32_t b ) { wlog("."); if( _cb ) _cb(a+b); return a+b; }
|
|
|
|
|
int32_t sub( int32_t a, int32_t b ) { wlog(".");if( _cb ) _cb(a-b); return a-b; }
|
|
|
|
|
void on_result( const std::function<void(int32_t)>& cb ) { wlog( "set callback" ); _cb = cb; return ; }
|
2015-05-04 18:07:22 +00:00
|
|
|
void on_result2( const std::function<void(int32_t)>& cb, int test ){}
|
2015-03-31 15:31:56 +00:00
|
|
|
std::function<void(int32_t)> _cb;
|
2015-03-09 22:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
2015-03-27 20:29:33 +00:00
|
|
|
using namespace fc::http;
|
|
|
|
|
using namespace fc::rpc;
|
2015-03-09 22:50:20 +00:00
|
|
|
|
2018-03-12 21:50:59 +00:00
|
|
|
#define MAX_DEPTH 10
|
|
|
|
|
|
2019-05-04 21:57:55 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE(api_tests)
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(login_test) {
|
2015-03-31 15:31:56 +00:00
|
|
|
try {
|
2015-03-27 20:29:33 +00:00
|
|
|
fc::api<calculator> calc_api( std::make_shared<some_calculator>() );
|
|
|
|
|
|
2019-05-04 02:51:15 +00:00
|
|
|
auto server = std::make_shared<fc::http::websocket_server>();
|
|
|
|
|
server->on_connection([&]( const websocket_connection_ptr& c ){
|
2019-04-03 13:37:38 +00:00
|
|
|
auto wsc = std::make_shared<websocket_api_connection>(c, MAX_DEPTH);
|
2015-03-27 20:29:33 +00:00
|
|
|
auto login = std::make_shared<login_api>();
|
|
|
|
|
login->calc = calc_api;
|
|
|
|
|
wsc->register_api(fc::api<login_api>(login));
|
|
|
|
|
c->set_session_data( wsc );
|
|
|
|
|
});
|
|
|
|
|
|
2019-05-04 22:44:33 +00:00
|
|
|
server->listen( 0 );
|
|
|
|
|
auto listen_port = server->get_listening_port();
|
2019-05-04 02:51:15 +00:00
|
|
|
server->start_accept();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
auto client = std::make_shared<fc::http::websocket_client>();
|
2019-05-04 22:44:33 +00:00
|
|
|
auto con = client->connect( "ws://localhost:" + std::to_string(listen_port) );
|
|
|
|
|
server->stop_listening();
|
2019-05-04 02:51:15 +00:00
|
|
|
auto apic = std::make_shared<websocket_api_connection>(con, MAX_DEPTH);
|
|
|
|
|
auto remote_login_api = apic->get_remote_api<login_api>();
|
|
|
|
|
auto remote_calc = remote_login_api->get_calc();
|
|
|
|
|
bool remote_triggered = false;
|
|
|
|
|
remote_calc->on_result( [&remote_triggered]( uint32_t r ) { remote_triggered = true; } );
|
2019-05-04 21:57:55 +00:00
|
|
|
BOOST_CHECK_EQUAL(remote_calc->add( 4, 5 ), 9);
|
|
|
|
|
BOOST_CHECK(remote_triggered);
|
2019-05-04 02:51:15 +00:00
|
|
|
|
2019-05-04 22:44:33 +00:00
|
|
|
client->synchronous_close();
|
|
|
|
|
server->synchronous_close();
|
|
|
|
|
fc::usleep(fc::milliseconds(50));
|
2019-05-04 02:51:15 +00:00
|
|
|
client.reset();
|
|
|
|
|
server.reset();
|
|
|
|
|
} FC_LOG_AND_RETHROW()
|
|
|
|
|
} FC_LOG_AND_RETHROW()
|
2019-05-04 21:57:55 +00:00
|
|
|
}
|
2015-03-10 22:52:27 +00:00
|
|
|
|
2019-05-04 21:57:55 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(optionals_test) {
|
2015-03-11 14:49:30 +00:00
|
|
|
try {
|
2019-05-04 02:51:15 +00:00
|
|
|
auto optionals = std::make_shared<optionals_api>();
|
|
|
|
|
fc::api<optionals_api> oapi(optionals);
|
2019-05-04 21:57:55 +00:00
|
|
|
BOOST_CHECK_EQUAL(oapi->foo("a"), "[\"a\",null,null]");
|
|
|
|
|
BOOST_CHECK_EQUAL(oapi->foo("a", "b"), "[\"a\",\"b\",null]");
|
|
|
|
|
BOOST_CHECK_EQUAL(oapi->foo("a", "b", "c"), "[\"a\",\"b\",\"c\"]");
|
|
|
|
|
BOOST_CHECK_EQUAL(oapi->foo("a", {}, "c"), "[\"a\",null,\"c\"]");
|
2019-05-04 02:51:15 +00:00
|
|
|
|
|
|
|
|
auto server = std::make_shared<fc::http::websocket_server>();
|
|
|
|
|
server->on_connection([&]( const websocket_connection_ptr& c ){
|
|
|
|
|
auto wsc = std::make_shared<websocket_api_connection>(*c, MAX_DEPTH);
|
|
|
|
|
wsc->register_api(fc::api<optionals_api>(optionals));
|
|
|
|
|
c->set_session_data( wsc );
|
|
|
|
|
});
|
2015-03-10 13:14:33 +00:00
|
|
|
|
2019-05-04 22:44:33 +00:00
|
|
|
server->listen( 0 );
|
|
|
|
|
auto listen_port = server->get_listening_port();
|
2019-05-04 02:51:15 +00:00
|
|
|
server->start_accept();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
auto client = std::make_shared<fc::http::websocket_client>();
|
2019-05-04 22:44:33 +00:00
|
|
|
auto con = client->connect( "ws://localhost:" + std::to_string(listen_port) );
|
|
|
|
|
server->stop_listening();
|
2019-05-04 02:51:15 +00:00
|
|
|
auto apic = std::make_shared<websocket_api_connection>(*con, MAX_DEPTH);
|
|
|
|
|
auto remote_optionals = apic->get_remote_api<optionals_api>();
|
|
|
|
|
|
2019-05-04 21:57:55 +00:00
|
|
|
BOOST_CHECK_EQUAL(remote_optionals->foo("a"), "[\"a\",null,null]");
|
|
|
|
|
BOOST_CHECK_EQUAL(remote_optionals->foo("a", "b"), "[\"a\",\"b\",null]");
|
|
|
|
|
BOOST_CHECK_EQUAL(remote_optionals->foo("a", "b", "c"), "[\"a\",\"b\",\"c\"]");
|
|
|
|
|
BOOST_CHECK_EQUAL(remote_optionals->foo("a", {}, "c"), "[\"a\",null,\"c\"]");
|
2019-05-04 02:51:15 +00:00
|
|
|
|
2019-05-04 22:44:33 +00:00
|
|
|
client->synchronous_close();
|
|
|
|
|
server->synchronous_close();
|
|
|
|
|
fc::usleep(fc::milliseconds(50));
|
2019-05-04 02:51:15 +00:00
|
|
|
client.reset();
|
|
|
|
|
server.reset();
|
|
|
|
|
} FC_LOG_AND_RETHROW()
|
|
|
|
|
} FC_LOG_AND_RETHROW()
|
2015-03-09 22:50:20 +00:00
|
|
|
}
|
2019-05-04 21:57:55 +00:00
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|