what-core 1.7.5

Core framework for What - an HTML-first web framework powered by Rust
Documentation
mod common;

use axum::http::StatusCode;
use common::*;
use what_core::server::create_router;

#[tokio::test]
async fn flash_success_after_create() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<h1>#flash.success#</h1>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    // POST to create an item — should set flash
    let resp = post_form(&router, "/w-action/tasks?w-redirect=/", "title=Test").await;
    assert_eq!(resp.status, StatusCode::SEE_OTHER);

    // Extract session cookie from redirect response
    let cookie = resp.set_cookie().expect("should set session cookie");

    // GET the redirect target — flash should be consumed and rendered
    let resp = get_with_headers(&router, "/", vec![("cookie", cookie)]).await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("Item created in tasks");
}

#[tokio::test]
async fn flash_consumed_on_second_load() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<h1>#flash.success|default:"none"#</h1>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    // Create action sets flash
    let resp = post_form(&router, "/w-action/items?w-redirect=/", "name=Test").await;
    let cookie = resp.set_cookie().expect("should set session cookie");

    // First load — flash present
    let resp = get_with_headers(&router, "/", vec![("cookie", cookie)]).await;
    resp.assert_contains("Item created in items");

    // Second load — flash consumed
    let cookie2 = resp.set_cookie().unwrap_or(cookie);
    let resp = get_with_headers(&router, "/", vec![("cookie", cookie2)]).await;
    resp.assert_not_contains("Item created");
    resp.assert_contains("none");
}

#[tokio::test]
async fn flash_error_on_action_failure() {
    let proj = TestProject::new();
    proj.add_page("form.html", r##"<p>#flash.error|default:"no-error"#</p>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    // Try an unknown action — should fail
    let resp = post_form(
        &router,
        "/w-action/tasks?w-action=unknown&w-redirect=/form",
        "name=Test",
    )
    .await;
    let cookie = resp.set_cookie().expect("should set session cookie");

    // Load the form page — should show error flash
    let resp = get_with_headers(&router, "/form", vec![("cookie", cookie)]).await;
    resp.assert_contains("Failed to create");
}

#[tokio::test]
async fn has_errors_false_by_default() {
    let proj = TestProject::new();
    proj.add_page("index.html", r##"<p>errors: #has_errors#</p>"##);
    let (_dir, state) = proj.build_state();
    let router = create_router(state);

    let resp = get(&router, "/").await;
    assert_eq!(resp.status, StatusCode::OK);
    resp.assert_contains("errors: false");
}