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

This commit is contained in:
Paul Campbell 2024-11-04 10:22:31 +00:00
parent e264bea729
commit 461e682212
7 changed files with 200 additions and 16 deletions

View file

@ -14,35 +14,24 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
# logging
tracing = "0.1"
# network
async-trait = "0.1" async-trait = "0.1"
derive_more = { version = "1.0", features = [ "from", "display", "constructor" ] }
http = "1.1" http = "1.1"
path-clean = "1.0"
reqwest = "0.12" reqwest = "0.12"
secrecy = "0.10" secrecy = "0.10"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
serde-xml-rs = "0.6" serde-xml-rs = "0.6"
thiserror = "2.0"
# fs
tempfile = "3.10" tempfile = "3.10"
path-clean = "1.0" thiserror = "2.0"
tracing = "0.1"
# boilerplate
derive_more = { version = "1.0.0-beta", features = [
"from",
"display",
"constructor",
] }
[dev-dependencies] [dev-dependencies]
# testing
assert2 = "0.3" assert2 = "0.3"
pretty_assertions = "1.4" pretty_assertions = "1.4"
test-log = "0.2" test-log = "0.2"
tokio = { version = "1.41", features = ["macros"] }
tokio-test = "0.4" tokio-test = "0.4"
[package.metadata.bin] [package.metadata.bin]

View file

@ -1,3 +1,6 @@
// //
pub mod fs; pub mod fs;
pub mod net;
#[deprecated]
pub mod network; pub mod network;

77
src/net/mock.rs Normal file
View file

@ -0,0 +1,77 @@
//
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)
}
}

22
src/net/mod.rs Normal file
View file

@ -0,0 +1,22 @@
//! Provides a generic interface for network operations.
//!
//!
mod mock;
mod system;
mod result;
pub use result::{Error, Result};
pub use system::Net;
/// Creates a new `Net`.
pub const fn new() -> Net {
Net::new()
}
/// Creates a new `MockNet` for use in tests.
pub fn mock() -> mock::MockNet {
mock::MockNet::new()
}

29
src/net/result.rs Normal file
View file

@ -0,0 +1,29 @@
//
use derive_more::derive::From;
/// Represents a error accessing the network.
#[derive(Debug, From, derive_more::Display)]
pub enum Error {
Reqwest(reqwest::Error),
Request(String),
#[display("Unexpected request: {0}", 0.to_string())]
UnexpectedMockRequest(reqwest::Request),
}
impl std::error::Error for Error {}
impl Clone for Error {
fn clone(&self) -> Self {
match self {
Self::Reqwest(req) => Self::Request(req.to_string()),
Self::Request(req) => Self::Request(req.clone()),
Self::UnexpectedMockRequest(_) => todo!(),
}
}
}
/// Represents a success or a failure.
///
/// Any failure is related to `std::io`, a Path Traversal
/// (i.e. trying to escape the base of the `FileSystem`),
/// or attempting to use a file as a directory or /vise versa/.
pub type Result<T> = core::result::Result<T, Error>;

31
src/net/system.rs Normal file
View file

@ -0,0 +1,31 @@
//
use super::{mock::MockNet, Error};
pub struct Net {
mock: Option<MockNet>,
}
impl Net {
pub(crate) const fn mock(mock: MockNet) -> Self {
Self { mock: Some(mock) }
}
}
impl Net {
pub(crate) const fn new() -> Self {
Self { mock: None }
}
pub fn client(&self) -> reqwest::Client {
reqwest::Client::new()
}
pub async fn send(&mut self, request: reqwest::RequestBuilder) -> Result<reqwest::Response, Error> {
if let Some(mock) = &mut self.mock {
mock.send(request).await
} else {
request.send().await.map_err(Error::from)
}
}
}

33
tests/net.rs Normal file
View file

@ -0,0 +1,33 @@
//
use kxio::net::Error;
type TestResult = Result<(), Error>;
mod get {
use super::*;
#[tokio::test]
async fn test() -> TestResult {
let mut net_mock = kxio::net::mock();
let url = "https://www.example.com";
let request = net_mock.client().get(url).build()?;
let my_response = net_mock.response().status(200).body("OK").unwrap();
net_mock.on(request).response(my_response.clone().into());
let mut net = net_mock.into_net();
let client = net.client();
let response = net.send(client.get(url)).await?;
assert_eq!(response.status(), my_response.status());
// net_mock.assert()?;
// let my_net = net::new();
// my_net.send(request).await?;
Ok(())
}
}