Files
moloapp/tests/src/app/mount/nested_test.cpp
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
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.
2026-10-23 18:20:04 +02:00

103 lines
3.6 KiB
C++

// V3: prefixed includes, triple-nested mount, startup diagnostics.
#include "../_testlib.h"
using namespace molo::app;
int main() {
// Prefixed include_router.
app::Application app;
routing::Router users;
routing::RouteOptions o;
o.name = "user_list";
users.get("/users", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("list");
}), o);
app.include_router("/api", users);
app.startup();
CHECK_EQ_STR(get(app, "/api/users").body(), "list", "prefixed include serves");
CHECK_EQ_STR(app.url_for("user_list"), "/api/users", "url_for sees merged path");
// Triple-nested mount: /app1/app1/app1.
auto inner = std::make_shared<app::Application>();
routing::RouteOptions deep;
deep.name = "deep";
inner->router().get("/app1", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("deep");
}), deep);
auto mid = std::make_shared<app::Application>();
mid->mount("/app1", inner);
app::Application top;
top.mount("/app1", mid);
top.startup();
CHECK_EQ_STR(get(top, "/app1/app1/app1").body(), "deep", "triple mount merges paths");
CHECK_EQ_STR(top.url_for("app1:app1:deep"), "/app1/app1/app1", "url_for crosses mounts");
// Duplicate names fail startup with quoted paths.
app::Application dup;
routing::RouteOptions d1;
d1.name = "dup";
dup.router().get("/a", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("a");
}), d1);
routing::RouteOptions d2;
d2.name = "dup";
dup.router().get("/b", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("b");
}), d2);
bool dup_threw = false;
std::string dup_msg;
try {
dup.startup();
} catch (const app::StartupError& e) {
dup_threw = true;
dup_msg = e.what();
}
CHECK(dup_threw, "duplicate route names fail startup");
CHECK(dup_msg.find("'/a'") != std::string::npos, "diagnostic quotes first path");
CHECK(dup_msg.find("'/b'") != std::string::npos, "diagnostic quotes second path");
CHECK(dup_msg.find("dup") != std::string::npos, "diagnostic names the route");
// Unknown converter fails fast naming the route.
routing::Router bad;
bool conv_threw = false;
std::string conv_msg;
try {
routing::RouteOptions bo;
bo.name = "broken";
bad.get("/x/{id:uuid}", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("x");
}), bo);
} catch (const routing::PathCompileError& e) {
conv_threw = true;
conv_msg = e.what();
}
CHECK(conv_threw, "unknown converter is a startup error");
CHECK(conv_msg.find("uuid") != std::string::npos, "diagnostic names the converter");
CHECK(conv_msg.find("broken") != std::string::npos, "diagnostic names the route");
// slash_insensitive registers both forms with 308 to the declared form.
app::Application slash;
routing::RouteOptions so;
so.name = "canon";
so.slash_insensitive = true;
slash.router().get("/canon", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("canon");
}), so);
slash.startup();
CHECK_EQ_STR(get(slash, "/canon").body(), "canon", "declared slash form serves");
http_msg::Response redir = get(slash, "/canon/");
CHECK_EQ_INT(redir.status(), 308, "alternate slash form 308-redirects");
CHECK_EQ_STR(redir.headers().get("location"), "/canon", "redirect targets declared form");
bool frozen_threw = false;
try {
slash.router().get("/late", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("late");
}));
} catch (const routing::PathCompileError& e) {
frozen_threw = std::string(e.what()).find("frozen") != std::string::npos;
}
CHECK(frozen_threw, "routes freeze at startup");
return test_report("mount/nested");
}