Skip to content

Commit 4c24713

Browse files
feat: add IrmaClient::health() wrapper for the /health endpoint
Adds IrmaClient::health() which issues GET {base}/health (the endpoint added in irmago v0.15.0) and returns Ok(()) on a 2xx response, mapping any failure to Error::NetworkError. Useful as a liveness/readiness check before starting a session, mirroring the existing client method style. Includes an integration test gated behind RUN_INTEGRATION_TESTS. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 4a04d81 commit 4c24713

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/irmaclient.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ impl IrmaClient {
142142
status => Err(Error::SessionNotFinished(status)),
143143
}
144144
}
145+
146+
/// Check whether the irma server is healthy and ready to serve sessions.
147+
///
148+
/// Issues `GET {base}/health` (added in irmago v0.15.0) and returns
149+
/// `Ok(())` on a 2xx response, or [`Error::NetworkError`] otherwise.
150+
/// Useful as a liveness/readiness check before starting a session.
151+
pub async fn health(&self) -> Result<(), Error> {
152+
self.client
153+
.get(self.url.join("health")?)
154+
.send()
155+
.await?
156+
.error_for_status()?;
157+
Ok(())
158+
}
145159
}
146160

147161
/// Builder for IRMA clients

tests/test_health.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::process::Stdio;
2+
3+
use irmars::IrmaClient;
4+
use serial_test::serial;
5+
use tokio::{
6+
io::{AsyncBufReadExt, BufReader},
7+
process::Command,
8+
};
9+
10+
#[test]
11+
#[serial]
12+
fn test_health() {
13+
if option_env!("RUN_INTEGRATION_TESTS").is_some() {
14+
tokio_test::block_on(async {
15+
let mut irmaserver = Command::new("irma")
16+
.arg("server")
17+
.stderr(Stdio::piped())
18+
.spawn()
19+
.expect("Could not start irma server");
20+
21+
let irmaserver_stderr = irmaserver
22+
.stderr
23+
.take()
24+
.expect("No stderr available from irma server");
25+
let mut irmaserver_lines = BufReader::new(irmaserver_stderr).lines();
26+
loop {
27+
let line = irmaserver_lines
28+
.next_line()
29+
.await
30+
.expect("Error reading from irma server stderr")
31+
.expect("No line recieved");
32+
if line.contains("Server listening") {
33+
break;
34+
}
35+
}
36+
37+
println!("Server started");
38+
39+
// Create an irma client
40+
let client = IrmaClient::new("http://localhost:8088/").unwrap();
41+
42+
// A running server should report itself as healthy.
43+
client.health().await.expect("Health check failed");
44+
45+
irmaserver.kill().await.expect("Error killing irma server");
46+
});
47+
}
48+
}

0 commit comments

Comments
 (0)