Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ jobs:
# full excludes that opt-in, so also check that TokioExecutor does not
# propagate spans by default. Limit this extra run to runtime tests.
- run: "cargo test --features full --lib rt::"
# Test client-legacy without the tcp feature to ensure custom connectors
# work on targets where mio/tcp is unsupported.
- run: "cargo test --features client-legacy,http1,tokio --test client_no_tcp"

msrv:
name: Check MSRV (${{ matrix.rust }}) on ${{ matrix.os }}
Expand Down
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ full = [
"service",
"http1",
"http2",
"tcp",
"tokio",
"tracing",
]

client = ["hyper/client", "dep:tracing", "dep:futures-channel", "dep:tower-service"]
client-legacy = ["client", "tokio/net", "dep:socket2", "tokio/sync", "dep:libc", "dep:futures-util", "dep:httparse", "dep:sync_wrapper"]
client-legacy = ["client", "tokio/sync", "dep:futures-util", "dep:httparse", "dep:sync_wrapper"]
client-pool = ["client", "dep:futures-util", "dep:tower-layer", "tokio/sync"]
client-proxy = ["client", "dep:base64", "dep:ipnet", "dep:percent-encoding"]
client-proxy-system = ["dep:system-configuration", "dep:windows-registry"]
Expand All @@ -93,6 +94,8 @@ service = ["dep:tower-service"]
http1 = ["hyper/http1"]
http2 = ["hyper/http2"]

tcp = ["tokio", "tokio/net", "dep:socket2", "dep:libc"]

tokio = ["dep:tokio", "tokio/rt", "tokio/time"]

tracing = ["dep:tracing"]
Expand All @@ -105,7 +108,11 @@ __internal_happy_eyeballs_tests = []

[[example]]
name = "client"
required-features = ["client-legacy", "http1", "tokio"]
required-features = ["client-legacy", "http1", "tcp"]

[[example]]
name = "client_tracing"
required-features = ["client-legacy", "http1", "tcp", "tracing"]

[[example]]
name = "server"
Expand Down
7 changes: 5 additions & 2 deletions src/client/legacy/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use hyper::{Method, Request, Response, Uri, Version, body::Body};
use sync_wrapper::SyncWrapper;
use tracing::{debug, trace, warn};

#[cfg(feature = "tokio")]
#[cfg(feature = "tcp")]
use super::connect::HttpConnector;
use super::connect::capture::CaptureConnectionExtension;
use super::connect::{Alpn, Connect, Connected, Connection};
Expand Down Expand Up @@ -1555,6 +1555,8 @@ impl Builder {
{
#[cfg(feature = "http2")]
self.h2_builder.timer(timer);
#[cfg(not(feature = "http2"))]
let _ = timer;
self
}

Expand Down Expand Up @@ -1611,7 +1613,8 @@ impl Builder {
}

/// Build a client with this configuration and the default `HttpConnector`.
#[cfg(feature = "tokio")]
#[cfg(feature = "tcp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
pub fn build_http<B>(&self) -> Client<HttpConnector, B>
where
B: Body + Send,
Expand Down
8 changes: 5 additions & 3 deletions src/client/legacy/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ use std::{

use ::http::Extensions;

#[cfg(feature = "tokio")]
#[cfg(feature = "tcp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
pub use self::http::{HttpConnector, HttpInfo};

#[cfg(feature = "tokio")]
#[cfg(feature = "tcp")]
#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
pub mod dns;
#[cfg(feature = "tokio")]
#[cfg(feature = "tcp")]
mod http;

pub mod proxy;
Expand Down
118 changes: 118 additions & 0 deletions tests/client_no_tcp.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the intent of this file that is be run without the tcp feature flag active?

i should note that we run tests with --all-features, so i am worried this this isn't exercising the tcp feature flag in the way that we think it is.

a comment here to explain the purpose of this test would be helpful.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

presuming this is intended to be run without the tcp feature flag, i'd suggest adding a step to the test workflow here:

# full excludes that opt-in, so also check that TokioExecutor does not
# propagate spans by default. Limit this extra run to runtime tests.
- run: "cargo test --features full --lib rt::"

something like: cargo test --features client-legacy,http1,tokio --test client_no_tcp

then, since this doesn't exercise anything meaningful when the tcp feature flag is active, we should refine this gate further like so:

#![cfg(all(
    feature = "client-legacy",
    feature = "http1",
    feature = "tokio",
    not(feature = "tcp")
))]

