Compare commits

...

3 commits

Author SHA1 Message Date
e8a4b04533 feat(net): be more permisive in what parameters are accepted 2024-11-10 09:56:22 +00:00
e4c695501c doc(fs): minor tidy up broken links 2024-11-10 09:56:22 +00:00
f1cfae7153 fix(fs): make TempFileSystem public
Some checks failed
Rust / build (map[name:stable]) (push) Failing after 35s
Rust / build (map[name:nightly]) (push) Failing after 1m26s
2024-11-10 09:56:22 +00:00
4 changed files with 55 additions and 30 deletions

View file

@ -1,10 +1,10 @@
//! Provides an injectable reference to part of the filesystem.
//!
//! Create a new `FileSystem` to access a directory using `kxio::fs::new(path)`.
//! Create a new `TempFileSystem` to access a temporary directory using `kxio::fs::temp()?`;
//! Create a new [FileSystem] to access a directory using [crate::fs::new].
//! Create a new [TempFileSystem] to access a temporary directory using [crate::fs::temp()].
//!
//! `TempFileSystem` derefs automatically to `FileSystem` so can be used anywhere
//! you would use `FileSystem`.
//! [TempFileSystem] derefs automatically to [FileSystem] so can be used anywhere
//! you would use [FileSystem].
//!
//! ```
//! # use std::path::PathBuf;
@ -29,12 +29,12 @@
//!
//! # Standard library equivalents
//!
//! Given a `FileSystem` `fs`:
//! Given a [FileSystem] `fs`:
//!
//! ```no_run
//! let fs = kxio::fs::temp().expect("temp fs"); // for testing
//! // or
//! # let pathbuf = fs.base().join("foo").to_path_buf();
//! # let pathbuf = fs.base().join("foo");
//! let fs = kxio::fs::new(pathbuf);
//! ```
//!
@ -68,10 +68,11 @@ mod system;
mod temp;
pub use dir_item::{DirItem, DirItemIterator};
pub use path::*;
pub use path::{DirMarker, FileMarker, PathMarker, PathReal};
pub use reader::Reader;
pub use result::{Error, Result};
pub use system::{DirHandle, FileHandle, FileSystem, PathHandle};
pub use temp::TempFileSystem;
/// Creates a new `FileSystem` for the path.
///

View file

@ -9,6 +9,7 @@ pub enum Error {
#[display("Unexpected request: {0}", 0.to_string())]
UnexpectedMockRequest(reqwest::Request),
RwLockLocked,
Http(http::Error),
}
impl std::error::Error for Error {}
impl Clone for Error {

View file

@ -1,5 +1,7 @@
//
use std::{marker::PhantomData, ops::Deref, sync::RwLock};
use std::{marker::PhantomData, sync::RwLock};
use reqwest::Client;
use super::{Error, Result};
@ -56,7 +58,10 @@ impl Net {
Default::default()
}
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
pub async fn send(
&self,
request: impl Into<reqwest::RequestBuilder>,
) -> Result<reqwest::Response> {
match &self.mock {
Some(mock) => mock.send(request).await,
None => self.inner.send(request).await,
@ -68,17 +73,29 @@ impl Net {
pub struct MockNet {
inner: InnerNet<Mocked>,
}
impl Deref for MockNet {
type Target = InnerNet<Mocked>;
fn deref(&self) -> &Self::Target {
&self.inner
impl MockNet {
pub fn client(&self) -> Client {
self.inner.client()
}
pub fn on(&self, request: impl Into<reqwest::Request>) -> OnRequest {
self.inner.on(request)
}
pub fn response(&self) -> http::response::Builder {
self.inner.response()
}
pub async fn send(&self, request: impl Into<reqwest::RequestBuilder>) -> Result<reqwest::Response> {
self.inner.send(request).await
}
pub fn reset(&self) -> Result<()> {
self.inner.reset()
}
}
impl From<MockNet> for Net {
fn from(mock_net: MockNet) -> Self {
Self {
inner: InnerNet::<Unmocked>::new(),
// keep the original `inner` around to allow it's Drop impelmentation to run when we go
// out of scope at the end of the test
mock: Some(mock_net.inner),
}
}
@ -97,8 +114,11 @@ impl InnerNet<Unmocked> {
}
}
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
request.send().await.map_err(Error::from)
pub async fn send(
&self,
request: impl Into<reqwest::RequestBuilder>,
) -> Result<reqwest::Response> {
request.into().send().await.map_err(Error::from)
}
}
@ -115,8 +135,11 @@ impl InnerNet<Mocked> {
}
}
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
let request = request.build()?;
pub async fn send(
&self,
request: impl Into<reqwest::RequestBuilder>,
) -> Result<reqwest::Response> {
let request = request.into().build()?;
let read_plans = self.plans.read().map_err(|_| Error::RwLockLocked)?;
let index = read_plans.iter().position(|plan| {
// METHOD
@ -163,10 +186,10 @@ impl InnerNet<Mocked> {
Default::default()
}
pub fn on(&self, request: reqwest::Request) -> OnRequest {
pub fn on(&self, request: impl Into<reqwest::Request>) -> OnRequest {
OnRequest {
net: self,
request,
request: request.into(),
match_on: vec![MatchOn::Method, MatchOn::Url],
}
}
@ -214,7 +237,7 @@ impl<'net> OnRequest<'net> {
match_on,
}
}
pub fn respond(self, response: reqwest::Response) -> Result<()> {
self.net._on(self.request, response, self.match_on)
pub fn respond(self, response: impl Into<reqwest::Response>) -> Result<()> {
self.net._on(self.request, response.into(), self.match_on)
}
}

View file

@ -17,7 +17,7 @@ async fn test_get_url() {
.expect("request body");
net.on(request)
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when
@ -46,7 +46,7 @@ async fn test_get_wrong_url() {
.expect("request body");
net.on(request)
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when
@ -77,7 +77,7 @@ async fn test_post_url() {
.expect("request body");
net.on(request)
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when
@ -110,7 +110,7 @@ async fn test_post_by_method() {
MatchOn::Method,
// MatchOn::Url
])
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when
@ -144,7 +144,7 @@ async fn test_post_by_url() {
// MatchOn::Method,
MatchOn::Url,
])
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when
@ -183,7 +183,7 @@ async fn test_post_by_body() {
// MatchOn::Url
MatchOn::Body,
])
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when
@ -226,7 +226,7 @@ async fn test_post_by_headers() {
// MatchOn::Url
MatchOn::Headers,
])
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when
@ -265,7 +265,7 @@ async fn test_unused_post() {
.expect("request body");
net.on(request)
.respond(my_response.into())
.respond(my_response)
.expect("on request, respond");
//when