feat(net): chose what to match on for request

Default match on method and url
This commit is contained in:
Paul Campbell 2024-11-06 21:22:25 +00:00
parent a3369ce890
commit b4ca82e85a
3 changed files with 88 additions and 15 deletions

View file

@ -7,7 +7,7 @@ mod result;
pub use result::{Error, Result};
pub use system::Net;
pub use system::{MatchOn, Net};
use system::{Mocked, Unmocked};

View file

@ -1,6 +1,6 @@
//
use std::marker::PhantomData;
//
use super::{Error, Result};
pub trait NetType {}
@ -12,10 +12,19 @@ impl NetType for Unmocked {}
type Plans = Vec<Plan>;
#[derive(Debug, PartialEq, Eq)]
pub enum MatchOn {
Method,
Url,
Body,
Headers,
}
#[derive(Debug)]
pub struct Plan {
request: reqwest::Request,
response: reqwest::Response,
match_on: Vec<MatchOn>,
}
pub struct Net<T: NetType> {
@ -50,15 +59,34 @@ impl Net<Mocked> {
pub 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()) {
// 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(request)) => plan.as_bytes() == request.as_bytes(),
_ => false,
}
&& plan.request.headers() == request.headers()
} else {
true
})
// HEADERS
&& (if plan.match_on.contains(&MatchOn::Headers) {
plan.request.headers() == request.headers()
} else {
true
})
});
match index {
Some(i) => Ok(self.plans.remove(i).response),
@ -70,11 +98,24 @@ impl Net<Mocked> {
}
pub fn on(&mut self, request: reqwest::Request) -> OnRequest {
OnRequest { net: self, request }
OnRequest {
net: self,
request,
match_on: vec![MatchOn::Method, MatchOn::Url],
}
}
fn _on(&mut self, request: reqwest::Request, response: reqwest::Response) {
self.plans.push(Plan { request, response })
fn _on(
&mut self,
request: reqwest::Request,
response: reqwest::Response,
match_on: Vec<MatchOn>,
) {
self.plans.push(Plan {
request,
response,
match_on,
})
}
}
impl<T: NetType> Drop for Net<T> {
@ -86,9 +127,13 @@ impl<T: NetType> Drop for Net<T> {
pub struct OnRequest<'net> {
net: &'net mut Net<Mocked>,
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}
}
pub fn respond(self, response: reqwest::Response) {
self.net._on(self.request, response)
self.net._on(self.request, response, self.match_on)
}
}

View file

@ -1,5 +1,5 @@
//
use kxio::net::Error;
use kxio::net::{Error, MatchOn};
type TestResult = Result<(), Error>;
@ -30,7 +30,7 @@ async fn test_get() -> TestResult {
}
#[tokio::test]
async fn test_post() -> TestResult {
async fn test_post() {
//given
let mut net = kxio::net::mock();
let client = net.client();
@ -46,13 +46,41 @@ async fn test_post() -> TestResult {
net.on(request).respond(my_response.into());
//when
let response = net.send(client.post(url)).await?;
let response = net.send(client.post(url)).await.expect("reponse");
//then
assert_eq!(response.status(), http::StatusCode::OK);
assert_eq!(response.bytes().await.expect("response body"), "Post OK");
}
Ok(())
#[tokio::test]
async fn test_post_by_url() {
//given
let mut net = kxio::net::mock();
let client = net.client();
let url = "https://www.example.com";
let request = client.post(url).build().expect("build request");
let my_response = net
.response()
.status(200)
.body("Post OK")
.expect("request body");
net.on(request)
.match_on(vec![
// MatchOn::Method,
MatchOn::Url
])
.respond(my_response.into());
//when
// This request is a GET, not POST - but should still match
let response = net.send(client.get(url)).await.expect("response");
//then
assert_eq!(response.status(), http::StatusCode::OK);
assert_eq!(response.bytes().await.expect("response body"), "Post OK");
}
#[tokio::test]