Skip to content

Commit d1e4731

Browse files
SabrinaJewsonseanmonstar
authored andcommitted
Set size_hint for identity compession bodies
1 parent 66dd321 commit d1e4731

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tower-http/src/compression/body.rs

+8
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ where
269269
},
270270
}
271271
}
272+
273+
fn size_hint(&self) -> http_body::SizeHint {
274+
if let BodyInner::Identity { inner } = &self.inner {
275+
inner.size_hint()
276+
} else {
277+
http_body::SizeHint::new()
278+
}
279+
}
272280
}
273281

274282
#[cfg(feature = "compression-gzip")]

tower-http/src/compression/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ mod tests {
9999
ACCEPT_ENCODING, ACCEPT_RANGES, CONTENT_ENCODING, CONTENT_RANGE, CONTENT_TYPE, RANGE,
100100
};
101101
use http::{HeaderMap, HeaderName, HeaderValue, Request, Response};
102+
use http_body::Body as _;
102103
use http_body_util::BodyExt;
103104
use std::convert::Infallible;
104105
use std::io::Read;
@@ -495,4 +496,16 @@ mod tests {
495496
assert_eq!(headers[CONTENT_ENCODING], "gzip");
496497
assert_eq!(decompressed, "Hello, World!");
497498
}
499+
500+
#[tokio::test]
501+
async fn size_hint_identity() {
502+
let msg = "Hello, world!";
503+
let svc = service_fn(|_| async { Ok::<_, std::io::Error>(Response::new(Body::from(msg))) });
504+
let mut svc = Compression::new(svc);
505+
506+
let req = Request::new(Body::empty());
507+
let res = svc.ready().await.unwrap().call(req).await.unwrap();
508+
let body = res.into_body();
509+
assert_eq!(body.size_hint().exact().unwrap(), msg.len() as u64);
510+
}
498511
}

0 commit comments

Comments
 (0)