Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
66 lines
2.0 KiB
C++
66 lines
2.0 KiB
C++
#include "TestClient.hpp"
|
|
|
|
#include "Caps.hpp"
|
|
#include "Http.hpp"
|
|
|
|
namespace molo::example::fw {
|
|
|
|
TestClient::TestClient(std::shared_ptr<Application> app)
|
|
: app_(std::move(app)) {}
|
|
|
|
Response TestClient::get(const std::string& path) {
|
|
return request(Method::Get, path);
|
|
}
|
|
|
|
Response TestClient::post(const std::string& path, const std::string& body,
|
|
const std::string& content_type) {
|
|
return request(Method::Post, path, body, {{"Content-Type", content_type}});
|
|
}
|
|
|
|
Response TestClient::request(
|
|
Method method, const std::string& path, const std::string& body,
|
|
const std::map<std::string, std::string>& headers) {
|
|
if (body.size() > caps::kMaxTestBody)
|
|
return TextResponse("test body over cap", 413);
|
|
Request req;
|
|
req.method = method;
|
|
std::size_t q = path.find('?');
|
|
if (q == std::string::npos) {
|
|
req.path = path;
|
|
} else {
|
|
req.path = path.substr(0, q);
|
|
req.query = parse_query(path.substr(q + 1));
|
|
}
|
|
req.headers.set("User-Agent", "living-example-test/1.0");
|
|
for (const auto& [k, v] : headers) req.headers.set(k, v);
|
|
if (!jar_.empty()) {
|
|
std::string cookie;
|
|
for (const auto& [k, v] : jar_) {
|
|
if (!cookie.empty()) cookie += "; ";
|
|
cookie += k + "=" + v;
|
|
}
|
|
req.cookies = jar_;
|
|
req.headers.set("Cookie", cookie);
|
|
}
|
|
if (!frozen_time_.empty()) req.headers.set("X-Test-Time", frozen_time_);
|
|
req.body = body;
|
|
Response res = app_->dispatch(std::move(req));
|
|
std::string set_cookie = res.headers.get("Set-Cookie");
|
|
if (!set_cookie.empty()) {
|
|
std::size_t semi = set_cookie.find(';');
|
|
std::string pair = set_cookie.substr(0, semi);
|
|
std::size_t eq = pair.find('=');
|
|
if (eq != std::string::npos)
|
|
jar_[pair.substr(0, eq)] = pair.substr(eq + 1);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
void TestClient::freeze_time(const std::string& iso) { frozen_time_ = iso; }
|
|
|
|
const std::string& TestClient::frozen_time() const { return frozen_time_; }
|
|
|
|
void TestClient::clear_cookies() { jar_.clear(); }
|
|
|
|
} // namespace molo::example::fw
|