|
| 1 | +#include "contract_test_big_segment_store.hpp" |
| 2 | + |
| 3 | +#include <boost/asio/connect.hpp> |
| 4 | +#include <boost/asio/io_context.hpp> |
| 5 | +#include <boost/asio/ip/tcp.hpp> |
| 6 | +#include <boost/beast/core.hpp> |
| 7 | +#include <boost/beast/http.hpp> |
| 8 | +#include <boost/url.hpp> |
| 9 | + |
| 10 | +#include <chrono> |
| 11 | +#include <optional> |
| 12 | +#include <utility> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +namespace beast = boost::beast; |
| 16 | +namespace http = beast::http; |
| 17 | +namespace net = boost::asio; |
| 18 | +using tcp = net::ip::tcp; |
| 19 | + |
| 20 | +using namespace launchdarkly::server_side::integrations; |
| 21 | + |
| 22 | +ContractTestBigSegmentStore::ContractTestBigSegmentStore( |
| 23 | + std::string callback_uri) |
| 24 | + : callback_uri_(std::move(callback_uri)) {} |
| 25 | + |
| 26 | +tl::expected<nlohmann::json, std::string> ContractTestBigSegmentStore::Post( |
| 27 | + std::string const& path, |
| 28 | + nlohmann::json const& body) const noexcept { |
| 29 | + try { |
| 30 | + auto uri_result = boost::urls::parse_uri(callback_uri_); |
| 31 | + if (!uri_result) { |
| 32 | + return tl::make_unexpected("invalid callback URI: " + |
| 33 | + callback_uri_); |
| 34 | + } |
| 35 | + auto uri = *uri_result; |
| 36 | + std::string const host(uri.host()); |
| 37 | + std::string const port = |
| 38 | + uri.has_port() ? std::string(uri.port()) : "80"; |
| 39 | + std::string base(uri.path()); |
| 40 | + // The callback URI carries a base path; the sub-path (/getMembership, |
| 41 | + // /getMetadata) is appended. Drop any trailing slash to avoid "//". |
| 42 | + if (!base.empty() && base.back() == '/') { |
| 43 | + base.pop_back(); |
| 44 | + } |
| 45 | + std::string const target = base + path; |
| 46 | + |
| 47 | + net::io_context ioc; |
| 48 | + tcp::resolver resolver(ioc); |
| 49 | + beast::tcp_stream stream(ioc); |
| 50 | + stream.connect(resolver.resolve(host, port)); |
| 51 | + |
| 52 | + http::request<http::string_body> req{http::verb::post, target, 11}; |
| 53 | + req.set(http::field::host, host); |
| 54 | + req.set(http::field::user_agent, "cpp-server-sdk-contract-tests"); |
| 55 | + req.set(http::field::content_type, "application/json"); |
| 56 | + req.body() = body.dump(); |
| 57 | + req.prepare_payload(); |
| 58 | + http::write(stream, req); |
| 59 | + |
| 60 | + beast::flat_buffer buffer; |
| 61 | + http::response<http::string_body> res; |
| 62 | + http::read(stream, buffer, res); |
| 63 | + |
| 64 | + beast::error_code ec; |
| 65 | + stream.socket().shutdown(tcp::socket::shutdown_both, ec); |
| 66 | + |
| 67 | + if (res.result_int() != 200) { |
| 68 | + return tl::make_unexpected(res.body()); |
| 69 | + } |
| 70 | + return nlohmann::json::parse(res.body()); |
| 71 | + } catch (std::exception const& e) { |
| 72 | + return tl::make_unexpected(e.what()); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +ContractTestBigSegmentStore::GetMembershipResult |
| 77 | +ContractTestBigSegmentStore::GetMembership( |
| 78 | + std::string const& context_hash) const noexcept { |
| 79 | + auto result = Post("/getMembership", {{"contextHash", context_hash}}); |
| 80 | + if (!result) { |
| 81 | + return tl::make_unexpected(result.error()); |
| 82 | + } |
| 83 | + try { |
| 84 | + std::vector<std::string> included; |
| 85 | + std::vector<std::string> excluded; |
| 86 | + auto const it = result->find("values"); |
| 87 | + if (it != result->end() && it->is_object()) { |
| 88 | + for (auto const& [segment_ref, member] : it->items()) { |
| 89 | + (member.get<bool>() ? included : excluded) |
| 90 | + .push_back(segment_ref); |
| 91 | + } |
| 92 | + } |
| 93 | + return Membership::FromSegmentRefs(included, excluded); |
| 94 | + } catch (std::exception const& e) { |
| 95 | + return tl::make_unexpected(e.what()); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +ContractTestBigSegmentStore::GetMetadataResult |
| 100 | +ContractTestBigSegmentStore::GetMetadata() const noexcept { |
| 101 | + auto result = Post("/getMetadata", nlohmann::json::object()); |
| 102 | + if (!result) { |
| 103 | + return tl::make_unexpected(result.error()); |
| 104 | + } |
| 105 | + try { |
| 106 | + auto const it = result->find("lastUpToDate"); |
| 107 | + // Absent or zero means the store has never been synchronized. |
| 108 | + if (it == result->end() || it->is_null()) { |
| 109 | + return std::optional<StoreMetadata>{std::nullopt}; |
| 110 | + } |
| 111 | + auto const millis = it->get<std::uint64_t>(); |
| 112 | + if (millis == 0) { |
| 113 | + return std::optional<StoreMetadata>{std::nullopt}; |
| 114 | + } |
| 115 | + return std::optional<StoreMetadata>{ |
| 116 | + StoreMetadata{std::chrono::system_clock::time_point{ |
| 117 | + std::chrono::milliseconds{millis}}}}; |
| 118 | + } catch (std::exception const& e) { |
| 119 | + return tl::make_unexpected(e.what()); |
| 120 | + } |
| 121 | +} |
0 commit comments