Skip to content

Commit

Permalink
feat: add integration with request builder
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Jan 24, 2025
1 parent 1a8eac3 commit 1c0743d
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 @@ -89,6 +89,8 @@ pub use http::header::{HeaderName, HeaderValue};
mod util;
mod common;
mod map_ext;
mod request_builder_ext;

pub use self::common::*;
pub use self::map_ext::HeaderMapExt;
pub use self::request_builder_ext::RequestBuilderExt;
38 changes: 38 additions & 0 deletions src/request_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 request builder.
pub trait RequestBuilderExt: self::sealed::Sealed {
/// Appends a typed header to this request builder.
fn typed_header(self, header: impl Header) -> Self;
}

impl RequestBuilderExt for http::request::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::request::Builder {}
}

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

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

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

0 comments on commit 1c0743d

Please sign in to comment.