Files
moloapp/tests/src/app/users/detail_test.cpp
T

76 lines
3.2 KiB
C++
Raw Normal View History

// V2: static/converter/catch-all matches, 404 shape, 405 Allow, url_for.
#include "../_testlib.h"
using namespace molo::app;
int main() {
app::Application app;
app.router().get("/users/me", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("me");
}));
routing::RouteOptions detail;
detail.name = "user_detail";
app.router().get("/users/{id:int}", views::adapt([](http_msg::Request& req) {
return http_msg::TextResponse("user " + req.path_param("id"));
}), detail);
routing::RouteOptions file;
file.name = "file_catch";
app.router().get("/files/{rest:path}", views::adapt([](http_msg::Request& req) {
return http_msg::TextResponse("file " + req.path_param("rest"));
}), file);
routing::RouteOptions multi;
multi.name = "multi";
app.router().add({http_msg::Method::GET, http_msg::Method::POST}, "/multi",
views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("m");
}),
multi);
app.startup();
CHECK_EQ_STR(get(app, "/users/me").body(), "me", "static route wins over converter");
CHECK_EQ_STR(get(app, "/users/42").body(), "user 42", "int converter binds");
CHECK_EQ_STR(get(app, "/files/a/b/c").body(), "file a/b/c", "path catch-all binds rest");
http_msg::Response nf = get(app, "/nope");
CHECK_EQ_INT(nf.status(), 404, "missing route is 404");
validate::Value body = validate::parse_json(nf.body());
const auto* root = std::get_if<validate::Object>(&body.storage());
CHECK(root != nullptr && root->count("error"), "404 has error shape");
const auto* inner = std::get_if<validate::Object>(&root->at("error").storage());
CHECK(inner != nullptr && inner->at("code") == validate::Value("not_found"),
"404 code is not_found");
CHECK(inner != nullptr && !inner->at("request_id").as_text().empty(),
"404 carries request_id");
http_msg::Request bad = make_request("POST", "/users/me");
http_msg::Response m = run_request(app, bad);
CHECK_EQ_INT(m.status(), 405, "method mismatch is 405");
CHECK_EQ_STR(m.headers().get("allow"), "GET", "405 Allow lists GET");
http_msg::Request bad2 = make_request("DELETE", "/multi");
http_msg::Response m2 = run_request(app, bad2);
CHECK_EQ_INT(m2.status(), 405, "multi-method mismatch is 405");
CHECK_EQ_STR(m2.headers().get("allow"), "GET, POST", "405 Allow exact value");
CHECK_EQ_STR(app.url_for("user_detail", {{"id", "7"}}), "/users/7", "url_for converter");
CHECK_EQ_STR(app.url_for("file_catch", {{"rest", "a/b"}}), "/files/a/b", "url_for catch-all");
CHECK_EQ_STR(app.url_for("multi", {}, "a=1"), "/multi?a=1", "url_for with query");
bool threw = false;
try {
app.url_for("no_such_route");
} catch (const errors::NotFound&) {
threw = true;
}
CHECK(threw, "url_for unknown name throws NotFound");
bool slash_404 = (get(app, "/users/me/").status() == 404);
CHECK(slash_404, "trailing slash is strict by default");
std::string long_path = "/" + std::string(2048, 'x');
http_msg::Response too_long = get(app, long_path);
CHECK_EQ_INT(too_long.status(), 414, "overlong path yields 414");
CHECK(!too_long.headers().get("x-request-id").empty(), "414 echoes request id");
return test_report("users/detail");
}