@@ -22,7 +22,7 @@ impl EncodingProposal {
22
22
pub fn new ( encoding : impl Into < Encoding > , weight : Option < f32 > ) -> crate :: Result < Self > {
23
23
if let Some ( weight) = weight {
24
24
ensure ! (
25
- weight < 0.0 || weight > 1.0 ,
25
+ weight. is_sign_positive ( ) && weight <= 1.0 ,
26
26
"EncodingProposal should have a weight between 0.0 and 1.0"
27
27
)
28
28
}
@@ -103,3 +103,29 @@ impl From<EncodingProposal> for HeaderValue {
103
103
unsafe { HeaderValue :: from_bytes_unchecked ( s. into_bytes ( ) ) }
104
104
}
105
105
}
106
+
107
+ #[ cfg( test) ]
108
+ mod test {
109
+ use super :: * ;
110
+
111
+ #[ test]
112
+ fn smoke ( ) -> crate :: Result < ( ) > {
113
+ let _ = EncodingProposal :: new ( Encoding :: Gzip , Some ( 0.0 ) ) . unwrap ( ) ;
114
+ let _ = EncodingProposal :: new ( Encoding :: Gzip , Some ( 0.5 ) ) . unwrap ( ) ;
115
+ let _ = EncodingProposal :: new ( Encoding :: Gzip , Some ( 1.0 ) ) . unwrap ( ) ;
116
+ Ok ( ( ) )
117
+ }
118
+
119
+ #[ test]
120
+ fn error_code_500 ( ) -> crate :: Result < ( ) > {
121
+ let err = EncodingProposal :: new ( Encoding :: Gzip , Some ( 1.1 ) ) . unwrap_err ( ) ;
122
+ assert_eq ! ( err. status( ) , 500 ) ;
123
+
124
+ let err = EncodingProposal :: new ( Encoding :: Gzip , Some ( -0.1 ) ) . unwrap_err ( ) ;
125
+ assert_eq ! ( err. status( ) , 500 ) ;
126
+
127
+ let err = EncodingProposal :: new ( Encoding :: Gzip , Some ( -0.0 ) ) . unwrap_err ( ) ;
128
+ assert_eq ! ( err. status( ) , 500 ) ;
129
+ Ok ( ( ) )
130
+ }
131
+ }
0 commit comments