1 | use url::Url; |
2 | |
3 | #[derive (Debug, Clone, PartialEq)] |
4 | pub(crate) struct ResponseUrl(pub Url); |
5 | |
6 | /// Extension trait for http::response::Builder objects |
7 | /// |
8 | /// Allows the user to add a `Url` to the http::Response |
9 | pub trait ResponseBuilderExt { |
10 | /// A builder method for the `http::response::Builder` type that allows the user to add a `Url` |
11 | /// to the `http::Response` |
12 | fn url(self, url: Url) -> Self; |
13 | } |
14 | |
15 | impl ResponseBuilderExt for http::response::Builder { |
16 | fn url(self, url: Url) -> Self { |
17 | self.extension(ResponseUrl(url)) |
18 | } |
19 | } |
20 | |
21 | #[cfg (test)] |
22 | mod tests { |
23 | use super::{ResponseBuilderExt, ResponseUrl}; |
24 | use http::response::Builder; |
25 | use url::Url; |
26 | |
27 | #[test ] |
28 | fn test_response_builder_ext() { |
29 | let url = Url::parse("http://example.com" ).unwrap(); |
30 | let response = Builder::new() |
31 | .status(200) |
32 | .url(url.clone()) |
33 | .body(()) |
34 | .unwrap(); |
35 | |
36 | assert_eq!( |
37 | response.extensions().get::<ResponseUrl>(), |
38 | Some(&ResponseUrl(url)) |
39 | ); |
40 | } |
41 | } |
42 | |