Skip to content

Commit 00a40d6

Browse files
committed
feat: add integration with response builder
1 parent 1c0743d commit 00a40d6

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
@@ -90,7 +90,9 @@ mod util;
9090
mod common;
9191
mod map_ext;
9292
mod request_builder_ext;
93+
mod response_builder_ext;
9394

9495
pub use self::common::*;
9596
pub use self::map_ext::HeaderMapExt;
9697
pub use self::request_builder_ext::RequestBuilderExt;
98+
pub use self::response_builder_ext::ResponseBuilderExt;

src/response_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 response builder.
4+
pub trait ResponseBuilderExt: self::sealed::Sealed {
5+
/// Appends a typed header to this response builder.
6+
fn typed_header(self, header: impl Header) -> Self;
7+
}
8+
9+
impl ResponseBuilderExt for http::response::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::response::Builder {}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::ResponseBuilderExt;
25+
26+
#[test]
27+
fn test_with_header_map_get_method() {
28+
let response = http::Response::builder()
29+
.typed_header(crate::ContentType::text())
30+
.body(())
31+
.unwrap();
32+
33+
assert_eq!(
34+
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
35+
"text/plain",
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)