Skip to content

Commit

Permalink
Merge pull request #2282 from input-output-hk/dlachaume/add-accept-en…
Browse files Browse the repository at this point in the history
…coding-header-in-signer-and-client-requests

Feat: support compression in HTTP clients
  • Loading branch information
dlachaume authored Feb 7, 2025
2 parents 7635d4e + d3228da commit 52f97d4
Show file tree
Hide file tree
Showing 15 changed files with 256 additions and 20 deletions.
64 changes: 59 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/client-wasm-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/client-wasm-web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.7.0"
version = "0.7.1"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down Expand Up @@ -34,7 +34,13 @@ paste = "1.0.15"
prometheus = "0.13.4"
rayon = "1.10.0"
regex = "1.11.1"
reqwest = { version = "0.12.12", features = ["json"] }
reqwest = { version = "0.12.12", features = [
"json",
"gzip",
"zstd",
"deflate",
"brotli"
] }
semver = "1.0.25"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
Expand Down
4 changes: 2 additions & 2 deletions mithril-client-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client-wasm"
version = "0.8.0"
version = "0.8.1"
description = "Mithril client WASM"
authors = { workspace = true }
edition = { workspace = true }
Expand All @@ -17,7 +17,7 @@ anyhow = "1.0.95"
async-trait = "0.1.86"
chrono = { version = "0.4.39", features = ["serde"] }
futures = "0.3.31"
mithril-client = { path = "../mithril-client", features = ["unstable"] }
mithril-client = { path = "../mithril-client", default-features = false, features = ["unstable", "num-integer-backend"] }
serde = { version = "1.0.217", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
serde_json = "1.0.138"
Expand Down
2 changes: 1 addition & 1 deletion mithril-client-wasm/ci-test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-client-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mithril-dev/mithril-client-wasm",
"version": "0.8.0",
"version": "0.8.1",
"description": "Mithril client WASM",
"license": "Apache-2.0",
"collaborators": [
Expand Down
7 changes: 5 additions & 2 deletions mithril-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client"
version = "0.11.0"
version = "0.11.1"
description = "Mithril client library"
authors = { workspace = true }
edition = { workspace = true }
Expand Down Expand Up @@ -84,7 +84,7 @@ warp = "0.3.7"

[features]
# Include native-tls in reqwest by default
default = ["native-tls", "rug-backend"]
default = ["native-tls", "rug-backend", "enable-http-compression"]

# Full feature set
full = ["fs"]
Expand All @@ -105,6 +105,9 @@ rustls-tls-manual-roots = ["reqwest/rustls-tls-manual-roots"]
rustls-tls-webpki-roots = ["reqwest/rustls-tls-webpki-roots"]
rustls-tls-native-roots = ["reqwest/rustls-tls-native-roots"]

# Support compressed traffic with `request`
enable-http-compression = ["reqwest/gzip", "reqwest/zstd", "reqwest/deflate", "reqwest/brotli"]

# Enables `rug-backend` features for `mithril-common` dependency
rug-backend = ["mithril-common/rug-backend"]
# Enables `num-integer-backend` features for `mithril-common` dependency
Expand Down
54 changes: 54 additions & 0 deletions mithril-client/src/aggregator_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,58 @@ mod tests {
.await
.expect("GET request should succeed");
}

#[tokio::test]
async fn test_client_sends_accept_encoding_header_with_correct_values() {
let (aggregator, client) = setup_server_and_client();
aggregator.mock(|when, then| {
when.matches(|req| {
let headers = req.headers.clone().expect("HTTP headers not found");
let accept_encoding_header = headers
.iter()
.find(|(name, _values)| name.to_lowercase() == "accept-encoding")
.expect("Accept-Encoding header not found");

let header_value = accept_encoding_header.clone().1;
["gzip", "br", "deflate", "zstd"]
.iter()
.all(|&value| header_value.contains(value))
});

then.status(200).body("ok");
});

client
.get_content(AggregatorRequest::ListCertificates)
.await
.expect("GET request should succeed with Accept-Encoding header");
}

#[tokio::test]
async fn test_client_with_custom_headers_sends_accept_encoding_header_with_correct_values() {
let mut http_headers = HashMap::new();
http_headers.insert("Custom-Header".to_string(), "CustomValue".to_string());
let (aggregator, client) = setup_server_and_client_with_custom_headers(http_headers);
aggregator.mock(|when, then| {
when.matches(|req| {
let headers = req.headers.clone().expect("HTTP headers not found");
let accept_encoding_header = headers
.iter()
.find(|(name, _values)| name.to_lowercase() == "accept-encoding")
.expect("Accept-Encoding header not found");

let header_value = accept_encoding_header.clone().1;
["gzip", "br", "deflate", "zstd"]
.iter()
.all(|&value| header_value.contains(value))
});

then.status(200).body("ok");
});

client
.get_content(AggregatorRequest::ListCertificates)
.await
.expect("GET request should succeed with Accept-Encoding header");
}
}
2 changes: 1 addition & 1 deletion mithril-explorer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions mithril-relay/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-relay"
version = "0.1.33"
version = "0.1.34"
description = "A Mithril relay"
authors = { workspace = true }
edition = { workspace = true }
Expand Down Expand Up @@ -33,7 +33,13 @@ libp2p = { version = "0.54.1", features = [
] }
mithril-common = { path = "../mithril-common", features = ["full"] }
mithril-doc = { path = "../internal/mithril-doc" }
reqwest = { version = "0.12.12", features = ["json"] }
reqwest = { version = "0.12.12", features = [
"json",
"gzip",
"zstd",
"deflate",
"brotli"
] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
serde_yaml = "0.9.34"
Expand All @@ -48,6 +54,7 @@ tokio = { version = "1.43.0", features = ["full"] }
warp = "0.3.7"

[dev-dependencies]
httpmock = "0.7.0"
slog-scope = "4.4.0"
slog-term = "2.9.1"

Expand Down
39 changes: 39 additions & 0 deletions mithril-relay/src/relay/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,42 @@ impl AggregatorRelay {
self.peer.addr_peer.to_owned()
}
}

#[cfg(test)]
mod tests {
use httpmock::MockServer;

use crate::test_tools::TestLogger;

use super::*;

#[tokio::test]
async fn sends_accept_encoding_header_with_correct_values() {
let server = MockServer::start();
server.mock(|when, then| {
when.matches(|req| {
let headers = req.headers.clone().expect("HTTP headers not found");
let accept_encoding_header = headers
.iter()
.find(|(name, _values)| name.to_lowercase() == "accept-encoding")
.expect("Accept-Encoding header not found");

let header_value = accept_encoding_header.clone().1;
["gzip", "br", "deflate", "zstd"]
.iter()
.all(|&value| header_value.contains(value))
});

then.status(201).body("ok");
});
let addr: Multiaddr = "/ip4/0.0.0.0/tcp/0".parse().unwrap();
let relay = AggregatorRelay::start(&addr, &server.url(""), &TestLogger::stdout())
.await
.unwrap();

relay
.notify_signature_to_aggregator(&RegisterSignatureMessage::dummy())
.await
.expect("Should succeed with Accept-Encoding header");
}
}
Loading

0 comments on commit 52f97d4

Please sign in to comment.