Use generics to separate mocked from unmocked
This commit is contained in:
parent
461e682212
commit
821b61084c
4 changed files with 88 additions and 104 deletions
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,21 +2,21 @@
|
||||||
//!
|
//!
|
||||||
//!
|
//!
|
||||||
|
|
||||||
mod mock;
|
|
||||||
mod system;
|
mod system;
|
||||||
mod result;
|
mod result;
|
||||||
|
|
||||||
pub use result::{Error, Result};
|
pub use result::{Error, Result};
|
||||||
|
|
||||||
pub use system::Net;
|
pub use system::Net;
|
||||||
|
use system::{Mocked, Unmocked};
|
||||||
|
|
||||||
|
|
||||||
/// Creates a new `Net`.
|
/// Creates a new `Net`.
|
||||||
pub const fn new() -> Net {
|
pub const fn new() -> Net<Unmocked> {
|
||||||
Net::new()
|
Net::<Unmocked>::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new `MockNet` for use in tests.
|
/// Creates a new `MockNet` for use in tests.
|
||||||
pub fn mock() -> mock::MockNet {
|
pub fn mock() -> Net<Mocked> {
|
||||||
mock::MockNet::new()
|
Net::<Mocked>::new()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,93 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
//
|
//
|
||||||
use super::{mock::MockNet, Error};
|
use super::{Error, Result};
|
||||||
|
|
||||||
pub struct Net {
|
pub trait NetType {}
|
||||||
mock: Option<MockNet>,
|
|
||||||
}
|
|
||||||
impl Net {
|
|
||||||
pub(crate) const fn mock(mock: MockNet) -> Self {
|
|
||||||
Self { mock: Some(mock) }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
pub struct Mocked;
|
||||||
|
impl NetType for Mocked {}
|
||||||
|
pub struct Unmocked;
|
||||||
|
impl NetType for Unmocked {}
|
||||||
|
|
||||||
|
type Plans = Vec<Plan>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Plan {
|
||||||
|
request: reqwest::Request,
|
||||||
|
response: reqwest::Response,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Net {
|
pub struct Net<T: NetType> {
|
||||||
|
_type: PhantomData<T>,
|
||||||
|
plans: Plans,
|
||||||
|
}
|
||||||
|
impl Net<Unmocked> {
|
||||||
pub(crate) const fn new() -> Self {
|
pub(crate) const fn new() -> Self {
|
||||||
Self { mock: None }
|
Self {
|
||||||
|
_type: PhantomData,
|
||||||
|
plans: vec![],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn send(&mut self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
|
||||||
|
request.send().await.map_err(Error::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: NetType> Net<T> {
|
||||||
pub fn client(&self) -> reqwest::Client {
|
pub fn client(&self) -> reqwest::Client {
|
||||||
reqwest::Client::new()
|
reqwest::Client::new()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
pub async fn send(&mut self, request: reqwest::RequestBuilder) -> Result<reqwest::Response, Error> {
|
impl Net<Mocked> {
|
||||||
if let Some(mock) = &mut self.mock {
|
pub(crate) const fn new() -> Self {
|
||||||
mock.send(request).await
|
Self {
|
||||||
} else {
|
_type: PhantomData,
|
||||||
request.send().await.map_err(Error::from)
|
plans: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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()) {
|
||||||
|
(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 response(&self) -> http::response::Builder {
|
||||||
|
http::Response::builder()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on(&mut self, request: reqwest::Request) -> OnRequest {
|
||||||
|
OnRequest { net: self, request }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _on(&mut self, request: reqwest::Request, response: reqwest::Response) {
|
||||||
|
self.plans.push(Plan { request, response })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn assert(&self) -> Result<()> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct OnRequest<'net> {
|
||||||
|
net: &'net mut Net<Mocked>,
|
||||||
|
request: reqwest::Request,
|
||||||
|
}
|
||||||
|
impl<'net> OnRequest<'net> {
|
||||||
|
pub fn response(self, response: reqwest::Response) {
|
||||||
|
self.net._on(self.request, response)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,15 +8,14 @@ mod get {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test() -> TestResult {
|
async fn test() -> TestResult {
|
||||||
let mut net_mock = kxio::net::mock();
|
let mut net = kxio::net::mock();
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
let url = "https://www.example.com";
|
||||||
let request = net_mock.client().get(url).build()?;
|
let request = net.client().get(url).build()?;
|
||||||
let my_response = net_mock.response().status(200).body("OK").unwrap();
|
let my_response = net.response().status(200).body("OK").unwrap();
|
||||||
|
|
||||||
net_mock.on(request).response(my_response.clone().into());
|
net.on(request).response(my_response.clone().into());
|
||||||
|
|
||||||
let mut net = net_mock.into_net();
|
|
||||||
let client = net.client();
|
let client = net.client();
|
||||||
|
|
||||||
let response = net.send(client.get(url)).await?;
|
let response = net.send(client.get(url)).await?;
|
||||||
|
|
Loading…
Reference in a new issue