Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions test/protocols/native/native_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
34 changes: 27 additions & 7 deletions test/protocols/native/native_setup_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "native_setup_fixture.hpp"
#include <future>

using namespace system;
using namespace boost::beast;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
Expand Down Expand Up @@ -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{};
Expand All @@ -188,28 +189,47 @@ 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<uint8_t>(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());

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());

websocket_.value().write(net::buffer(message), ec);
BOOST_REQUIRE_MESSAGE(!ec, ec.message());

return boost::json::parse(ws_receive());
return ws_receive();
}
8 changes: 5 additions & 3 deletions test/protocols/native/native_setup_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
Loading