2024-11-04 10:22:31 +00:00
|
|
|
//
|
2024-11-09 12:12:11 +00:00
|
|
|
use std::{marker::PhantomData, ops::Deref, sync::RwLock};
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
use super::{Error, Result};
|
|
|
|
|
|
|
|
pub trait NetType {}
|
|
|
|
|
2024-11-09 12:12:11 +00:00
|
|
|
#[derive(Debug)]
|
2024-11-04 10:22:31 +00:00
|
|
|
pub struct Mocked;
|
|
|
|
impl NetType for Mocked {}
|
2024-11-09 12:12:11 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-11-04 10:22:31 +00:00
|
|
|
pub struct Unmocked;
|
|
|
|
impl NetType for Unmocked {}
|
|
|
|
|
|
|
|
type Plans = Vec<Plan>;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum MatchOn {
|
|
|
|
Method,
|
|
|
|
Url,
|
|
|
|
Body,
|
|
|
|
Headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-11-09 12:12:11 +00:00
|
|
|
struct Plan {
|
2024-11-04 10:22:31 +00:00
|
|
|
request: reqwest::Request,
|
|
|
|
response: reqwest::Response,
|
|
|
|
match_on: Vec<MatchOn>,
|
|
|
|
}
|
|
|
|
|
2024-11-09 12:13:06 +00:00
|
|
|
#[derive(Debug)]
|
2024-11-09 12:12:11 +00:00
|
|
|
pub struct Net {
|
|
|
|
inner: InnerNet<Unmocked>,
|
|
|
|
mock: Option<InnerNet<Mocked>>,
|
|
|
|
}
|
|
|
|
impl Net {
|
|
|
|
// constructors
|
|
|
|
pub(super) const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
inner: InnerNet::<Unmocked>::new(),
|
|
|
|
mock: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) const fn mock() -> MockNet {
|
|
|
|
MockNet {
|
|
|
|
inner: InnerNet::<Mocked>::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Net {
|
|
|
|
// public interface
|
|
|
|
pub fn client(&self) -> reqwest::Client {
|
2024-11-09 17:19:42 +00:00
|
|
|
Default::default()
|
2024-11-09 12:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
|
|
|
|
match &self.mock {
|
|
|
|
Some(mock) => mock.send(request).await,
|
|
|
|
None => self.inner.send(request).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MockNet {
|
|
|
|
inner: InnerNet<Mocked>,
|
|
|
|
}
|
|
|
|
impl Deref for MockNet {
|
|
|
|
type Target = InnerNet<Mocked>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<MockNet> for Net {
|
|
|
|
fn from(mock_net: MockNet) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: InnerNet::<Unmocked>::new(),
|
|
|
|
mock: Some(mock_net.inner),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InnerNet<T: NetType> {
|
2024-11-04 10:22:31 +00:00
|
|
|
_type: PhantomData<T>,
|
2024-11-09 12:13:06 +00:00
|
|
|
plans: RwLock<Plans>,
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
2024-11-09 12:12:11 +00:00
|
|
|
impl InnerNet<Unmocked> {
|
|
|
|
const fn new() -> Self {
|
2024-11-04 10:22:31 +00:00
|
|
|
Self {
|
|
|
|
_type: PhantomData,
|
2024-11-09 12:13:06 +00:00
|
|
|
plans: RwLock::new(vec![]),
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-09 12:13:06 +00:00
|
|
|
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
|
2024-11-04 10:22:31 +00:00
|
|
|
request.send().await.map_err(Error::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-09 12:12:11 +00:00
|
|
|
impl<T: NetType> InnerNet<T> {
|
2024-11-04 10:22:31 +00:00
|
|
|
pub fn client(&self) -> reqwest::Client {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 12:12:11 +00:00
|
|
|
impl InnerNet<Mocked> {
|
|
|
|
const fn new() -> Self {
|
2024-11-04 10:22:31 +00:00
|
|
|
Self {
|
|
|
|
_type: PhantomData,
|
2024-11-09 12:13:06 +00:00
|
|
|
plans: RwLock::new(vec![]),
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-09 12:13:06 +00:00
|
|
|
|
|
|
|
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
|
2024-11-04 10:22:31 +00:00
|
|
|
let request = request.build()?;
|
2024-11-09 12:13:06 +00:00
|
|
|
let read_plans = self.plans.read().map_err(|_| Error::RwLockLocked)?;
|
|
|
|
let index = read_plans.iter().position(|plan| {
|
2024-11-04 10:22:31 +00:00
|
|
|
// METHOD
|
|
|
|
(if plan.match_on.contains(&MatchOn::Method) {
|
|
|
|
plan.request.method() == request.method()
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
})
|
|
|
|
// URL
|
|
|
|
&& (if plan.match_on.contains(&MatchOn::Url) {
|
|
|
|
plan.request.url() == request.url()
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
})
|
|
|
|
// BODY
|
|
|
|
&& (if plan.match_on.contains(&MatchOn::Body) {
|
|
|
|
match (plan.request.body(), request.body()) {
|
|
|
|
(None, None) => true,
|
|
|
|
(Some(plan), Some(req)) => plan.as_bytes().eq(&req.as_bytes()),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
})
|
|
|
|
// HEADERS
|
|
|
|
&& (if plan.match_on.contains(&MatchOn::Headers) {
|
|
|
|
plan.request.headers() == request.headers()
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
})
|
|
|
|
});
|
2024-11-09 12:13:06 +00:00
|
|
|
drop(read_plans);
|
2024-11-04 10:22:31 +00:00
|
|
|
match index {
|
2024-11-09 12:13:06 +00:00
|
|
|
Some(i) => {
|
|
|
|
let mut write_plans = self.plans.write().map_err(|_| Error::RwLockLocked)?;
|
|
|
|
Ok(write_plans.remove(i).response)
|
|
|
|
}
|
2024-11-04 10:22:31 +00:00
|
|
|
None => Err(Error::UnexpectedMockRequest(request)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-09 12:13:06 +00:00
|
|
|
/// Creates a [http::response::Builder] to be extended and returned by a mocked network request.
|
2024-11-04 10:22:31 +00:00
|
|
|
pub fn response(&self) -> http::response::Builder {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
2024-11-09 12:13:06 +00:00
|
|
|
pub fn on(&self, request: reqwest::Request) -> OnRequest {
|
2024-11-04 10:22:31 +00:00
|
|
|
OnRequest {
|
|
|
|
net: self,
|
|
|
|
request,
|
|
|
|
match_on: vec![MatchOn::Method, MatchOn::Url],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _on(
|
2024-11-09 12:13:06 +00:00
|
|
|
&self,
|
2024-11-04 10:22:31 +00:00
|
|
|
request: reqwest::Request,
|
|
|
|
response: reqwest::Response,
|
|
|
|
match_on: Vec<MatchOn>,
|
2024-11-09 12:13:06 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
let mut write_plans = self.plans.write().map_err(|_| Error::RwLockLocked)?;
|
|
|
|
write_plans.push(Plan {
|
2024-11-04 10:22:31 +00:00
|
|
|
request,
|
|
|
|
response,
|
|
|
|
match_on,
|
2024-11-09 12:13:06 +00:00
|
|
|
});
|
|
|
|
Ok(())
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
2024-11-09 12:13:06 +00:00
|
|
|
pub fn reset(&self) -> Result<()> {
|
|
|
|
let mut write_plans = self.plans.write().map_err(|_| Error::RwLockLocked)?;
|
|
|
|
write_plans.clear();
|
|
|
|
Ok(())
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-09 12:12:11 +00:00
|
|
|
impl<T: NetType> Drop for InnerNet<T> {
|
2024-11-04 10:22:31 +00:00
|
|
|
fn drop(&mut self) {
|
2024-11-09 12:13:06 +00:00
|
|
|
let Ok(read_plans) = self.plans.read() else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
assert!(read_plans.is_empty())
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OnRequest<'net> {
|
2024-11-09 12:12:11 +00:00
|
|
|
net: &'net InnerNet<Mocked>,
|
2024-11-04 10:22:31 +00:00
|
|
|
request: reqwest::Request,
|
|
|
|
match_on: Vec<MatchOn>,
|
|
|
|
}
|
|
|
|
impl<'net> OnRequest<'net> {
|
|
|
|
pub fn match_on(self, match_on: Vec<MatchOn>) -> Self {
|
|
|
|
Self {
|
|
|
|
net: self.net,
|
|
|
|
request: self.request,
|
|
|
|
match_on,
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 12:13:06 +00:00
|
|
|
pub fn respond(self, response: reqwest::Response) -> Result<()> {
|
2024-11-04 10:22:31 +00:00
|
|
|
self.net._on(self.request, response, self.match_on)
|
|
|
|
}
|
|
|
|
}
|