Skip to content

Commit

Permalink
feat: add integration with response builder
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Jan 24, 2025
1 parent 1c0743d commit 00a40d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ mod util;
mod common;
mod map_ext;
mod request_builder_ext;
mod response_builder_ext;

pub use self::common::*;
pub use self::map_ext::HeaderMapExt;
pub use self::request_builder_ext::RequestBuilderExt;
pub use self::response_builder_ext::ResponseBuilderExt;
38 changes: 38 additions & 0 deletions src/response_builder_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::{Header, HeaderMapExt};

/// An external trait adding helper methods to http response builder.
pub trait ResponseBuilderExt: self::sealed::Sealed {
/// Appends a typed header to this response builder.
fn typed_header(self, header: impl Header) -> Self;
}

impl ResponseBuilderExt for http::response::Builder {
fn typed_header(mut self, header: impl Header) -> Self {
self.headers_mut()
.map(|header_map| header_map.typed_insert(header));
self
}
}

mod sealed {
pub trait Sealed {}
impl Sealed for http::response::Builder {}
}

#[cfg(test)]
mod tests {
use super::ResponseBuilderExt;

#[test]
fn test_with_header_map_get_method() {
let response = http::Response::builder()
.typed_header(crate::ContentType::text())
.body(())
.unwrap();

assert_eq!(
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/plain",
);
}
}

0 comments on commit 00a40d6

Please sign in to comment.