@@ -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