From 76cbf384231e602d888e49932bf9c4fafdd88051 Mon Sep 17 00:00:00 2001 From: Prabhjyot Singh Sodhi Date: Wed, 16 Sep 2015 11:45:42 +0530 Subject: [PATCH] feat(headers): add PartialEq impl for Headers struct compare the raw representations of the headers for the lack of a better alternative helpful when asserting HttpRequest/ HttpResponse in tests elsewhere --- src/header/mod.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/header/mod.rs b/src/header/mod.rs index acd7ee9439..fa12d1defd 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -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() { @@ -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) {