peerplays-fc/tests/network/http/websocket_test.cpp

59 lines
1.9 KiB
C++
Raw Normal View History

2015-08-30 20:15:20 +00:00
#include <boost/test/unit_test.hpp>
#include <fc/network/http/websocket.hpp>
#include <iostream>
BOOST_AUTO_TEST_SUITE(fc_network)
BOOST_AUTO_TEST_CASE(websocket_test)
{
fc::http::websocket_client client;
fc::http::websocket_connection_ptr s_conn, c_conn;
2019-10-08 06:43:42 +00:00
int port;
2015-08-30 20:15:20 +00:00
{
fc::http::websocket_server server;
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);
});
});
2019-10-08 06:43:42 +00:00
server.listen( 0 );
port = server.get_listening_port();
2015-08-30 20:15:20 +00:00
server.start_accept();
std::string echo;
2019-10-08 06:43:42 +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-10-08 06:43:42 +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-10-08 06:43:42 +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-10-08 06:43:42 +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
2019-10-08 06:43:42 +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-10-08 06:43:42 +00:00
fc::usleep( fc::milliseconds(100) );
2015-08-30 20:15:20 +00:00
BOOST_CHECK_EQUAL("echo: hello world", echo);
}
2019-10-08 06:43:42 +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);
2015-08-30 20:15:20 +00:00
}
2019-10-08 06:43:42 +00:00
2015-08-30 20:15:20 +00:00
BOOST_AUTO_TEST_SUITE_END()