@@ -86,20 +86,20 @@ impl AcceptEncoding {
86
86
/// # Errors
87
87
///
88
88
/// If no suitable encoding is found, an error with the status of `406` will be returned.
89
- pub fn negotiate ( & mut self , encodings : & [ Encoding ] ) -> crate :: Result < ContentEncoding > {
89
+ pub fn negotiate ( & mut self , available : & [ Encoding ] ) -> crate :: Result < ContentEncoding > {
90
90
// Start by ordering the encodings.
91
91
self . sort ( ) ;
92
92
93
93
// Try and find the first encoding that matches.
94
94
for encoding in & self . entries {
95
- if encodings . contains ( & encoding) {
95
+ if available . contains ( & encoding) {
96
96
return Ok ( encoding. into ( ) ) ;
97
97
}
98
98
}
99
99
100
100
// If no encoding matches and wildcard is set, send whichever encoding we got.
101
101
if self . wildcard {
102
- if let Some ( encoding) = encodings . iter ( ) . next ( ) {
102
+ if let Some ( encoding) = available . iter ( ) . next ( ) {
103
103
return Ok ( encoding. into ( ) ) ;
104
104
}
105
105
}
@@ -352,15 +352,52 @@ mod test {
352
352
accept. push ( EncodingProposal :: new ( Encoding :: Gzip , None ) ?) ;
353
353
accept. push ( EncodingProposal :: new ( Encoding :: Brotli , Some ( 0.8 ) ) ?) ;
354
354
355
- let mut headers = Response :: new ( 200 ) ;
356
- accept. apply ( & mut headers ) ;
355
+ let mut res = Response :: new ( 200 ) ;
356
+ accept. apply ( & mut res ) ;
357
357
358
- let mut accept = AcceptEncoding :: from_headers ( headers ) ?. unwrap ( ) ;
358
+ let mut accept = AcceptEncoding :: from_headers ( res ) ?. unwrap ( ) ;
359
359
accept. sort ( ) ;
360
360
let mut accept = accept. iter ( ) ;
361
361
assert_eq ! ( accept. next( ) . unwrap( ) , Encoding :: Brotli ) ;
362
362
assert_eq ! ( accept. next( ) . unwrap( ) , Encoding :: Gzip ) ;
363
363
assert_eq ! ( accept. next( ) . unwrap( ) , Encoding :: Identity ) ;
364
364
Ok ( ( ) )
365
365
}
366
+
367
+ #[ test]
368
+ fn negotiate ( ) -> crate :: Result < ( ) > {
369
+ let mut accept = AcceptEncoding :: new ( ) ;
370
+ accept. push ( EncodingProposal :: new ( Encoding :: Brotli , Some ( 0.8 ) ) ?) ;
371
+ accept. push ( EncodingProposal :: new ( Encoding :: Gzip , Some ( 0.4 ) ) ?) ;
372
+ accept. push ( EncodingProposal :: new ( Encoding :: Identity , None ) ?) ;
373
+
374
+ assert_eq ! (
375
+ accept. negotiate( & [ Encoding :: Brotli , Encoding :: Gzip ] ) ?,
376
+ Encoding :: Brotli ,
377
+ ) ;
378
+ Ok ( ( ) )
379
+ }
380
+
381
+ #[ test]
382
+ fn negotiate_not_acceptable ( ) -> crate :: Result < ( ) > {
383
+ let mut accept = AcceptEncoding :: new ( ) ;
384
+ let err = accept. negotiate ( & [ Encoding :: Gzip ] ) . unwrap_err ( ) ;
385
+ assert_eq ! ( err. status( ) , 406 ) ;
386
+
387
+ let mut accept = AcceptEncoding :: new ( ) ;
388
+ accept. push ( EncodingProposal :: new ( Encoding :: Brotli , Some ( 0.8 ) ) ?) ;
389
+ let err = accept. negotiate ( & [ Encoding :: Gzip ] ) . unwrap_err ( ) ;
390
+ assert_eq ! ( err. status( ) , 406 ) ;
391
+ Ok ( ( ) )
392
+ }
393
+
394
+ #[ test]
395
+ fn negotiate_wildcard ( ) -> crate :: Result < ( ) > {
396
+ let mut accept = AcceptEncoding :: new ( ) ;
397
+ accept. push ( EncodingProposal :: new ( Encoding :: Brotli , Some ( 0.8 ) ) ?) ;
398
+ accept. set_wildcard ( true ) ;
399
+
400
+ assert_eq ! ( accept. negotiate( & [ Encoding :: Gzip ] ) ?, Encoding :: Gzip ) ;
401
+ Ok ( ( ) )
402
+ }
366
403
}
0 commit comments