Read request bodies before test-http-api handlers run

Hyper closes a keep-alive connection when a handler leaves the body unread,
and browsers don't retry the next POST on it, which made e2e tests flaky.
This commit is contained in:
Rūdolfs Ošiņš
2026-10-01 18:16:29 +02:00
parent b0a519d1c5
commit 851fbc54be
2 changed files with 82 additions and 0 deletions
+14
View File
@@ -192,6 +192,7 @@ pub fn router(shared: Shared) -> Router {
.route("/list_notifications", post(list_notifications_handler))
.route("/notification_count", post(notification_count_handler))
.merge(writes)
.layer(middleware::from_fn(read_body))
.layer(
CorsLayer::new()
.allow_origin(cors::AllowOrigin::list(
@@ -203,6 +204,19 @@ pub fn router(shared: Shared) -> Router {
.with_state(shared)
}
/// Hyper closes the keep-alive connection when a handler leaves the body
/// unread, and browsers don't retry the next POST on it.
async fn read_body(request: Request, next: Next) -> Response {
let (parts, body) = request.into_parts();
match axum::body::to_bytes(body, usize::MAX).await {
Ok(bytes) => {
next.run(Request::from_parts(parts, axum::body::Body::from(bytes)))
.await
}
Err(e) => (hyper::StatusCode::BAD_REQUEST, e.to_string()).into_response(),
}
}
async fn serialize_writes(
State(lock): State<Arc<Mutex<()>>>,
request: Request,
+68
View File
@@ -0,0 +1,68 @@
use std::io::{BufRead as _, BufReader, Read as _, Write as _};
use std::net::{SocketAddr, TcpListener, TcpStream};
use std::{thread, time};
fn start_server() -> SocketAddr {
let addr = TcpListener::bind("127.0.0.1:0")
.and_then(|listener| listener.local_addr())
.expect("free port");
thread::spawn(move || {
tokio::runtime::Runtime::new()
.expect("runtime")
.block_on(test_http_api::run(test_http_api::Options { listen: addr }))
.ok();
});
for _ in 0..100 {
if TcpStream::connect(addr).is_ok() {
return addr;
}
thread::sleep(time::Duration::from_millis(20));
}
panic!("test-http-api did not start on {addr}");
}
fn read_response(reader: &mut BufReader<TcpStream>) -> String {
let mut status = String::new();
reader.read_line(&mut status).expect("status line");
let mut length = 0;
loop {
let mut line = String::new();
reader.read_line(&mut line).expect("header line");
if line == "\r\n" || line.is_empty() {
break;
}
if let Some(value) = line.to_ascii_lowercase().strip_prefix("content-length:") {
length = value.trim().parse().expect("content length");
}
}
let mut body = vec![0; length];
reader.read_exact(&mut body).expect("body");
status.trim_end().to_string()
}
#[test]
fn keeps_the_connection_open_when_the_body_arrives_late() {
let addr = start_server();
let stream = TcpStream::connect(addr).expect("connect");
stream
.set_read_timeout(Some(time::Duration::from_secs(5)))
.expect("read timeout");
let mut writer = stream.try_clone().expect("clone stream");
let mut reader = BufReader::new(stream);
// `version` takes no arguments, so its handler never reads the body.
for _ in 0..3 {
writer
.write_all(
b"POST /version HTTP/1.1\r\nHost: localhost\r\n\
Content-Type: application/json\r\nContent-Length: 2\r\n\r\n",
)
.expect("write head");
// Let the handler respond before the body arrives.
thread::sleep(time::Duration::from_millis(50));
writer.write_all(b"{}").expect("write body");
assert_eq!(read_response(&mut reader), "HTTP/1.1 200 OK");
}
}