Skip to content

Commit

Permalink
Add tests for ping messages
Browse files Browse the repository at this point in the history
  • Loading branch information
garethsb committed Jun 6, 2019
1 parent ce53a15 commit 468ce71
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
51 changes: 47 additions & 4 deletions Release/tests/functional/websockets/client/send_msg_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,32 @@ SUITE(send_msg_tests)
}

template<class SocketClientClass>
pplx::task<void> send_pong_msg_helper(SocketClientClass & client, web::uri uri, test_websocket_server & server)
pplx::task<void> send_ping_msg_helper(SocketClientClass & client, web::uri uri, test_websocket_server & server,
const std::string& body = "")
{
server.next_message(
[](test_websocket_msg msg) // Handler to verify the message sent by the client.
{ websocket_asserts::assert_message_equals(msg, "", test_websocket_message_type::WEB_SOCKET_PONG_TYPE); });
[body](test_websocket_msg msg) // Handler to verify the message sent by the client.
{ websocket_asserts::assert_message_equals(msg, body, test_websocket_message_type::WEB_SOCKET_PING_TYPE); });

client.connect(uri).wait();

websocket_outgoing_message msg;
msg.set_pong_message();
msg.set_ping_message(body);
return client.send(msg);
}

template<class SocketClientClass>
pplx::task<void> send_pong_msg_helper(SocketClientClass & client, web::uri uri, test_websocket_server & server,
const std::string& body = "")
{
server.next_message(
[body](test_websocket_msg msg) // Handler to verify the message sent by the client.
{ websocket_asserts::assert_message_equals(msg, body, test_websocket_message_type::WEB_SOCKET_PONG_TYPE); });

client.connect(uri).wait();

websocket_outgoing_message msg;
msg.set_pong_message(body);
return client.send(msg);
}

Expand Down Expand Up @@ -493,6 +509,24 @@ SUITE(send_msg_tests)
}

#if !defined(__cplusplus_winrt)
// Send a ping message to the server
TEST_FIXTURE(uri_address, send_ping_msg)
{
test_websocket_server server;
websocket_client client;
send_ping_msg_helper(client, m_uri, server).wait();
client.close().wait();
}

// Send a ping message to the server with a body
TEST_FIXTURE(uri_address, send_ping_msg_body)
{
test_websocket_server server;
websocket_client client;
send_ping_msg_helper(client, m_uri, server, "abcdefghijklmnopqrstuvwxyz").wait();
client.close().wait();
}

// Send an unsolicited pong message to the server
TEST_FIXTURE(uri_address, send_pong_msg)
{
Expand All @@ -502,6 +536,15 @@ SUITE(send_msg_tests)
client.close().wait();
}

// Send an unsolicited pong message to the server with a body
TEST_FIXTURE(uri_address, send_pong_msg_body)
{
test_websocket_server server;
websocket_client client;
send_pong_msg_helper(client, m_uri, server, "abcdefghijklmnopqrstuvwxyz").wait();
client.close().wait();
}

// Send an unsolicited pong message to the server with websocket_callback_client
TEST_FIXTURE(uri_address, send_pong_msg_callback_client)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ class _test_websocket_server
m_server_connected.set_exception(std::runtime_error("Connection attempt failed."));
});

m_srv.set_ping_handler([this](websocketpp::connection_hdl hdl, std::string input) {
auto fn = m_test_srv->get_next_message_handler();
assert(fn);

test_websocket_msg wsmsg;

wsmsg.set_data(std::vector<uint8_t>(input.begin(), input.end()));

wsmsg.set_msg_type(WEB_SOCKET_PING_TYPE);
fn(wsmsg);

return true;
});

m_srv.set_pong_handler([this](websocketpp::connection_hdl hdl, std::string input) {
auto fn = m_test_srv->get_next_message_handler();
assert(fn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum test_websocket_message_type
WEB_SOCKET_UTF8_MESSAGE_TYPE,
WEB_SOCKET_UTF8_FRAGMENT_TYPE,
WEB_SOCKET_CLOSE_TYPE,
WEB_SOCKET_PING_TYPE,
WEB_SOCKET_PONG_TYPE
};

Expand Down

0 comments on commit 468ce71

Please sign in to comment.