use std::time::Duration;
use axum::{routing::get, Json, Router};
use proa_framework_axum::{serve_with_shutdown, RELOAD_PORT_ENV, RELOAD_TOKEN_ENV};
const TOKEN: &str = "00112233445566778899aabbccddeeff";
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn proa_dev_injects_the_live_reload_client_into_html_over_real_http() {
std::env::set_var(RELOAD_PORT_ENV, "45999");
std::env::set_var(RELOAD_TOKEN_ENV, TOKEN);
let app = Router::new()
.route(
"/",
get(|| async {
axum::response::Html(
"<!DOCTYPE html><html><head><title>site</title></head><body>hi</body></html>",
)
}),
)
.route(
"/api",
get(|| async { Json(serde_json::json!({"ok": true})) }),
);
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
.await
.unwrap();
let address = listener.local_addr().unwrap();
let (stop, stopped) = tokio::sync::oneshot::channel::<()>();
let server = tokio::spawn(async move {
serve_with_shutdown(listener, app, Duration::from_secs(5), async {
let _ = stopped.await;
})
.await
});
let client = reqwest::Client::new();
let page = client
.get(format!("http://{address}/"))
.send()
.await
.expect("request the page");
assert!(
page.headers()
.get(reqwest::header::CONTENT_LENGTH)
.is_none(),
"{:?}",
page.headers()
);
let html = page.text().await.expect("read the page");
let script = html.find("<script>").expect("client injected");
assert!(script < html.find("</head>").unwrap(), "{html}");
assert!(html.contains("__proaLiveReload"), "{html}");
assert!(html.contains("45999"), "{html}");
assert!(html.contains(TOKEN), "{html}");
assert!(html.ends_with("<body>hi</body></html>"), "{html}");
std::env::remove_var(RELOAD_TOKEN_ENV);
assert!(proa_framework_axum::dev_reload::layer_from_env().is_none());
std::env::set_var(RELOAD_TOKEN_ENV, TOKEN);
let api = client
.get(format!("http://{address}/api"))
.send()
.await
.expect("request the API");
assert_eq!(api.text().await.unwrap(), r#"{"ok":true}"#);
let _ = stop.send(());
server.await.unwrap().unwrap();
}