Verify test server identity before accepting it

This commit is contained in:
gloamxun committed 2026-10-26 20:34:20 +08:00
1 parent 7c98c61b7a
commit 1d891b2c6a
1 file changed
+5 -3
+5 -3
View File
@@ -36,6 +36,7 @@ struct TestServer {
child: Child,
port: u16,
root: PathBuf,
marker: String,
}
impl TestServer {
@@ -54,9 +55,10 @@ impl TestServer {
));
let web = root.join("web");
fs::create_dir_all(&web).expect("create web dir");
let marker = format!("marker-{stamp}");
fs::write(
web.join("index.html"),
"<!doctype html><title>test-frontend</title><p>test-index</p>",
format!("<!doctype html><title>test-frontend</title><p>test-index</p><p>{marker}</p>"),
)
.expect("write index");
fs::write(web.join("styles.css"), "body { color: #123456; }").expect("write css");
@@ -88,6 +90,7 @@ impl TestServer {
child,
port,
root: root.clone(),
marker: marker.clone(),
};
match server.wait_ready() {
Ok(()) => return server,
@@ -107,8 +110,7 @@ impl TestServer {
fn wait_ready(&mut self) -> Result<(), String> {
for _ in 0..1800 {
if let Ok(response) = send(self.port, "GET", "/", &[], "text/plain") {
if response.status == 200 {
std::thread::sleep(Duration::from_millis(100));
if response.status == 200 && response.text().contains(&self.marker) {
if let Some(status) = self.child.try_wait().expect("poll server") {
return Err(format!(
"server on port {} exited with {status}:\n{}",