From 03dbbed4a950c426b718938d2aea05d2573d28e2 Mon Sep 17 00:00:00 2001 From: Tiga Ultraman Date: Mon, 4 Aug 2025 21:21:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ylong=5Fhttp=20Slice=20Body?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E6=97=B6=E7=A9=BAData=20Frame?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tiga Ultraman --- ylong_http_client/src/async_impl/request.rs | 48 ++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/ylong_http_client/src/async_impl/request.rs b/ylong_http_client/src/async_impl/request.rs index 79eeb59..5d99dca 100644 --- a/ylong_http_client/src/async_impl/request.rs +++ b/ylong_http_client/src/async_impl/request.rs @@ -352,7 +352,11 @@ impl Body { #[cfg(feature = "http2")] pub(crate) fn is_empty(&self) -> bool { - matches!(self.inner, BodyKind::Empty) + match self.inner { + BodyKind::Empty => true, + BodyKind::Slice(ref text) => text.get_ref().len() as u64 == text.position(), + _ => false, + } } pub(crate) async fn reuse(&mut self) -> std::io::Result<()> { @@ -524,4 +528,46 @@ mod ut_client_request { "https://www.example.com/data/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.txt" ); } + + /// UT test cases for `Body::is_empty`. + /// + /// # Brief + /// 1. Creates a `Body`. + /// 2. Checks if result is correct. + #[test] + #[cfg(feature = "http2")] + fn ut_body_empty() { + use std::pin::Pin; + use std::task::Poll; + use ylong_runtime::io::AsyncRead; + + let empty = Body::empty(); + assert!(empty.is_empty()); + let empty_slice = Body::slice(""); + assert!(empty_slice.is_empty()); + let mut data_slice = Body::slice("hello"); + assert!(!data_slice.is_empty()); + ylong_runtime::block_on(async move { + let mut container = [0u8; 5]; + let mut curr = 0; + loop { + let mut buf = ylong_runtime::io::ReadBuf::new(&mut container[curr..]); + let size = ylong_runtime::futures::poll_fn(|cx| { + match Pin::new(&mut data_slice).poll_read(cx, &mut buf) { + Poll::Ready(_) => { + let size = buf.filled().len(); + Poll::Ready(size) + } + Poll::Pending => Poll::Pending, + } + }) + .await; + curr += size; + if curr == 5 { + break; + } + } + assert!(data_slice.is_empty()); + }); + } } -- Gitee