2015-08-30 20:15:20 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
|
|
#include <fc/network/http/websocket.hpp>
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
2019-05-23 14:56:11 +00:00
|
|
|
#include <fc/log/logger.hpp>
|
|
|
|
|
#include <fc/log/console_appender.hpp>
|
2015-08-30 20:15:20 +00:00
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(fc_network)
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(websocket_test)
|
|
|
|
|
{
|
2019-05-23 14:56:11 +00:00
|
|
|
// set up logging
|
2019-06-21 18:39:16 +00:00
|
|
|
fc::appender::ptr ca(new fc::console_appender);
|
2019-05-23 14:56:11 +00:00
|
|
|
fc::logger l = fc::logger::get("rpc");
|
|
|
|
|
l.add_appender( ca );
|
|
|
|
|
|
2015-08-30 20:15:20 +00:00
|
|
|
fc::http::websocket_client client;
|
|
|
|
|
fc::http::websocket_connection_ptr s_conn, c_conn;
|
2018-03-22 16:33:17 +00:00
|
|
|
int port;
|
2015-08-30 20:15:20 +00:00
|
|
|
{
|
2020-05-01 21:46:19 +00:00
|
|
|
// even with this key set, if the remote does not provide it, it will revert to
|
2019-05-24 16:15:02 +00:00
|
|
|
// the remote endpoint
|
2020-05-01 21:46:19 +00:00
|
|
|
fc::http::websocket_server server("MyProxyHeaderKey");
|
2015-08-30 20:15:20 +00:00
|
|
|
server.on_connection([&]( const fc::http::websocket_connection_ptr& c ){
|
|
|
|
|
s_conn = c;
|
|
|
|
|
c->on_message_handler([&](const std::string& s){
|
|
|
|
|
c->send_message("echo: " + s);
|
|
|
|
|
});
|
2020-05-02 09:46:21 +00:00
|
|
|
c->on_http_handler([&](const std::string& s){
|
|
|
|
|
fc::http::reply reply;
|
|
|
|
|
reply.body_as_string = "http-echo: " + s;
|
|
|
|
|
return reply;
|
|
|
|
|
});
|
2015-08-30 20:15:20 +00:00
|
|
|
});
|
|
|
|
|
|
2019-05-06 18:19:24 +00:00
|
|
|
server.listen( 0 );
|
|
|
|
|
port = server.get_listening_port();
|
2018-03-22 16:33:17 +00:00
|
|
|
|
2015-08-30 20:15:20 +00:00
|
|
|
server.start_accept();
|
|
|
|
|
|
|
|
|
|
std::string echo;
|
2018-03-22 16:33:17 +00:00
|
|
|
c_conn = client.connect( "ws://localhost:" + fc::to_string(port) );
|
2015-08-30 20:15:20 +00:00
|
|
|
c_conn->on_message_handler([&](const std::string& s){
|
|
|
|
|
echo = s;
|
|
|
|
|
});
|
|
|
|
|
c_conn->send_message( "hello world" );
|
2019-05-06 18:19:24 +00:00
|
|
|
fc::usleep( fc::milliseconds(100) );
|
2015-08-30 20:15:20 +00:00
|
|
|
BOOST_CHECK_EQUAL("echo: hello world", echo);
|
|
|
|
|
c_conn->send_message( "again" );
|
2019-05-06 18:19:24 +00:00
|
|
|
fc::usleep( fc::milliseconds(100) );
|
2015-08-30 20:15:20 +00:00
|
|
|
BOOST_CHECK_EQUAL("echo: again", echo);
|
|
|
|
|
|
|
|
|
|
s_conn->close(0, "test");
|
2019-05-06 18:19:24 +00:00
|
|
|
fc::usleep( fc::milliseconds(100) );
|
|
|
|
|
BOOST_CHECK_THROW(c_conn->send_message( "again" ), fc::exception);
|
2015-08-30 20:15:20 +00:00
|
|
|
|
2018-03-22 16:33:17 +00:00
|
|
|
c_conn = client.connect( "ws://localhost:" + fc::to_string(port) );
|
2015-08-30 20:15:20 +00:00
|
|
|
c_conn->on_message_handler([&](const std::string& s){
|
|
|
|
|
echo = s;
|
|
|
|
|
});
|
|
|
|
|
c_conn->send_message( "hello world" );
|
2019-05-06 18:19:24 +00:00
|
|
|
fc::usleep( fc::milliseconds(100) );
|
2015-08-30 20:15:20 +00:00
|
|
|
BOOST_CHECK_EQUAL("echo: hello world", echo);
|
2020-05-02 09:46:21 +00:00
|
|
|
|
|
|
|
|
// Testing on_http
|
|
|
|
|
std::string server_endpoint_string = "127.0.0.1:" + fc::to_string(port);
|
|
|
|
|
fc::ip::endpoint server_endpoint = fc::ip::endpoint::from_string( server_endpoint_string );
|
|
|
|
|
fc::http::connection c_http_conn;
|
|
|
|
|
c_http_conn.connect_to( server_endpoint );
|
|
|
|
|
|
|
|
|
|
std::string url = "http://localhost:" + fc::to_string(port);
|
|
|
|
|
fc::http::reply reply = c_http_conn.request( "POST", url, "hello world again" );
|
|
|
|
|
std::string reply_body( reply.body.begin(), reply.body.end() );
|
|
|
|
|
BOOST_CHECK_EQUAL(200, reply.status);
|
|
|
|
|
BOOST_CHECK_EQUAL("http-echo: hello world again", reply_body );
|
2015-08-30 20:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-06 18:19:24 +00:00
|
|
|
BOOST_CHECK_THROW(c_conn->send_message( "again" ), fc::assert_exception);
|
|
|
|
|
BOOST_CHECK_THROW(client.connect( "ws://localhost:" + fc::to_string(port) ), fc::exception);
|
2019-05-24 15:41:21 +00:00
|
|
|
l.remove_appender(ca);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(websocket_test_with_proxy_header)
|
|
|
|
|
{
|
|
|
|
|
// set up logging
|
2019-06-21 18:39:16 +00:00
|
|
|
fc::appender::ptr ca(new fc::console_appender);
|
2019-05-24 15:41:21 +00:00
|
|
|
fc::logger l = fc::logger::get("rpc");
|
2020-05-02 08:40:28 +00:00
|
|
|
auto old_log_level = l.get_log_level();
|
|
|
|
|
l.set_log_level( fc::log_level::info );
|
2019-05-24 15:41:21 +00:00
|
|
|
l.add_appender( ca );
|
|
|
|
|
|
|
|
|
|
fc::http::websocket_client client;
|
|
|
|
|
// add the proxy header element
|
|
|
|
|
client.append_header("MyProxyHeaderKey", "MyServer:8080");
|
|
|
|
|
|
|
|
|
|
fc::http::websocket_connection_ptr s_conn, c_conn;
|
|
|
|
|
int port;
|
|
|
|
|
{
|
|
|
|
|
// the server will be on the lookout for the key in the header
|
2020-05-01 21:46:19 +00:00
|
|
|
fc::http::websocket_server server("MyProxyHeaderKey");
|
2019-05-24 15:41:21 +00:00
|
|
|
server.on_connection([&]( const fc::http::websocket_connection_ptr& c ){
|
|
|
|
|
s_conn = c;
|
|
|
|
|
c->on_message_handler([&](const std::string& s){
|
|
|
|
|
c->send_message("echo: " + s);
|
|
|
|
|
});
|
2020-05-02 09:46:21 +00:00
|
|
|
c->on_http_handler([&](const std::string& s){
|
|
|
|
|
fc::http::reply reply;
|
|
|
|
|
reply.body_as_string = "http-echo: " + s;
|
|
|
|
|
return reply;
|
|
|
|
|
});
|
2019-05-24 15:41:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen( 0 );
|
|
|
|
|
port = server.get_listening_port();
|
|
|
|
|
|
|
|
|
|
server.start_accept();
|
|
|
|
|
|
|
|
|
|
std::string echo;
|
|
|
|
|
c_conn = client.connect( "ws://localhost:" + fc::to_string(port) );
|
|
|
|
|
c_conn->on_message_handler([&](const std::string& s){
|
|
|
|
|
echo = s;
|
|
|
|
|
});
|
|
|
|
|
c_conn->send_message( "hello world" );
|
|
|
|
|
fc::usleep( fc::milliseconds(100) );
|
|
|
|
|
BOOST_CHECK_EQUAL("echo: hello world", echo);
|
|
|
|
|
c_conn->send_message( "again" );
|
|
|
|
|
fc::usleep( fc::milliseconds(100) );
|
|
|
|
|
BOOST_CHECK_EQUAL("echo: again", echo);
|
|
|
|
|
|
|
|
|
|
s_conn->close(0, "test");
|
|
|
|
|
fc::usleep( fc::milliseconds(100) );
|
|
|
|
|
BOOST_CHECK_THROW(c_conn->send_message( "again" ), fc::exception);
|
|
|
|
|
|
|
|
|
|
c_conn = client.connect( "ws://localhost:" + fc::to_string(port) );
|
|
|
|
|
c_conn->on_message_handler([&](const std::string& s){
|
|
|
|
|
echo = s;
|
|
|
|
|
});
|
|
|
|
|
c_conn->send_message( "hello world" );
|
|
|
|
|
fc::usleep( fc::milliseconds(100) );
|
|
|
|
|
BOOST_CHECK_EQUAL("echo: hello world", echo);
|
2020-05-02 09:46:21 +00:00
|
|
|
|
|
|
|
|
// Testing on_http
|
|
|
|
|
std::string server_endpoint_string = "127.0.0.1:" + fc::to_string(port);
|
|
|
|
|
fc::ip::endpoint server_endpoint = fc::ip::endpoint::from_string( server_endpoint_string );
|
|
|
|
|
fc::http::connection c_http_conn;
|
|
|
|
|
c_http_conn.connect_to( server_endpoint );
|
|
|
|
|
|
|
|
|
|
std::string url = "http://localhost:" + fc::to_string(port);
|
|
|
|
|
fc::http::header fwd("MyProxyHeaderKey", "MyServer:8080");
|
|
|
|
|
fc::http::headers headers {fwd};
|
|
|
|
|
fc::http::reply reply = c_http_conn.request( "POST", url, "hello world again", headers );
|
|
|
|
|
std::string reply_body( reply.body.begin(), reply.body.end() );
|
|
|
|
|
BOOST_CHECK_EQUAL(200, reply.status);
|
|
|
|
|
BOOST_CHECK_EQUAL("http-echo: hello world again", reply_body );
|
2019-05-24 15:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(c_conn->send_message( "again" ), fc::assert_exception);
|
|
|
|
|
BOOST_CHECK_THROW(client.connect( "ws://localhost:" + fc::to_string(port) ), fc::exception);
|
2020-05-02 08:40:28 +00:00
|
|
|
|
2019-05-24 15:41:21 +00:00
|
|
|
l.remove_appender(ca);
|
2020-05-02 08:40:28 +00:00
|
|
|
l.set_log_level(old_log_level);
|
2015-08-30 20:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|