From daa63d3bc6b277b1d70697708c17e3831b8a394a Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Sat, 14 Nov 2020 16:35:11 -0800 Subject: [PATCH] Update some more doctests. I missed these in my previous doctest PR. The doctests all now run without accessing the network. Tested by turning off networking and running them. Request.call's doctest wouldn't run because it relied on making a custom AgentBuilder and building it, which bypasses the test_agent. I concluded that this doctest was mostly illustrating behavior of AgentBuilder, not call(), and simplified it to be more like the other calling methods on request. --- src/response.rs | 41 +++++++++++++++++++++++------------------ src/testserver.rs | 4 ++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/response.rs b/src/response.rs index de28a05..09e00cc 100644 --- a/src/response.rs +++ b/src/response.rs @@ -318,10 +318,10 @@ impl Response { /// /// ## Charset support /// - /// Requires feature `ureq = { version = "*", features = ["charset"] }` - /// - /// Attempts to respect the character encoding of the `Content-Type` header and - /// falls back to `utf-8`. + /// If you enable feature `ureq = { version = "*", features = ["charset"] }`, into_string() + /// attempts to respect the character encoding of the `Content-Type` header. If there is no + /// Content-Type header, or the Content-Type header does not specify a charset, into_string() + /// uses `utf-8`. /// /// I.e. `Content-Length: text/plain; charset=iso-8859-1` would be decoded in latin-1. /// @@ -350,13 +350,15 @@ impl Response { /// Example: /// /// ``` - /// let resp = - /// ureq::get("http://ureq.s3.eu-central-1.amazonaws.com/hello_world.json") - /// .call().unwrap(); - /// - /// let json = resp.into_json().unwrap(); + /// # fn main() -> Result<(), ureq::Error> { + /// # ureq::is_test(true); + /// let json: serde_json::Value = ureq::get("http://example.com/hello_world.json") + /// .call()? + /// .into_json()?; /// /// assert_eq!(json["hello"], "world"); + /// # Ok(()) + /// # } /// ``` #[cfg(feature = "json")] pub fn into_json(self) -> io::Result { @@ -381,27 +383,30 @@ impl Response { }) } - /// Turn the body of this response into a type implementing the (serde) Deserialize trait. + /// Turn the body of this response into a type that implements the [serde::Deserialize] trait. /// /// Requires feature `ureq = { version = "*", features = ["json"] }` /// /// Example: /// /// ``` - /// use serde::Deserialize; + /// # fn main() -> Result<(), ureq::Error> { + /// # ureq::is_test(true); + /// use serde::{Deserialize, de::DeserializeOwned}; /// /// #[derive(Deserialize)] - /// struct Hello { + /// struct Message { /// hello: String, /// } /// - /// let resp = - /// ureq::get("http://ureq.s3.eu-central-1.amazonaws.com/hello_world.json") - /// .call().unwrap(); + /// let message: Message = + /// ureq::get("http://example.com/hello_world.json") + /// .call()? + /// .into_json_deserialize()?; /// - /// let json = resp.into_json_deserialize::().unwrap(); - /// - /// assert_eq!(json.hello, "world"); + /// assert_eq!(message.hello, "world"); + /// # Ok(()) + /// # } /// ``` #[cfg(feature = "json")] pub fn into_json_deserialize(self) -> io::Result { diff --git a/src/testserver.rs b/src/testserver.rs index 1b36f75..eaf4148 100644 --- a/src/testserver.rs +++ b/src/testserver.rs @@ -22,6 +22,10 @@ pub(crate) fn test_agent() -> Agent { stream.write_all(b"Content-Length: 100\r\n")?; stream.write_all(b"\r\n")?; stream.write_all(&[0; 100])?; + } else if headers.path() == "/hello_world.json" { + stream.write_all(b"HTTP/1.1 200 OK\r\n")?; + stream.write_all(b"\r\n")?; + stream.write_all(br#"{"hello": "world"}"#)?; } else if headers.path() == "/redirect/3" { stream.write_all(b"HTTP/1.1 302 Found\r\n")?; stream.write_all(b"Location: /redirect/3\r\n")?;