Skip to main content

doido_controller/
testing.rs

1//! Integration/system test helpers: drive a `Router` in-process and inspect the
2//! response (Rails integration/system tests). Pair with `doido_model` factories
3//! and `TestDb` for full-stack tests.
4
5use axum::body::Body;
6use axum::Router;
7use http::{Request, StatusCode};
8use tower::ServiceExt;
9
10/// The status + body captured from a test request.
11pub struct TestResponse {
12    pub status: StatusCode,
13    pub body: String,
14}
15
16/// Send a request to `router` in-process and capture the response.
17pub async fn send(router: Router, method: &str, uri: &str, body: &str) -> TestResponse {
18    let request = Request::builder()
19        .method(method)
20        .uri(uri)
21        .body(Body::from(body.to_string()))
22        .expect("valid test request");
23    let response = router
24        .oneshot(request)
25        .await
26        .expect("router handled the request");
27    let status = response.status();
28    let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
29        .await
30        .expect("read response body");
31    TestResponse {
32        status,
33        body: String::from_utf8_lossy(&bytes).to_string(),
34    }
35}