Skip to content

Commit

Permalink
feat(headers): add PartialEq impl for Headers struct
Browse files Browse the repository at this point in the history
compare the raw representations of the headers for the lack of a better alternative
helpful when asserting HttpRequest/ HttpResponse in tests elsewhere
  • Loading branch information
psdh committed Sep 17, 2015
1 parent 3d05a90 commit 76cbf38
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ impl Headers {
}
}

impl PartialEq for Headers {
fn eq(&self, other: &Headers) -> bool {
if self.len() != other.len() {
return false;
}

for header in self.iter() {
match other.get_raw(header.name()) {
Some(val) if val == self.get_raw(header.name()).unwrap() => {},
_ => { return false; }
}
}
return true;
}
}

impl fmt::Display for Headers {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for header in self.iter() {
Expand Down Expand Up @@ -695,6 +711,36 @@ mod tests {
}
}

#[test]
fn test_eq() {
let mut headers1 = Headers::new();
let mut headers2 = Headers::new();

assert_eq!(headers1, headers2);

headers1.set(ContentLength(11));
headers2.set(Host {hostname: "foo.bar".to_owned(), port: None});
assert!(headers1 != headers2);

headers1 = Headers::new();
headers2 = Headers::new();

headers1.set(ContentLength(11));
headers2.set(ContentLength(11));
assert_eq!(headers1, headers2);

headers1.set(ContentLength(10));
assert!(headers1 != headers2);

headers1 = Headers::new();
headers2 = Headers::new();

headers1.set(Host { hostname: "foo.bar".to_owned(), port: None });
headers1.set(ContentLength(11));
headers2.set(ContentLength(11));
assert!(headers1 != headers2);
}

#[cfg(feature = "nightly")]
#[bench]
fn bench_headers_new(b: &mut Bencher) {
Expand Down

0 comments on commit 76cbf38

Please sign in to comment.