Skip to content

Commit 1c0743d

Browse files
committed
feat: add integration with request builder
1 parent 1a8eac3 commit 1c0743d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ pub use http::header::{HeaderName, HeaderValue};
8989
mod util;
9090
mod common;
9191
mod map_ext;
92+
mod request_builder_ext;
9293

9394
pub use self::common::*;
9495
pub use self::map_ext::HeaderMapExt;
96+
pub use self::request_builder_ext::RequestBuilderExt;

src/request_builder_ext.rs

+38
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)