2024-11-04 10:22:31 +00:00
|
|
|
//
|
2024-11-10 09:44:30 +00:00
|
|
|
use std::{marker::PhantomData, sync::RwLock};
|
|
|
|
|
|
|
|
use reqwest::Client;
|
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-10 17:11:54 +00:00
|
|
|
/// An abstraction for the network
|
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
|
|
|
}
|
|
|
|
|
2024-11-10 09:44:30 +00:00
|
|
|
pub async fn send(
|
|
|
|
&self,
|
|
|
|
request: impl Into<reqwest::RequestBuilder>,
|
|
|
|
) -> Result<reqwest::Response> {
|
2024-11-09 12:12:11 +00:00
|
|
|
match &self.mock {
|
|
|
|
Some(mock) => mock.send(request).await,
|
|
|
|
None => self.inner.send(request).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-10 17:11:54 +00:00
|
|
|
impl Default for Net {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
inner: InnerNet::<Unmocked>::new(),
|
|
|
|
mock: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-10 09:44:30 +00:00
|
|
|
impl TryFrom<Net> for MockNet {
|
|
|
|
type Error = super::Error;
|
|
|
|
|
|
|
|
fn try_from(net: Net) -> std::result::Result<Self, Self::Error> {
|
|
|
|
match net.mock {
|
|
|
|
Some(inner_mock) => Ok(MockNet { inner: inner_mock }),
|
|
|
|
None => Err(Self::Error::NetIsNotAMock),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-09 12:12:11 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MockNet {
|
|
|
|
inner: InnerNet<Mocked>,
|
|
|
|
}
|
2024-11-10 09:44:30 +00:00
|
|
|
impl MockNet {
|
|
|
|
pub fn client(&self) -> Client {
|
|
|
|
Default::default()
|
|
|
|
}
|
2024-11-10 17:11:54 +00:00
|
|
|
pub fn on(&self, request: impl Into<reqwest::RequestBuilder>) -> OnRequest {
|
2024-11-10 09:44:30 +00:00
|
|
|
self.inner.on(request)
|
|
|
|
}
|
|
|
|
/// Creates a [http::response::Builder] to be extended and returned by a mocked network request.
|
|
|
|
pub fn response(&self) -> http::response::Builder {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
pub async fn send(
|
|
|
|
&self,
|
|
|
|
request: impl Into<reqwest::RequestBuilder>,
|
|
|
|
) -> Result<reqwest::Response> {
|
|
|
|
self.inner.send(request).await
|
|
|
|
}
|
|
|
|
pub fn reset(&self) -> Result<()> {
|
|
|
|
self.inner.reset()
|
2024-11-09 12:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<MockNet> for Net {
|
|
|
|
fn from(mock_net: MockNet) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: InnerNet::<Unmocked>::new(),
|
2024-11-10 09:44:30 +00:00
|
|
|
// keep the original `inner` around to allow it's Drop impelmentation to run when we go
|
|
|
|
// out of scope at the end of the test
|
2024-11-09 12:12:11 +00:00
|
|
|
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-10 09:44:30 +00:00
|
|
|
pub async fn send(
|
|
|
|
&self,
|
|
|
|
request: impl Into<reqwest::RequestBuilder>,
|
|
|
|
) -> Result<reqwest::Response> {
|
|
|
|
request.into().send().await.map_err(Error::from)
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-11-10 09:44:30 +00:00
|
|
|
pub async fn send(
|
|
|
|
&self,
|
|
|
|
request: impl Into<reqwest::RequestBuilder>,
|
|
|
|
) -> Result<reqwest::Response> {
|
|
|
|
let request = request.into().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-10 17:11:54 +00:00
|
|
|
pub fn on(&self, request_builder: impl Into<reqwest::RequestBuilder>) -> OnRequest {
|
|
|
|
match request_builder.into().build() {
|
|
|
|
Ok(request) => OnRequest::Valid {
|
|
|
|
net: self,
|
|
|
|
request,
|
|
|
|
match_on: vec![MatchOn::Method, MatchOn::Url],
|
|
|
|
},
|
|
|
|
Err(err) => OnRequest::Error(err.into()),
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 17:11:54 +00:00
|
|
|
pub enum OnRequest<'net> {
|
|
|
|
Valid {
|
|
|
|
net: &'net InnerNet<Mocked>,
|
|
|
|
request: reqwest::Request,
|
|
|
|
match_on: Vec<MatchOn>,
|
|
|
|
},
|
|
|
|
Error(super::Error),
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
impl<'net> OnRequest<'net> {
|
|
|
|
pub fn match_on(self, match_on: Vec<MatchOn>) -> Self {
|
2024-11-10 17:11:54 +00:00
|
|
|
if let OnRequest::Valid {
|
|
|
|
net,
|
|
|
|
request,
|
|
|
|
match_on: _,
|
|
|
|
} = self
|
|
|
|
{
|
|
|
|
Self::Valid {
|
|
|
|
net,
|
|
|
|
request,
|
|
|
|
match_on,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn respond(self, response: impl Into<http::response::Builder>) -> Result<()> {
|
|
|
|
match self {
|
|
|
|
OnRequest::Valid {
|
|
|
|
net: _,
|
|
|
|
request: _,
|
|
|
|
match_on: _,
|
|
|
|
} => self.respond_with_body(response.into().body("")),
|
|
|
|
OnRequest::Error(error) => Err(error),
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-10 17:11:54 +00:00
|
|
|
pub fn respond_with_body<T>(
|
|
|
|
self,
|
|
|
|
body_result: std::result::Result<http::Response<T>, http::Error>,
|
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
T: Into<reqwest::Body>,
|
|
|
|
{
|
|
|
|
match body_result {
|
|
|
|
Ok(response) => match self {
|
|
|
|
OnRequest::Valid {
|
|
|
|
net,
|
|
|
|
request,
|
|
|
|
match_on,
|
|
|
|
} => net._on(request, response.into(), match_on),
|
|
|
|
OnRequest::Error(error) => Err(error),
|
|
|
|
},
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
}
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|