Skip to content

Commit

Permalink
Set size_hint for identity compession bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
SabrinaJewson authored and seanmonstar committed Nov 18, 2024
1 parent 66dd321 commit d1e4731
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tower-http/src/compression/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ where
},
}
}

fn size_hint(&self) -> http_body::SizeHint {
if let BodyInner::Identity { inner } = &self.inner {
inner.size_hint()
} else {
http_body::SizeHint::new()
}
}
}

#[cfg(feature = "compression-gzip")]
Expand Down
13 changes: 13 additions & 0 deletions tower-http/src/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ mod tests {
ACCEPT_ENCODING, ACCEPT_RANGES, CONTENT_ENCODING, CONTENT_RANGE, CONTENT_TYPE, RANGE,
};
use http::{HeaderMap, HeaderName, HeaderValue, Request, Response};
use http_body::Body as _;
use http_body_util::BodyExt;
use std::convert::Infallible;
use std::io::Read;
Expand Down Expand Up @@ -495,4 +496,16 @@ mod tests {
assert_eq!(headers[CONTENT_ENCODING], "gzip");
assert_eq!(decompressed, "Hello, World!");
}

#[tokio::test]
async fn size_hint_identity() {
let msg = "Hello, world!";
let svc = service_fn(|_| async { Ok::<_, std::io::Error>(Response::new(Body::from(msg))) });
let mut svc = Compression::new(svc);

let req = Request::new(Body::empty());
let res = svc.ready().await.unwrap().call(req).await.unwrap();
let body = res.into_body();
assert_eq!(body.size_hint().exact().unwrap(), msg.len() as u64);
}
}

0 comments on commit d1e4731

Please sign in to comment.