159 lines
5.1 KiB
C++
159 lines
5.1 KiB
C++
#include <Void/Data/StrictJson.hpp>
|
|
#include <Void/Net/HttpServer.hpp>
|
|
#include <Void/ShortUrlRemote/RemoteProvider.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
Void::Testing::Test shorturl_remote_provider_capabilities_retry_after_and_unknown()
|
|
{
|
|
using namespace Void::ShortUrl::V2;
|
|
|
|
Void::Net::HttpServer server;
|
|
std::atomic<int> shorten_calls{0};
|
|
std::atomic<bool> stall_next{false};
|
|
std::atomic<bool> release_stall{false};
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_route(
|
|
Void::Net::Method::Post, "/v2/shorten",
|
|
[&](const Void::Net::ServerRequest& req)
|
|
-> Void::Net::RouteResult {
|
|
const int n =
|
|
shorten_calls.fetch_add(1, std::memory_order_relaxed);
|
|
if (stall_next.load(std::memory_order_acquire)) {
|
|
for (int i = 0; i < 200 &&
|
|
!release_stall.load(
|
|
std::memory_order_acquire);
|
|
++i) {
|
|
std::this_thread::sleep_for(
|
|
std::chrono::milliseconds{20});
|
|
}
|
|
return Void::Net::ServerResponse{.status = 200,
|
|
.body = "{}"};
|
|
}
|
|
if (n == 1) {
|
|
return Void::Net::ServerResponse{
|
|
.status = 429,
|
|
.headers = {{"Retry-After", "45"}},
|
|
.body = "slow down"};
|
|
}
|
|
auto parsed = Void::Json::Strict::parse(req.body);
|
|
Void::Testing::require(parsed.has_value());
|
|
const auto& obj =
|
|
std::get<Void::Json::Strict::Value::Object>(
|
|
parsed->value);
|
|
const std::string target =
|
|
std::get<std::string>(obj.at("target_url").value);
|
|
return Void::Net::ServerResponse{
|
|
.status = 200,
|
|
.headers = {{"Content-Type", "application/json"}},
|
|
.body = "{\"key\":\"rem01\",\"short_url\":\"https://"
|
|
"rem.test/rem01\",\"target_url\":\"" +
|
|
target + "\"}",
|
|
};
|
|
})
|
|
.has_value());
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_route(
|
|
Void::Net::Method::Get, "/v2/resolve/:key",
|
|
[](const Void::Net::ServerRequest& req)
|
|
-> Void::Net::RouteResult {
|
|
const auto it = req.parameters.find("key");
|
|
if (it == req.parameters.end() || it->second != "rem01") {
|
|
return Void::Net::ServerResponse{.status = 404,
|
|
.body = "missing"};
|
|
}
|
|
return Void::Net::ServerResponse{
|
|
.status = 200,
|
|
.headers = {{"Content-Type", "application/json"}},
|
|
.body =
|
|
"{\"target_url\":\"https://example.com/remote\","
|
|
"\"short_url\":\"https://rem.test/rem01\"}",
|
|
};
|
|
})
|
|
.has_value());
|
|
|
|
Void::Testing::require(
|
|
server
|
|
.add_route(Void::Net::Method::Delete, "/v2/links/:key",
|
|
[](const Void::Net::ServerRequest&)
|
|
-> Void::Net::RouteResult {
|
|
return Void::Net::ServerResponse{.status = 204,
|
|
.body = ""};
|
|
})
|
|
.has_value());
|
|
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
// 1. Unsupported capability rejected BEFORE any network call!
|
|
{
|
|
RemoteProviderOptions opts;
|
|
opts.base_url = server.base_url();
|
|
opts.capabilities.supports_custom_key = false;
|
|
opts.capabilities.supports_expiry = false;
|
|
opts.capabilities.supports_resolve = false;
|
|
RemoteProvider restricted{opts};
|
|
|
|
auto no_custom = restricted.shorten({
|
|
.target_url = "https://example.com/a",
|
|
.custom_key = "nope",
|
|
});
|
|
Void::Testing::expect(!no_custom.has_value());
|
|
Void::Testing::expect(v2_error_code_from(no_custom.error()) ==
|
|
V2ErrorCode::Unsupported);
|
|
Void::Testing::expect_eq(shorten_calls.load(), 0);
|
|
|
|
auto no_resolve = restricted.resolve("rem01");
|
|
Void::Testing::expect(!no_resolve.has_value());
|
|
Void::Testing::expect(v2_error_code_from(no_resolve.error()) ==
|
|
V2ErrorCode::Unsupported);
|
|
}
|
|
|
|
// 2. Full round-trip, 429 Retry-After preservation, and timeout ->
|
|
// UnknownOutcome without blind replay.
|
|
RemoteProviderOptions opts;
|
|
opts.base_url = server.base_url();
|
|
opts.timeout = std::chrono::seconds{1};
|
|
auto provider = std::make_shared<RemoteProvider>(opts);
|
|
|
|
auto ok = provider->shorten({.target_url = "https://example.com/remote"},
|
|
ShortenOptions{.operation_id = "rem-op-1"});
|
|
Void::Testing::require(ok.has_value());
|
|
Void::Testing::expect_eq(ok->key, std::string{"rem01"});
|
|
|
|
auto res = provider->resolve("rem01");
|
|
Void::Testing::require(res.has_value());
|
|
Void::Testing::expect_eq(res->target_url,
|
|
std::string{"https://example.com/remote"});
|
|
|
|
Void::Testing::require(provider->revoke("rem01").has_value());
|
|
|
|
// Second shorten hits 429 with Retry-After: 45.
|
|
auto rl = provider->shorten({.target_url = "https://example.com/rate"});
|
|
Void::Testing::expect(!rl.has_value());
|
|
Void::Testing::expect(v2_error_code_from(rl.error()) ==
|
|
V2ErrorCode::RateLimited);
|
|
Void::Testing::expect_eq(provider->last_retry_after_seconds(),
|
|
std::uint32_t{45});
|
|
|
|
// Third shorten stalls until 1s timeout -> UnknownOutcome (and V1Adapter
|
|
// maps it to provider_error).
|
|
stall_next.store(true, std::memory_order_release);
|
|
auto unk = provider->shorten({.target_url = "https://example.com/lost"});
|
|
Void::Testing::expect(!unk.has_value());
|
|
Void::Testing::expect(v2_error_code_from(unk.error()) ==
|
|
V2ErrorCode::UnknownOutcome);
|
|
|
|
release_stall.store(true, std::memory_order_release);
|
|
server.shutdown();
|
|
}
|