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,54 @@
|
||||
// V7: view method dispatch, 405 on unimplemented, redirects, flash-once.
|
||||
#include "../_testlib.h"
|
||||
|
||||
using namespace molo::app;
|
||||
|
||||
class UserView : public views::View {
|
||||
public:
|
||||
using View::View;
|
||||
async::Task<http_msg::Response> get() override {
|
||||
std::string out = "got " + request().path_param("id");
|
||||
co_return http_msg::TextResponse(out);
|
||||
}
|
||||
async::Task<http_msg::Response> post() override {
|
||||
co_return http_msg::TextResponse("posted");
|
||||
}
|
||||
std::vector<std::string> allowed_methods() const override { return {"GET", "POST"}; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
app::Application app;
|
||||
app.router().add({http_msg::Method::GET, http_msg::Method::POST, http_msg::Method::PUT},
|
||||
"/users/{id:int}", views::view_handler<UserView>());
|
||||
app.router().get("/flash", views::adapt([](http_msg::Request& req) {
|
||||
http_msg::Response res = http_msg::TextResponse("flashed");
|
||||
views::Flash::flash(req, res, "info", "hello once");
|
||||
return res;
|
||||
}));
|
||||
app.router().get("/show", views::adapt([](http_msg::Request& req) {
|
||||
std::vector<seams::FlashMessage> msgs = views::Flash::take(req);
|
||||
std::string out;
|
||||
for (const auto& m : msgs) out += m.level + ":" + m.text + ";";
|
||||
return http_msg::TextResponse(out);
|
||||
}));
|
||||
app.startup();
|
||||
|
||||
CHECK_EQ_STR(get(app, "/users/9").body(), "got 9", "view dispatches GET");
|
||||
http_msg::Request post = make_request("POST", "/users/9");
|
||||
CHECK_EQ_STR(run_request(app, post).body(), "posted", "view dispatches POST");
|
||||
http_msg::Request put = make_request("PUT", "/users/9");
|
||||
http_msg::Response m = run_request(app, put);
|
||||
CHECK_EQ_INT(m.status(), 405, "unimplemented view method yields 405");
|
||||
CHECK_EQ_STR(m.headers().get("allow"), "GET, POST", "view 405 Allow value");
|
||||
|
||||
http_msg::Response f1 = get(app, "/flash");
|
||||
CHECK_EQ_INT(f1.status(), 200, "flash route serves");
|
||||
std::string set_cookie = f1.headers().get("set-cookie");
|
||||
CHECK(set_cookie.find("molo_session=") != std::string::npos, "flash sets session cookie");
|
||||
std::string cookie = set_cookie.substr(0, set_cookie.find(';'));
|
||||
http_msg::Response s1 = get(app, "/show", {{"cookie", cookie}});
|
||||
CHECK_EQ_STR(s1.body(), "info:hello once;", "flash appears on next request");
|
||||
http_msg::Response s2 = get(app, "/show", {{"cookie", cookie}});
|
||||
CHECK_EQ_STR(s2.body(), "", "flash appears exactly once");
|
||||
return test_report("views/dispatch");
|
||||
}
|
||||
Reference in New Issue
Block a user