2026-10-24 01:59:27 +02:00
|
|
|
// Route: /validate/user (all-failures validation, JSON and HTML form)
|
2026-10-23 18:20:04 +02:00
|
|
|
#include "support/Assert.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace molo::example::test;
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
Fixture f;
|
|
|
|
|
auto ok = f.client->post(
|
|
|
|
|
"/validate/user",
|
|
|
|
|
"{\"username\":\"ada\",\"email\":\"ada@x.io\",\"age\":\"36\"}",
|
|
|
|
|
"application/json");
|
|
|
|
|
check(ok.status == 200, "valid model is 200");
|
|
|
|
|
auto bad = f.client->post("/validate/user",
|
|
|
|
|
"{\"username\":\"ad\",\"age\":\"old\"}",
|
|
|
|
|
"application/json");
|
|
|
|
|
check(bad.status == 422, "invalid model is 422");
|
|
|
|
|
check(contains(bad.body, "username"), "username failure listed");
|
|
|
|
|
check(contains(bad.body, "email"), "missing email listed");
|
|
|
|
|
check(contains(bad.body, "age"), "bad age listed");
|
2026-10-24 01:59:27 +02:00
|
|
|
|
|
|
|
|
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");
|
2026-10-23 18:20:04 +02:00
|
|
|
return finish("validate/user");
|
|
|
|
|
}
|