#![cfg(not(loom))]
#![forbid(unsafe_code)]
use std::time::Duration;
use bytes::Bytes;
use futures::StreamExt;
use rstest::rstest;
use tokio::io::AsyncReadExt;
use wireframe::request::{RequestBodyReader, RequestBodyStream, RequestParts, body_channel};
use wireframe_testing::TestResult;
#[rstest]
#[tokio::test]
async fn back_pressure_suspends_sender_when_full() -> TestResult<()> {
let (tx, _rx) = body_channel(1);
tx.send(Ok(Bytes::from_static(b"first"))).await?;
let send_fut = tx.send(Ok(Bytes::from_static(b"second")));
let timeout = tokio::time::timeout(Duration::from_millis(50), send_fut).await;
assert!(
timeout.is_err(),
"send should have blocked due to back-pressure"
);
Ok(())
}
#[rstest]
#[tokio::test]
async fn reader_consumes_full_stream() -> TestResult<()> {
let (tx, stream) = body_channel(4);
tokio::spawn(async move {
drop(tx.send(Ok(Bytes::from_static(b"hello "))).await);
drop(tx.send(Ok(Bytes::from_static(b"world"))).await);
});
let mut reader = RequestBodyReader::new(stream);
let mut buf = Vec::new();
reader.read_to_end(&mut buf).await?;
assert_eq!(buf, b"hello world", "buf should equal b\"hello world\"");
Ok(())
}
#[rstest]
fn buffered_fallback_uses_metadata_directly() {
let parts = RequestParts::new(42, Some(1), vec![0x01, 0x02, 0x03]);
assert_eq!(parts.id(), 42, "parts.id() should equal 42");
assert_eq!(
parts.correlation_id(),
Some(1),
"parts.correlation_id() should equal Some(1)"
);
assert_eq!(
parts.metadata(),
&[0x01, 0x02, 0x03],
"parts.metadata() should equal &[0x01, 0x02, 0x03]"
);
}
#[rstest]
#[tokio::test]
async fn stream_error_propagates_to_handler() -> TestResult<()> {
let (tx, stream) = body_channel(4);
tokio::spawn(async move {
drop(tx.send(Ok(Bytes::from_static(b"ok"))).await);
drop(
tx.send(Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"corrupt data",
)))
.await,
);
});
let mut body_stream: RequestBodyStream = stream;
let results: Vec<_> = body_stream.by_ref().collect().await;
assert_eq!(results.len(), 2, "results.len() should equal 2");
assert!(
results.first().expect("first result").is_ok(),
"assertion failed: results.first().expect(\"first result\").is_ok()"
);
assert!(
results.get(1).expect("second result").is_err(),
"assertion failed: results.get(1).expect(\"second result\").is_err()"
);
Ok(())
}
#[rstest]
#[tokio::test]
async fn reader_surfaces_stream_error() -> TestResult<()> {
let (tx, stream) = body_channel(4);
tokio::spawn(async move {
drop(
tx.send(Err(std::io::Error::new(
std::io::ErrorKind::BrokenPipe,
"connection lost",
)))
.await,
);
});
let mut reader = RequestBodyReader::new(stream);
let mut buf = [0u8; 16];
let result = reader.read(&mut buf).await;
assert!(result.is_err(), "assertion failed: result.is_err()");
assert_eq!(
result.expect_err("should be error").kind(),
std::io::ErrorKind::BrokenPipe,
"result.expect_err(\"should be error\").kind() should equal std::io::ErrorKind::BrokenPipe"
);
Ok(())
}
#[rstest]
#[tokio::test]
async fn dropped_receiver_signals_sender() -> TestResult<()> {
let (tx, rx) = body_channel(4);
drop(rx);
let result = tx.send(Ok(Bytes::from_static(b"orphaned"))).await;
assert!(result.is_err(), "send should fail when receiver is dropped");
Ok(())
}
#[rstest]
#[tokio::test]
async fn sequential_chunks_delivered_in_order() -> TestResult<()> {
let (tx, stream) = body_channel(8);
tokio::spawn(async move {
for i in 0u8..5 {
let chunk = Bytes::from(vec![i]);
drop(tx.send(Ok(chunk)).await);
}
});
let chunks: Vec<_> = stream.collect().await;
let values: Vec<u8> = chunks
.into_iter()
.filter_map(Result::ok)
.flat_map(|b| b.to_vec())
.collect();
assert_eq!(
values,
vec![0, 1, 2, 3, 4],
"values should equal vec![0, 1, 2, 3, 4]"
);
Ok(())
}