use caramelo::{MatchType::ToHave, Matcher, TypedMatcher};
use crate::mock::Request;
pub trait AsMethod {
fn into_method(self) -> http::Method;
}
impl AsMethod for http::Method {
fn into_method(self) -> http::Method {
self
}
}
impl AsMethod for String {
fn into_method(self) -> http::Method {
self.as_str()
.into_method()
}
}
impl AsMethod for &str {
fn into_method(self) -> http::Method {
match self {
"GET" | "get" => http::Method::GET,
"POST" | "post" => http::Method::POST,
"PUT" | "put" => http::Method::PUT,
"DELETE" | "delete" => http::Method::DELETE,
"PATCH" | "patch" => http::Method::PATCH,
"HEAD" | "head" => http::Method::HEAD,
"OPTIONS" | "options" => http::Method::OPTIONS,
_ => panic!("Invalid method"),
}
}
}
pub fn method<M>(value: M) -> Method
where
M: AsMethod,
{
Method(value.into_method())
}
#[derive(Clone)]
pub struct Method(http::Method);
impl Matcher<Request> for Method {
fn matches(&self, value: &Request) -> bool {
self.0 == value.method()
}
fn description(&self) -> String {
format!("method matching {}", self.0)
}
}
impl TypedMatcher<Request> for Method {
fn matcher_type(&self) -> caramelo::MatchType {
ToHave
}
}