Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testdeps feature to pull in hootbin when needed #729

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features "${{ matrix.tls }} ${{ matrix.feature }}"
args: --no-default-features --features "testdeps ${{ matrix.tls }} ${{ matrix.feature }}"


cargo-deny:
Expand Down
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: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ http-interop = ["dep:http-02"]
# http-crate is for http crate version 1.0 (full release)
http-crate = ["dep:http"]
proxy-from-env = []
# Doc tests require hootbin.
testdeps = ["dep:hootbin"]

[dependencies]
base64 = "0.21"
Expand All @@ -60,7 +62,7 @@ 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" }
hootbin = { version = "0.1.5", optional = true }

[dev-dependencies]
serde = { version = "1", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl Response {
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// let text = ureq::get("http://httpbin.org/get/success")
/// let text = ureq::get("http://httpbin.org/get?success")
/// .call()?
/// .into_string()?;
///
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(feature = "testdeps"))]
fn test_server_handler(_stream: TcpStream) -> io::Result<()> {
Ok(())
}

#[cfg(feature = "testdeps")]
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
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export RUST_BACKTRACE=1
export RUSTFLAGS="-D dead_code -D unused-variables -D unused"

for feature in "" tls json charset cookies socks-proxy "tls native-certs" native-tls gzip brotli http-interop http-crate; do
if ! cargo test --no-default-features --features "${feature}" ; then
echo Command failed: cargo test --no-default-features --features \"${feature}\"
if ! cargo test --no-default-features --features "testdeps ${feature}" ; then
echo Command failed: cargo test --no-default-features --features \"testdeps ${feature}\"
exit 1
fi
done
Loading