Add ValuePolicy (#1458)
This commit is contained in:
parent
6c3aeed35c
commit
aeb386f286
@ -16,7 +16,13 @@
|
||||
#include "azure/core/uuid.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Azure { namespace Core { namespace Http {
|
||||
|
||||
@ -415,4 +421,58 @@ namespace Azure { namespace Core { namespace Http {
|
||||
static constexpr auto const HttpTransportAdapter = Classification(4);
|
||||
};
|
||||
|
||||
namespace Details {
|
||||
/**
|
||||
* @brief @ValuePolicy options.
|
||||
*/
|
||||
struct ValuePolicyOptions
|
||||
{
|
||||
std::map<std::string, std::string> HeaderValues;
|
||||
std::map<std::string, std::string> QueryValues;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Value policy.
|
||||
*
|
||||
* @details Applies key-value pair values to each HTTP request (either HTTP headers or query
|
||||
* parameters).
|
||||
*/
|
||||
class ValuePolicy : public HttpPolicy {
|
||||
private:
|
||||
ValuePolicyOptions m_options;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a @ValuePolicy with the @ValuePolicyOptions provided.
|
||||
* @param options @ValuePolicyOptions.
|
||||
*/
|
||||
explicit ValuePolicy(ValuePolicyOptions options) : m_options(std::move(options)) {}
|
||||
|
||||
std::unique_ptr<HttpPolicy> Clone() const override
|
||||
{
|
||||
return std::make_unique<ValuePolicy>(*this);
|
||||
}
|
||||
|
||||
std::unique_ptr<RawResponse> Send(
|
||||
Context const& ctx,
|
||||
Request& request,
|
||||
NextHttpPolicy nextHttpPolicy) const override
|
||||
{
|
||||
for (auto const& hdrPair : m_options.HeaderValues)
|
||||
{
|
||||
request.AddHeader(hdrPair.first, hdrPair.second);
|
||||
}
|
||||
|
||||
{
|
||||
auto& url = request.GetUrl();
|
||||
for (auto const& qryPair : m_options.QueryValues)
|
||||
{
|
||||
url.AppendQueryParameter(qryPair.first, qryPair.second);
|
||||
}
|
||||
}
|
||||
|
||||
return nextHttpPolicy.Send(ctx, request);
|
||||
}
|
||||
};
|
||||
} // namespace Details
|
||||
}}} // namespace Azure::Core::Http
|
||||
|
||||
@ -7,6 +7,24 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
class NoOpPolicy : public Azure::Core::Http::HttpPolicy {
|
||||
public:
|
||||
std::unique_ptr<Azure::Core::Http::HttpPolicy> Clone() const override
|
||||
{
|
||||
return std::make_unique<NoOpPolicy>(*this);
|
||||
}
|
||||
|
||||
std::unique_ptr<Azure::Core::Http::RawResponse> Send(
|
||||
Azure::Core::Context const&,
|
||||
Azure::Core::Http::Request&,
|
||||
Azure::Core::Http::NextHttpPolicy) const override
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST(Policy, throwWhenNoTransportPolicy)
|
||||
{
|
||||
// Construct pipeline without exception
|
||||
@ -44,3 +62,28 @@ TEST(Policy, throwWhenNoTransportPolicyMessage)
|
||||
EXPECT_STREQ("Invalid pipeline. No transport policy found. Endless policy.", err.what());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Policy, ValuePolicy)
|
||||
{
|
||||
using namespace Azure::Core;
|
||||
using namespace Azure::Core::Http;
|
||||
|
||||
Azure::Core::Http::Details::ValuePolicyOptions options
|
||||
= {{{"hdrkey1", "HdrVal1"}, {"hdrkey2", "HdrVal2"}},
|
||||
{{"QryKey1", "QryVal1"}, {"QryKey2", "QryVal2"}}};
|
||||
|
||||
std::vector<std::unique_ptr<HttpPolicy>> policies;
|
||||
policies.emplace_back(std::make_unique<Azure::Core::Http::Details::ValuePolicy>(options));
|
||||
policies.emplace_back(std::make_unique<NoOpPolicy>());
|
||||
HttpPipeline pipeline(policies);
|
||||
|
||||
Request request(HttpMethod::Get, Url("https:://www.example.com"));
|
||||
|
||||
pipeline.Send(GetApplicationContext(), request);
|
||||
|
||||
auto headers = request.GetHeaders();
|
||||
auto queryParams = request.GetUrl().GetQueryParameters();
|
||||
|
||||
ASSERT_EQ(headers, decltype(headers)({{"hdrkey1", "HdrVal1"}, {"hdrkey2", "HdrVal2"}}));
|
||||
ASSERT_EQ(queryParams, decltype(queryParams)({{"QryKey1", "QryVal1"}, {"QryKey2", "QryVal2"}}));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user