Add a check method to the Transcript.
This commit is contained in:
parent
5c9cd52baf
commit
e8d42264e8
|
|
@ -1,12 +1,12 @@
|
||||||
//! A [`Service`](tower::Service) implementation based on a fixed transcript.
|
//! A [`Service`](tower::Service) implementation based on a fixed transcript.
|
||||||
|
|
||||||
use color_eyre::eyre::{eyre, Report};
|
use color_eyre::eyre::{ensure, eyre, Report};
|
||||||
use futures::future::{ready, Ready};
|
use futures::future::{ready, Ready};
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
use tower::Service;
|
use tower::{Service, ServiceExt};
|
||||||
|
|
||||||
pub struct Transcript<R, S, I>
|
pub struct Transcript<R, S, I>
|
||||||
where
|
where
|
||||||
|
|
@ -24,6 +24,32 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<R, S, I> Transcript<R, S, I>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = (R, S)>,
|
||||||
|
R: Debug,
|
||||||
|
S: Debug + Eq,
|
||||||
|
{
|
||||||
|
pub async fn check<C>(mut self, mut to_check: C) -> Result<(), Report>
|
||||||
|
where
|
||||||
|
C: Service<R, Response = S>,
|
||||||
|
C::Error: Debug,
|
||||||
|
{
|
||||||
|
while let Some((req, expected_rsp)) = self.messages.next() {
|
||||||
|
// These unwraps could propagate errors with the correct
|
||||||
|
// bound on C::Error
|
||||||
|
let rsp = to_check.ready_and().await.unwrap().call(req).await.unwrap();
|
||||||
|
ensure!(
|
||||||
|
rsp == expected_rsp,
|
||||||
|
"Expected {:?}, got {:?}",
|
||||||
|
expected_rsp,
|
||||||
|
rsp
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<R, S, I> Service<R> for Transcript<R, S, I>
|
impl<R, S, I> Service<R> for Transcript<R, S, I>
|
||||||
where
|
where
|
||||||
R: Debug + Eq,
|
R: Debug + Eq,
|
||||||
|
|
|
||||||
|
|
@ -32,3 +32,10 @@ async fn transcript_errors_wrong_request() {
|
||||||
);
|
);
|
||||||
assert!(svc.ready_and().await.unwrap().call("bad").await.is_err());
|
assert!(svc.ready_and().await.unwrap().call("bad").await.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn self_check() {
|
||||||
|
let t1 = Transcript::from(TRANSCRIPT_DATA.iter().cloned());
|
||||||
|
let t2 = Transcript::from(TRANSCRIPT_DATA.iter().cloned());
|
||||||
|
assert!(t1.check(t2).await.is_ok());
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue