feat(workflow): example stats page with embedded Chart.js, inline form validation errors
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +1,37 @@
|
||||
// Route: /forms/contact (form render plus validation)
|
||||
// Route: /forms/contact (form render plus validation shown inline)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
const char* form_type = "application/x-www-form-urlencoded";
|
||||
auto form = f.client->get("/forms/contact");
|
||||
check(form.status == 200, "form renders 200");
|
||||
check(contains(form.body, "<form"), "form markup present");
|
||||
check(!contains(form.body, "form-errors"), "fresh form has no error box");
|
||||
auto ok = f.client->post("/forms/contact",
|
||||
"name=ada&email=ada@x.io&message=hi",
|
||||
"application/x-www-form-urlencoded");
|
||||
"name=ada&email=ada@x.io&message=hi", form_type);
|
||||
check(ok.status == 200, "valid post is 200");
|
||||
check(contains(ok.body, "Thanks ada"), "success page names user");
|
||||
auto bad = f.client->post("/forms/contact", "name=ada",
|
||||
"application/x-www-form-urlencoded");
|
||||
|
||||
auto bad = f.client->post("/forms/contact", "name=ada&email=nope", form_type);
|
||||
check(bad.status == 422, "invalid post is 422");
|
||||
check(contains(bad.body, "validation_error"), "failures listed");
|
||||
check(contains(bad.body, "<form"), "form is shown again");
|
||||
check(contains(bad.body, "form-errors"), "summary box rendered");
|
||||
check(contains(bad.body, "Please fix 2 field(s)"), "summary counts failures");
|
||||
check(contains(bad.body, "must be a valid email address"),
|
||||
"email message inline");
|
||||
check(contains(bad.body, "id=\"e-message\""), "message error attached");
|
||||
check(contains(bad.body, "aria-invalid=\"true\""), "invalid input flagged");
|
||||
check(contains(bad.body, "value=\"ada\""), "valid input kept on redisplay");
|
||||
check(!contains(bad.body, "validation_error"), "no raw JSON in the page");
|
||||
check(!contains(bad.body, "{""), "no escaped JSON in the page");
|
||||
|
||||
auto api = f.client->request(
|
||||
molo::example::fw::Method::Post, "/forms/contact", "name=ada",
|
||||
{{"Content-Type", form_type}, {"Accept", "application/json"}});
|
||||
check(api.status == 422, "json client gets 422");
|
||||
check(contains(api.body, "validation_error"), "json client gets failures");
|
||||
return finish("forms/contact");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Route: GET /stats/data (request, connection, and service statistics)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
f.client->get("/health");
|
||||
f.client->get("/health");
|
||||
f.client->get("/no/such/route");
|
||||
f.client->post("/forms/contact", "name=ada",
|
||||
"application/x-www-form-urlencoded");
|
||||
auto res = f.client->get("/stats/data");
|
||||
check(res.status == 200, "status 200");
|
||||
check(contains(res.headers.get("Content-Type"), "application/json"), "json");
|
||||
check(contains(res.body, "\"requests\":4"), "four requests before this one");
|
||||
check(contains(res.body, "\"errors\":2"), "404 and 422 counted as errors");
|
||||
check(contains(res.body, "\"404\":1") && contains(res.body, "\"422\":1"),
|
||||
"status breakdown");
|
||||
check(contains(res.body, "\"/health\""), "route pattern grouped");
|
||||
check(contains(res.body, "\"(unmatched)\""), "unmatched bucket");
|
||||
check(contains(res.body, "\"POST\":1") && contains(res.body, "\"GET\":3"),
|
||||
"method breakdown");
|
||||
check(contains(res.body, "\"p95_ms\""), "latency percentiles");
|
||||
check(contains(res.body, "\"connections\""), "connection block");
|
||||
check(contains(res.body, "\"series\""), "time series");
|
||||
check(f.services.stats->total_requests() == 5, "side effect: 5 recorded");
|
||||
return finish("stats/data");
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// Route: GET /stats (Chart.js page; library comes from the embedded bundle)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
Fixture f;
|
||||
auto page = f.client->get("/stats");
|
||||
check(page.status == 200, "status 200");
|
||||
check(contains(page.headers.get("Content-Type"), "text/html"), "html");
|
||||
check(contains(page.body, "<canvas id=\"c-requests\""), "request chart");
|
||||
check(contains(page.body, "<canvas id=\"c-conns\""), "connection chart");
|
||||
check(contains(page.body, "/embedded/chart.umd.min.js"), "chart.js linked");
|
||||
auto lib = f.client->get("/embedded/chart.umd.min.js");
|
||||
check(lib.status == 200, "chart.js served");
|
||||
check(lib.headers.get("X-Asset-Source") == "embedded", "from the bundle");
|
||||
check(contains(lib.body, "Chart.js v"), "real chart.js build embedded");
|
||||
auto js = f.client->get("/embedded/stats.js");
|
||||
check(js.status == 200 && contains(js.body, "/stats/data"), "page script");
|
||||
check(f.client->request(molo::example::fw::Method::Post, "/stats").status ==
|
||||
405,
|
||||
"post is 405");
|
||||
return finish("stats/page");
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// Route: POST /validate/user (all-failures validation error)
|
||||
// Route: /validate/user (all-failures validation, JSON and HTML form)
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
@@ -17,5 +17,19 @@ int main() {
|
||||
check(contains(bad.body, "username"), "username failure listed");
|
||||
check(contains(bad.body, "email"), "missing email listed");
|
||||
check(contains(bad.body, "age"), "bad age listed");
|
||||
|
||||
auto page = f.client->get("/validate/user");
|
||||
check(page.status == 200, "form page is 200");
|
||||
check(contains(page.body, "name=\"username\""), "username input");
|
||||
auto html = f.client->request(
|
||||
molo::example::fw::Method::Post, "/validate/user",
|
||||
"username=ad&email=x&age=200",
|
||||
{{"Content-Type", "application/x-www-form-urlencoded"},
|
||||
{"Accept", "text/html"}});
|
||||
check(html.status == 422, "browser post is 422");
|
||||
check(contains(html.body, "Please fix 3 field(s)"), "all three listed");
|
||||
check(contains(html.body, "must be at least 3 characters"), "username text");
|
||||
check(contains(html.body, "must be between 0 and 150"), "age text");
|
||||
check(!contains(html.body, "validation_error"), "no raw JSON in the page");
|
||||
return finish("validate/user");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user