From b146e93a3a30b0208fbfc356671a4f787be07359 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Fri, 24 Jul 2020 12:49:36 -0700 Subject: [PATCH] (unittest) port most unittests to the new server API --- docs/CHANGELOG.md | 4 +- ixwebsocket/IXWebSocketVersion.h | 2 +- test/IXTest.cpp | 15 +++--- test/IXWebSocketBroadcastTest.cpp | 69 +++++++++++++------------- test/IXWebSocketChatTest.cpp | 16 +++--- test/IXWebSocketCloseTest.cpp | 68 ++++++++++++-------------- test/IXWebSocketServerTest.cpp | 76 ++++++++++++++--------------- test/IXWebSocketSubProtocolTest.cpp | 65 ++++++++++++------------ 8 files changed, 151 insertions(+), 164 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4de9eaaa..d1776601 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,9 +1,9 @@ # Changelog All changes to this project will be documented in this file. -## [9.10.4] - 2020-07-24 +## [9.10.5] - 2020-07-24 -(ws) port ws snake to the new server API +(unittest) port most unittests to the new server API ## [9.10.3] - 2020-07-24 diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index 74d5589e..24d3976c 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "9.10.4" +#define IX_WEBSOCKET_VERSION "9.10.5" diff --git a/test/IXTest.cpp b/test/IXTest.cpp index bfd01941..d6a185ce 100644 --- a/test/IXTest.cpp +++ b/test/IXTest.cpp @@ -84,12 +84,12 @@ namespace ix bool startWebSocketEchoServer(ix::WebSocketServer& server) { - server.setOnConnectionCallback([&server](std::shared_ptr webSocket, - std::shared_ptr connectionState, - std::unique_ptr connectionInfo) { - auto remoteIp = connectionInfo->remoteIp; - webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &server]( - const ix::WebSocketMessagePtr& msg) { + server.setOnClientMessageCallback( + [&server](std::shared_ptr connectionState, + ConnectionInfo& connectionInfo, + WebSocket& webSocket, + const ix::WebSocketMessagePtr& msg) { + auto remoteIp = connectionInfo.remoteIp; if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New connection"; @@ -109,14 +109,13 @@ namespace ix { for (auto&& client : server.getClients()) { - if (client != webSocket) + if (client.get() != &webSocket) { client->send(msg->str, msg->binary); } } } }); - }); auto res = server.listen(); if (!res.first) diff --git a/test/IXWebSocketBroadcastTest.cpp b/test/IXWebSocketBroadcastTest.cpp index d10bc6c7..74ef3c7f 100644 --- a/test/IXWebSocketBroadcastTest.cpp +++ b/test/IXWebSocketBroadcastTest.cpp @@ -189,44 +189,43 @@ namespace bool preferTLS = true; server.setTLSOptions(makeServerTLSOptions(preferTLS)); - server.setOnConnectionCallback( - [&server, &connectionId](std::shared_ptr webSocket, - std::shared_ptr connectionState, - std::unique_ptr connectionInfo) { - auto remoteIp = connectionInfo->remoteIp; - webSocket->setOnMessageCallback( - [webSocket, connectionState, remoteIp, &connectionId, &server]( - const ix::WebSocketMessagePtr& msg) { - if (msg->type == ix::WebSocketMessageType::Open) - { - TLogger() << "New connection"; - connectionState->computeId(); - TLogger() << "remote ip: " << remoteIp; - TLogger() << "id: " << connectionState->getId(); - TLogger() << "Uri: " << msg->openInfo.uri; - TLogger() << "Headers:"; - for (auto it : msg->openInfo.headers) - { - TLogger() << it.first << ": " << it.second; - } + server.setOnClientMessageCallback( + [&server, &connectionId](std::shared_ptr connectionState, + ConnectionInfo& connectionInfo, + WebSocket& webSocket, + const ix::WebSocketMessagePtr& msg) { + auto remoteIp = connectionInfo.remoteIp; - connectionId = connectionState->getId(); - } - else if (msg->type == ix::WebSocketMessageType::Close) + + if (msg->type == ix::WebSocketMessageType::Open) + { + TLogger() << "New connection"; + connectionState->computeId(); + TLogger() << "remote ip: " << remoteIp; + TLogger() << "id: " << connectionState->getId(); + TLogger() << "Uri: " << msg->openInfo.uri; + TLogger() << "Headers:"; + for (auto it : msg->openInfo.headers) + { + TLogger() << it.first << ": " << it.second; + } + + connectionId = connectionState->getId(); + } + else if (msg->type == ix::WebSocketMessageType::Close) + { + TLogger() << "Closed connection"; + } + else if (msg->type == ix::WebSocketMessageType::Message) + { + for (auto&& client : server.getClients()) + { + if (client.get() != &webSocket) { - TLogger() << "Closed connection"; + client->send(msg->str, msg->binary); } - else if (msg->type == ix::WebSocketMessageType::Message) - { - for (auto&& client : server.getClients()) - { - if (client != webSocket) - { - client->send(msg->str, msg->binary); - } - } - } - }); + } + } }); auto res = server.listen(); diff --git a/test/IXWebSocketChatTest.cpp b/test/IXWebSocketChatTest.cpp index 9b806fe0..9d7aee8c 100644 --- a/test/IXWebSocketChatTest.cpp +++ b/test/IXWebSocketChatTest.cpp @@ -193,13 +193,12 @@ namespace bool startServer(ix::WebSocketServer& server) { - server.setOnConnectionCallback([&server](std::shared_ptr webSocket, - std::shared_ptr connectionState, - - std::unique_ptr connectionInfo) { - auto remoteIp = connectionInfo->remoteIp; - webSocket->setOnMessageCallback([webSocket, connectionState, remoteIp, &server]( - const ix::WebSocketMessagePtr& msg) { + server.setOnClientMessageCallback( + [&server](std::shared_ptr connectionState, + ConnectionInfo& connectionInfo, + WebSocket& webSocket, + const ix::WebSocketMessagePtr& msg) { + auto remoteIp = connectionInfo.remoteIp; if (msg->type == ix::WebSocketMessageType::Open) { TLogger() << "New connection"; @@ -220,14 +219,13 @@ namespace { for (auto&& client : server.getClients()) { - if (client != webSocket) + if (client.get() != &webSocket) { client->sendBinary(msg->str); } } } }); - }); auto res = server.listen(); if (!res.first) diff --git a/test/IXWebSocketCloseTest.cpp b/test/IXWebSocketCloseTest.cpp index 8091a53a..bdb90934 100644 --- a/test/IXWebSocketCloseTest.cpp +++ b/test/IXWebSocketCloseTest.cpp @@ -168,45 +168,39 @@ namespace std::mutex& mutexWrite) { // A dev/null server - server.setOnConnectionCallback( - [&receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, &mutexWrite]( - std::shared_ptr webSocket, - std::shared_ptr connectionState, - std::unique_ptr connectionInfo) { - auto remoteIp = connectionInfo->remoteIp; - webSocket->setOnMessageCallback([webSocket, - connectionState, - remoteIp, - &receivedCloseCode, - &receivedCloseReason, - &receivedCloseRemote, - &mutexWrite](const ix::WebSocketMessagePtr& msg) { - if (msg->type == ix::WebSocketMessageType::Open) - { - TLogger() << "New server connection"; - TLogger() << "remote ip: " << remoteIp; - TLogger() << "id: " << connectionState->getId(); - TLogger() << "Uri: " << msg->openInfo.uri; - TLogger() << "Headers:"; - for (auto it : msg->openInfo.headers) - { - TLogger() << it.first << ": " << it.second; - } - } - else if (msg->type == ix::WebSocketMessageType::Close) - { - std::stringstream ss; - ss << "Server closed connection(" << msg->closeInfo.code << "," - << msg->closeInfo.reason << ")"; - log(ss.str()); + server.setOnClientMessageCallback( + [&receivedCloseCode, &receivedCloseReason, &receivedCloseRemote, &mutexWrite - std::lock_guard lck(mutexWrite); - - receivedCloseCode = msg->closeInfo.code; - receivedCloseReason = std::string(msg->closeInfo.reason); - receivedCloseRemote = msg->closeInfo.remote; + ](std::shared_ptr connectionState, + ConnectionInfo& connectionInfo, + WebSocket& webSocket, + const ix::WebSocketMessagePtr& msg) { + auto remoteIp = connectionInfo.remoteIp; + if (msg->type == ix::WebSocketMessageType::Open) + { + TLogger() << "New server connection"; + TLogger() << "remote ip: " << remoteIp; + TLogger() << "id: " << connectionState->getId(); + TLogger() << "Uri: " << msg->openInfo.uri; + TLogger() << "Headers:"; + for (auto it : msg->openInfo.headers) + { + TLogger() << it.first << ": " << it.second; } - }); + } + else if (msg->type == ix::WebSocketMessageType::Close) + { + std::stringstream ss; + ss << "Server closed connection(" << msg->closeInfo.code << "," + << msg->closeInfo.reason << ")"; + log(ss.str()); + + std::lock_guard lck(mutexWrite); + + receivedCloseCode = msg->closeInfo.code; + receivedCloseReason = std::string(msg->closeInfo.reason); + receivedCloseRemote = msg->closeInfo.remote; + } }); auto res = server.listen(); diff --git a/test/IXWebSocketServerTest.cpp b/test/IXWebSocketServerTest.cpp index 62fa1388..5acf7bb7 100644 --- a/test/IXWebSocketServerTest.cpp +++ b/test/IXWebSocketServerTest.cpp @@ -33,45 +33,45 @@ namespace ix }; server.setConnectionStateFactory(factory); - server.setOnConnectionCallback( - [&server, &connectionId](std::shared_ptr webSocket, - std::shared_ptr connectionState, - std::unique_ptr connectionInfo) { - auto remoteIp = connectionInfo->remoteIp; - webSocket->setOnMessageCallback( - [webSocket, connectionState, remoteIp, &connectionId, &server]( - const ix::WebSocketMessagePtr& msg) { - if (msg->type == ix::WebSocketMessageType::Open) - { - TLogger() << "New connection"; - connectionState->computeId(); - TLogger() << "remote ip: " << remoteIp; - TLogger() << "id: " << connectionState->getId(); - TLogger() << "Uri: " << msg->openInfo.uri; - TLogger() << "Headers:"; - for (auto it : msg->openInfo.headers) - { - TLogger() << it.first << ": " << it.second; - } + server.setOnClientMessageCallback([&server, &connectionId - connectionId = connectionState->getId(); - } - else if (msg->type == ix::WebSocketMessageType::Close) - { - TLogger() << "Closed connection"; - } - else if (msg->type == ix::WebSocketMessageType::Message) - { - for (auto&& client : server.getClients()) - { - if (client != webSocket) - { - client->send(msg->str, msg->binary); - } - } - } - }); - }); + ](std::shared_ptr connectionState, + ConnectionInfo& connectionInfo, + WebSocket& webSocket, + const ix::WebSocketMessagePtr& msg) { + auto remoteIp = connectionInfo.remoteIp; + + + if (msg->type == ix::WebSocketMessageType::Open) + { + TLogger() << "New connection"; + connectionState->computeId(); + TLogger() << "remote ip: " << remoteIp; + TLogger() << "id: " << connectionState->getId(); + TLogger() << "Uri: " << msg->openInfo.uri; + TLogger() << "Headers:"; + for (auto it : msg->openInfo.headers) + { + TLogger() << it.first << ": " << it.second; + } + + connectionId = connectionState->getId(); + } + else if (msg->type == ix::WebSocketMessageType::Close) + { + TLogger() << "Closed connection"; + } + else if (msg->type == ix::WebSocketMessageType::Message) + { + for (auto&& client : server.getClients()) + { + if (client.get() != &webSocket) + { + client->send(msg->str, msg->binary); + } + } + } + }); auto res = server.listen(); if (!res.first) diff --git a/test/IXWebSocketSubProtocolTest.cpp b/test/IXWebSocketSubProtocolTest.cpp index d2bb0d03..931c6a0d 100644 --- a/test/IXWebSocketSubProtocolTest.cpp +++ b/test/IXWebSocketSubProtocolTest.cpp @@ -16,43 +16,40 @@ using namespace ix; bool startServer(ix::WebSocketServer& server, std::string& subProtocols) { - server.setOnConnectionCallback( - [&server, &subProtocols](std::shared_ptr webSocket, - std::shared_ptr connectionState, - std::unique_ptr connectionInfo) { - auto remoteIp = connectionInfo->remoteIp; - webSocket->setOnMessageCallback( - [webSocket, connectionState, remoteIp, &server, &subProtocols]( - const ix::WebSocketMessagePtr& msg) { - if (msg->type == ix::WebSocketMessageType::Open) - { - TLogger() << "New connection"; - TLogger() << "remote ip: " << remoteIp; - TLogger() << "id: " << connectionState->getId(); - TLogger() << "Uri: " << msg->openInfo.uri; - TLogger() << "Headers:"; - for (auto it : msg->openInfo.headers) - { - TLogger() << it.first << ": " << it.second; - } + server.setOnClientMessageCallback( + [&server, &subProtocols](std::shared_ptr connectionState, + ConnectionInfo& connectionInfo, + WebSocket& webSocket, + const ix::WebSocketMessagePtr& msg) { + auto remoteIp = connectionInfo.remoteIp; + if (msg->type == ix::WebSocketMessageType::Open) + { + TLogger() << "New connection"; + TLogger() << "remote ip: " << remoteIp; + TLogger() << "id: " << connectionState->getId(); + TLogger() << "Uri: " << msg->openInfo.uri; + TLogger() << "Headers:"; + for (auto it : msg->openInfo.headers) + { + TLogger() << it.first << ": " << it.second; + } - subProtocols = msg->openInfo.headers["Sec-WebSocket-Protocol"]; - } - else if (msg->type == ix::WebSocketMessageType::Close) + subProtocols = msg->openInfo.headers["Sec-WebSocket-Protocol"]; + } + else if (msg->type == ix::WebSocketMessageType::Close) + { + log("Closed connection"); + } + else if (msg->type == ix::WebSocketMessageType::Message) + { + for (auto&& client : server.getClients()) + { + if (client.get() != &webSocket) { - log("Closed connection"); + client->sendBinary(msg->str); } - else if (msg->type == ix::WebSocketMessageType::Message) - { - for (auto&& client : server.getClients()) - { - if (client != webSocket) - { - client->sendBinary(msg->str); - } - } - } - }); + } + } }); auto res = server.listen();