use std::{
cell::RefCell,
future::{Future, ready},
pin::pin,
task::{Context, Poll, Waker},
};
use crate::client::{Transport, TransportError};
pub struct Recorded {
answer: String,
sent: RefCell<Vec<(String, String)>>,
}
impl Recorded {
pub fn answering(answer: &str) -> Recorded {
Recorded { answer: answer.to_owned(), sent: RefCell::new(Vec::new()) }
}
pub fn sent(&self) -> Vec<(String, String)> {
self.sent.borrow().clone()
}
}
impl Transport for Recorded {
fn post(&self, path: &str, body: String) -> impl Future<Output = Result<String, TransportError>> {
self.sent.borrow_mut().push((path.to_owned(), body));
ready(Ok(self.answer.clone()))
}
}
pub fn block_on<F: Future>(future: F) -> F::Output {
let mut future = pin!(future);
let mut context = Context::from_waker(Waker::noop());
loop {
if let Poll::Ready(value) = future.as_mut().poll(&mut context) {
return value;
}
}
}