From 1be9c34f01a8f645686a4dfb2e61446272701ba9 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 25 Aug 2021 10:57:10 +0200 Subject: [PATCH] Make ResponseBuilderExt not target-dependent --- src/async_impl/mod.rs | 2 +- src/async_impl/response.rs | 41 +++++--------------------------------- src/lib.rs | 4 +++- src/response.rs | 41 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 src/response.rs diff --git a/src/async_impl/mod.rs b/src/async_impl/mod.rs index 1be0b66..b8bc8aa 100644 --- a/src/async_impl/mod.rs +++ b/src/async_impl/mod.rs @@ -1,7 +1,7 @@ pub use self::body::Body; pub use self::client::{Client, ClientBuilder}; pub use self::request::{Request, RequestBuilder}; -pub use self::response::{Response, ResponseBuilderExt}; +pub use self::response::Response; #[cfg(feature = "blocking")] pub(crate) use self::decoder::Decoder; diff --git a/src/async_impl/response.rs b/src/async_impl/response.rs index 7d98840..b12b2c2 100644 --- a/src/async_impl/response.rs +++ b/src/async_impl/response.rs @@ -20,6 +20,7 @@ use super::body::Body; use super::decoder::{Accepts, Decoder}; #[cfg(feature = "cookies")] use crate::cookie; +use crate::response::ResponseUrl; /// A Response to a submitted `Request`. pub struct Response { @@ -426,45 +427,13 @@ impl From for Body { } } -#[derive(Debug, Clone, PartialEq)] -struct ResponseUrl(Url); - -/// Extension trait for http::response::Builder objects -/// -/// Allows the user to add a `Url` to the http::Response -pub trait ResponseBuilderExt { - /// A builder method for the `http::response::Builder` type that allows the user to add a `Url` - /// to the `http::Response` - fn url(self, url: Url) -> Self; -} - -impl ResponseBuilderExt for http::response::Builder { - fn url(self, url: Url) -> Self { - self.extension(ResponseUrl(url)) - } -} - #[cfg(test)] mod tests { - use super::{Response, ResponseBuilderExt, ResponseUrl}; + use super::Response; + use crate::ResponseBuilderExt; use http::response::Builder; use url::Url; - #[test] - fn test_response_builder_ext() { - let url = Url::parse("http://example.com").unwrap(); - let response = Builder::new() - .status(200) - .url(url.clone()) - .body(()) - .unwrap(); - - assert_eq!( - response.extensions().get::(), - Some(&ResponseUrl(url)) - ); - } - #[test] fn test_from_http_response() { let url = Url::parse("http://example.com").unwrap(); @@ -475,7 +444,7 @@ mod tests { .unwrap(); let response = Response::from(response); - assert_eq!(response.status, 200); - assert_eq!(response.url, Box::new(url)); + assert_eq!(response.status(), 200); + assert_eq!(*response.url(), url); } } diff --git a/src/lib.rs b/src/lib.rs index 2b154b6..4bad6c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -226,9 +226,11 @@ pub use url::Url; #[macro_use] mod error; mod into_url; +mod response; pub use self::error::{Error, Result}; pub use self::into_url::IntoUrl; +pub use self::response::ResponseBuilderExt; /// Shortcut method to quickly make a `GET` request. /// @@ -294,7 +296,7 @@ if_hyper! { doctest!("../README.md"); pub use self::async_impl::{ - Body, Client, ClientBuilder, Request, RequestBuilder, Response, ResponseBuilderExt, + Body, Client, ClientBuilder, Request, RequestBuilder, Response, }; pub use self::proxy::Proxy; #[cfg(feature = "__tls")] diff --git a/src/response.rs b/src/response.rs new file mode 100644 index 0000000..9c92cba --- /dev/null +++ b/src/response.rs @@ -0,0 +1,41 @@ +use url::Url; + +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct ResponseUrl(pub Url); + +/// Extension trait for http::response::Builder objects +/// +/// Allows the user to add a `Url` to the http::Response +pub trait ResponseBuilderExt { + /// A builder method for the `http::response::Builder` type that allows the user to add a `Url` + /// to the `http::Response` + fn url(self, url: Url) -> Self; +} + +impl ResponseBuilderExt for http::response::Builder { + fn url(self, url: Url) -> Self { + self.extension(ResponseUrl(url)) + } +} + +#[cfg(test)] +mod tests { + use super::{ResponseBuilderExt, ResponseUrl}; + use http::response::Builder; + use url::Url; + + #[test] + fn test_response_builder_ext() { + let url = Url::parse("http://example.com").unwrap(); + let response = Builder::new() + .status(200) + .url(url.clone()) + .body(()) + .unwrap(); + + assert_eq!( + response.extensions().get::(), + Some(&ResponseUrl(url)) + ); + } +}