feat: Net and MockNet wrappers for InnerNet<Mocker> and InnerNet<Unmocked>
This commit is contained in:
parent
69c1ac8565
commit
ac3527ce90
3 changed files with 89 additions and 24 deletions
|
@ -7,15 +7,14 @@ mod system;
|
||||||
|
|
||||||
pub use result::{Error, Result};
|
pub use result::{Error, Result};
|
||||||
|
|
||||||
pub use system::{MatchOn, Net};
|
pub use system::{MatchOn, MockNet, Net};
|
||||||
use system::{Mocked, Unmocked};
|
|
||||||
|
|
||||||
/// Creates a new `Net`.
|
/// Creates a new `Net`.
|
||||||
pub const fn new() -> Net<Unmocked> {
|
pub const fn new() -> Net {
|
||||||
Net::<Unmocked>::new()
|
Net::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new `MockNet` for use in tests.
|
/// Creates a new `MockNet` for use in tests.
|
||||||
pub fn mock() -> Net<Mocked> {
|
pub fn mock() -> MockNet {
|
||||||
Net::<Mocked>::new()
|
Net::mock()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
//
|
//
|
||||||
use std::{marker::PhantomData, sync::RwLock};
|
use std::{marker::PhantomData, ops::Deref, sync::RwLock};
|
||||||
|
|
||||||
use super::{Error, Result};
|
use super::{Error, Result};
|
||||||
|
|
||||||
pub trait NetType {}
|
pub trait NetType {}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Mocked;
|
pub struct Mocked;
|
||||||
impl NetType for Mocked {}
|
impl NetType for Mocked {}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Unmocked;
|
pub struct Unmocked;
|
||||||
impl NetType for Unmocked {}
|
impl NetType for Unmocked {}
|
||||||
|
|
||||||
|
@ -21,19 +24,73 @@ pub enum MatchOn {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Plan {
|
struct Plan {
|
||||||
request: reqwest::Request,
|
request: reqwest::Request,
|
||||||
response: reqwest::Response,
|
response: reqwest::Response,
|
||||||
match_on: Vec<MatchOn>,
|
match_on: Vec<MatchOn>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Net<T: NetType> {
|
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 {
|
||||||
|
self.inner.client()
|
||||||
|
}
|
||||||
|
|
||||||
|
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> {
|
||||||
_type: PhantomData<T>,
|
_type: PhantomData<T>,
|
||||||
plans: RwLock<Plans>,
|
plans: RwLock<Plans>,
|
||||||
}
|
}
|
||||||
impl Net<Unmocked> {
|
impl InnerNet<Unmocked> {
|
||||||
pub(crate) const fn new() -> Self {
|
const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
_type: PhantomData,
|
_type: PhantomData,
|
||||||
plans: RwLock::new(vec![]),
|
plans: RwLock::new(vec![]),
|
||||||
|
@ -45,13 +102,13 @@ impl Net<Unmocked> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: NetType> Net<T> {
|
impl<T: NetType> InnerNet<T> {
|
||||||
pub fn client(&self) -> reqwest::Client {
|
pub fn client(&self) -> reqwest::Client {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Net<Mocked> {
|
impl InnerNet<Mocked> {
|
||||||
pub(crate) const fn new() -> Self {
|
const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
_type: PhantomData,
|
_type: PhantomData,
|
||||||
plans: RwLock::new(vec![]),
|
plans: RwLock::new(vec![]),
|
||||||
|
@ -135,7 +192,7 @@ impl Net<Mocked> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T: NetType> Drop for Net<T> {
|
impl<T: NetType> Drop for InnerNet<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let Ok(read_plans) = self.plans.read() else {
|
let Ok(read_plans) = self.plans.read() else {
|
||||||
return;
|
return;
|
||||||
|
@ -145,7 +202,7 @@ impl<T: NetType> Drop for Net<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct OnRequest<'net> {
|
pub struct OnRequest<'net> {
|
||||||
net: &'net Net<Mocked>,
|
net: &'net InnerNet<Mocked>,
|
||||||
request: reqwest::Request,
|
request: reqwest::Request,
|
||||||
match_on: Vec<MatchOn>,
|
match_on: Vec<MatchOn>,
|
||||||
}
|
}
|
||||||
|
|
25
tests/net.rs
25
tests/net.rs
|
@ -1,6 +1,6 @@
|
||||||
use assert2::let_assert;
|
use assert2::let_assert;
|
||||||
//
|
//
|
||||||
use kxio::net::{Error, MatchOn};
|
use kxio::net::{Error, MatchOn, Net};
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_url() {
|
async fn test_get_url() {
|
||||||
|
@ -21,7 +21,10 @@ async fn test_get_url() {
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let response = net.send(client.get(url)).await.expect("response");
|
let response = Net::from(net)
|
||||||
|
.send(client.get(url))
|
||||||
|
.await
|
||||||
|
.expect("response");
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
assert_eq!(response.status(), http::StatusCode::OK);
|
||||||
|
@ -78,7 +81,10 @@ async fn test_post_url() {
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let response = net.send(client.post(url)).await.expect("reponse");
|
let response = Net::from(net)
|
||||||
|
.send(client.post(url))
|
||||||
|
.await
|
||||||
|
.expect("reponse");
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
assert_eq!(response.status(), http::StatusCode::OK);
|
||||||
|
@ -109,7 +115,7 @@ async fn test_post_by_method() {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
// This request is a different url - but should still match
|
// This request is a different url - but should still match
|
||||||
let response = net
|
let response = Net::from(net)
|
||||||
.send(client.post("https://some.other.url"))
|
.send(client.post("https://some.other.url"))
|
||||||
.await
|
.await
|
||||||
.expect("response");
|
.expect("response");
|
||||||
|
@ -143,7 +149,10 @@ async fn test_post_by_url() {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
// This request is a GET, not POST - but should still match
|
// This request is a GET, not POST - but should still match
|
||||||
let response = net.send(client.get(url)).await.expect("response");
|
let response = Net::from(net)
|
||||||
|
.send(client.get(url))
|
||||||
|
.await
|
||||||
|
.expect("response");
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
assert_eq!(response.status(), http::StatusCode::OK);
|
||||||
|
@ -179,7 +188,7 @@ async fn test_post_by_body() {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
// This request is a GET, not POST - but should still match
|
// This request is a GET, not POST - but should still match
|
||||||
let response = net
|
let response = Net::from(net)
|
||||||
.send(client.get("https://some.other.url").body("match on body"))
|
.send(client.get("https://some.other.url").body("match on body"))
|
||||||
.await
|
.await
|
||||||
.expect("response");
|
.expect("response");
|
||||||
|
@ -222,7 +231,7 @@ async fn test_post_by_headers() {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
// This request is a GET, not POST - but should still match
|
// This request is a GET, not POST - but should still match
|
||||||
let response = net
|
let response = Net::from(net)
|
||||||
.send(
|
.send(
|
||||||
client
|
client
|
||||||
.get("https://some.other.url")
|
.get("https://some.other.url")
|
||||||
|
@ -261,7 +270,7 @@ async fn test_unused_post() {
|
||||||
|
|
||||||
//when
|
//when
|
||||||
// don't send the planned request
|
// don't send the planned request
|
||||||
// let _response = net.send(client.post(url)).await.expect("send");
|
// let _response = Net::from(net).send(client.post(url)).await.expect("send");
|
||||||
|
|
||||||
//then
|
//then
|
||||||
// Drop implementation for net should panic
|
// Drop implementation for net should panic
|
||||||
|
|
Loading…
Reference in a new issue