Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add integration with request builder and response builder #170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ pub use http::header::{HeaderName, HeaderValue};
mod util;
mod common;
mod map_ext;
mod request_builder_ext;
mod response_builder_ext;

pub use self::common::*;
pub use self::map_ext::HeaderMapExt;
pub use self::request_builder_ext::RequestBuilderExt;
pub use self::response_builder_ext::ResponseBuilderExt;
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",
);
}
}
38 changes: 38 additions & 0 deletions src/response_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 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",
);
}
}