Skip to content

Commit

Permalink
fix(headers): Fix formatting of 0 qualites and formatting of empty li…
Browse files Browse the repository at this point in the history
…st header fields.
  • Loading branch information
pyfisch committed Apr 28, 2015
1 parent 29c8dd1 commit 621ef52
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 33 deletions.
21 changes: 2 additions & 19 deletions src/header/common/accept_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}
}
9 changes: 4 additions & 5 deletions src/header/common/allow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/header/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
13 changes: 9 additions & 4 deletions src/header/shared/quality_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}
}
}
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 621ef52

Please sign in to comment.