diff --git a/src/irmaclient.rs b/src/irmaclient.rs index 760cc7e..26c3a0f 100644 --- a/src/irmaclient.rs +++ b/src/irmaclient.rs @@ -142,6 +142,20 @@ impl IrmaClient { status => Err(Error::SessionNotFinished(status)), } } + + /// Check whether the irma server is healthy and ready to serve sessions. + /// + /// Issues `GET {base}/health` (added in irmago v0.15.0) and returns + /// `Ok(())` on a 2xx response, or [`Error::NetworkError`] otherwise. + /// Useful as a liveness/readiness check before starting a session. + pub async fn health(&self) -> Result<(), Error> { + self.client + .get(self.url.join("health")?) + .send() + .await? + .error_for_status()?; + Ok(()) + } } /// Builder for IRMA clients diff --git a/tests/test_health.rs b/tests/test_health.rs new file mode 100644 index 0000000..be75613 --- /dev/null +++ b/tests/test_health.rs @@ -0,0 +1,48 @@ +use std::process::Stdio; + +use irmars::IrmaClient; +use serial_test::serial; +use tokio::{ + io::{AsyncBufReadExt, BufReader}, + process::Command, +}; + +#[test] +#[serial] +fn test_health() { + if option_env!("RUN_INTEGRATION_TESTS").is_some() { + tokio_test::block_on(async { + let mut irmaserver = Command::new("irma") + .arg("server") + .stderr(Stdio::piped()) + .spawn() + .expect("Could not start irma server"); + + let irmaserver_stderr = irmaserver + .stderr + .take() + .expect("No stderr available from irma server"); + let mut irmaserver_lines = BufReader::new(irmaserver_stderr).lines(); + loop { + let line = irmaserver_lines + .next_line() + .await + .expect("Error reading from irma server stderr") + .expect("No line recieved"); + if line.contains("Server listening") { + break; + } + } + + println!("Server started"); + + // Create an irma client + let client = IrmaClient::new("http://localhost:8088/").unwrap(); + + // A running server should report itself as healthy. + client.health().await.expect("Health check failed"); + + irmaserver.kill().await.expect("Error killing irma server"); + }); + } +}