From 4e744f87c1bf90ca6fd6639edeff5704624e5a5f Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Mon, 4 May 2020 21:45:27 +0200 Subject: [PATCH] Keep the old Response::into_json around and call this Response::into_json_deserialize --- src/response.rs | 49 ++++++++++++++++++++++++++++++++++++++++---- src/test/simple.rs | 18 +++++++++++++++- tests/https-agent.rs | 2 +- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/response.rs b/src/response.rs index 4afcc63..d664ce9 100644 --- a/src/response.rs +++ b/src/response.rs @@ -24,7 +24,8 @@ pub const DEFAULT_CHARACTER_SET: &str = "utf-8"; /// /// The `Response` is used to read response headers and decide what to do with the body. /// Note that the socket connection is open and the body not read until one of -/// [`into_reader()`](#method.into_reader), [`into_json()`](#method.into_json) or +/// [`into_reader()`](#method.into_reader), [`into_json()`](#method.into_json), +/// [`into_json_deserialize()`](#method.into_json_deserialize) or /// [`into_string()`](#method.into_string) consumes the response. /// /// ``` @@ -382,6 +383,32 @@ impl Response { /// Example: /// /// ``` + /// let resp = + /// ureq::get("https://ureq.s3.eu-central-1.amazonaws.com/hello_world.json") + /// .call(); + /// + /// let json = resp.into_json().unwrap(); + /// + /// assert_eq!(json["hello"], "world"); + /// ``` + #[cfg(feature = "json")] + pub fn into_json(self) -> IoResult { + let reader = self.into_reader(); + serde_json::from_reader(reader).map_err(|e| { + IoError::new( + ErrorKind::InvalidData, + format!("Failed to read JSON: {}", e), + ) + }) + } + + /// Turn the body of this response into a type implementing the (serde) Deserialize trait. + /// + /// Requires feature `ureq = { version = "*", features = ["json"] }` + /// + /// Example: + /// + /// ``` /// # use serde::Deserialize; /// /// #[derive(Deserialize)] @@ -393,12 +420,12 @@ impl Response { /// ureq::get("https://ureq.s3.eu-central-1.amazonaws.com/hello_world.json") /// .call(); /// - /// let json = resp.into_json::().unwrap(); + /// let json = resp.into_json_deserialize::().unwrap(); /// /// assert_eq!(json.hello, "world"); /// ``` #[cfg(feature = "json")] - pub fn into_json(self) -> IoResult { + pub fn into_json_deserialize(self) -> IoResult { let reader = self.into_reader(); serde_json::from_reader(reader).map_err(|e| { IoError::new( @@ -730,6 +757,20 @@ mod tests { #[test] #[cfg(feature = "json")] fn parse_simple_json() { + let s = "HTTP/1.1 200 OK\r\n\ + \r\n\ + {\"hello\":\"world\"}"; + let resp = s.parse::().unwrap(); + let v = resp.into_json().unwrap(); + let compare = "{\"hello\":\"world\"}" + .parse::() + .unwrap(); + assert_eq!(v, compare); + } + + #[test] + #[cfg(feature = "json")] + fn parse_deserialize_json() { use serde::Deserialize; #[derive(Deserialize)] @@ -741,7 +782,7 @@ mod tests { \r\n\ {\"hello\":\"world\"}"; let resp = s.parse::().unwrap(); - let v = resp.into_json::().unwrap(); + let v = resp.into_json_deserialize::().unwrap(); assert_eq!(v.hello, "world"); } diff --git a/src/test/simple.rs b/src/test/simple.rs index aff836b..ccc1a0b 100644 --- a/src/test/simple.rs +++ b/src/test/simple.rs @@ -61,6 +61,22 @@ fn body_as_text() { #[test] #[cfg(feature = "json")] fn body_as_json() { + test::set_handler("/body_as_json", |_unit| { + test::make_response( + 200, + "OK", + vec![], + "{\"hello\":\"world\"}".to_string().into_bytes(), + ) + }); + let resp = get("test://host/body_as_json").call(); + let json = resp.into_json().unwrap(); + assert_eq!(json["hello"], "world"); +} + +#[test] +#[cfg(feature = "json")] +fn body_as_json_deserialize() { use serde::Deserialize; #[derive(Deserialize)] @@ -77,7 +93,7 @@ fn body_as_json() { ) }); let resp = get("test://host/body_as_json").call(); - let json = resp.into_json::().unwrap(); + let json = resp.into_json_deserialize::().unwrap(); assert_eq!(json.hello, "world"); } diff --git a/tests/https-agent.rs b/tests/https-agent.rs index 0e65a77..315d3a2 100644 --- a/tests/https-agent.rs +++ b/tests/https-agent.rs @@ -38,7 +38,7 @@ fn agent_set_cookie() { assert_eq!(resp.status(), 200); assert_eq!( "name=value", - resp.into_json::() + resp.into_json_deserialize::() .unwrap() .headers .get("Cookie")