From 91112516fc26bdd0b7201030dbd9451107a5a19e Mon Sep 17 00:00:00 2001 From: Eric Kidd Date: Mon, 24 Nov 2014 07:43:05 -0500 Subject: [PATCH 1/2] Allow hyper::method::Method to be put in HashMap This is needed for porting iron/router to hyper. --- src/method.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/method.rs b/src/method.rs index af096f4a..8bd1b96e 100644 --- a/src/method.rs +++ b/src/method.rs @@ -13,7 +13,7 @@ use self::Method::{Options, Get, Post, Put, Delete, Head, Trace, Connect, Patch, /// /// It may make sense to grow this to include all variants currently /// registered with IANA, if they are at all common to use. -#[deriving(Clone, PartialEq)] +#[deriving(Clone, PartialEq, Eq, Hash)] pub enum Method { /// OPTIONS Options, From ccef7953f8c4de52a9d0310f741c03777b3a8bdb Mon Sep 17 00:00:00 2001 From: Eric Kidd Date: Mon, 24 Nov 2014 20:22:10 -0500 Subject: [PATCH 2/2] Add some basic tests for hyper::method::Method This test case does not attempt to test all the methods exhaustively, but it does at least test all the methods in the APIs. There's also a check to make sure that Method can be used as part of a hash-table key. --- src/method.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/method.rs b/src/method.rs index 8bd1b96e..0e0b89e1 100644 --- a/src/method.rs +++ b/src/method.rs @@ -100,3 +100,45 @@ impl fmt::Show for Method { }.fmt(fmt) } } + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + use std::str::FromStr; + use super::Method; + use super::Method::{Get, Post, Put, Extension}; + + #[test] + fn test_safe() { + assert_eq!(true, Get.safe()); + assert_eq!(false, Post.safe()); + } + + #[test] + fn test_idempotent() { + assert_eq!(true, Get.idempotent()); + assert_eq!(true, Put.idempotent()); + assert_eq!(false, Post.idempotent()); + } + + #[test] + fn test_from_str() { + assert_eq!(Some(Get), FromStr::from_str("GET")); + assert_eq!(Some(Extension("MOVE".to_string())), + FromStr::from_str("MOVE")); + } + + #[test] + fn test_fmt() { + assert_eq!("GET".to_string(), format!("{}", Get)); + assert_eq!("MOVE".to_string(), + format!("{}", Extension("MOVE".to_string()))); + } + + #[test] + fn test_hashable() { + let mut counter: HashMap = HashMap::new(); + counter.insert(Get, 1); + assert_eq!(Some(&1), counter.get(&Get)); + } +}