diff options
| author | Per Larsson <[email protected]> | 2022-02-21 15:14:11 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2022-02-21 15:14:11 +0100 |
| commit | db1c9605e3afbaf86f4231ba4eb7976d896f286b (patch) | |
| tree | 54b451da4247c69575ff1a05ed006ecef3905c85 /zenserver-test/zenserver-test.cpp | |
| parent | If open(O_CREAT) is used then a file mode must be given (diff) | |
| parent | Removed optional offset for GetView. (diff) | |
| download | zen-db1c9605e3afbaf86f4231ba4eb7976d896f286b.tar.xz zen-db1c9605e3afbaf86f4231ba4eb7976d896f286b.zip | |
Initial support for websockets.
Diffstat (limited to 'zenserver-test/zenserver-test.cpp')
| -rw-r--r-- | zenserver-test/zenserver-test.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp index 293af7816..6a1b54b79 100644 --- a/zenserver-test/zenserver-test.cpp +++ b/zenserver-test/zenserver-test.cpp @@ -19,6 +19,7 @@ #include <zencore/timer.h> #include <zenhttp/httpclient.h> #include <zenhttp/httpshared.h> +#include <zenhttp/websocket.h> #include <zenhttp/zenhttp.h> #include <zenutil/cache/cache.h> #include <zenutil/zenserverprocess.h> @@ -2588,6 +2589,56 @@ private: std::vector<std::unique_ptr<ZenServerInstance> > m_Instances; }; +class IoDispatcher +{ +public: + IoDispatcher(asio::io_context& IoCtx) : m_IoCtx(IoCtx) {} + ~IoDispatcher() { Stop(); } + + void Run() + { + Stop(); + + m_Running = true; + + m_IoThread = std::thread([this]() { + try + { + m_IoCtx.run(); + } + catch (std::exception& Error) + { + m_Error = Error; + } + + m_Running = false; + }); + } + + void Stop() + { + if (m_Running) + { + m_Running = false; + + if (m_IoThread.joinable()) + { + m_IoThread.join(); + } + } + } + + bool IsRunning() const { return m_Running; } + + const std::exception& Error() { return m_Error; } + +private: + asio::io_context& m_IoCtx; + std::thread m_IoThread; + std::exception m_Error; + std::atomic_bool m_Running{false}; +}; + TEST_CASE("http.basics") { using namespace std::literals; @@ -2655,6 +2706,51 @@ TEST_CASE("http.package") CHECK_EQ(ResponsePackage, TestPackage); } +TEST_CASE("websocket.basic") +{ + std::filesystem::path TestDir = TestEnv.CreateNewTestDir(); + const uint16_t PortNumber = 13337; + const auto MaxWaitTime = std::chrono::seconds(5); + + ZenServerInstance Inst(TestEnv); + Inst.SetTestDir(TestDir); + Inst.SpawnServer(PortNumber, "--websocket-port=8848"sv); + Inst.WaitUntilReady(); + + asio::io_context IoCtx; + IoDispatcher IoDispatcher(IoCtx); + auto WebSocket = WebSocketClient::Create(IoCtx); + + auto ConnectFuture = WebSocket->Connect({.Host = "127.0.0.1", .Port = 8848, .Endpoint = "/zen"}); + IoDispatcher.Run(); + + ConnectFuture.wait_for(MaxWaitTime); + CHECK(ConnectFuture.get()); + + for (size_t Idx = 0; Idx < 10; Idx++) + { + CbObjectWriter Request; + Request << "Method"sv + << "SayHello"sv; + + WebSocketMessage RequestMsg; + RequestMsg.SetMessageType(WebSocketMessageType::kRequest); + RequestMsg.SetBody(Request.Save()); + + auto ResponseFuture = WebSocket->SendRequest(std::move(RequestMsg)); + ResponseFuture.wait_for(MaxWaitTime); + + CbObject Response = ResponseFuture.get().Body().GetObject(); + std::string_view Message = Response["Result"].AsString(); + + CHECK(Message == "Hello Friend!!"sv); + } + + WebSocket->Disconnect(); + + IoDispatcher.Stop(); +} + # if 0 TEST_CASE("lifetime.owner") { |