Files
moloapp/tests/src/app/health/ping_test.cpp
T

66 lines
2.8 KiB
C++
Raw Normal View History

// V1: response subclass round-trips through the real stack.
#include "../_testlib.h"
using namespace molo::app;
int main() {
app::Application app;
routing::RouteOptions o;
app.router().get("/ping", views::adapt([](http_msg::Request&) {
return http_msg::TextResponse("pong");
}));
app.router().get("/user", views::adapt([](http_msg::Request&) {
validate::Object u;
u["name"] = validate::Value("ada");
u["id"] = validate::Value(7);
return http_msg::Response(http_msg::JsonResponse(validate::Value(u)));
}));
app.router().get("/page", views::adapt([](http_msg::Request&) {
return http_msg::Response(http_msg::HtmlResponse("<h1>hi</h1>"));
}));
app.router().get("/feed", views::adapt([](http_msg::Request&) {
return http_msg::Response(http_msg::XmlResponse("<a/>"));
}));
app.router().get("/old", views::adapt([](http_msg::Request&) {
return http_msg::Response(http_msg::RedirectResponse::temporary("/page"));
}));
app.router().get("/moved", views::adapt([](http_msg::Request&) {
return http_msg::Response(http_msg::RedirectResponse::permanent("/page"));
}));
app.startup();
http_msg::Response t = get(app, "/ping");
CHECK_EQ_INT(t.status(), 200, "text status");
CHECK_EQ_STR(t.body(), "pong", "text body");
CHECK_EQ_STR(t.content_type(), "text/plain; charset=utf-8", "text content type");
http_msg::Response j = get(app, "/user");
CHECK_EQ_INT(j.status(), 200, "json status");
CHECK_EQ_STR(j.content_type(), "application/json", "json content type");
validate::Value parsed = validate::parse_json(j.body());
const auto* obj = std::get_if<validate::Object>(&parsed.storage());
CHECK(obj != nullptr, "json body parses to object");
CHECK(obj != nullptr && obj->at("name") == validate::Value("ada"), "json name field");
CHECK(obj != nullptr && obj->at("id") == validate::Value(7), "json id field");
http_msg::Response h = get(app, "/page");
CHECK_EQ_INT(h.status(), 200, "html status");
CHECK_EQ_STR(h.content_type(), "text/html; charset=utf-8", "html content type");
CHECK_EQ_STR(h.body(), "<h1>hi</h1>", "html body");
http_msg::Response x = get(app, "/feed");
CHECK_EQ_INT(x.status(), 200, "xml status");
CHECK_EQ_STR(x.content_type(), "application/xml; charset=utf-8", "xml content type");
http_msg::Response r307 = get(app, "/old");
CHECK_EQ_INT(r307.status(), 307, "temporary redirect keeps method with 307");
CHECK_EQ_STR(r307.headers().get("location"), "/page", "307 location");
http_msg::Response r308 = get(app, "/moved");
CHECK_EQ_INT(r308.status(), 308, "permanent redirect keeps method with 308");
CHECK_EQ_STR(r308.headers().get("location"), "/page", "308 location");
CHECK(!t.headers().get("x-request-id").empty(), "request id echoed on response");
return test_report("health/ping");
}