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.
This commit is contained in:
committed by
Martin Algesten
parent
203573d27c
commit
daa63d3bc6
@@ -318,10 +318,10 @@ impl Response {
|
|||||||
///
|
///
|
||||||
/// ## Charset support
|
/// ## Charset support
|
||||||
///
|
///
|
||||||
/// Requires feature `ureq = { version = "*", features = ["charset"] }`
|
/// 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
|
||||||
/// Attempts to respect the character encoding of the `Content-Type` header and
|
/// Content-Type header, or the Content-Type header does not specify a charset, into_string()
|
||||||
/// falls back to `utf-8`.
|
/// uses `utf-8`.
|
||||||
///
|
///
|
||||||
/// I.e. `Content-Length: text/plain; charset=iso-8859-1` would be decoded in latin-1.
|
/// I.e. `Content-Length: text/plain; charset=iso-8859-1` would be decoded in latin-1.
|
||||||
///
|
///
|
||||||
@@ -350,13 +350,15 @@ impl Response {
|
|||||||
/// Example:
|
/// Example:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let resp =
|
/// # fn main() -> Result<(), ureq::Error> {
|
||||||
/// ureq::get("http://ureq.s3.eu-central-1.amazonaws.com/hello_world.json")
|
/// # ureq::is_test(true);
|
||||||
/// .call().unwrap();
|
/// let json: serde_json::Value = ureq::get("http://example.com/hello_world.json")
|
||||||
///
|
/// .call()?
|
||||||
/// let json = resp.into_json().unwrap();
|
/// .into_json()?;
|
||||||
///
|
///
|
||||||
/// assert_eq!(json["hello"], "world");
|
/// assert_eq!(json["hello"], "world");
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
pub fn into_json(self) -> io::Result<serde_json::Value> {
|
pub fn into_json(self) -> io::Result<serde_json::Value> {
|
||||||
@@ -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"] }`
|
/// Requires feature `ureq = { version = "*", features = ["json"] }`
|
||||||
///
|
///
|
||||||
/// Example:
|
/// Example:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use serde::Deserialize;
|
/// # fn main() -> Result<(), ureq::Error> {
|
||||||
|
/// # ureq::is_test(true);
|
||||||
|
/// use serde::{Deserialize, de::DeserializeOwned};
|
||||||
///
|
///
|
||||||
/// #[derive(Deserialize)]
|
/// #[derive(Deserialize)]
|
||||||
/// struct Hello {
|
/// struct Message {
|
||||||
/// hello: String,
|
/// hello: String,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// let resp =
|
/// let message: Message =
|
||||||
/// ureq::get("http://ureq.s3.eu-central-1.amazonaws.com/hello_world.json")
|
/// ureq::get("http://example.com/hello_world.json")
|
||||||
/// .call().unwrap();
|
/// .call()?
|
||||||
|
/// .into_json_deserialize()?;
|
||||||
///
|
///
|
||||||
/// let json = resp.into_json_deserialize::<Hello>().unwrap();
|
/// assert_eq!(message.hello, "world");
|
||||||
///
|
/// # Ok(())
|
||||||
/// assert_eq!(json.hello, "world");
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
pub fn into_json_deserialize<T: DeserializeOwned>(self) -> io::Result<T> {
|
pub fn into_json_deserialize<T: DeserializeOwned>(self) -> io::Result<T> {
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ pub(crate) fn test_agent() -> Agent {
|
|||||||
stream.write_all(b"Content-Length: 100\r\n")?;
|
stream.write_all(b"Content-Length: 100\r\n")?;
|
||||||
stream.write_all(b"\r\n")?;
|
stream.write_all(b"\r\n")?;
|
||||||
stream.write_all(&[0; 100])?;
|
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" {
|
} else if headers.path() == "/redirect/3" {
|
||||||
stream.write_all(b"HTTP/1.1 302 Found\r\n")?;
|
stream.write_all(b"HTTP/1.1 302 Found\r\n")?;
|
||||||
stream.write_all(b"Location: /redirect/3\r\n")?;
|
stream.write_all(b"Location: /redirect/3\r\n")?;
|
||||||
|
|||||||
Reference in New Issue
Block a user