Skip to content

Commit

Permalink
Off by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Diane Huxley committed Oct 1, 2024
1 parent a0fe4aa commit 65786d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions crates/web5/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

10 changes: 5 additions & 5 deletions crates/web5/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@ pub struct HttpResponse {

static HTTP_CLIENT: OnceCell<Arc<dyn HttpClient>> = 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<dyn HttpClient>) {
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<dyn HttpClient>) {
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;
Expand Down
14 changes: 14 additions & 0 deletions crates/web5/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}

0 comments on commit 65786d6

Please sign in to comment.