use strum_macros::{Display, EnumString};
#[derive(Debug, Display, EnumString, PartialEq, Clone, Copy)]
#[strum(serialize_all = "UPPERCASE")]
pub enum HttpMethod {
GET,
POST,
DELETE,
PUT,
PATCH,
HEAD,
}
#[cfg(test)]
mod tests {
use crate::http::methods::HttpMethod;
use std::str::FromStr;
use strum::ParseError;
#[test]
fn test_str_to_http_method() {
assert_eq!(Ok(HttpMethod::GET), HttpMethod::from_str("GET"));
assert_eq!(Ok(HttpMethod::POST), HttpMethod::from_str("POST"));
assert_eq!(Ok(HttpMethod::DELETE), HttpMethod::from_str("DELETE"));
assert_eq!(Ok(HttpMethod::PUT), HttpMethod::from_str("PUT"));
assert_eq!(Ok(HttpMethod::PATCH), HttpMethod::from_str("PATCH"));
}
#[test]
fn test_str_to_http_method_when_lowercase() {
assert_eq!(
Err(ParseError::VariantNotFound),
HttpMethod::from_str("get")
);
assert_eq!(
Err(ParseError::VariantNotFound),
HttpMethod::from_str("post")
);
assert_eq!(
Err(ParseError::VariantNotFound),
HttpMethod::from_str("delete")
);
assert_eq!(
Err(ParseError::VariantNotFound),
HttpMethod::from_str("put")
);
assert_eq!(
Err(ParseError::VariantNotFound),
HttpMethod::from_str("patch")
);
}
#[test]
fn test_str_to_http_method_when_unknown_method() {
assert_eq!(
Err(ParseError::VariantNotFound),
HttpMethod::from_str("unknown")
);
assert_eq!(
Err(ParseError::VariantNotFound),
HttpMethod::from_str("UNKNOWN")
);
}
}