Skip to content

Commit 083feb4

Browse files
committed
chore: Unit test for gzip compression
1 parent 3ec851a commit 083feb4

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

opentelemetry-otlp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
5454
futures-util = { workspace = true }
5555
temp-env = { workspace = true }
5656
tonic = { workspace = true, features = ["router", "server"] }
57+
async-trait = { workspace = true }
5758

5859
[features]
5960
# telemetry pillars and functions

opentelemetry-otlp/src/exporter/http/mod.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,99 @@ mod tests {
810810
assert_eq!(url, "http://localhost:4318/v1/tracesbutnotreally");
811811
});
812812
}
813+
814+
#[cfg(feature = "gzip-http")]
815+
mod compression_tests {
816+
use super::super::OtlpHttpClient;
817+
use flate2::read::GzDecoder;
818+
use std::io::Read;
819+
use opentelemetry_http::{HttpClient, Bytes};
820+
821+
#[test]
822+
fn test_gzip_compression_and_decompression() {
823+
let client = OtlpHttpClient::new(
824+
std::sync::Arc::new(MockHttpClient),
825+
"http://localhost:4318".parse().unwrap(),
826+
std::collections::HashMap::new(),
827+
crate::Protocol::HttpBinary,
828+
std::time::Duration::from_secs(10),
829+
Some(crate::Compression::Gzip),
830+
);
831+
832+
// Test with some sample data
833+
let test_data = b"Hello, world! This is test data for compression.";
834+
let result = client.compress_body(test_data.to_vec()).unwrap();
835+
let (compressed_body, content_encoding) = result;
836+
837+
// Verify encoding header is set
838+
assert_eq!(content_encoding, Some("gzip"));
839+
840+
// Verify we can decompress the body
841+
let mut decoder = GzDecoder::new(&compressed_body[..]);
842+
let mut decompressed = Vec::new();
843+
decoder.read_to_end(&mut decompressed).unwrap();
844+
845+
// Verify decompressed data matches original
846+
assert_eq!(decompressed, test_data);
847+
// Verify compression actually happened (compressed should be different)
848+
assert_ne!(compressed_body, test_data.to_vec());
849+
}
850+
851+
#[test]
852+
fn test_no_compression_when_disabled() {
853+
let client = OtlpHttpClient::new(
854+
std::sync::Arc::new(MockHttpClient),
855+
"http://localhost:4318".parse().unwrap(),
856+
std::collections::HashMap::new(),
857+
crate::Protocol::HttpBinary,
858+
std::time::Duration::from_secs(10),
859+
None, // No compression
860+
);
861+
862+
let body = vec![1, 2, 3, 4];
863+
let result = client.compress_body(body.clone()).unwrap();
864+
let (result_body, content_encoding) = result;
865+
866+
// Body should be unchanged and no encoding header
867+
assert_eq!(result_body, body);
868+
assert_eq!(content_encoding, None);
869+
}
870+
871+
#[cfg(not(feature = "gzip-http"))]
872+
#[test]
873+
fn test_gzip_error_when_feature_disabled() {
874+
let client = OtlpHttpClient::new(
875+
std::sync::Arc::new(MockHttpClient),
876+
"http://localhost:4318".parse().unwrap(),
877+
std::collections::HashMap::new(),
878+
crate::Protocol::HttpBinary,
879+
std::time::Duration::from_secs(10),
880+
Some(crate::Compression::Gzip),
881+
);
882+
883+
let body = vec![1, 2, 3, 4];
884+
let result = client.compress_body(body);
885+
886+
// Should return error when gzip requested but feature not enabled
887+
assert!(result.is_err());
888+
assert!(result.unwrap_err().contains("gzip-http feature not enabled"));
889+
}
890+
891+
// Mock HTTP client for testing
892+
#[derive(Debug)]
893+
struct MockHttpClient;
894+
895+
#[async_trait::async_trait]
896+
impl HttpClient for MockHttpClient {
897+
async fn send_bytes(
898+
&self,
899+
_request: http::Request<Bytes>,
900+
) -> Result<http::Response<Bytes>, opentelemetry_http::HttpError> {
901+
Ok(http::Response::builder()
902+
.status(200)
903+
.body(Bytes::new())
904+
.unwrap())
905+
}
906+
}
907+
}
813908
}

0 commit comments

Comments
 (0)