diff --git a/Cargo.lock b/Cargo.lock index 7011a37a..4792f1bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -272,9 +272,9 @@ dependencies = [ [[package]] name = "hoot" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df22a4d90f1b0e65fe3e0d6ee6a4608cc4d81f4b2eb3e670f44bb6bde711e452" +checksum = "fceae3b65325d8086333faa87d165c595464b57a50963911a5a2c1ee87cd4e56" dependencies = [ "httparse", "log", @@ -282,9 +282,9 @@ dependencies = [ [[package]] name = "hootbin" -version = "0.1.1" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "354e60868e49ea1a39c44b9562ad207c4259dc6eabf9863bf3b0f058c55cfdb2" +checksum = "30ddcb3df3c9bc5170488febcb4c633a240764f341a1943f54fa441ea511465e" dependencies = [ "fastrand", "hoot", diff --git a/Cargo.toml b/Cargo.toml index abdf481d..a8eaa43b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,14 +58,12 @@ brotli-decompressor = { version = "2.3.2", optional = true } http-02 = { package = "http", version = "0.2", optional = true } http = { version = "1.0", optional = true } -# This can't be in dev-dependencies due to doc tests. -hootbin = { version = "0.1.1" } - [dev-dependencies] serde = { version = "1", features = ["derive"] } env_logger = "<=0.9" rustls = { version = "0.22.0" } rustls-pemfile = { version = "2.0" } +hootbin = { version = "0.1.4" } [[example]] name = "cureq" diff --git a/src/agent.rs b/src/agent.rs index 202869ff..faddc8f0 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -87,7 +87,7 @@ pub(crate) struct AgentConfig { /// But by creating an agent as entry point for the request, we /// can keep a state. /// -/// ``` +/// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let mut agent = ureq::agent(); @@ -144,7 +144,7 @@ impl Agent { /// /// If you've got an already-parsed [Url], try [request_url][Agent::request_url]. /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// use ureq::Response; @@ -166,7 +166,7 @@ impl Agent { /// you want to parse the URL and then modify it before making the request. /// If you'd just like to pass a String or a `&str`, try [request][Agent::request]. /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// use {url::Url, ureq::Response}; diff --git a/src/error.rs b/src/error.rs index 805395ec..8bd70771 100644 --- a/src/error.rs +++ b/src/error.rs @@ -65,7 +65,7 @@ use crate::Response; /// If you'd like to treat all status code errors as normal, successful responses, /// you can use [OrAnyStatus::or_any_status] like this: /// -/// ``` +/// ```no_run /// use ureq::Error::Status; /// # fn main() -> std::result::Result<(), ureq::Transport> { /// # ureq::is_test(true); @@ -289,7 +289,7 @@ impl Error { /// The type of this error. /// - /// ``` + /// ```no_run /// # ureq::is_test(true); /// let err = ureq::get("http://httpbin.org/status/500") /// .call().unwrap_err(); diff --git a/src/lib.rs b/src/lib.rs index a99d1a0a..1b4c4a07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,7 +57,7 @@ //! //! In its simplest form, ureq looks like this: //! -//! ```rust +//! ```no_run //! fn main() -> Result<(), ureq::Error> { //! # ureq::is_test(true); //! let body: String = ureq::get("http://example.com") @@ -100,7 +100,7 @@ //! Ureq supports sending and receiving json, if you enable the "json" feature: //! //! ```rust -//! # #[cfg(feature = "json")] +//! # #[cfg(feature = "hootbin", "json")] //! # fn main() -> std::result::Result<(), ureq::Error> { //! # ureq::is_test(true); //! // Requires the `json` feature enabled. @@ -503,7 +503,7 @@ pub fn agent() -> Agent { /// /// If you've got an already-parsed [`Url`], try [`request_url()`]. /// -/// ``` +/// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp: ureq::Response = ureq::request("OPTIONS", "http://example.com/") @@ -514,13 +514,14 @@ pub fn agent() -> Agent { pub fn request(method: &str, path: &str) -> Request { agent().request(method, path) } + /// Make a request using an already-parsed [Url]. /// /// This is useful if you've got a parsed [`Url`] from some other source, or if /// you want to parse the URL and then modify it before making the request. /// If you'd just like to pass a [`String`] or a [`&str`], try [`request()`]. /// -/// ``` +/// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// use url::Url; diff --git a/src/request.rs b/src/request.rs index 33c68eed..796090c9 100644 --- a/src/request.rs +++ b/src/request.rs @@ -15,7 +15,7 @@ pub type Result = std::result::Result; /// Request instances are builders that creates a request. /// -/// ``` +/// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let response = ureq::get("http://example.com/get") @@ -67,7 +67,7 @@ impl Request { /// Use this with GET, HEAD, OPTIONS or TRACE. It sends neither /// Content-Length nor Transfer-Encoding. /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp = ureq::get("http://example.com/") @@ -204,7 +204,7 @@ impl Request { /// /// The `Content-Length` header is implicitly set to the length of the serialized value. /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp = ureq::put("http://httpbin.org/put") @@ -229,7 +229,7 @@ impl Request { /// attempt to encode the string using that character set. If it fails, we fall back /// on utf-8. /// - /// ``` + /// ```no_run /// // this example requires features = ["charset"] /// /// # fn main() -> Result<(), ureq::Error> { @@ -251,7 +251,7 @@ impl Request { /// The `Content-Type` header is implicitly set to application/x-www-form-urlencoded. /// The `Content-Length` header is implicitly set to the length of the serialized value. /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp = ureq::post("http://httpbin.org/post") @@ -281,7 +281,7 @@ impl Request { /// /// The input from the reader is buffered into chunks of size 16,384, the max size of a TLS fragment. /// - /// ``` + /// ```no_run /// use std::io::Cursor; /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); @@ -297,7 +297,7 @@ impl Request { /// Set a header field. /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp = ureq::get("http://httpbin.org/bytes/1000") @@ -369,7 +369,7 @@ impl Request { /// /// For example, to set `?format=json&dest=/login` /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp = ureq::get("http://httpbin.org/get") @@ -393,7 +393,7 @@ impl Request { /// /// For example, to set `?format=json&dest=/login` /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// diff --git a/src/response.rs b/src/response.rs index 0223068a..69cfffca 100644 --- a/src/response.rs +++ b/src/response.rs @@ -62,7 +62,7 @@ enum BodyType { /// is returned to the [`Agent`] connection pool used (notice there is always /// an agent present, even when not explicitly configured by the user). /// -/// ``` +/// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let response = ureq::get("http://example.com/").call()?; @@ -198,7 +198,7 @@ impl Response { /// /// Example: /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp = ureq::get("http://example.com/charset/iso").call()?; @@ -222,7 +222,7 @@ impl Response { /// /// Example: /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let resp = ureq::get("http://example.com/charset/iso").call()?; @@ -260,7 +260,7 @@ impl Response { /// /// Example: /// - /// ``` + /// ```no_run /// use std::io::Read; /// # fn main() -> Result<(), Box> { /// # ureq::is_test(true); @@ -432,7 +432,7 @@ impl Response { /// /// Example: /// - /// ``` + /// ```no_run /// # fn main() -> Result<(), ureq::Error> { /// # ureq::is_test(true); /// let text = ureq::get("http://httpbin.org/get/success") diff --git a/src/testserver.rs b/src/testserver.rs index fd844efc..04afacfa 100644 --- a/src/testserver.rs +++ b/src/testserver.rs @@ -8,31 +8,39 @@ use std::time::Duration; use crate::{Agent, AgentBuilder}; +#[cfg(not(test))] +fn test_server_handler(_stream: TcpStream) -> io::Result<()> { + Ok(()) +} + +#[cfg(test)] +fn test_server_handler(stream: TcpStream) -> io::Result<()> { + use hootbin::serve_single; + let o = stream.try_clone().expect("TcpStream to be clonable"); + let i = stream; + match serve_single(i, o, "https://hootbin.test/") { + Ok(()) => {} + Err(e) => { + if let hootbin::Error::Io(ioe) = &e { + if ioe.kind() == io::ErrorKind::UnexpectedEof { + // accept this. the pre-connect below is always erroring. + return Ok(()); + } + } + + println!("TestServer error: {:?}", e); + } + }; + Ok(()) +} + // An agent to be installed by default for tests and doctests, such // that all hostnames resolve to a TestServer on localhost. pub(crate) fn test_agent() -> Agent { #[cfg(test)] let _ = env_logger::try_init(); - let testserver = TestServer::new(|stream: TcpStream| -> io::Result<()> { - use hootbin::serve_single; - let o = stream.try_clone().expect("TcpStream to be clonable"); - let i = stream; - match serve_single(i, o, "https://hootbin.test/") { - Ok(()) => {} - Err(e) => { - if let hootbin::Error::Io(ioe) = &e { - if ioe.kind() == io::ErrorKind::UnexpectedEof { - // accept this. the pre-connect below is always erroring. - return Ok(()); - } - } - - println!("TestServer error: {:?}", e); - } - }; - Ok(()) - }); + let testserver = TestServer::new(test_server_handler); // Slightly tricky thing here: we want to make sure the TestServer lives // as long as the agent. This is accomplished by `move`ing it into the // closure, which becomes owned by the agent.