fix(body): return exactly 0 SizeHint for empty body (#2122)
This commit is contained in:
@@ -331,19 +331,15 @@ impl HttpBody for Body {
|
|||||||
|
|
||||||
fn size_hint(&self) -> SizeHint {
|
fn size_hint(&self) -> SizeHint {
|
||||||
match self.kind {
|
match self.kind {
|
||||||
Kind::Once(Some(ref val)) => {
|
Kind::Once(Some(ref val)) => SizeHint::with_exact(val.len() as u64),
|
||||||
let mut hint = SizeHint::default();
|
Kind::Once(None) => SizeHint::with_exact(0),
|
||||||
hint.set_exact(val.len() as u64);
|
|
||||||
hint
|
|
||||||
}
|
|
||||||
Kind::Once(None) => SizeHint::default(),
|
|
||||||
#[cfg(feature = "stream")]
|
#[cfg(feature = "stream")]
|
||||||
Kind::Wrapped(..) => SizeHint::default(),
|
Kind::Wrapped(..) => SizeHint::default(),
|
||||||
Kind::Chan { content_length, .. } | Kind::H2 { content_length, .. } => {
|
Kind::Chan { content_length, .. } | Kind::H2 { content_length, .. } => {
|
||||||
let mut hint = SizeHint::default();
|
let mut hint = SizeHint::default();
|
||||||
|
|
||||||
if let Some(content_length) = content_length.into_opt() {
|
if let Some(content_length) = content_length.into_opt() {
|
||||||
hint.set_exact(content_length as u64);
|
hint.set_exact(content_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
hint
|
hint
|
||||||
@@ -547,7 +543,7 @@ mod tests {
|
|||||||
use std::mem;
|
use std::mem;
|
||||||
use std::task::Poll;
|
use std::task::Poll;
|
||||||
|
|
||||||
use super::{Body, DecodedLength, HttpBody, Sender};
|
use super::{Body, DecodedLength, HttpBody, Sender, SizeHint};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_size_of() {
|
fn test_size_of() {
|
||||||
@@ -578,6 +574,27 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn size_hint() {
|
||||||
|
fn eq(body: Body, b: SizeHint, note: &str) {
|
||||||
|
let a = body.size_hint();
|
||||||
|
assert_eq!(a.lower(), b.lower(), "lower for {:?}", note);
|
||||||
|
assert_eq!(a.upper(), b.upper(), "upper for {:?}", note);
|
||||||
|
}
|
||||||
|
|
||||||
|
eq(Body::from("Hello"), SizeHint::with_exact(5), "from str");
|
||||||
|
|
||||||
|
eq(Body::empty(), SizeHint::with_exact(0), "empty");
|
||||||
|
|
||||||
|
eq(Body::channel().1, SizeHint::new(), "channel");
|
||||||
|
|
||||||
|
eq(
|
||||||
|
Body::new_channel(DecodedLength::new(4), /*wanter =*/ false).1,
|
||||||
|
SizeHint::with_exact(4),
|
||||||
|
"channel with length",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn channel_abort() {
|
async fn channel_abort() {
|
||||||
let (tx, mut rx) = Body::channel();
|
let (tx, mut rx) = Body::channel();
|
||||||
|
|||||||
Reference in New Issue
Block a user