From 14b113f2b0e7059092aafc8cc061175f87bebee1 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Tue, 31 Dec 2019 04:14:17 +0000 Subject: [PATCH 1/9] Update reqwest requirement from 0.9 to 0.10 Updates the requirements on [reqwest](https://github.com/seanmonstar/reqwest) to permit the latest version. - [Release notes](https://github.com/seanmonstar/reqwest/releases) - [Changelog](https://github.com/seanmonstar/reqwest/blob/master/CHANGELOG.md) - [Commits](https://github.com/seanmonstar/reqwest/compare/v0.9.0...v0.10.0) Signed-off-by: dependabot-preview[bot] --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 62d4824..a0e5772 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ base64 = "0.10.1" error-chain = "0.12" http = "0.1" log = "0.4.0" -reqwest = "0.9" +reqwest = "0.10" serde = "1.0" serde_derive = "1.0" serde_json = "1.0" From cc7d99122464df07f3827372c1f7ea6b9c0231ea Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 May 2020 15:11:53 -0400 Subject: [PATCH 2/9] gitignore: allow target to be a symlink --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4308d82..6936990 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -target/ +/target **/*.rs.bk Cargo.lock From 5855dcdb64459d9d6d59f330a15d58a5f1cf6dc8 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 May 2020 15:20:31 -0400 Subject: [PATCH 3/9] cargo: update reqwest and http crate dependencies Fixes: #21 Fixes: #22 --- Cargo.toml | 5 +++-- src/body.rs | 4 ++-- src/client/direct.rs | 4 ++-- src/config.rs | 6 +++--- src/lib.rs | 4 +++- src/response.rs | 3 +-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a0e5772..8920c7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,13 +11,14 @@ keywords = ["http", "request", "client"] [dependencies] base64 = "0.10.1" error-chain = "0.12" -http = "0.1" +http = "0.2" log = "0.4.0" -reqwest = "0.10" +reqwest = { version = "0.10", features = ["blocking", "gzip"] } serde = "1.0" serde_derive = "1.0" serde_json = "1.0" twox-hash = "1.1" +url = "2.1" [dev-dependencies] futures = "0.1" diff --git a/src/body.rs b/src/body.rs index 4e5bf37..7562d7c 100644 --- a/src/body.rs +++ b/src/body.rs @@ -36,8 +36,8 @@ impl Body { } } -impl From for ::reqwest::Body { - fn from(b: Body) -> ::reqwest::Body { +impl From for ::reqwest::blocking::Body { + fn from(b: Body) -> ::reqwest::blocking::Body { match b.value { BodyValue::Bytes(b) => b.into(), BodyValue::File(f) => f.into(), diff --git a/src/client/direct.rs b/src/client/direct.rs index 7c128c3..8857488 100644 --- a/src/client/direct.rs +++ b/src/client/direct.rs @@ -34,7 +34,7 @@ impl Client for DirectClient { let config = config.unwrap_or_else(|| &self.config); // Setup the client instance. - let mut client_builder = ::reqwest::Client::builder() + let mut client_builder = ::reqwest::blocking::Client::builder() .gzip(config.gzip) .redirect(config.redirect.clone().into()) .referer(config.referer); @@ -46,7 +46,7 @@ impl Client for DirectClient { // Build the request. let mut builder = client.request(request.header.method, request.header.url); if let Some(body) = request.body { - builder = builder.body(::reqwest::Body::from(body)); + builder = builder.body(::reqwest::blocking::Body::from(body)); } // Send the request. diff --git a/src/config.rs b/src/config.rs index 6b7979f..ad71d88 100644 --- a/src/config.rs +++ b/src/config.rs @@ -58,11 +58,11 @@ impl Default for RedirectPolicy { } } -impl From for ::reqwest::RedirectPolicy { +impl From for ::reqwest::redirect::Policy { fn from(p: RedirectPolicy) -> Self { match p { - RedirectPolicy::Limit(n) => ::reqwest::RedirectPolicy::limited(n), - RedirectPolicy::None => ::reqwest::RedirectPolicy::none(), + RedirectPolicy::Limit(n) => ::reqwest::redirect::Policy::limited(n), + RedirectPolicy::None => ::reqwest::redirect::Policy::none(), } } } diff --git a/src/lib.rs b/src/lib.rs index 1609fc1..27dccd2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,6 +56,7 @@ extern crate serde; extern crate serde_derive; extern crate serde_json; extern crate twox_hash; +extern crate url; mod helper; @@ -74,4 +75,5 @@ mod request_builder; pub use self::client::*; pub use self::error::Error; -pub use reqwest::{header, IntoUrl, Method, StatusCode, Url, UrlError}; +pub use reqwest::{header, IntoUrl, Method, StatusCode, Url}; +pub use url::ParseError as UrlError; diff --git a/src/response.rs b/src/response.rs index 812e00f..53fa3ae 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,6 +1,5 @@ use base64; use error::Error; -use http::HttpTryFrom; use reqwest::header::HeaderMap; use reqwest::{StatusCode, Url}; use serde::de::Error as DeError; @@ -94,7 +93,7 @@ impl<'de> Visitor<'de> for ResponseVisitor { return Err(DeError::duplicate_field(F_STATUS)); } let s: u16 = map.next_value()?; - status = Some(StatusCode::try_from(s).map_err(|_| { + status = Some(StatusCode::from_u16(s).map_err(|_| { DeError::invalid_value(Unexpected::Unsigned(s as u64), &"StatusCode") })?); } From 93c4124d140c581ead75e7b422b4c6ea10f6eea3 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 May 2020 15:20:54 -0400 Subject: [PATCH 4/9] warning: add missing `dyn` keyword --- tests/helper/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/helper/mod.rs b/tests/helper/mod.rs index 41fd365..e6d1fa2 100644 --- a/tests/helper/mod.rs +++ b/tests/helper/mod.rs @@ -31,7 +31,7 @@ impl Service for TestServer { type Request = Request; type Response = Response; type Error = hyper::Error; - type Future = Box>; + type Future = Box>; fn call(&self, req: Request) -> Self::Future { // Extract the request headers. From 1848344d91540d07af850f35af46e7660038556b Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 May 2020 17:16:45 -0400 Subject: [PATCH 5/9] http: support converting to core http types This allows reqwest_mock to be used with http-using APIs rather than forcing the entire API stack to use the types in this crate. --- src/request.rs | 16 ++++++++++++++++ src/response.rs | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/request.rs b/src/request.rs index f924657..60f6b37 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,4 +1,5 @@ use body::Body; +use http::Request as HttpRequest; use reqwest::{Method, Url}; use reqwest::header::HeaderMap; use serde::ser::{Serialize, SerializeStruct, Serializer}; @@ -33,6 +34,21 @@ impl Request { } } +impl From> for Request where T: Into { + fn from(r: HttpRequest) -> Self { + let header = RequestHeader { + url: Url::parse(&format!("{}", r.uri())).unwrap(), + method: r.method().clone(), + headers: r.headers().clone(), + }; + + Request { + header, + body: Some(r.into_body().into()), + } + } +} + #[derive(Clone, Debug, PartialEq)] pub struct RequestMem { pub header: RequestHeader, diff --git a/src/response.rs b/src/response.rs index 53fa3ae..0542db2 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,5 +1,6 @@ use base64; use error::Error; +use http::Response as HttpResponse; use reqwest::header::HeaderMap; use reqwest::{StatusCode, Url}; use serde::de::Error as DeError; @@ -124,6 +125,22 @@ impl<'de> Visitor<'de> for ResponseVisitor { } } +impl From for HttpResponse> { + fn from(r: Response) -> HttpResponse> { + let mut http_rsp = HttpResponse::builder() + .status(r.status); + let headers = http_rsp.headers_mut().unwrap(); + let mut last_header = None; + for (key, value) in r.headers { + if key.is_some() { + last_header = key.clone(); + } + headers.insert(last_header.clone().unwrap(), value); + } + http_rsp.body(r.body).unwrap() + } +} + impl<'de> Deserialize<'de> for Response { fn deserialize(deserializer: D) -> Result where From 52bd60619b15f9fb7a0aa5ee3c6861dca8368994 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 4 May 2020 17:53:58 -0400 Subject: [PATCH 6/9] stub: add missing impl Debug for error types --- src/client/stub/error.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client/stub/error.rs b/src/client/stub/error.rs index dae9116..509600b 100644 --- a/src/client/stub/error.rs +++ b/src/client/stub/error.rs @@ -4,6 +4,7 @@ use std::io; // TODO: impl Error // TODO: Should be crate or module visible. +#[derive(Debug)] pub struct FieldError { /// The name of the missing field. pub field_name: &'static str, @@ -12,6 +13,7 @@ pub struct FieldError { } // TODO: Variants should be private. +#[derive(Debug)] pub enum RegisterStubError { // "Tried registering stub without `{}` even though `{}` requires its presence." MissingField(FieldError), From f9bdb7920e85df67186c60b1604ac3acfee88b41 Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Tue, 5 May 2020 12:55:14 +0200 Subject: [PATCH 7/9] Simplify conversion to HttpResponse & test it. --- src/response.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/response.rs b/src/response.rs index 4be9375..9b076bc 100644 --- a/src/response.rs +++ b/src/response.rs @@ -128,15 +128,12 @@ impl<'de> Visitor<'de> for ResponseVisitor { impl From for HttpResponse> { fn from(r: Response) -> HttpResponse> { - let mut http_rsp = HttpResponse::builder() - .status(r.status); + let mut http_rsp = HttpResponse::builder().status(r.status); let headers = http_rsp.headers_mut().unwrap(); - let mut last_header = None; for (key, value) in r.headers { - if key.is_some() { - last_header = key.clone(); + if let Some(k) = key { + headers.append(&k, value); } - headers.insert(last_header.clone().unwrap(), value); } http_rsp.body(r.body).unwrap() } @@ -155,25 +152,39 @@ impl<'de> Deserialize<'de> for Response { #[cfg(test)] mod tests { use super::*; + use reqwest::header::{CONTENT_LENGTH, USER_AGENT}; - #[test] - fn serde() { - use reqwest::header::{CONTENT_LENGTH, USER_AGENT}; + fn dummy_response() -> Response { let mut headers = HeaderMap::new(); headers.insert(CONTENT_LENGTH, 2000.into()); headers.insert(USER_AGENT, "Testing Code".parse().unwrap()); - let resp1 = Response { + Response { url: Url::parse("http://example.com/index.html").unwrap(), status: StatusCode::OK, headers: headers, body: vec![2, 4, 8, 16, 32, 64, 42], - }; + } + } + #[test] + fn serde() { + let resp1 = dummy_response(); let json = ::serde_json::to_string(&resp1).unwrap(); let resp2 = ::serde_json::from_str(json.as_ref()).unwrap(); assert_eq!(resp1, resp2); } + + #[test] + fn http_response() { + let resp = dummy_response(); + let http_resp = HttpResponse::>::from(resp); + + assert_eq!(http_resp.status(), http::StatusCode::OK); + assert_eq!(http_resp.headers().get(CONTENT_LENGTH).unwrap().to_str().unwrap(), "2000"); + assert_eq!(http_resp.headers().get(USER_AGENT).unwrap(), "Testing Code"); + assert_eq!(http_resp.body(), &vec![2u8, 4, 8, 16, 32, 64, 42]); + } } From 173019f6a35509bf038a66b21a575f85293608f0 Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Tue, 5 May 2020 12:56:10 +0200 Subject: [PATCH 8/9] Add todo --- src/request.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/request.rs b/src/request.rs index 2393b52..9dc51be 100644 --- a/src/request.rs +++ b/src/request.rs @@ -37,6 +37,8 @@ impl Request { impl From> for Request where T: Into { fn from(r: HttpRequest) -> Self { let header = RequestHeader { + // TODO: Handle error when converting. + // Potentially https://github.com/seanmonstar/reqwest/issues/668 would provide a solution in the future. url: Url::parse(&format!("{}", r.uri())).unwrap(), method: r.method().clone(), headers: r.headers().clone(), From 4ea202f34c6fe831946977562af720396a3d2c1a Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Tue, 5 May 2020 13:01:32 +0200 Subject: [PATCH 9/9] Bump version for next release. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9e59987..ec8dc91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reqwest_mock" -version = "0.6.0" +version = "0.7.0" authors = ["Leo Schwarz "] license = "Apache-2.0" description = "Provides a mockable reqwest-like HTTP client."