use std::net::TcpListener;
use std::process::Command as StdCommand;
use std::time::Duration;
use tokio::process::Command as TokioCommand;
#[tokio::test]
pub async fn test_http_custom_host() {
let bin = assert_cmd::cargo::cargo_bin("dft");
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to a random port");
let addr = listener.local_addr().unwrap();
drop(listener);
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to a random port");
let metrics_addr = listener.local_addr().unwrap();
drop(listener);
let _server = TokioCommand::new(bin)
.env("RUST_LOG", "off")
.arg("serve-http")
.arg("--addr")
.arg(format!("{addr}"))
.arg("--metrics-addr")
.arg(format!("{metrics_addr}"))
.kill_on_drop(true)
.spawn()
.expect("Failed to spawn server");
tokio::time::sleep(Duration::from_secs(3)).await;
let response = StdCommand::new("curl")
.arg(format!("http://{}", addr))
.output()
.unwrap();
assert!(
String::from_utf8_lossy(&response.stdout).contains("Hello, from DFT!"),
"Server should respond on custom host"
);
}