Compare commits

..

11 commits

Author SHA1 Message Date
edf5af2e63 tests(fs): add more test 2024-11-08 18:00:59 +00:00
aad2531c9f tests(net): add more tests 2024-11-08 18:00:59 +00:00
fdb6c32fc7 build: remove unlinked files
UNLINK
2024-11-08 18:00:59 +00:00
cad98d2b67 build: ignore cargo-mutants output 2024-11-08 18:00:59 +00:00
b4ca82e85a feat(net): chose what to match on for request
Default match on method and url
2024-11-08 18:00:59 +00:00
a3369ce890 refactor(net): rename OnRequest::response as respond 2024-11-08 18:00:59 +00:00
3b2610d6c9 test(net): add test to check that all expected requests are made 2024-11-08 18:00:59 +00:00
82cfaf337d test(net): add the first tests 2024-11-08 18:00:59 +00:00
e6fbfb90f0 feat(network)!: remove legacy network interface 2024-11-08 18:00:59 +00:00
821b61084c Use generics to separate mocked from unmocked 2024-11-08 18:00:59 +00:00
461e682212 feat!: fluent api for net
Some checks failed
Rust / build (map[name:stable]) (push) Failing after 7s
Rust / build (map[name:nightly]) (push) Failing after 16s
2024-11-08 18:00:59 +00:00

View file

@ -1,77 +0,0 @@
//
use super::{Error, Net, Result};
#[derive(Debug)]
struct Plan {
request: reqwest::Request,
response: reqwest::Response,
}
#[derive(Default, Debug)]
pub struct MockNet {
plans: Vec<Plan>,
}
impl MockNet {
pub(crate) fn new() -> Self {
Self::default()
}
pub fn into_net(self) -> Net {
Net::mock(self)
}
pub fn client(&self) -> reqwest::Client {
reqwest::Client::new()
}
pub fn response(&self) -> http::response::Builder {
http::Response::builder()
}
pub fn on(&mut self, request: reqwest::Request) -> OnRequest {
OnRequest {
mock: self,
request,
}
}
fn _on(&mut self, request: reqwest::Request, response: reqwest::Response) {
self.plans.push(Plan { request, response })
}
pub(crate) async fn send(
&mut self,
request: reqwest::RequestBuilder,
) -> Result<reqwest::Response> {
let request = request.build()?;
let index = self.plans.iter().position(|plan| {
// TODO: add support or only matching on selected criteria
plan.request.method() == request.method()
&& plan.request.url() == request.url()
&& match (plan.request.body(), request.body()) {
(None, None) => true,
(Some(plan), Some(request)) => plan.as_bytes() == request.as_bytes(),
_ => false,
}
&& plan.request.headers() == request.headers()
});
match index {
Some(i) => Ok(self.plans.remove(i).response),
None => Err(Error::UnexpectedMockRequest(request)),
}
}
pub fn assert(&self) -> Result<()> {
todo!()
}
}
pub struct OnRequest<'mock> {
mock: &'mock mut MockNet,
request: reqwest::Request,
}
impl<'mock> OnRequest<'mock> {
pub fn response(self, response: reqwest::Response) {
self.mock._on(self.request, response)
}
}