Files
EchoionPluginUrlShortenerEx…/tests/ExternalShortenerTests.cpp
T

179 lines
5.9 KiB
C++

#include <echoion/plugins/short_url/ExternalShortener.hpp>
#include <Void/Data/Json.hpp>
#include <Void/ShortUrl/ShortUrl.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <string_view>
namespace
{
class EnvironmentVariable
{
public:
EnvironmentVariable(std::string name, std::string value)
: name_(std::move(name))
{
setenv(name_.c_str(), value.c_str(), 1);
}
~EnvironmentVariable() { unsetenv(name_.c_str()); }
EnvironmentVariable(const EnvironmentVariable&) = delete;
EnvironmentVariable& operator=(const EnvironmentVariable&) = delete;
private:
std::string name_;
};
} // namespace
int main()
{
using Echoion::Plugins::ShortUrl::ExternalShortener;
using Echoion::Plugins::ShortUrl::hasExternalConfiguration;
using Echoion::Plugins::ShortUrl::parseExternalConfig;
int failures{};
const auto check = [&failures](const bool condition,
const std::string_view message) {
if (!condition) {
std::cerr << message << '\n';
++failures;
}
};
EnvironmentVariable token{"ECHOION_SHORTENER_TEST_TOKEN", "test secret"};
check(!hasExternalConfiguration(
R"JSON({"settings":{},"_host":{"stateDirectory":"/tmp"}})JSON"),
"empty host envelope did not keep the optional provider dormant");
check(
hasExternalConfiguration(R"JSON({"settings":{"endpoint":false}})JSON"),
"partially configured provider was incorrectly treated as dormant");
check(hasExternalConfiguration("{"),
"malformed provider configuration was incorrectly treated as "
"dormant");
const auto jsonConfig = R"JSON({
"endpoint":"https://short.example/api",
"method":"POST",
"encoding":"json",
"fields":{"longUrl":"${url}","tenant":"${env:ECHOION_SHORTENER_TEST_TOKEN}"},
"headers":{"Authorization":"Bearer ${env:ECHOION_SHORTENER_TEST_TOKEN}"},
"response":{"type":"json","pointer":"/data/short"}
})JSON";
auto parsed = parseExternalConfig(jsonConfig);
check(parsed.has_value(), "JSON provider configuration was rejected");
if (!parsed) {
return 1;
}
auto shortener = ExternalShortener::create(std::move(*parsed));
check(shortener.has_value(), "external shortener could not be created");
if (!shortener) {
return 1;
}
auto request =
(*shortener)->previewRequest("https://example.com/a?x=1&y=2");
check(request.has_value() && request->body.has_value(),
"JSON request was not constructed");
if (request && request->body) {
auto body = Void::Json::parse(*request->body);
check(body &&
body->get_str("longUrl") == "https://example.com/a?x=1&y=2",
"target URL was not safely represented in JSON");
check(body && body->get_str("tenant") == "test secret",
"environment template was not expanded in a field");
check(request->headers.size() == 2,
"configured and automatic Content-Type headers were not emitted");
}
Void::Net::Response jsonResponse{
.status = 200,
.body = R"({"data":{"short":"https://sho.rt/Ab1"}})",
};
auto extracted = (*shortener)->previewExtract(jsonResponse);
check(extracted && *extracted == "https://sho.rt/Ab1",
"JSON Pointer response extraction failed");
const auto formConfig = R"JSON({
"endpoint":"https://short.example/api",
"method":"PATCH",
"encoding":"form",
"fields":{"long url":"${url}"},
"response":{"type":"plain"}
})JSON";
auto form = parseExternalConfig(formConfig);
check(form.has_value(), "form provider configuration was rejected");
if (form) {
auto formShortener = ExternalShortener::create(std::move(*form));
auto formRequest =
formShortener
? (*formShortener)
->previewRequest("https://example.com/a?x=1&y=2")
: std::expected<Void::Net::Request, std::string>{
std::unexpected{"create failed"}};
check(formRequest &&
formRequest->body ==
"long+url=https%3A%2F%2Fexample.com%2Fa%3Fx%3D1%26y%3D2",
"form request was not percent encoded");
}
const auto queryConfig = R"JSON({
"endpoint":"https://short.example/api?token=${env:ECHOION_SHORTENER_TEST_TOKEN}",
"method":"GET",
"encoding":"query",
"fields":{"url":"${url}"},
"response":{"type":"header","header":"Location"}
})JSON";
auto query = parseExternalConfig(queryConfig);
check(query.has_value(), "query provider configuration was rejected");
if (query) {
auto queryShortener = ExternalShortener::create(std::move(*query));
auto queryRequest =
queryShortener
? (*queryShortener)->previewRequest("https://example.com/a?x=1")
: std::expected<Void::Net::Request, std::string>{
std::unexpected{"create failed"}};
check(queryRequest &&
queryRequest->url.find("token=test+secret") !=
std::string::npos &&
queryRequest->url.find("url=https%3A%2F%2Fexample.com") !=
std::string::npos,
"query and environment values were not percent encoded");
if (queryShortener) {
Void::Net::Response headerResponse{
.status = 201,
.headers = {{.name = "Location", .value = "https://sho.rt/Q1"}},
};
auto header = (*queryShortener)->previewExtract(headerResponse);
check(header && *header == "https://sho.rt/Q1",
"response-header extraction failed");
}
}
check(!parseExternalConfig(R"({"endpoint":"http://short.example/api"})"),
"plain HTTP endpoint was accepted without opt-in");
check(
!parseExternalConfig(
R"({"endpoint":"https://127.0.0.1/api","fields":{"url":"${url}"}})"),
"private endpoint was accepted without opt-in");
check(
!parseExternalConfig(
R"({"endpoint":"https://short.example/api","headers":{"Authorization":"${env:ECHOION_MISSING_SHORTENER_TOKEN}"}})"),
"missing environment secret did not reject configuration");
const Void::ShortUrl::ShortenRequest unsupportedRequest{
.target_url = "https://example.com/a",
.custom_key = "wanted",
};
const auto unsupported = (*shortener)->shorten(unsupportedRequest);
check(!unsupported &&
unsupported.error.code == Void::ShortUrl::ErrorCode::unsupported,
"external adapter silently ignored a custom key");
return failures == 0 ? 0 : 1;
}