Skip to content

Commit 3f15cfd

Browse files
committed
Add EncodingPropsosal tests
1 parent 291009e commit 3f15cfd

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/content/encoding_proposal.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl EncodingProposal {
2222
pub fn new(encoding: impl Into<Encoding>, weight: Option<f32>) -> crate::Result<Self> {
2323
if let Some(weight) = weight {
2424
ensure!(
25-
weight < 0.0 || weight > 1.0,
25+
weight.is_sign_positive() && weight <= 1.0,
2626
"EncodingProposal should have a weight between 0.0 and 1.0"
2727
)
2828
}
@@ -103,3 +103,29 @@ impl From<EncodingProposal> for HeaderValue {
103103
unsafe { HeaderValue::from_bytes_unchecked(s.into_bytes()) }
104104
}
105105
}
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

Comments
 (0)