From 621ef521f6723ba2d59beff05ff39ae8fd6df2c3 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Tue, 28 Apr 2015 08:46:26 +0200 Subject: [PATCH] fix(headers): Fix formatting of 0 qualites and formatting of empty list header fields. --- src/header/common/accept_encoding.rs | 21 ++------------------- src/header/common/allow.rs | 9 ++++----- src/header/parsing.rs | 9 ++++----- src/header/shared/quality_item.rs | 13 +++++++++---- 4 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/header/common/accept_encoding.rs b/src/header/common/accept_encoding.rs index ff9d475166..0441d2fd62 100644 --- a/src/header/common/accept_encoding.rs +++ b/src/header/common/accept_encoding.rs @@ -24,24 +24,7 @@ header! { test_header!(test3, vec![b"*"]); // Note: Removed quality 1 from gzip test_header!(test4, vec![b"compress;q=0.5, gzip"]); - // FIXME: Formatting of 0 as quality value - // test_header!(test5, vec![b"gzip;q=1.0, identity; q=0.5, *;q=0"]); - } -} - -#[cfg(test)] -mod tests { - use header::{Encoding, Header, qitem, Quality, QualityItem}; - - use super::*; - - #[test] - fn test_parse_header() { - let a: AcceptEncoding = Header::parse_header([b"gzip;q=1.0, identity; q=0.5".to_vec()].as_ref()).unwrap(); - let b = AcceptEncoding(vec![ - qitem(Encoding::Gzip), - QualityItem::new(Encoding::Identity, Quality(500)), - ]); - assert_eq!(a, b); + // Note: Removed quality 1 from gzip + test_header!(test5, vec![b"gzip, identity; q=0.5, *;q=0"]); } } diff --git a/src/header/common/allow.rs b/src/header/common/allow.rs index 778a807a2c..9db846cad8 100644 --- a/src/header/common/allow.rs +++ b/src/header/common/allow.rs @@ -35,11 +35,10 @@ header! { Method::Connect, Method::Patch, Method::Extension("fOObAr".to_string())]))); - // FIXME: Formatting fails - // test_header!( - // test3, - // vec![b""], - // Some(HeaderField(Vec::::new()))); + test_header!( + test3, + vec![b""], + Some(HeaderField(Vec::::new()))); } } diff --git a/src/header/parsing.rs b/src/header/parsing.rs index f1af86611c..1ee74e01eb 100644 --- a/src/header/parsing.rs +++ b/src/header/parsing.rs @@ -40,13 +40,12 @@ pub fn from_one_comma_delimited(raw: &[u8]) -> Option> { } /// Format an array into a comma-delimited string. -pub fn fmt_comma_delimited(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::Result { - let last = parts.len() - 1; +pub fn fmt_comma_delimited(f: &mut fmt::Formatter, parts: &[T]) -> fmt::Result { for (i, part) in parts.iter().enumerate() { - try!(write!(fmt, "{}", part)); - if i < last { - try!(write!(fmt, ", ")); + if i > 0 { + try!(write!(f, ", ")); } + try!(write!(f, "{}", part)); } Ok(()) } diff --git a/src/header/shared/quality_item.rs b/src/header/shared/quality_item.rs index b8223ba7e1..ebee49f836 100644 --- a/src/header/shared/quality_item.rs +++ b/src/header/shared/quality_item.rs @@ -24,10 +24,10 @@ pub struct Quality(pub u16); impl fmt::Display for Quality { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if self.0 == 1000 { - write!(f, "") - } else { - write!(f, "; q=0.{}", format!("{:03}", self.0).trim_right_matches('0')) + match self.0 { + 1000 => Ok(()), + 0 => f.write_str("; q=0"), + x => write!(f, "; q=0.{}", format!("{:03}", x).trim_right_matches('0')) } } } @@ -196,6 +196,11 @@ mod tests { assert_eq!(q(0.5), Quality(500)); } + #[test] + fn test_quality2() { + assert_eq!(format!("{}", q(0.0)), "; q=0"); + } + #[test] #[should_panic] fn test_quality_invalid() {