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.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
// V10: JSON, urlencoded and multipart bind the same model; cap fixtures
|
||||
// are rejected with the documented status.
|
||||
#include "../_testlib.h"
|
||||
|
||||
using namespace molo::app;
|
||||
|
||||
static std::shared_ptr<validate::Model> user_model() {
|
||||
auto m = std::make_shared<validate::Model>("user");
|
||||
validate::Field name("username", validate::FieldType::String);
|
||||
name.required().min_len(3);
|
||||
validate::Field age("age", validate::FieldType::Int);
|
||||
age.required().ge(0);
|
||||
m->field(name).field(age);
|
||||
return m;
|
||||
}
|
||||
|
||||
int main() {
|
||||
app::Application app;
|
||||
routing::RouteOptions o;
|
||||
o.name = "create";
|
||||
o.body_model = user_model();
|
||||
app.router().post("/users", views::adapt([](http_msg::Request& req) {
|
||||
const validate::Object* b = req.validated("body");
|
||||
std::string out = b->at("username").as_text() + ":" +
|
||||
std::to_string((long long)b->at("age").as_number());
|
||||
return http_msg::TextResponse(out);
|
||||
}), o);
|
||||
routing::RouteOptions tiny;
|
||||
tiny.name = "tiny";
|
||||
tiny.body_model = user_model();
|
||||
tiny.max_body = 10;
|
||||
app.router().post("/tiny", views::adapt([](http_msg::Request&) {
|
||||
return http_msg::TextResponse("tiny");
|
||||
}), tiny);
|
||||
app.startup();
|
||||
|
||||
http_msg::Request json =
|
||||
make_request("POST", "/users", {{"content-type", "application/json"}},
|
||||
R"({"username":"ada","age":36})");
|
||||
CHECK_EQ_STR(run_request(app, json).body(), "ada:36", "JSON binds to model");
|
||||
|
||||
http_msg::Request url =
|
||||
make_request("POST", "/users", {{"content-type", "application/x-www-form-urlencoded"}},
|
||||
"username=ada&age=36");
|
||||
CHECK_EQ_STR(run_request(app, url).body(), "ada:36", "urlencoded binds to model");
|
||||
|
||||
std::string mp =
|
||||
"--B\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nada\r\n"
|
||||
"--B\r\nContent-Disposition: form-data; name=\"age\"\r\n\r\n36\r\n"
|
||||
"--B--\r\n";
|
||||
http_msg::Request multi =
|
||||
make_request("POST", "/users", {{"content-type", "multipart/form-data; boundary=B"}}, mp);
|
||||
CHECK_EQ_STR(run_request(app, multi).body(), "ada:36", "multipart binds to model");
|
||||
|
||||
http_msg::Request bad =
|
||||
make_request("POST", "/users", {{"content-type", "application/json"}},
|
||||
R"({"username":"al","age":-1})");
|
||||
http_msg::Response v = run_request(app, bad);
|
||||
CHECK_EQ_INT(v.status(), 422, "bad body fields yield one 422");
|
||||
CHECK(v.body().find("[\"body\",\"username\"]") != std::string::npos, "username failure listed");
|
||||
CHECK(v.body().find("[\"body\",\"age\"]") != std::string::npos, "age failure listed");
|
||||
|
||||
// 1001 urlencoded fields exceed the form cap.
|
||||
std::string many;
|
||||
for (int i = 0; i < 1001; ++i) {
|
||||
if (i) many += '&';
|
||||
many += "f" + std::to_string(i) + "=v";
|
||||
}
|
||||
http_msg::Request flood =
|
||||
make_request("POST", "/users", {{"content-type", "application/x-www-form-urlencoded"}}, many);
|
||||
CHECK_EQ_INT(run_request(app, flood).status(), 413, "1001 fields rejected with 413");
|
||||
|
||||
// JSON depth 33 exceeds the depth cap.
|
||||
std::string deep(33, '[');
|
||||
deep += std::string(33, ']');
|
||||
http_msg::Request deep_req =
|
||||
make_request("POST", "/users", {{"content-type", "application/json"}}, deep);
|
||||
CHECK_EQ_INT(run_request(app, deep_req).status(), 400, "depth 33 rejected with 400");
|
||||
|
||||
// Oversized single value exceeds the 1 MiB cap.
|
||||
std::string big(1048577, 'x');
|
||||
http_msg::Request big_req = make_request(
|
||||
"POST", "/users", {{"content-type", "application/x-www-form-urlencoded"}}, "v=" + big);
|
||||
CHECK_EQ_INT(run_request(app, big_req).status(), 413, "oversized value rejected with 413");
|
||||
|
||||
// Per-route document cap.
|
||||
http_msg::Request over =
|
||||
make_request("POST", "/tiny", {{"content-type", "application/json"}},
|
||||
R"({"username":"ada","age":36})");
|
||||
CHECK_EQ_INT(run_request(app, over).status(), 413, "per-route max_body enforced");
|
||||
return test_report("forms/bind");
|
||||
}
|
||||
Reference in New Issue
Block a user