Skip to content

Commit

Permalink
no_run for doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Feb 16, 2024
1 parent 36f5c6c commit 0d29b92
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 49 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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> {

Check failure on line 104 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test

multiple `cfg` predicates are specified

Check failure on line 104 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (native-tls)

multiple `cfg` predicates are specified

Check failure on line 104 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (http-crate http-interop)

multiple `cfg` predicates are specified

Check failure on line 104 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (http-crate)

multiple `cfg` predicates are specified

Check failure on line 104 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (native-certs)

multiple `cfg` predicates are specified

Check failure on line 104 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (gzip)

multiple `cfg` predicates are specified

Check failure on line 104 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (cookies)

multiple `cfg` predicates are specified
//! # ureq::is_test(true);
//! // Requires the `json` feature enabled.
Expand Down Expand Up @@ -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/")
Expand All @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub type Result<T> = std::result::Result<T, Error>;

/// 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")
Expand Down Expand Up @@ -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/")
Expand Down Expand Up @@ -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")
Expand All @@ -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> {
Expand All @@ -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")
Expand Down Expand Up @@ -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);
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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);
///
Expand Down
10 changes: 5 additions & 5 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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()?;
Expand All @@ -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()?;
Expand Down Expand Up @@ -260,7 +260,7 @@ impl Response {
///
/// Example:
///
/// ```
/// ```no_run
/// use std::io::Read;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # ureq::is_test(true);
Expand Down Expand Up @@ -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")
Expand Down
46 changes: 27 additions & 19 deletions src/testserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0d29b92

Please sign in to comment.