|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <data_model/data_model.hpp> |
| 4 | +#include <launchdarkly/server_side/hooks/hook.hpp> |
| 5 | + |
| 6 | +#include <boost/asio/any_io_executor.hpp> |
| 7 | +#include <memory> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +/** |
| 11 | + * ContractTestHook implements the Hook interface for contract testing. |
| 12 | + * |
| 13 | + * It posts hook execution payloads to a callback URI specified in the test |
| 14 | + * configuration, allowing the test harness to verify hook behavior. |
| 15 | + */ |
| 16 | +class ContractTestHook : public launchdarkly::server_side::hooks::Hook { |
| 17 | + public: |
| 18 | + /** |
| 19 | + * Constructs a contract test hook. |
| 20 | + * @param executor IO executor for async HTTP operations. |
| 21 | + * @param config Hook configuration from the test harness. |
| 22 | + */ |
| 23 | + ContractTestHook(boost::asio::any_io_executor executor, |
| 24 | + ConfigHookInstance config); |
| 25 | + |
| 26 | + ~ContractTestHook() override = default; |
| 27 | + |
| 28 | + [[nodiscard]] launchdarkly::server_side::hooks::HookMetadata const& |
| 29 | + Metadata() const override; |
| 30 | + |
| 31 | + launchdarkly::server_side::hooks::EvaluationSeriesData BeforeEvaluation( |
| 32 | + launchdarkly::server_side::hooks::EvaluationSeriesContext const& |
| 33 | + series_context, |
| 34 | + launchdarkly::server_side::hooks::EvaluationSeriesData data) override; |
| 35 | + |
| 36 | + launchdarkly::server_side::hooks::EvaluationSeriesData AfterEvaluation( |
| 37 | + launchdarkly::server_side::hooks::EvaluationSeriesContext const& |
| 38 | + series_context, |
| 39 | + launchdarkly::server_side::hooks::EvaluationSeriesData data, |
| 40 | + launchdarkly::EvaluationDetail<launchdarkly::Value> const& detail) |
| 41 | + override; |
| 42 | + |
| 43 | + void AfterTrack(launchdarkly::server_side::hooks::TrackSeriesContext const& |
| 44 | + series_context) override; |
| 45 | + |
| 46 | + private: |
| 47 | + /** |
| 48 | + * Posts a hook execution payload to the callback URI. |
| 49 | + * @param stage The stage being executed. |
| 50 | + * @param payload The JSON payload to send. |
| 51 | + */ |
| 52 | + void PostCallback(std::string const& stage, nlohmann::json const& payload); |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets configured data for a specific stage. |
| 56 | + * @param stage The stage name. |
| 57 | + * @return Optional map of key-value pairs for this stage. |
| 58 | + */ |
| 59 | + std::optional<std::unordered_map<std::string, nlohmann::json>> GetDataForStage( |
| 60 | + std::string const& stage) const; |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets configured error for a specific stage. |
| 64 | + * @param stage The stage name. |
| 65 | + * @return Optional error message for this stage. |
| 66 | + */ |
| 67 | + std::optional<std::string> GetErrorForStage(std::string const& stage) const; |
| 68 | + |
| 69 | + boost::asio::any_io_executor executor_; |
| 70 | + ConfigHookInstance config_; |
| 71 | + launchdarkly::server_side::hooks::HookMetadata metadata_; |
| 72 | +}; |
0 commit comments