Verify test server identity before accepting it
This commit is contained in:
1 parent
7c98c61b7a
commit
1d891b2c6a
1 file changed
+5
-3
+5
-3
@@ -36,6 +36,7 @@ struct TestServer {
|
|||||||
child: Child,
|
child: Child,
|
||||||
port: u16,
|
port: u16,
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
|
marker: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestServer {
|
impl TestServer {
|
||||||
@@ -54,9 +55,10 @@ impl TestServer {
|
|||||||
));
|
));
|
||||||
let web = root.join("web");
|
let web = root.join("web");
|
||||||
fs::create_dir_all(&web).expect("create web dir");
|
fs::create_dir_all(&web).expect("create web dir");
|
||||||
|
let marker = format!("marker-{stamp}");
|
||||||
fs::write(
|
fs::write(
|
||||||
web.join("index.html"),
|
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");
|
.expect("write index");
|
||||||
fs::write(web.join("styles.css"), "body { color: #123456; }").expect("write css");
|
fs::write(web.join("styles.css"), "body { color: #123456; }").expect("write css");
|
||||||
@@ -88,6 +90,7 @@ impl TestServer {
|
|||||||
child,
|
child,
|
||||||
port,
|
port,
|
||||||
root: root.clone(),
|
root: root.clone(),
|
||||||
|
marker: marker.clone(),
|
||||||
};
|
};
|
||||||
match server.wait_ready() {
|
match server.wait_ready() {
|
||||||
Ok(()) => return server,
|
Ok(()) => return server,
|
||||||
@@ -107,8 +110,7 @@ impl TestServer {
|
|||||||
fn wait_ready(&mut self) -> Result<(), String> {
|
fn wait_ready(&mut self) -> Result<(), String> {
|
||||||
for _ in 0..1800 {
|
for _ in 0..1800 {
|
||||||
if let Ok(response) = send(self.port, "GET", "/", &[], "text/plain") {
|
if let Ok(response) = send(self.port, "GET", "/", &[], "text/plain") {
|
||||||
if response.status == 200 {
|
if response.status == 200 && response.text().contains(&self.marker) {
|
||||||
std::thread::sleep(Duration::from_millis(100));
|
|
||||||
if let Some(status) = self.child.try_wait().expect("poll server") {
|
if let Some(status) = self.child.try_wait().expect("poll server") {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"server on port {} exited with {status}:\n{}",
|
"server on port {} exited with {status}:\n{}",
|
||||||
|
|||||||
Reference in new issue
Block a user