use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::task::JoinHandle;
const MAX_REQUEST_BYTES: usize = 4 * 1024 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordedRequest {
pub method: String,
pub path: String,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
impl RecordedRequest {
pub fn header(&self, name: &str) -> Option<&str> {
let name = name.to_ascii_lowercase();
self.headers.iter().find(|(header, _)| *header == name).map(|(_, value)| value.as_str())
}
pub fn body_text(&self) -> String {
String::from_utf8_lossy(&self.body).into_owned()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FakeResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
impl FakeResponse {
pub fn json(status: u16, value: &serde_json::Value) -> Self {
Self {
status,
headers: vec![("content-type".into(), "application/json".into())],
body: value.to_string().into_bytes(),
}
}
pub fn status(status: u16) -> Self {
Self { status, headers: Vec::new(), body: Vec::new() }
}
pub fn with_header(mut self, name: &str, value: &str) -> Self {
self.headers.push((name.to_owned(), value.to_owned()));
self
}
}
#[derive(Debug, Clone)]
pub struct Fault {
pub path_prefix: String,
pub response: FakeResponse,
pub count: u32,
}
pub type FakeHandler = Arc<dyn Fn(&RecordedRequest) -> FakeResponse + Send + Sync>;
#[derive(Default)]
struct SharedState {
requests: Vec<RecordedRequest>,
faults: VecDeque<Fault>,
}
pub struct FakeServer {
address: SocketAddr,
state: Arc<Mutex<SharedState>>,
task: JoinHandle<()>,
}
impl std::fmt::Debug for FakeServer {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.debug_struct("FakeServer").field("address", &self.address).finish_non_exhaustive()
}
}
impl FakeServer {
pub async fn start(handler: FakeHandler) -> std::io::Result<Self> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let address = listener.local_addr()?;
let state = Arc::new(Mutex::new(SharedState::default()));
let task_state = state.clone();
let task = tokio::spawn(async move {
while let Ok((stream, _)) = listener.accept().await {
let state = task_state.clone();
let handler = handler.clone();
tokio::spawn(async move {
let _connection_result = serve_connection(stream, state, handler).await;
});
}
});
Ok(Self { address, state, task })
}
pub fn base_url(&self) -> String {
format!("http://{}", self.address)
}
pub fn inject(&self, fault: Fault) {
lock(&self.state).faults.push_back(fault);
}
pub fn requests(&self) -> Vec<RecordedRequest> {
lock(&self.state).requests.clone()
}
pub fn clear_faults(&self) {
lock(&self.state).faults.clear();
}
pub fn clear_requests(&self) {
lock(&self.state).requests.clear();
}
}
impl Drop for FakeServer {
fn drop(&mut self) {
self.task.abort();
}
}
fn lock(state: &Mutex<SharedState>) -> std::sync::MutexGuard<'_, SharedState> {
state.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn take_fault(state: &Mutex<SharedState>, path: &str) -> Option<FakeResponse> {
let mut state = lock(state);
let position = state.faults.iter().position(|fault| path.starts_with(&fault.path_prefix))?;
let fault = state.faults.get_mut(position)?;
let response = fault.response.clone();
fault.count = fault.count.saturating_sub(1);
if fault.count == 0 {
state.faults.remove(position);
}
Some(response)
}
async fn serve_connection(
mut stream: TcpStream,
state: Arc<Mutex<SharedState>>,
handler: FakeHandler,
) -> std::io::Result<()> {
let Some(request) = read_request(&mut stream).await? else { return Ok(()) };
lock(&state).requests.push(request.clone());
let response = take_fault(&state, &request.path).unwrap_or_else(|| handler(&request));
stream.write_all(&encode_response(&response)).await?;
stream.shutdown().await
}
async fn read_request(stream: &mut TcpStream) -> std::io::Result<Option<RecordedRequest>> {
let mut buffer = Vec::with_capacity(4096);
let mut chunk = [0u8; 4096];
let header_end = loop {
if let Some(position) = find_subslice(&buffer, b"\r\n\r\n") {
break position;
}
let read = stream.read(&mut chunk).await?;
if read == 0 || buffer.len() > MAX_REQUEST_BYTES {
return Ok(None);
}
buffer.extend_from_slice(chunk.get(..read).unwrap_or_default());
};
let head = String::from_utf8_lossy(buffer.get(..header_end).unwrap_or_default()).into_owned();
let mut lines = head.split("\r\n");
let mut request_line = lines.next().unwrap_or_default().split(' ');
let method = request_line.next().unwrap_or_default().to_owned();
let path = request_line.next().unwrap_or_default().to_owned();
let headers: Vec<(String, String)> = lines
.filter_map(|line| line.split_once(':'))
.map(|(name, value)| (name.trim().to_ascii_lowercase(), value.trim().to_owned()))
.collect();
let content_length = headers
.iter()
.find(|(name, _)| name == "content-length")
.and_then(|(_, value)| value.parse::<usize>().ok())
.unwrap_or(0);
let mut body = buffer.get(header_end + 4..).unwrap_or_default().to_vec();
while body.len() < content_length.min(MAX_REQUEST_BYTES) {
let read = stream.read(&mut chunk).await?;
if read == 0 {
break;
}
body.extend_from_slice(chunk.get(..read).unwrap_or_default());
}
Ok(Some(RecordedRequest { method, path, headers, body }))
}
fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).position(|window| window == needle)
}
fn encode_response(response: &FakeResponse) -> Vec<u8> {
let mut head = format!(
"HTTP/1.1 {} Fake\r\ncontent-length: {}\r\nconnection: close\r\n",
response.status,
response.body.len()
);
for (name, value) in &response.headers {
head.push_str(&format!("{name}: {value}\r\n"));
}
head.push_str("\r\n");
let mut bytes = head.into_bytes();
bytes.extend_from_slice(&response.body);
bytes
}