test(header): test valid corner cases

Test corner cases that are easy to miss when coding such as equal sign
in the cookie value and empty and empty value.
This commit is contained in:
Yves Dorfsman
2017-04-29 13:36:55 -06:00
parent 34323962de
commit 276170fd1c

View File

@@ -46,8 +46,8 @@ impl Cookie {
/// This will remove all other instances with the same name, /// This will remove all other instances with the same name,
/// and insert the new value. /// and insert the new value.
pub fn set<K, V>(&mut self, key: K, value: V) pub fn set<K, V>(&mut self, key: K, value: V)
where K: Into<Cow<'static, str>>, where K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>
{ {
let key = key.into(); let key = key.into();
let value = value.into(); let value = value.into();
@@ -69,8 +69,8 @@ impl Cookie {
/// cookie.append("foo", "quux"); /// cookie.append("foo", "quux");
/// assert_eq!(cookie.to_string(), "foo=bar; foo=quux"); /// assert_eq!(cookie.to_string(), "foo=bar; foo=quux");
pub fn append<K, V>(&mut self, key: K, value: V) pub fn append<K, V>(&mut self, key: K, value: V)
where K: Into<Cow<'static, str>>, where K: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>
{ {
self.0.append(key.into(), value.into()); self.0.append(key.into(), value.into());
} }
@@ -158,7 +158,7 @@ impl fmt::Display for Cookie {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ::header::Header; use header::Header;
use super::Cookie; use super::Cookie;
#[test] #[test]
@@ -220,7 +220,26 @@ mod tests {
let parsed = Cookie::parse_header(&b" foo = bar;baz= quux ".to_vec().into()).unwrap(); let parsed = Cookie::parse_header(&b" foo = bar;baz= quux ".to_vec().into()).unwrap();
assert_eq!(cookie, parsed); assert_eq!(cookie, parsed);
let parsed = Cookie::parse_header(&vec![b"foo = bar".to_vec(),b"baz= quux ".to_vec()].into()).unwrap(); let parsed =
Cookie::parse_header(&vec![b"foo = bar".to_vec(), b"baz= quux ".to_vec()].into())
.unwrap();
assert_eq!(cookie, parsed);
let parsed = Cookie::parse_header(&b"foo=bar; baz=quux ; empty=".to_vec().into()).unwrap();
cookie.append("empty", "");
assert_eq!(cookie, parsed);
let mut cookie = Cookie::new();
let parsed = Cookie::parse_header(&b"middle=equals=in=the=middle".to_vec().into()).unwrap();
cookie.append("middle", "equals=in=the=middle");
assert_eq!(cookie, parsed);
let parsed =
Cookie::parse_header(&b"middle=equals=in=the=middle; double==2".to_vec().into())
.unwrap();
cookie.append("double", "=2");
assert_eq!(cookie, parsed); assert_eq!(cookie, parsed);
Cookie::parse_header(&b"foo;bar=baz;quux".to_vec().into()).unwrap_err(); Cookie::parse_header(&b"foo;bar=baz;quux".to_vec().into()).unwrap_err();
@@ -228,4 +247,6 @@ mod tests {
} }
} }
bench_header!(bench, Cookie, { vec![b"foo=bar; baz=quux".to_vec()] }); bench_header!(bench, Cookie, {
vec![b"foo=bar; baz=quux".to_vec()]
});