2026-10-24 01:59:27 +02:00
|
|
|
// Route: /forms/contact (form render plus validation shown inline)
|
2026-10-23 18:20:04 +02:00
|
|
|
#include "support/Assert.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace molo::example::test;
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
Fixture f;
|
2026-10-24 01:59:27 +02:00
|
|
|
const char* form_type = "application/x-www-form-urlencoded";
|
2026-10-23 18:20:04 +02:00
|
|
|
auto form = f.client->get("/forms/contact");
|
|
|
|
|
check(form.status == 200, "form renders 200");
|
|
|
|
|
check(contains(form.body, "<form"), "form markup present");
|
2026-10-24 01:59:27 +02:00
|
|
|
check(!contains(form.body, "form-errors"), "fresh form has no error box");
|
2026-10-23 18:20:04 +02:00
|
|
|
auto ok = f.client->post("/forms/contact",
|
2026-10-24 01:59:27 +02:00
|
|
|
"name=ada&email=ada@x.io&message=hi", form_type);
|
2026-10-23 18:20:04 +02:00
|
|
|
check(ok.status == 200, "valid post is 200");
|
|
|
|
|
check(contains(ok.body, "Thanks ada"), "success page names user");
|
2026-10-24 01:59:27 +02:00
|
|
|
|
|
|
|
|
auto bad = f.client->post("/forms/contact", "name=ada&email=nope", form_type);
|
2026-10-23 18:20:04 +02:00
|
|
|
check(bad.status == 422, "invalid post is 422");
|
2026-10-24 01:59:27 +02:00
|
|
|
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");
|
2026-10-23 18:20:04 +02:00
|
|
|
return finish("forms/contact");
|
|
|
|
|
}
|