@@ -133,6 +133,42 @@ pub mod tests {
133133
134134 pub use super :: dns_resolver:: tests:: * ;
135135
136+ /// Gzip-compresses the given bytes.
137+ fn gzip_encode ( data : & [ u8 ] ) -> Vec < u8 > {
138+ use flate2:: { Compression , write:: GzEncoder } ;
139+ use std:: io:: Write ;
140+ let mut encoder = GzEncoder :: new ( Vec :: new ( ) , Compression :: fast ( ) ) ;
141+ encoder. write_all ( data) . unwrap ( ) ;
142+ encoder. finish ( ) . unwrap ( )
143+ }
144+
145+ #[ tokio:: test]
146+ async fn http_client_negotiates_gzip_compression ( ) -> anyhow:: Result < ( ) > {
147+ let server = httpmock:: MockServer :: start ( ) ;
148+ let body = serde_json:: json!( { "message" : "hello from gzip" } ) ;
149+ let body_bytes = serde_json:: to_vec ( & body) ?;
150+ let compressed = gzip_encode ( & body_bytes) ;
151+
152+ let mock = server. mock ( |when, then| {
153+ when. method ( httpmock:: Method :: GET )
154+ . path ( "/gzip-test" )
155+ . header_exists ( "accept-encoding" ) ;
156+ then. status ( 200 )
157+ . header ( "Content-Type" , "application/json" )
158+ . header ( "Content-Encoding" , "gzip" )
159+ . body ( compressed) ;
160+ } ) ;
161+
162+ let client = Client :: builder ( ) . build ( ) ?;
163+ let response = client. get ( server. url ( "/gzip-test" ) ) . send ( ) . await ?;
164+ let json: serde_json:: Value = response. json ( ) . await ?;
165+
166+ mock. assert ( ) ;
167+ assert_eq ! ( json, body) ;
168+
169+ Ok ( ( ) )
170+ }
171+
136172 #[ tokio:: test]
137173 async fn correctly_checks_public_web_urls ( ) -> anyhow:: Result < ( ) > {
138174 let public_network = Network :: new (
0 commit comments