use super::support::{config, get, request};
use servlet::{
App,
ecosystem::{
tokio,
zlid::{Family, ZLID},
},
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
time::timeout,
web::{Body, http::Request},
};
use std::time::Duration;
#[tokio::test]
async fn runtime_errors_are_safe_problems_and_request_ids_ignore_clients() {
let mut settings = config();
settings.request_body_limit = 4;
settings.request_timeout = Duration::from_millis(40);
let running = App::new("problems")
.config(settings)
.get("/slow", || async {
std::future::pending::<&'static str>().await
})
.get("/identity", |request: Request<Body>| async move {
assert_eq!(request.headers()["forwarded"], "for=known-secret");
assert_eq!(request.headers()["x-forwarded-for"], "known-secret");
request
.extensions()
.get::<servlet::web::RequestId>()
.unwrap()
.0
.clone()
})
.start()
.await
.unwrap();
let address = running.local_addr().unwrap();
let first = request(address, "GET /identity HTTP/1.1\r\nHost: localhost\r\nX-Request-Id: known-secret\r\nForwarded: for=known-secret\r\nX-Forwarded-For: known-secret\r\nConnection: close\r\n\r\n").await;
assert!(!first.contains("known-secret"));
let first_id = first
.lines()
.find_map(|line| line.strip_prefix("x-request-id: "))
.unwrap();
assert_eq!(
ZLID::parse_canonical(first_id).unwrap().family(),
Some(Family::Random)
);
assert_eq!(first.split_once("\r\n\r\n").unwrap().1, first_id);
let second = get(address, "/slow").await;
assert!(second.starts_with("HTTP/1.1 504"));
assert!(second.contains("application/problem+json"));
assert!(second.contains("\"code\":\"request_timeout\""));
assert!(!second.contains(first_id));
let large = request(
address,
"POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 100\r\nConnection: close\r\n\r\n",
)
.await;
assert!(large.starts_with("HTTP/1.1 413"));
assert!(large.contains("\"code\":\"body_too_large\""));
running.handle().shutdown();
running.wait().await.unwrap();
}
#[tokio::test]
async fn slow_headers_and_excessive_headers_are_connection_scoped() {
let mut settings = config();
settings.header_timeout = Duration::from_millis(50);
let running = App::new("headers")
.config(settings)
.get("/", || async { "okay" })
.start()
.await
.unwrap();
let address = running.local_addr().unwrap();
let mut stream = TcpStream::connect(address).await.unwrap();
stream.write_all(b"GET / HTTP/1.1\r\nHost:").await.unwrap();
let mut bytes = Vec::new();
let _ = timeout(Duration::from_secs(1), stream.read_to_end(&mut bytes))
.await
.unwrap();
assert!(bytes.is_empty() || String::from_utf8_lossy(&bytes).starts_with("HTTP/1.1 408"));
let mut headers = "GET / HTTP/1.1\r\nHost: localhost\r\n".to_string();
for _ in 0..70 {
headers.push_str("X-Extra: yes\r\n");
}
headers.push_str("\r\n");
assert!(request(address, &headers).await.starts_with("HTTP/1.1 431"));
assert!(get(address, "/").await.contains("okay"));
running.handle().shutdown();
running.wait().await.unwrap();
}
#[tokio::test]
async fn request_body_idle_reclaims_capacity() {
let mut settings = config();
settings.body_idle_timeout = Duration::from_millis(40);
settings.max_in_flight = 1;
let running = App::new("body-idle")
.config(settings)
.post("/", |body: String| async move { body })
.get("/", || async { "okay" })
.start()
.await
.unwrap();
let address = running.local_addr().unwrap();
let mut stream = TcpStream::connect(address).await.unwrap();
stream.write_all(b"POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 10\r\nConnection: close\r\n\r\nx").await.unwrap();
let mut bytes = Vec::new();
let _ = timeout(Duration::from_secs(1), stream.read_to_end(&mut bytes))
.await
.unwrap();
assert!(!String::from_utf8_lossy(&bytes).starts_with("HTTP/1.1 200"));
assert!(get(address, "/").await.contains("okay"));
running.handle().shutdown();
running.wait().await.unwrap();
}
#[tokio::test]
async fn ambiguous_framing_closes_connection_and_conflicting_lengths_fail() {
let running = App::new("framing")
.config(config())
.post("/", |body: String| async move { body })
.start()
.await
.unwrap();
let address = running.local_addr().unwrap();
let conflicting = request(
address,
"POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 1\r\nContent-Length: 2\r\n\r\nxx",
)
.await;
assert!(conflicting.starts_with("HTTP/1.1 400"));
let both = request(address, "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 99\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nx\r\n0\r\n\r\nPOST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n").await;
assert_eq!(both.matches("HTTP/1.1 ").count(), 1);
assert!(both.contains("connection: close"));
running.handle().shutdown();
running.wait().await.unwrap();
}