//! Stateful HTTP fixture. It verifies wire methods and bodies, not live credentials. use serde_json::{Value, json}; use std::{ collections::BTreeMap, sync::{Arc, Mutex}, }; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, net::TcpListener, }; #[derive(Default)] pub struct State { pub objects: BTreeMap, pub writes: usize, pub fail_once: Option, pub external_override: bool, pub users: Vec, pub lookups: usize, } pub struct Server { pub base: String, pub state: Arc>, task: tokio::task::JoinHandle<()>, } impl Drop for Server { fn drop(&mut self) { self.task.abort(); } } impl Server { pub async fn start() -> Self { let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let base = format!("http://{}", listener.local_addr().unwrap()); let state = Arc::new(Mutex::new(State::default())); let copy = state.clone(); let task = tokio::spawn(async move { loop { let (mut socket, _) = listener.accept().await.unwrap(); let shared = copy.clone(); tokio::spawn(async move { let mut buffer = vec![]; let (head_end, length) = loop { let mut chunk = [0; 4096]; let n = socket.read(&mut chunk).await.unwrap(); if n == 0 { return; } buffer.extend_from_slice(&chunk[..n]); if let Some(end) = buffer.windows(4).position(|w| w == b"\r\n\r\n") { let head = String::from_utf8_lossy(&buffer[..end]); let length = head .lines() .find_map(|l| { l.to_lowercase() .strip_prefix("content-length: ") .and_then(|v| v.parse::().ok()) }) .unwrap_or(0); break (end + 4, length); } }; while buffer.len() < head_end + length { let mut chunk = [0; 4096]; let n = socket.read(&mut chunk).await.unwrap(); if n == 0 { return; } buffer.extend_from_slice(&chunk[..n]); } let head = String::from_utf8_lossy(&buffer[..head_end]); assert!(head.to_lowercase().contains("authorization: basic ")); let mut first = head.lines().next().unwrap().split_whitespace(); let method = first.next().unwrap(); let path = first.next().unwrap().split('?').next().unwrap(); let body = if length > 0 { serde_json::from_slice(&buffer[head_end..head_end + length]).unwrap() } else { Value::Null }; let (code, result) = handle(&mut shared.lock().unwrap(), method, path, body); let body = result.to_string(); let response = format!( "HTTP/1.1 {code} Result\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len() ); socket.write_all(response.as_bytes()).await.unwrap(); }); } }); Self { base, state, task } } } fn handle(s: &mut State, method: &str, path: &str, mut body: Value) -> (u16, Value) { if method == "POST" && s.fail_once.as_ref().is_some_and(|p| path.ends_with(p)) { s.fail_once = None; return (429, json!({"error":"Try later"})); } if method == "GET" { if path.ends_with("/user/search") { s.lookups += 1; return (200, json!(s.users)); } if path.ends_with("/overrides") { return ( 200, json!({"values":if s.external_override{vec![json!({"alias":"external"})]}else{vec![]}}), ); } if let Some(parent) = path.strip_suffix("/rotations") { return ( 200, json!({"values":s.objects.get(parent).unwrap()["rotations"]}), ); } if let Some(value) = s.objects.get(path) { return (200, value.clone()); } if path.ends_with("/schedules") || path.ends_with("/escalations") || path.ends_with("/routing-rules") { return ( 200, json!({"values":s.objects.iter().filter(|(k,_)|k.rsplit_once('/').is_some_and(|(p,_)|p==path)).map(|(_,v)|v.clone()).collect::>()}), ); } return (404, json!({})); } if method == "DELETE" { s.writes += 1; if path.ends_with("/overrides/external") { s.external_override = false; } else { s.objects.remove(path); } return (200, json!({"result":"deleted"})); } if path.contains("routing-rules") { assert!(body["order"].as_u64().unwrap() <= 100); assert!(body["notify"].is_object()); } if path.contains("escalations") { assert!((1..=20).contains(&body["repeat"]["count"].as_u64().unwrap())); assert!(body["rules"].is_array()); } if path.contains("schedules") { for rotation in body["rotations"].as_array().unwrap() { assert_eq!(rotation["participants"].as_array().unwrap().len(), 1); assert!( rotation["endDate"].as_str().unwrap() > rotation["startDate"].as_str().unwrap() ); } } s.writes += 1; if method == "POST" { let id = uuid::Uuid::new_v4().to_string(); body["id"] = json!(id); s.objects.insert(format!("{path}/{id}"), body.clone()); return (201, body); } assert_eq!(method, "PATCH"); if let Some(previous) = s.objects.get(path) { body["id"] = previous["id"].clone(); s.objects.insert(path.into(), body.clone()); (200, body) } else { (404, json!({})) } }