use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Full};
use hyper::body::Bytes;
use hyper::Response;
use std::convert::Infallible;
pub trait ResponseExt {
fn into_boxed(self) -> Response<BoxBody<Bytes, hyper::Error>>;
}
impl ResponseExt for Response<Full<Bytes>> {
fn into_boxed(self) -> Response<BoxBody<Bytes, hyper::Error>> {
self.map(|b| BoxBody::new(b.map_err(|never: Infallible| match never {})))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_into_boxed_preserves_status() {
let response = Response::builder()
.status(404)
.body(Full::new(Bytes::from("not found")))
.unwrap();
let boxed = response.into_boxed();
assert_eq!(boxed.status(), 404);
}
#[test]
fn test_into_boxed_preserves_headers() {
let response = Response::builder()
.header("X-Custom", "value")
.body(Full::new(Bytes::from("test")))
.unwrap();
let boxed = response.into_boxed();
assert_eq!(
boxed.headers().get("X-Custom").map(|v| v.to_str().unwrap()),
Some("value")
);
}
#[test]
fn test_into_boxed_empty_body() {
let response = Response::new(Full::new(Bytes::new()));
let boxed = response.into_boxed();
assert_eq!(boxed.status(), 200);
}
}