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
2 changes: 0 additions & 2 deletions .github/workflows/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ jobs:
with:
# Inform the test harness of test service's port.
test_service_port: ${{ env.TEST_SERVICE_PORT }}
extra_params: '-skip-from ./contract-tests/server-contract-tests/test-suppressions.txt'
token: ${{ secrets.GITHUB_TOKEN }}

contract-tests-curl:
Expand All @@ -54,7 +53,6 @@ jobs:
with:
# Inform the test harness of test service's port.
test_service_port: ${{ env.TEST_SERVICE_PORT }}
extra_params: '-skip-from ./contract-tests/server-contract-tests/test-suppressions.txt'
token: ${{ secrets.GITHUB_TOKEN }}

build-test-server:
Expand Down
34 changes: 33 additions & 1 deletion contract-tests/data-model/include/data_model/data_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigTags,
applicationId,
applicationVersion);

enum class HookStage {
BeforeEvaluation,
AfterEvaluation,
AfterTrack
};

NLOHMANN_JSON_SERIALIZE_ENUM(HookStage,
{{HookStage::BeforeEvaluation, "beforeEvaluation"},
{HookStage::AfterEvaluation, "afterEvaluation"},
{HookStage::AfterTrack, "afterTrack"}})

struct ConfigHookInstance {
std::string name;
std::string callbackUri;
std::optional<std::unordered_map<std::string, std::unordered_map<std::string, nlohmann::json>>> data;
std::optional<std::unordered_map<std::string, std::string>> errors;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigHookInstance,
name,
callbackUri,
data,
errors);

struct ConfigHooksParams {
std::vector<ConfigHookInstance> hooks;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigHooksParams, hooks);

struct ConfigParams {
std::string credential;
std::optional<uint32_t> startWaitTimeMs;
Expand All @@ -125,6 +155,7 @@ struct ConfigParams {
std::optional<ConfigTags> tags;
std::optional<ConfigTLSParams> tls;
std::optional<ConfigProxyParams> proxy;
std::optional<ConfigHooksParams> hooks;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigParams,
Expand All @@ -138,7 +169,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(ConfigParams,
clientSide,
tags,
tls,
proxy);
proxy,
hooks);

struct ContextSingleParams {
std::optional<std::string> kind;
Expand Down
1 change: 1 addition & 0 deletions contract-tests/server-contract-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_executable(server-tests
src/session.cpp
src/entity_manager.cpp
src/client_entity.cpp
src/contract_test_hook.cpp
)

target_link_libraries(server-tests PRIVATE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <data_model/data_model.hpp>
#include <launchdarkly/server_side/hooks/hook.hpp>

#include <boost/asio/any_io_executor.hpp>
#include <memory>
#include <string>

/**
* ContractTestHook implements the Hook interface for contract testing.
*
* It posts hook execution payloads to a callback URI specified in the test
* configuration, allowing the test harness to verify hook behavior.
*/
class ContractTestHook : public launchdarkly::server_side::hooks::Hook {
public:
/**
* Constructs a contract test hook.
* @param executor IO executor for async HTTP operations.
* @param config Hook configuration from the test harness.
*/
ContractTestHook(boost::asio::any_io_executor executor,
ConfigHookInstance config);

~ContractTestHook() override = default;

[[nodiscard]] launchdarkly::server_side::hooks::HookMetadata const&
Metadata() const override;

launchdarkly::server_side::hooks::EvaluationSeriesData BeforeEvaluation(
launchdarkly::server_side::hooks::EvaluationSeriesContext const&
series_context,
launchdarkly::server_side::hooks::EvaluationSeriesData data) override;

launchdarkly::server_side::hooks::EvaluationSeriesData AfterEvaluation(
launchdarkly::server_side::hooks::EvaluationSeriesContext const&
series_context,
launchdarkly::server_side::hooks::EvaluationSeriesData data,
launchdarkly::EvaluationDetail<launchdarkly::Value> const& detail)
override;

void AfterTrack(launchdarkly::server_side::hooks::TrackSeriesContext const&
series_context) override;

private:
/**
* Posts a hook execution payload to the callback URI.
* @param stage The stage being executed.
* @param payload The JSON payload to send.
*/
void PostCallback(std::string const& stage, nlohmann::json const& payload);

/**
* Gets configured data for a specific stage.
* @param stage The stage name.
* @return Optional map of key-value pairs for this stage.
*/
std::optional<std::unordered_map<std::string, nlohmann::json>> GetDataForStage(
std::string const& stage) const;

/**
* Gets configured error for a specific stage.
* @param stage The stage name.
* @return Optional error message for this stage.
*/
std::optional<std::string> GetErrorForStage(std::string const& stage) const;

boost::asio::any_io_executor executor_;
ConfigHookInstance config_;
launchdarkly::server_side::hooks::HookMetadata metadata_;
};
Loading