Skip to content

Commit c2b4764

Browse files
committed
feat: add integration with request builder
1 parent cedf5e7 commit c2b4764

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ pub use http::header::{HeaderName, HeaderValue};
9494
mod util;
9595
mod common;
9696
mod map_ext;
97+
mod request_builder_ext;
9798

9899
pub use self::common::*;
99100
pub use self::map_ext::HeaderMapExt;
101+
pub use self::request_builder_ext::RequestBuilderExt;

src/request_builder_ext.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::{Header, HeaderMapExt};
2+
3+
/// An external trait adding helper methods to http request builder.
4+
pub trait RequestBuilderExt: self::sealed::Sealed {
5+
/// Appends a typed header to this request builder.
6+
fn typed_header(self, header: impl Header) -> Self;
7+
}
8+
9+
impl RequestBuilderExt for http::request::Builder {
10+
fn typed_header(mut self, header: impl Header) -> Self {
11+
self.headers_mut()
12+
.map(|header_map| header_map.typed_insert(header));
13+
self
14+
}
15+
}
16+
17+
mod sealed {
18+
pub trait Sealed {}
19+
impl Sealed for http::request::Builder {}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::RequestBuilderExt;
25+
26+
#[test]
27+
fn test_with_header_map_get_method() {
28+
let request = http::Request::builder()
29+
.typed_header(crate::ContentType::text())
30+
.body(())
31+
.unwrap();
32+
33+
assert_eq!(
34+
request.headers().get(http::header::CONTENT_TYPE).unwrap(),
35+
"text/plain",
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)