Skip to content

Commit 437d941

Browse files
authoredFeb 25, 2025··
Merge pull request #120 from FalkWoldmann/master
Refactoring and cleanup
2 parents 1cb9751 + 373d3c6 commit 437d941

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed
 

‎Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ keywords = ["http", "mock", "test"]
99
categories = ["development-tools::testing"]
1010
license = "MIT"
1111
repository = "https://github.com/alexliesenfeld/httpmock"
12+
rust-version = "1.74.1"
1213

1314
[dependencies]
1415
serde = { version = "1.0", features = ["derive"] }
1516
serde_json = "1.0"
1617
serde_regex = "1.1"
17-
lazy_static = "1.4"
1818
base64 = "0.22"
1919
regex = "1.10"
2020
log = "0.4"
@@ -42,6 +42,7 @@ env_logger = { version = "0.11", optional = true }
4242
serde_yaml = { version = "0.9", optional = true }
4343
async-std = { version = "1.12", features = ["attributes", "unstable"] }
4444
headers = { version = "0.4", optional = true }
45+
once_cell = "1.20.3"
4546

4647
### TLS / HTTPS / PROXY
4748
rustls = { version = "0.23", default-features = false, features = ["std", "tls12"], optional = true }
@@ -54,8 +55,6 @@ futures-timer = "3"
5455

5556
[dev-dependencies]
5657
env_logger = "0.11"
57-
tokio-test = "0.4"
58-
quote = "1.0"
5958
actix-rt = "2.9"
6059
colored = "2.1"
6160
reqwest = { version = "0.12", features = ["blocking", "cookies", "rustls-tls", "rustls-tls-native-roots"] }

‎src/api/server.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::server::{state::HttpMockStateManager, HttpMockServerBuilder};
3636

3737
use crate::Mock;
3838
use async_object_pool::Pool;
39-
use lazy_static::lazy_static;
39+
use once_cell::sync::Lazy;
4040
use std::{
4141
cell::Cell,
4242
future::pending,
@@ -1364,31 +1364,33 @@ const LOCAL_SERVER_ADAPTER_GENERATOR: fn() -> Arc<dyn MockServerAdapter + Send +
13641364
Arc::new(LocalMockServerAdapter::new(addr, state_manager))
13651365
};
13661366

1367-
lazy_static! {
1368-
static ref LOCAL_SERVER_POOL_REF: Arc<Pool<Arc<dyn MockServerAdapter + Send + Sync>>> = {
1367+
static LOCAL_SERVER_POOL_REF: Lazy<Arc<Pool<Arc<dyn MockServerAdapter + Send + Sync>>>> =
1368+
Lazy::new(|| {
13691369
let max_servers = read_env("HTTPMOCK_MAX_SERVERS", "25")
13701370
.parse::<usize>()
13711371
.expect("Cannot parse environment variable HTTPMOCK_MAX_SERVERS as an integer");
13721372
Arc::new(Pool::new(max_servers))
1373-
};
1374-
static ref REMOTE_SERVER_POOL_REF: Arc<Pool<Arc<dyn MockServerAdapter + Send + Sync>>> =
1375-
Arc::new(Pool::new(1));
1376-
}
1373+
});
1374+
1375+
static REMOTE_SERVER_POOL_REF: Lazy<Arc<Pool<Arc<dyn MockServerAdapter + Send + Sync>>>> =
1376+
Lazy::new(|| Arc::new(Pool::new(1)));
13771377

13781378
#[cfg(feature = "remote")]
1379-
lazy_static! {
1380-
// TODO: REFACTOR to use a runtime agnostic HTTP client for remote access.
1381-
// This solution does not require OpenSSL and less dependencies compared to
1382-
// other HTTP clients (tested: isahc, surf). Curl seems to use OpenSSL by default,
1383-
// so this is not an option. Optimally, the HTTP client uses rustls to avoid the
1384-
// dependency on OpenSSL installed on the OS.
1385-
static ref REMOTE_SERVER_CLIENT: Arc<HttpMockHttpClient> = {
1386-
let max_workers = read_env("HTTPMOCK_HTTP_CLIENT_WORKER_THREADS", "1")
1387-
.parse::<usize>()
1388-
.expect("Cannot parse environment variable HTTPMOCK_HTTP_CLIENT_WORKER_THREADS as an integer");
1389-
let max_blocking_threads = read_env("HTTPMOCK_HTTP_CLIENT_MAX_BLOCKING_THREADS", "10")
1390-
.parse::<usize>()
1391-
.expect("Cannot parse environment variable HTTPMOCK_HTTP_CLIENT_MAX_BLOCKING_THREADS to an integer");
1392-
Arc::new(HttpMockHttpClient::new(Some(Arc::new(runtime::new(max_workers,max_blocking_threads).unwrap()))))
1393-
};
1394-
}
1379+
// TODO: REFACTOR to use a runtime agnostic HTTP client for remote access.
1380+
// This solution does not require OpenSSL and less dependencies compared to
1381+
// other HTTP clients (tested: isahc, surf). Curl seems to use OpenSSL by default,
1382+
// so this is not an option. Optimally, the HTTP client uses rustls to avoid the
1383+
// dependency on OpenSSL installed on the OS.
1384+
static REMOTE_SERVER_CLIENT: Lazy<Arc<HttpMockHttpClient>> = Lazy::new(|| {
1385+
let max_workers = read_env("HTTPMOCK_HTTP_CLIENT_WORKER_THREADS", "1")
1386+
.parse::<usize>()
1387+
.expect(
1388+
"Cannot parse environment variable HTTPMOCK_HTTP_CLIENT_WORKER_THREADS as an integer",
1389+
);
1390+
let max_blocking_threads = read_env("HTTPMOCK_HTTP_CLIENT_MAX_BLOCKING_THREADS", "10")
1391+
.parse::<usize>()
1392+
.expect("Cannot parse environment variable HTTPMOCK_HTTP_CLIENT_MAX_BLOCKING_THREADS to an integer");
1393+
Arc::new(HttpMockHttpClient::new(Some(Arc::new(
1394+
runtime::new(max_workers, max_blocking_threads).unwrap(),
1395+
))))
1396+
});

‎src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
//!
9595
//! This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9696
//! warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT Public License for more details.
97-
extern crate lazy_static;
9897
9998
use std::{borrow::BorrowMut, net::ToSocketAddrs};
10099

‎tests/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate lazy_static;
2-
31
use httpmock::server::{HttpMockServer, HttpMockServerBuilder};
42
use std::{sync::Mutex, thread};
53
use tokio::task::LocalSet;

0 commit comments

Comments
 (0)
Please sign in to comment.