diff options
-rw-r--r-- | tests/test-whiteboard.cpp | 115 |
1 files changed, 81 insertions, 34 deletions
diff --git a/tests/test-whiteboard.cpp b/tests/test-whiteboard.cpp index b183021..4ed10ae 100644 --- a/tests/test-whiteboard.cpp +++ b/tests/test-whiteboard.cpp @@ -155,44 +155,91 @@ protected: pid_t m_pid{}; }; +class WebsocketClient +{ +public: + WebsocketClient() + { + std::string host = "::1"; + auto const port = "9876" ; + + // These objects perform our I/O + boost::asio::ip::tcp::resolver resolver{ioc_}; + ws_ = std::make_unique<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>>(ioc_); + + // Look up the domain name + resolver_results_ = resolver.resolve(host, port); + + connect(); + handshake(); + } + + void connect() + { + // Make the connection on the IP address we get from a lookup + ep_ = boost::asio::connect(boost::beast::get_lowest_layer(*ws_), resolver_results_); + } + + void handshake() + { + // Update the host_ string. This will provide the value of the + // Host HTTP header during the WebSocket handshake. + // See https://tools.ietf.org/html/rfc7230#section-5.4 + std::string host{"[::1]:9876"}; + + // Set a decorator to change the User-Agent of the handshake + ws_->set_option(boost::beast::websocket::stream_base::decorator( + [](boost::beast::websocket::request_type& req) + { + req.set(boost::beast::http::field::user_agent, + std::string("Reichwein.IT Test Websocket Client")); + })); + + // Perform the websocket handshake + ws_->handshake(host, "/"); + + } + + void write(const std::string& data) + { + ws_->write(boost::asio::buffer(data)); + } + + std::string read() + { + boost::beast::flat_buffer buffer; + ws_->read(buffer); + return {boost::asio::buffers_begin(buffer.data()), boost::asio::buffers_end(buffer.data())}; + } + + ~WebsocketClient() + { + } + +private: + boost::asio::io_context ioc_; + boost::asio::ip::tcp::resolver::results_type resolver_results_; + std::unique_ptr<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>> ws_; + boost::asio::ip::tcp::endpoint ep_; +}; + TEST_F(WhiteboardTest, connection) { - std::string host = "::1"; - auto const port = "9876" ; - - // The io_context is required for all I/O - boost::asio::io_context ioc; - - // These objects perform our I/O - boost::asio::ip::tcp::resolver resolver{ioc}; - boost::beast::websocket::stream<boost::asio::ip::tcp::socket> ws{ioc}; - - // Look up the domain name - auto const results = resolver.resolve(host, port); - - // Make the connection on the IP address we get from a lookup - auto ep = boost::asio::connect(boost::beast::get_lowest_layer(ws), results); - - // Update the host_ string. This will provide the value of the - // Host HTTP header during the WebSocket handshake. - // See https://tools.ietf.org/html/rfc7230#section-5.4 - if (host == "::1") - host = "[" + host + "]"; - host += ':' + std::to_string(ep.port()); - - // Set a decorator to change the User-Agent of the handshake - ws.set_option(boost::beast::websocket::stream_base::decorator( - [](boost::beast::websocket::request_type& req) - { - req.set(boost::beast::http::field::user_agent, - std::string("Reichwein.IT Test Websocket Client")); - })); - - // Perform the websocket handshake - ws.handshake(host, "/"); + WebsocketClient wc; } -TEST_F(WhiteboardTest, getfile) +TEST_F(WhiteboardTest, generate_id) { + WebsocketClient wc; + + wc.write("<request><command>newid</command></request>"); + std::string result0 {wc.read()}; + ASSERT_EQ(result0.size(), 6); + + wc.write("<request><command>newid</command></request>"); + std::string result1 {wc.read()}; + ASSERT_EQ(result1.size(), 6); + + ASSERT_NE(result0, result1); } |