Skip to content

Commit

Permalink
tests: inline connector setup
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 20, 2024
1 parent b6d8c0f commit 2119f9c
Showing 1 changed file with 19 additions and 32 deletions.
51 changes: 19 additions & 32 deletions src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,59 +213,38 @@ mod tests {
use std::future::poll_fn;

use http::Uri;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::rt::TokioIo;
use tokio::net::TcpStream;
use tower_service::Service;

use super::HttpsConnector;
use crate::{ConfigBuilderExt, HttpsConnectorBuilder};
use super::*;
use crate::{ConfigBuilderExt, HttpsConnectorBuilder, MaybeHttpsStream};

#[tokio::test]
async fn connects_https() {
oneshot(https_or_http_connector(), true)
.await
.unwrap();
connect(false, true).await.unwrap();
}

#[tokio::test]
async fn connects_http() {
oneshot(https_or_http_connector(), false)
.await
.unwrap();
connect(false, false).await.unwrap();
}

#[tokio::test]
async fn connects_https_only() {
oneshot(https_only_connector(), true)
.await
.unwrap();
connect(true, true).await.unwrap();
}

#[tokio::test]
async fn enforces_https_only() {
let message = oneshot(https_only_connector(), false)
let message = connect(true, false)
.await
.unwrap_err()
.to_string();

assert_eq!(message, "unsupported scheme http");
}

fn https_or_http_connector() -> HttpsConnector<HttpConnector> {
HttpsConnectorBuilder::new()
.with_tls_config(tls_config())
.https_or_http()
.enable_http1()
.build()
}

fn https_only_connector() -> HttpsConnector<HttpConnector> {
HttpsConnectorBuilder::new()
.with_tls_config(tls_config())
.https_only()
.enable_http1()
.build()
}

fn tls_config() -> rustls::ClientConfig {
#[cfg(feature = "rustls-platform-verifier")]
return rustls::ClientConfig::builder()
Expand All @@ -284,10 +263,18 @@ mod tests {
.with_no_client_auth();
}

async fn oneshot<S: Service<Uri>>(
mut service: S,
async fn connect(
https_only: bool,
https: bool,
) -> Result<S::Response, S::Error> {
) -> Result<MaybeHttpsStream<TokioIo<TcpStream>>, BoxError> {
let builder = HttpsConnectorBuilder::new().with_tls_config(tls_config());
let mut service = match https_only {
true => builder.https_only(),
false => builder.https_or_http(),
}
.enable_http1()
.build();

poll_fn(|cx| service.poll_ready(cx)).await?;
service
.call(Uri::from_static(match https {
Expand Down

0 comments on commit 2119f9c

Please sign in to comment.