diff options
Diffstat (limited to 'src/zenhttp/servers/wstest.cpp')
| -rw-r--r-- | src/zenhttp/servers/wstest.cpp | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/src/zenhttp/servers/wstest.cpp b/src/zenhttp/servers/wstest.cpp index 2134e4ff1..042afd8ff 100644 --- a/src/zenhttp/servers/wstest.cpp +++ b/src/zenhttp/servers/wstest.cpp @@ -485,7 +485,7 @@ TEST_CASE("websocket.integration") Ref<HttpServer> Server = CreateHttpAsioServer(AsioConfig{}); - int Port = Server->Initialize(7575, TmpDir.Path()); + int Port = Server->Initialize(0, TmpDir.Path()); REQUIRE(Port != 0); Server->RegisterService(TestService); @@ -797,7 +797,7 @@ TEST_CASE("websocket.client") Ref<HttpServer> Server = CreateHttpAsioServer(AsioConfig{}); - int Port = Server->Initialize(7576, TmpDir.Path()); + int Port = Server->Initialize(0, TmpDir.Path()); REQUIRE(Port != 0); Server->RegisterService(TestService); @@ -913,6 +913,75 @@ TEST_CASE("websocket.client") } } +TEST_CASE("websocket.client.unixsocket") +{ + WsTestService TestService; + ScopedTemporaryDirectory TmpDir; + std::string SocketPath = (TmpDir.Path() / "ws.sock").string(); + + Ref<HttpServer> Server = CreateHttpAsioServer(AsioConfig{.UnixSocketPath = SocketPath}); + + int Port = Server->Initialize(0, TmpDir.Path()); + REQUIRE(Port != 0); + + Server->RegisterService(TestService); + + std::thread ServerThread([&]() { Server->Run(false); }); + + auto ServerGuard = MakeGuard([&]() { + Server->RequestExit(); + if (ServerThread.joinable()) + { + ServerThread.join(); + } + Server->Close(); + }); + + Sleep(100); + + SUBCASE("connect, echo, close over unix socket") + { + TestWsClientHandler Handler; + HttpWsClientSettings Settings; + Settings.UnixSocketPath = SocketPath; + + HttpWsClient Client("ws://localhost/wstest/ws", Handler, Settings); + Client.Connect(); + + // Wait for OnWsOpen + auto Deadline = std::chrono::steady_clock::now() + 5s; + while (Handler.m_OpenCount.load() == 0 && std::chrono::steady_clock::now() < Deadline) + { + Sleep(10); + } + REQUIRE_EQ(Handler.m_OpenCount.load(), 1); + CHECK(Client.IsOpen()); + + // Send text, expect echo + Client.SendText("hello over unix socket"); + + Deadline = std::chrono::steady_clock::now() + 5s; + while (Handler.m_MessageCount.load() == 0 && std::chrono::steady_clock::now() < Deadline) + { + Sleep(10); + } + CHECK_EQ(Handler.m_MessageCount.load(), 1); + CHECK_EQ(Handler.m_LastMessage, "hello over unix socket"); + + // Close + Client.Close(1000, "done"); + + Deadline = std::chrono::steady_clock::now() + 5s; + while (Handler.m_CloseCount.load() == 0 && std::chrono::steady_clock::now() < Deadline) + { + Sleep(10); + } + + Sleep(50); + CHECK_FALSE(Client.IsOpen()); + } +} + TEST_SUITE_END(); void |