|
1 | 1 | #include <string_view> |
2 | 2 | #include <vector> |
| 3 | +#include <random> |
3 | 4 | #include <boost/asio/co_spawn.hpp> |
4 | 5 | #include <boost/asio/connect.hpp> |
5 | 6 | #include <boost/asio/detached.hpp> |
@@ -165,24 +166,85 @@ TEST_SUITE("[ASYNC]") |
165 | 166 | TEST_CASE("WEBSOCKET") |
166 | 167 | { |
167 | 168 | boost::asio::io_context ioc{1}; |
168 | | - tcp_acceptor acceptor(ioc, {tcp::v4(), 6666}); |
| 169 | + tcp_acceptor acceptor(ioc, {tcp::v4(), 6667}); |
169 | 170 | tcp_socket peer(ioc); |
170 | 171 | tcp_socket client(ioc); |
171 | 172 | tcp_resolver resolver(ioc); |
172 | 173 | bool exception_thrown{false}; |
173 | 174 |
|
174 | | - std::vector<uint8_t> data_peer; |
175 | | - std::vector<uint8_t> data_client; |
176 | | - std::string text_peer; |
177 | | - std::string text_client; |
178 | | - |
179 | | - try |
| 175 | + http::request req; |
| 176 | + std::string buf; |
| 177 | + std::vector<char> data; |
| 178 | + std::string text; |
| 179 | + std::vector<char> data_peer; |
| 180 | + std::vector<char> data_client; |
| 181 | + |
| 182 | + std::mt19937 eng(std::random_device{}()); |
| 183 | + std::uniform_int_distribution<unsigned int> d{0, 255}; |
| 184 | + data.resize(1024); |
| 185 | + std::generate(begin(data), end(data), [&]{return static_cast<char>(d(eng));}); |
| 186 | + text = "Peace is a lie. There is only passion."; |
| 187 | + text += "Through passion, I gain strength."; |
| 188 | + text += "Through strength, I gain power."; |
| 189 | + text += "Through power, I gain victory."; |
| 190 | + |
| 191 | + SUBCASE("async - client sends") |
180 | 192 | { |
| 193 | + try |
| 194 | + { |
| 195 | + acceptor.async_accept(peer, [&](boost::system::error_code ec) { |
| 196 | + REQUIRE(!bool(ec)); |
| 197 | + http::async_http_read(peer, req, buf, [&](boost::system::error_code ec, size_t){ |
| 198 | + REQUIRE(!bool(ec)); |
| 199 | + REQUIRE(req.is_websocket_req()); |
| 200 | + http::async_ws_accept(peer, req, [&](boost::system::error_code ec, std::size_t) { |
| 201 | + REQUIRE(!bool(ec)); |
| 202 | + http::async_ws_read(peer, data_peer, true, [&](boost::system::error_code ec, bool is_text) { |
| 203 | + REQUIRE(!bool(ec)); |
| 204 | + REQUIRE(!is_text); |
| 205 | + REQUIRE(data_peer.size() == data.size()); |
| 206 | + REQUIRE(std::equal(begin(data_peer), end(data_peer), begin(data))); |
| 207 | + http::async_ws_read(peer, data_peer, true, [&](boost::system::error_code ec, bool is_text) { |
| 208 | + REQUIRE(!bool(ec)); |
| 209 | + REQUIRE(is_text); |
| 210 | + REQUIRE(data_peer.size() == text.size()); |
| 211 | + REQUIRE(std::equal(begin(data_peer), end(data_peer), begin(text))); |
| 212 | + http::async_ws_read(peer, data_peer, true, [&](boost::system::error_code ec, bool is_text) { |
| 213 | + REQUIRE(ec == http::ws_going_away); |
| 214 | + }); |
| 215 | + }); |
| 216 | + }); |
| 217 | + }); |
| 218 | + }); |
| 219 | + }); |
181 | 220 |
|
182 | | - } |
183 | | - catch(const std::exception& e) |
184 | | - { |
185 | | - exception_thrown = true; |
| 221 | + resolver.async_resolve("localhost", "6667", [&](boost::system::error_code ec, const auto& endpoints) { |
| 222 | + REQUIRE(!bool(ec)); |
| 223 | + boost::asio::async_connect(client, endpoints, [&](boost::system::error_code ec, auto endpoint) { |
| 224 | + REQUIRE(!bool(ec)); |
| 225 | + http::async_ws_handshake(client, "localhost", "/ws", [&](boost::system::error_code ec) { |
| 226 | + REQUIRE(!bool(ec)); |
| 227 | + data_client.assign(begin(data), end(data)); |
| 228 | + http::async_ws_write(client, data_client, false, false, [&](boost::system::error_code ec, std::size_t) { |
| 229 | + REQUIRE(!bool(ec)); |
| 230 | + data_client.assign(begin(text), end(text)); |
| 231 | + http::async_ws_write(client, data_client, true, false, [&](boost::system::error_code ec, std::size_t) { |
| 232 | + REQUIRE(!bool(ec)); |
| 233 | + http::async_ws_close(client, http::ws_going_away, false, [&](boost::system::error_code ec) { |
| 234 | + REQUIRE(!bool(ec)); |
| 235 | + }); |
| 236 | + }); |
| 237 | + }); |
| 238 | + }); |
| 239 | + }); |
| 240 | + }); |
| 241 | + |
| 242 | + ioc.run(); |
| 243 | + } |
| 244 | + catch(const std::exception& e) |
| 245 | + { |
| 246 | + exception_thrown = true; |
| 247 | + } |
186 | 248 | } |
187 | 249 |
|
188 | 250 | REQUIRE(!exception_thrown); |
|
0 commit comments