@@ -88,13 +88,14 @@ mod tests {
88
88
use super :: * ;
89
89
90
90
use crate :: compression:: predicate:: SizeAbove ;
91
- use crate :: test_helpers:: { Body , TowerHttpBodyExt } ;
91
+ use crate :: test_helpers:: { Body , WithTrailers } ;
92
92
93
93
use async_compression:: tokio:: write:: { BrotliDecoder , BrotliEncoder } ;
94
- use bytes:: BytesMut ;
95
94
use flate2:: read:: GzDecoder ;
96
95
use http:: header:: { ACCEPT_ENCODING , CONTENT_ENCODING , CONTENT_TYPE } ;
97
- use hyper:: { Error , Request , Response } ;
96
+ use http:: { HeaderMap , HeaderName , Request , Response } ;
97
+ use http_body_util:: BodyExt ;
98
+ use std:: convert:: Infallible ;
98
99
use std:: io:: Read ;
99
100
use std:: sync:: { Arc , RwLock } ;
100
101
use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
@@ -127,13 +128,9 @@ mod tests {
127
128
let res = svc. call ( req) . await . unwrap ( ) ;
128
129
129
130
// read the compressed body
130
- let mut body = res. into_body ( ) ;
131
- let mut data = BytesMut :: new ( ) ;
132
- while let Some ( chunk) = body. data ( ) . await {
133
- let chunk = chunk. unwrap ( ) ;
134
- data. extend_from_slice ( & chunk[ ..] ) ;
135
- }
136
- let compressed_data = data. freeze ( ) . to_vec ( ) ;
131
+ let collected = res. into_body ( ) . collect ( ) . await . unwrap ( ) ;
132
+ let trailers = collected. trailers ( ) . cloned ( ) . unwrap ( ) ;
133
+ let compressed_data = collected. to_bytes ( ) ;
137
134
138
135
// decompress the body
139
136
// doing this with flate2 as that is much easier than async-compression and blocking during
@@ -143,6 +140,9 @@ mod tests {
143
140
decoder. read_to_string ( & mut decompressed) . unwrap ( ) ;
144
141
145
142
assert_eq ! ( decompressed, "Hello, World!" ) ;
143
+
144
+ // trailers are maintained
145
+ assert_eq ! ( trailers[ "foo" ] , "bar" ) ;
146
146
}
147
147
148
148
#[ tokio:: test]
@@ -158,13 +158,8 @@ mod tests {
158
158
let res = svc. call ( req) . await . unwrap ( ) ;
159
159
160
160
// read the compressed body
161
- let mut body = res. into_body ( ) ;
162
- let mut data = BytesMut :: new ( ) ;
163
- while let Some ( chunk) = body. data ( ) . await {
164
- let chunk = chunk. unwrap ( ) ;
165
- data. extend_from_slice ( & chunk[ ..] ) ;
166
- }
167
- let compressed_data = data. freeze ( ) . to_vec ( ) ;
161
+ let body = res. into_body ( ) ;
162
+ let compressed_data = body. collect ( ) . await . unwrap ( ) . to_bytes ( ) ;
168
163
169
164
// decompress the body
170
165
let decompressed = zstd:: stream:: decode_all ( std:: io:: Cursor :: new ( compressed_data) ) . unwrap ( ) ;
@@ -215,12 +210,8 @@ mod tests {
215
210
) ;
216
211
217
212
// read the compressed body
218
- let mut body = res. into_body ( ) ;
219
- let mut data = BytesMut :: new ( ) ;
220
- while let Some ( chunk) = body. data ( ) . await {
221
- let chunk = chunk. unwrap ( ) ;
222
- data. extend_from_slice ( & chunk[ ..] ) ;
223
- }
213
+ let body = res. into_body ( ) ;
214
+ let data = body. collect ( ) . await . unwrap ( ) . to_bytes ( ) ;
224
215
225
216
// decompress the body
226
217
let data = {
@@ -237,8 +228,11 @@ mod tests {
237
228
assert_eq ! ( data, DATA . as_bytes( ) ) ;
238
229
}
239
230
240
- async fn handle ( _req : Request < Body > ) -> Result < Response < Body > , Error > {
241
- Ok ( Response :: new ( Body :: from ( "Hello, World!" ) ) )
231
+ async fn handle ( _req : Request < Body > ) -> Result < Response < WithTrailers < Body > > , Infallible > {
232
+ let mut trailers = HeaderMap :: new ( ) ;
233
+ trailers. insert ( HeaderName :: from_static ( "foo" ) , "bar" . parse ( ) . unwrap ( ) ) ;
234
+ let body = Body :: from ( "Hello, World!" ) . with_trailers ( trailers) ;
235
+ Ok ( Response :: builder ( ) . body ( body) . unwrap ( ) )
242
236
}
243
237
244
238
#[ tokio:: test]
@@ -259,6 +253,7 @@ mod tests {
259
253
#[ derive( Default , Clone ) ]
260
254
struct EveryOtherResponse ( Arc < RwLock < u64 > > ) ;
261
255
256
+ #[ allow( clippy:: dbg_macro) ]
262
257
impl Predicate for EveryOtherResponse {
263
258
fn should_compress < B > ( & self , _: & http:: Response < B > ) -> bool
264
259
where
@@ -279,12 +274,8 @@ mod tests {
279
274
let res = svc. call ( req) . await . unwrap ( ) ;
280
275
281
276
// read the uncompressed body
282
- let mut body = res. into_body ( ) ;
283
- let mut data = BytesMut :: new ( ) ;
284
- while let Some ( chunk) = body. data ( ) . await {
285
- let chunk = chunk. unwrap ( ) ;
286
- data. extend_from_slice ( & chunk[ ..] ) ;
287
- }
277
+ let body = res. into_body ( ) ;
278
+ let data = body. collect ( ) . await . unwrap ( ) . to_bytes ( ) ;
288
279
let still_uncompressed = String :: from_utf8 ( data. to_vec ( ) ) . unwrap ( ) ;
289
280
assert_eq ! ( DATA , & still_uncompressed) ;
290
281
@@ -296,18 +287,14 @@ mod tests {
296
287
let res = svc. call ( req) . await . unwrap ( ) ;
297
288
298
289
// read the compressed body
299
- let mut body = res. into_body ( ) ;
300
- let mut data = BytesMut :: new ( ) ;
301
- while let Some ( chunk) = body. data ( ) . await {
302
- let chunk = chunk. unwrap ( ) ;
303
- data. extend_from_slice ( & chunk[ ..] ) ;
304
- }
290
+ let body = res. into_body ( ) ;
291
+ let data = body. collect ( ) . await . unwrap ( ) . to_bytes ( ) ;
305
292
assert ! ( String :: from_utf8( data. to_vec( ) ) . is_err( ) ) ;
306
293
}
307
294
308
295
#[ tokio:: test]
309
296
async fn doesnt_compress_images ( ) {
310
- async fn handle ( _req : Request < Body > ) -> Result < Response < Body > , Error > {
297
+ async fn handle ( _req : Request < Body > ) -> Result < Response < Body > , Infallible > {
311
298
let mut res = Response :: new ( Body :: from (
312
299
"a" . repeat ( ( SizeAbove :: DEFAULT_MIN_SIZE * 2 ) as usize ) ,
313
300
) ) ;
@@ -332,7 +319,7 @@ mod tests {
332
319
333
320
#[ tokio:: test]
334
321
async fn does_compress_svg ( ) {
335
- async fn handle ( _req : Request < Body > ) -> Result < Response < Body > , Error > {
322
+ async fn handle ( _req : Request < Body > ) -> Result < Response < Body > , Infallible > {
336
323
let mut res = Response :: new ( Body :: from (
337
324
"a" . repeat ( ( SizeAbove :: DEFAULT_MIN_SIZE * 2 ) as usize ) ,
338
325
) ) ;
@@ -377,13 +364,8 @@ mod tests {
377
364
let res = svc. call ( req) . await . unwrap ( ) ;
378
365
379
366
// read the compressed body
380
- let mut body = res. into_body ( ) ;
381
- let mut data = BytesMut :: new ( ) ;
382
- while let Some ( chunk) = body. data ( ) . await {
383
- let chunk = chunk. unwrap ( ) ;
384
- data. extend_from_slice ( & chunk[ ..] ) ;
385
- }
386
- let compressed_data = data. freeze ( ) . to_vec ( ) ;
367
+ let body = res. into_body ( ) ;
368
+ let compressed_data = body. collect ( ) . await . unwrap ( ) . to_bytes ( ) ;
387
369
388
370
// build the compressed body with the same quality level
389
371
let compressed_with_level = {
@@ -401,7 +383,7 @@ mod tests {
401
383
} ;
402
384
403
385
assert_eq ! (
404
- compressed_data. as_slice ( ) ,
386
+ compressed_data,
405
387
compressed_with_level. as_slice( ) ,
406
388
"Compression level is not respected"
407
389
) ;
0 commit comments