Skip to main content

Module testing

Module testing 

Source
Expand description

Test an App in-process, without sockets

TestClient sends requests straight through routing, middleware and handlers, exactly as a running server would, and returns the response. No port is opened, so tests are fast and cannot collide.

use ullun::prelude::*;
use ullun::testing::TestClient;

let app = App::new().post("/echo", |req: Request| async move {
    let name: String = req.json::<serde_json::Value>()?["name"]
        .as_str()
        .unwrap_or("nobody")
        .to_string();
    Ok(Response::json(serde_json::json!({ "hello": name })))
});
let client = TestClient::new(app);

let res = client.post("/echo").json(&serde_json::json!({ "name": "Ada" })).send().await;
assert_eq!(res.status(), 200);
assert_eq!(res.json::<serde_json::Value>()["hello"], "Ada");

TestClient::with_cookies keeps a cookie jar between calls, which makes login flows and sessions testable. Background tasks registered with App::background are not started, and WebSocket upgrades need a real server (App::serve on port 0).

Structsยง

TestClient
Calls an app in-process, see the module documentation
TestRequest
A request under construction
TestResponse
What the app answered