fix(headers): Fix formatting of 0 qualites and formatting of empty list header fields.
This commit is contained in:
		| @@ -24,24 +24,7 @@ header! { | |||||||
|         test_header!(test3, vec![b"*"]); |         test_header!(test3, vec![b"*"]); | ||||||
|         // Note: Removed quality 1 from gzip |         // Note: Removed quality 1 from gzip | ||||||
|         test_header!(test4, vec![b"compress;q=0.5, gzip"]); |         test_header!(test4, vec![b"compress;q=0.5, gzip"]); | ||||||
|         // FIXME: Formatting of 0 as quality value |         // Note: Removed quality 1 from gzip | ||||||
|         // test_header!(test5, vec![b"gzip;q=1.0, identity; q=0.5, *;q=0"]); |         test_header!(test5, vec![b"gzip, 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); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -35,11 +35,10 @@ header! { | |||||||
|                 Method::Connect, |                 Method::Connect, | ||||||
|                 Method::Patch, |                 Method::Patch, | ||||||
|                 Method::Extension("fOObAr".to_string())]))); |                 Method::Extension("fOObAr".to_string())]))); | ||||||
|         // FIXME: Formatting fails |         test_header!( | ||||||
|         // test_header!( |             test3, | ||||||
|         //    test3, |             vec![b""], | ||||||
|         //    vec![b""], |             Some(HeaderField(Vec::<Method>::new()))); | ||||||
|         //    Some(HeaderField(Vec::<Method>::new()))); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -40,13 +40,12 @@ pub fn from_one_comma_delimited<T: str::FromStr>(raw: &[u8]) -> Option<Vec<T>> { | |||||||
| } | } | ||||||
|  |  | ||||||
| /// Format an array into a comma-delimited string. | /// Format an array into a comma-delimited string. | ||||||
| pub fn fmt_comma_delimited<T: fmt::Display>(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::Result { | pub fn fmt_comma_delimited<T: fmt::Display>(f: &mut fmt::Formatter, parts: &[T]) -> fmt::Result { | ||||||
|     let last = parts.len() - 1; |  | ||||||
|     for (i, part) in parts.iter().enumerate() { |     for (i, part) in parts.iter().enumerate() { | ||||||
|         try!(write!(fmt, "{}", part)); |         if i > 0 { | ||||||
|         if i < last { |             try!(write!(f, ", ")); | ||||||
|             try!(write!(fmt, ", ")); |  | ||||||
|         } |         } | ||||||
|  |         try!(write!(f, "{}", part)); | ||||||
|     } |     } | ||||||
|     Ok(()) |     Ok(()) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -24,10 +24,10 @@ pub struct Quality(pub u16); | |||||||
|  |  | ||||||
| impl fmt::Display for Quality { | impl fmt::Display for Quality { | ||||||
|     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
|         if self.0 == 1000 { |         match self.0 { | ||||||
|             write!(f, "") |             1000 => Ok(()), | ||||||
|         } else { |             0 => f.write_str("; q=0"), | ||||||
|             write!(f, "; q=0.{}", format!("{:03}", self.0).trim_right_matches('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)); |         assert_eq!(q(0.5), Quality(500)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_quality2() { | ||||||
|  |         assert_eq!(format!("{}", q(0.0)), "; q=0"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     #[should_panic] |     #[should_panic] | ||||||
|     fn test_quality_invalid() { |     fn test_quality_invalid() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user