Skip to content

Commit

Permalink
add ws test
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Sep 28, 2024
1 parent 8e07090 commit 97c2940
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/simple_socket/ws/WebSocketConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace simple_socket {

if (callbacks_.onClose) callbacks_.onClose(this);

if (!self) {
if (self) {
std::vector<uint8_t> closeFrame = {0x88};// FIN, Close frame
closeFrame.push_back(0);
conn_->write(closeFrame);
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ add_executable(test_un test_un.cpp)
add_test(NAME test_un COMMAND test_un)
target_link_libraries(test_un PRIVATE simple_socket Catch2::Catch2WithMain)

add_executable(test_ws test_ws.cpp)
add_test(NAME test_ws COMMAND test_ws)
target_link_libraries(test_ws PRIVATE simple_socket Catch2::Catch2WithMain)

add_executable(test_conversion test_conversion.cpp)
add_test(NAME test_conversion COMMAND test_conversion)
target_link_libraries(test_conversion PRIVATE simple_socket Catch2::Catch2WithMain)
Expand Down
1 change: 0 additions & 1 deletion tests/integration/run_ws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "simple_socket/WebSocket.hpp"

#include <iostream>
#include <thread>

using namespace simple_socket;

Expand Down
70 changes: 70 additions & 0 deletions tests/test_ws.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

#include "simple_socket/WebSocket.hpp"
#include "simple_socket/util/port_query.hpp"

#include <atomic>
#include <thread>

#include <catch2/catch_test_macros.hpp>

using namespace simple_socket;

TEST_CASE("test Websocket") {

const auto port = getAvailablePort(8000, 9000);
REQUIRE(port);

std::atomic_bool serverOpen{false};
std::atomic_bool serverClose{false};
std::string servermsg;

std::atomic_bool clientOpen{false};
std::atomic_bool clientClose{false};
std::string clientmsg;

WebSocket ws(*port);
ws.start();


ws.onOpen = [&](auto c) {
serverOpen = true;
c->send("Hello from server!");
};

ws.onMessage = [&](auto c, const auto& msg) {
servermsg = msg;
};

ws.onClose = [&](auto c) {
serverClose = true;
};


WebSocketClient client;
client.onOpen = [&](auto c) {
clientOpen = true;
c->send("Hello from client!");
};

client.onMessage = [&](auto c, const auto& msg) {
clientmsg = msg;
};

client.onClose = [&](auto c) {
clientClose = true;
};

client.connect("127.0.0.1", *port);
std::this_thread::sleep_for(std::chrono::seconds(1));

client.close();
ws.stop();

CHECK(serverOpen == true);
CHECK(serverClose == true);
CHECK(clientOpen == true);
CHECK(clientClose == true);

CHECK(servermsg == "Hello from client!");
CHECK(clientmsg == "Hello from server!");
}

0 comments on commit 97c2940

Please sign in to comment.