use crate::status::decode_error;
use async_trait::async_trait;
use std::collections::VecDeque;
use std::sync::{Mutex, MutexGuard};
use troupekit_core::error::ProviderError;
pub use troupekit_core::http::{
Body, ChunkBody, HttpError, HttpErrorKind, HttpRequest, HttpResponse, Method, Transport,
};
pub async fn read_body(body: &mut dyn Body, limit: usize) -> Result<Vec<u8>, ProviderError> {
let (bytes, over) = troupekit_core::http::read_limited(body, limit).await?;
if over {
return Err(decode_error(format!("reply body is over {limit} bytes")));
}
Ok(bytes)
}
#[derive(Debug, Clone)]
pub enum Scripted {
Reply {
status: u16,
headers: Vec<(String, String)>,
chunks: Vec<Vec<u8>>,
},
Fail(HttpError),
}
impl Scripted {
pub fn body(status: u16, body: impl Into<Vec<u8>>) -> Self {
Scripted::Reply {
status,
headers: Vec::new(),
chunks: vec![body.into()],
}
}
pub fn chunked(body: &str, size: usize) -> Self {
assert!(size > 0, "chunk size must be above zero");
let chunks = body.as_bytes().chunks(size).map(<[u8]>::to_vec).collect();
Scripted::Reply {
status: 200,
headers: Vec::new(),
chunks,
}
}
pub fn with_header(self, name: &str, value: &str) -> Self {
match self {
Scripted::Reply {
status,
mut headers,
chunks,
} => {
headers.push((name.to_owned(), value.to_owned()));
Scripted::Reply {
status,
headers,
chunks,
}
}
fail => fail,
}
}
}
#[derive(Debug, Default)]
pub struct ScriptedTransport {
replies: Mutex<VecDeque<Scripted>>,
sent: Mutex<Vec<HttpRequest>>,
}
impl ScriptedTransport {
pub fn new(replies: Vec<Scripted>) -> Self {
Self {
replies: Mutex::new(replies.into()),
sent: Mutex::new(Vec::new()),
}
}
pub fn sent(&self) -> Vec<HttpRequest> {
lock(&self.sent).clone()
}
}
#[async_trait]
impl Transport for ScriptedTransport {
async fn send(&self, request: HttpRequest) -> Result<HttpResponse, HttpError> {
let url = request.url.clone();
lock(&self.sent).push(request);
let next = lock(&self.replies).pop_front();
match next {
Some(Scripted::Reply {
status,
headers,
chunks,
}) => Ok(HttpResponse {
status,
url,
headers,
body: Box::new(ChunkBody::new(chunks)),
truncated: false,
}),
Some(Scripted::Fail(err)) => Err(err),
None => Err(HttpError::new(
HttpErrorKind::Network,
"script has no more replies",
)),
}
}
}
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
mutex
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}