that way, we don't bother with this test when the tcp flag is active.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great points, thank you @cratelyn! You're completely right — running --all-features\ enabled \ cp, so \client_no_tcp.rs\ was not exercising the no-tcp configuration in CI.

I've adopted your suggestions:

  1. Refined the crate gate to #![cfg(all(feature = \client-legacy, feature = \http1, feature = \tokio, not(feature = \tcp)))].
  2. Added an explanatory module-level doc comment clarifying the test's intent (verifying that \client-legacy\ can compile and run with custom connectors on targets where \ cp/\mio\ are absent).
  3. Added a dedicated step \cargo test --features client-legacy,http1,tokio --test client_no_tcp\ under the \ est\ matrix in .github/workflows/CI.yml.

Ready for another look!

Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//! Integration test verifying that `client-legacy` can be compiled and used with
//! a custom connector (e.g. for custom in-memory streams, TLS, or non-TCP platforms
//! like WASM and Fuchsia) without enabling the `tcp` feature or pulling in `mio`.

#![cfg(all(
feature = "client-legacy",
feature = "http1",
feature = "tokio",
not(feature = "tcp")
))]

use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};

use bytes::Bytes;
use http::{Request, StatusCode, Uri};
use http_body_util::{BodyExt, Empty};
use hyper::rt::{Read, ReadBufCursor, Write};
use hyper_util::client::legacy::Client;
use hyper_util::client::legacy::connect::{Connected, Connection};
use hyper_util::rt::{TokioExecutor, TokioIo};
use tower_service::Service;

struct MockStream(TokioIo<tokio_test::io::Mock>);

impl Read for MockStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: ReadBufCursor<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}

impl Write for MockStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, std::io::Error>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}

fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
Pin::new(&mut self.0).poll_flush(cx)
}

fn poll_shutdown(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), std::io::Error>> {
Pin::new(&mut self.0).poll_shutdown(cx)
}
}

impl Connection for MockStream {
fn connected(&self) -> Connected {
Connected::new()
}
}

#[derive(Clone)]
struct CustomConnector {
mock: Arc<Mutex<Option<tokio_test::io::Mock>>>,
}

impl Service<Uri> for CustomConnector {
type Response = MockStream;
type Error = std::io::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, _dst: Uri) -> Self::Future {
let mock = self
.mock
.lock()
.unwrap()
.take()
.expect("mock connection only called once");
Box::pin(async move { Ok(MockStream(TokioIo::new(mock))) })
}
}

#[tokio::test]
async fn test_client_legacy_with_custom_connector_without_tcp() {
let mock_io = tokio_test::io::Builder::new()
.write(b"GET /test HTTP/1.1\r\nhost: example.com\r\n\r\n")
.read(b"HTTP/1.1 200 OK\r\ncontent-length: 5\r\n\r\nhello")
.build();

let connector = CustomConnector {
mock: Arc::new(Mutex::new(Some(mock_io))),
};

let client = Client::builder(TokioExecutor::new())
.pool_max_idle_per_host(0)
.build(connector);

let req = Request::builder()
.uri("http://example.com/test")
.body(Empty::<Bytes>::new())
.unwrap();

let mut resp = client.request(req).await.expect("request succeeds");
assert_eq!(resp.status(), StatusCode::OK);

let body = resp.body_mut().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], b"hello");
}
6 changes: 5 additions & 1 deletion tests/legacy_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![cfg(all(feature = "client-legacy", any(feature = "http1", feature = "http2")))]
#![cfg(all(
feature = "client-legacy",
feature = "tcp",
any(feature = "http1", feature = "http2")
))]

mod test_utils;

Expand Down
2 changes: 1 addition & 1 deletion tests/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(all(feature = "client-legacy", feature = "client-proxy"))]
#![cfg(all(feature = "client-legacy", feature = "client-proxy", feature = "tcp"))]

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
Expand Down