-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathrequest_builder_ext.rs
More file actions
38 lines (32 loc) · 983 Bytes
/
request_builder_ext.rs
File metadata and controls
38 lines (32 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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",
);
}
}