From 65786d653c19c65abaf310e6917c9053f3fe70f8 Mon Sep 17 00:00:00 2001 From: Diane Huxley Date: Tue, 1 Oct 2024 14:08:03 -0700 Subject: [PATCH] Off by default --- crates/web5/Cargo.toml | 6 ++++-- crates/web5/src/http.rs | 10 +++++----- crates/web5/src/lib.rs | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/crates/web5/Cargo.toml b/crates/web5/Cargo.toml index 3cff498a..f5c65a76 100644 --- a/crates/web5/Cargo.toml +++ b/crates/web5/Cargo.toml @@ -37,9 +37,11 @@ reqwest = { version = "0.12", optional = true } once_cell = "1.19.0" [features] -default = ["default_http_client"] -default_http_client = ["reqwest"] +default = [] +http_reqwest = ["reqwest"] [dev-dependencies] mockito = "1.5.0" tokio = { version = "1.38.0", features = ["macros", "test-util"] } +web5 = { path = ".", features = ["http_reqwest"] } + diff --git a/crates/web5/src/http.rs b/crates/web5/src/http.rs index 5887a9b8..723ca88d 100644 --- a/crates/web5/src/http.rs +++ b/crates/web5/src/http.rs @@ -35,29 +35,29 @@ pub struct HttpResponse { static HTTP_CLIENT: OnceCell> = OnceCell::new(); -#[cfg(feature = "default_http_client")] +#[cfg(feature = "http_reqwest")] pub fn get_http_client() -> &'static dyn HttpClient { HTTP_CLIENT.get_or_init(|| { Arc::new(reqwest_http_client::ReqwestHttpClient::new()) }).as_ref() } -#[cfg(not(feature = "default_http_client"))] +#[cfg(not(feature = "http_reqwest"))] pub fn get_http_client() -> &'static dyn HttpClient { HTTP_CLIENT.get().expect("HttpClient has not been set. Please call set_http_client().").as_ref() } -#[cfg(feature = "default_http_client")] +#[cfg(feature = "http_reqwest")] pub fn set_http_client(_: Arc) { panic!("Cannot set a custom HttpClient when the reqwest feature is enabled."); } -#[cfg(not(feature = "default_http_client"))] +#[cfg(not(feature = "http_reqwest"))] pub fn set_http_client(client: Arc) { HTTP_CLIENT.set(client).unwrap_or_else(|_| panic!("HttpClient has already been set.")); } -#[cfg(feature = "default_http_client")] +#[cfg(feature = "http_reqwest")] mod reqwest_http_client { use super::*; use reqwest::Client; diff --git a/crates/web5/src/lib.rs b/crates/web5/src/lib.rs index 863d5cae..99580e6d 100644 --- a/crates/web5/src/lib.rs +++ b/crates/web5/src/lib.rs @@ -12,3 +12,17 @@ pub use http::set_http_client; #[cfg(test)] mod test_vectors; +#[cfg(test)] +mod tests { + #[cfg(feature = "http_reqwest")] + #[test] + fn test_with_reqwest_feature() { + println!("http_reqwest feature is enabled!"); + } + + #[cfg(not(feature = "http_reqwest"))] + #[test] + fn test_without_reqwest_feature() { + println!("http_reqwest feature is NOT enabled!"); + } +} \ No newline at end of file