diff --git a/test/protocols/native/native_block.cpp b/test/protocols/native/native_block.cpp index de2301cb..7cd46a70 100644 --- a/test/protocols/native/native_block.cpp +++ b/test/protocols/native/native_block.cpp @@ -57,27 +57,39 @@ BOOST_AUTO_TEST_CASE(native__top__formal_xml__not_acceptable) BOOST_AUTO_TEST_CASE(native__ws_upgrade__always__expected) { - const auto ec = ws_upgrade({}); + const auto ec = ws_upgrade(); BOOST_REQUIRE_MESSAGE(!ec, ec.message()); } +BOOST_AUTO_TEST_CASE(native__ws_top__json__expected) +{ + BOOST_REQUIRE(!ws_upgrade()); + + const auto response = ws_request_json("/v1/top?format=json"); + BOOST_REQUIRE(response.is_int64()); + BOOST_REQUIRE_EQUAL(response.as_int64(), 9); +} + BOOST_AUTO_TEST_CASE(native__ws_top__text__expected) { - const auto ec = ws_upgrade({}); - BOOST_REQUIRE_MESSAGE(!ec, ec.message()); + BOOST_REQUIRE(!ws_upgrade()); - const auto response = ws_request("/v1/top?format=text"); + const auto response = ws_request_text("/v1/top?format=text"); BOOST_REQUIRE_EQUAL(response, "09"); } -BOOST_AUTO_TEST_CASE(native__ws_top__json__expected) +BOOST_AUTO_TEST_CASE(native__ws_top__data__expected) { - const auto ec = ws_upgrade({}); - BOOST_REQUIRE_MESSAGE(!ec, ec.message()); + BOOST_REQUIRE(!ws_upgrade()); - const auto response = ws_request_json("/v1/top?format=json"); - BOOST_REQUIRE(response.is_int64()); - BOOST_REQUIRE_EQUAL(response.as_int64(), 9); + const auto response = ws_request_data("/v1/top?format=data"); + BOOST_REQUIRE_EQUAL(response, base16_chunk("09")); +} + +BOOST_AUTO_TEST_CASE(native__ws_top__xml__error_eof) +{ + BOOST_REQUIRE(!ws_upgrade()); + BOOST_REQUIRE(ws_dropped("/v1/top?format=xml")); } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/protocols/native/native_setup_fixture.cpp b/test/protocols/native/native_setup_fixture.cpp index f4e17ddf..c3786c1e 100644 --- a/test/protocols/native/native_setup_fixture.cpp +++ b/test/protocols/native/native_setup_fixture.cpp @@ -21,6 +21,7 @@ #include "native_setup_fixture.hpp" #include +using namespace system; using namespace boost::beast; BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) @@ -168,18 +169,18 @@ boost::json::value native_setup_fixture::get_json(std::string_view target) return boost::json::parse(response.body()); } -network::boost_code native_setup_fixture::ws_upgrade(std::string_view target) +network::boost_code native_setup_fixture::ws_upgrade() { network::boost_code ec{}; BOOST_REQUIRE(!websocket_.has_value()); websocket_.emplace(socket_); websocket_.value().text(true); - websocket_.value().handshake("localhost", target.empty() ? "/" : target, ec); + websocket_.value().handshake("localhost", "/", ec); return ec; } -std::string native_setup_fixture::ws_receive() +data_chunk native_setup_fixture::ws_receive() { flat_buffer buffer{}; network::boost_code ec{}; @@ -188,10 +189,11 @@ std::string native_setup_fixture::ws_receive() websocket_.value().read(buffer, ec); BOOST_REQUIRE_MESSAGE(!ec, ec.message()); - return buffers_to_string(buffer.data()); + const auto data = pointer_cast(buffer.data().data()); + return { data, std::next(data, buffer.data().size()) }; } -std::string native_setup_fixture::ws_request(std::string_view message) +bool native_setup_fixture::ws_dropped(std::string_view message) { network::boost_code ec{}; BOOST_REQUIRE(websocket_.has_value()); @@ -199,11 +201,29 @@ std::string native_setup_fixture::ws_request(std::string_view message) websocket_.value().write(net::buffer(message), ec); BOOST_REQUIRE_MESSAGE(!ec, ec.message()); - return ws_receive(); + flat_buffer buffer{}; + websocket_.value().read(buffer, ec); + return ec == boost::asio::error::eof; +} + +std::string native_setup_fixture::ws_request_text(std::string_view message) +{ + network::boost_code ec{}; + BOOST_REQUIRE(websocket_.has_value()); + + websocket_.value().write(net::buffer(message), ec); + BOOST_REQUIRE_MESSAGE(!ec, ec.message()); + + return to_string(ws_receive()); } boost::json::value native_setup_fixture::ws_request_json( std::string_view message) +{ + return boost::json::parse(ws_request_text(message)); +} + +data_chunk native_setup_fixture::ws_request_data(std::string_view message) { network::boost_code ec{}; BOOST_REQUIRE(websocket_.has_value()); @@ -211,5 +231,5 @@ boost::json::value native_setup_fixture::ws_request_json( websocket_.value().write(net::buffer(message), ec); BOOST_REQUIRE_MESSAGE(!ec, ec.message()); - return boost::json::parse(ws_receive()); + return ws_receive(); } diff --git a/test/protocols/native/native_setup_fixture.hpp b/test/protocols/native/native_setup_fixture.hpp index 0bfbc0fd..c6c7d24e 100644 --- a/test/protocols/native/native_setup_fixture.hpp +++ b/test/protocols/native/native_setup_fixture.hpp @@ -42,10 +42,12 @@ struct native_setup_fixture system::data_chunk get_data(std::string_view target); boost::json::value get_json(std::string_view target); - network::boost_code ws_upgrade(std::string_view target); - std::string ws_receive(); - std::string ws_request(std::string_view message); + network::boost_code ws_upgrade(); + system::data_chunk ws_receive(); + bool ws_dropped(std::string_view message); + std::string ws_request_text(std::string_view message); boost::json::value ws_request_json(std::string_view message); + system::data_chunk ws_request_data(std::string_view message); protected: server::configuration config_;