-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add integration with response builder
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
); | ||
} | ||
} |