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"*"]); | ||||
|         // 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"]); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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::<Method>::new()))); | ||||
|         test_header!( | ||||
|             test3, | ||||
|             vec![b""], | ||||
|             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. | ||||
| pub fn fmt_comma_delimited<T: fmt::Display>(fmt: &mut fmt::Formatter, parts: &[T]) -> fmt::Result { | ||||
|     let last = parts.len() - 1; | ||||
| pub fn fmt_comma_delimited<T: fmt::Display>(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(()) | ||||
| } | ||||
|   | ||||
| @@ -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() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user