use crate::{
matchers::Matcher,
mock_builder::{Then, When},
request::Request,
response::Response,
};
const DEFAULT_PRIORITY: u8 = 5;
#[derive(Debug, PartialEq)]
pub struct Mock {
pub matchers: Vec<Box<dyn Matcher>>,
pub response: Response,
pub priority: u8,
}
impl Mock {
pub fn new<F>(f: F) -> Self
where
F: FnOnce(When, Then),
{
let when = When::new();
let then = Then::new();
f(when.clone(), then.clone());
Self {
matchers: when.into_inner(),
response: then.into_inner(),
priority: DEFAULT_PRIORITY,
}
}
pub fn with_priority(mut self, priority: u8) -> Self {
self.priority = priority;
self
}
pub fn response(&self) -> &Response {
&self.response
}
pub fn priority(&self) -> u8 {
self.priority
}
pub fn matches(&self, req: &Request) -> bool {
self.matchers.iter().all(|matcher| matcher.matches(req))
}
}
impl From<(Vec<Box<dyn Matcher>>, Response)> for Mock {
fn from(value: (Vec<Box<dyn Matcher>>, Response)) -> Self {
Self {
matchers: value.0,
response: value.1,
priority: DEFAULT_PRIORITY,
}
}
}