Skip to content

Commit 8653954

Browse files
committed
feat(async): persist headers on AsyncClient
And add a test to verify that both async and blocking clients were built with headers.
1 parent d9c2a1f commit 8653954

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/async.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub struct AsyncClient<S = DefaultSleeper> {
4545
max_retries: usize,
4646
/// Marker for the type of sleeper used
4747
marker: PhantomData<S>,
48+
#[allow(unused)]
49+
/// HTTP headers to append on every request made to Esplora server.
50+
pub(crate) headers: HashMap<String, String>, // TODO: remove this when `bitreq` is implemented
4851
}
4952

5053
impl<S: Sleeper> AsyncClient<S> {
@@ -62,6 +65,8 @@ impl<S: Sleeper> AsyncClient<S> {
6265
client_builder = client_builder.timeout(core::time::Duration::from_secs(timeout));
6366
}
6467

68+
// TODO: remove this when `bitreq` is implemented
69+
let headers = builder.headers.clone();
6570
if !builder.headers.is_empty() {
6671
let mut headers = header::HeaderMap::new();
6772
for (k, v) in builder.headers {
@@ -78,6 +83,7 @@ impl<S: Sleeper> AsyncClient<S> {
7883
url: builder.base_url,
7984
client: client_builder.build()?,
8085
max_retries: builder.max_retries,
86+
headers,
8187
marker: PhantomData,
8288
})
8389
}
@@ -87,6 +93,7 @@ impl<S: Sleeper> AsyncClient<S> {
8793
AsyncClient {
8894
url,
8995
client,
96+
headers: HashMap::new(),
9097
max_retries: crate::DEFAULT_MAX_RETRIES,
9198
marker: PhantomData,
9299
}

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,21 @@ mod test {
493493
);
494494
}
495495

496+
#[cfg(all(feature = "blocking", feature = "async"))]
497+
#[tokio::test]
498+
async fn test_build_client_with_headers() {
499+
let env = TestEnv::new();
500+
let headers: HashMap<String, String> = [(
501+
"Authorization".to_string(),
502+
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==".to_string(),
503+
)]
504+
.into();
505+
let (blocking_client, async_client) = env.setup_clients_with_headers(headers.clone());
506+
507+
assert_eq!(blocking_client.headers, headers);
508+
assert_eq!(async_client.headers, headers);
509+
}
510+
496511
#[cfg(all(feature = "blocking", feature = "async"))]
497512
#[tokio::test]
498513
async fn test_get_tx() {

0 commit comments

Comments
 (0)