Compare commits
No commits in common. "e8a4b045333c8df9f26dc9272feba143d47e1f10" and "a0262a4d0533a779ec6272e9e488706f8c40cc0a" have entirely different histories.
e8a4b04533
...
a0262a4d05
4 changed files with 30 additions and 55 deletions
|
@ -1,10 +1,10 @@
|
||||||
//! Provides an injectable reference to part of the filesystem.
|
//! Provides an injectable reference to part of the filesystem.
|
||||||
//!
|
//!
|
||||||
//! Create a new [FileSystem] to access a directory using [crate::fs::new].
|
//! Create a new `FileSystem` to access a directory using `kxio::fs::new(path)`.
|
||||||
//! Create a new [TempFileSystem] to access a temporary directory using [crate::fs::temp()].
|
//! Create a new `TempFileSystem` to access a temporary directory using `kxio::fs::temp()?`;
|
||||||
//!
|
//!
|
||||||
//! [TempFileSystem] derefs automatically to [FileSystem] so can be used anywhere
|
//! `TempFileSystem` derefs automatically to `FileSystem` so can be used anywhere
|
||||||
//! you would use [FileSystem].
|
//! you would use `FileSystem`.
|
||||||
//!
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
//! # use std::path::PathBuf;
|
//! # use std::path::PathBuf;
|
||||||
|
@ -29,12 +29,12 @@
|
||||||
//!
|
//!
|
||||||
//! # Standard library equivalents
|
//! # Standard library equivalents
|
||||||
//!
|
//!
|
||||||
//! Given a [FileSystem] `fs`:
|
//! Given a `FileSystem` `fs`:
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! let fs = kxio::fs::temp().expect("temp fs"); // for testing
|
//! let fs = kxio::fs::temp().expect("temp fs"); // for testing
|
||||||
//! // or
|
//! // or
|
||||||
//! # let pathbuf = fs.base().join("foo");
|
//! # let pathbuf = fs.base().join("foo").to_path_buf();
|
||||||
//! let fs = kxio::fs::new(pathbuf);
|
//! let fs = kxio::fs::new(pathbuf);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
@ -68,11 +68,10 @@ mod system;
|
||||||
mod temp;
|
mod temp;
|
||||||
|
|
||||||
pub use dir_item::{DirItem, DirItemIterator};
|
pub use dir_item::{DirItem, DirItemIterator};
|
||||||
pub use path::{DirMarker, FileMarker, PathMarker, PathReal};
|
pub use path::*;
|
||||||
pub use reader::Reader;
|
pub use reader::Reader;
|
||||||
pub use result::{Error, Result};
|
pub use result::{Error, Result};
|
||||||
pub use system::{DirHandle, FileHandle, FileSystem, PathHandle};
|
pub use system::{DirHandle, FileHandle, FileSystem, PathHandle};
|
||||||
pub use temp::TempFileSystem;
|
|
||||||
|
|
||||||
/// Creates a new `FileSystem` for the path.
|
/// Creates a new `FileSystem` for the path.
|
||||||
///
|
///
|
||||||
|
|
|
@ -9,7 +9,6 @@ pub enum Error {
|
||||||
#[display("Unexpected request: {0}", 0.to_string())]
|
#[display("Unexpected request: {0}", 0.to_string())]
|
||||||
UnexpectedMockRequest(reqwest::Request),
|
UnexpectedMockRequest(reqwest::Request),
|
||||||
RwLockLocked,
|
RwLockLocked,
|
||||||
Http(http::Error),
|
|
||||||
}
|
}
|
||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
impl Clone for Error {
|
impl Clone for Error {
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
//
|
//
|
||||||
use std::{marker::PhantomData, sync::RwLock};
|
use std::{marker::PhantomData, ops::Deref, sync::RwLock};
|
||||||
|
|
||||||
use reqwest::Client;
|
|
||||||
|
|
||||||
use super::{Error, Result};
|
use super::{Error, Result};
|
||||||
|
|
||||||
|
@ -58,10 +56,7 @@ impl Net {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(
|
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
|
||||||
&self,
|
|
||||||
request: impl Into<reqwest::RequestBuilder>,
|
|
||||||
) -> Result<reqwest::Response> {
|
|
||||||
match &self.mock {
|
match &self.mock {
|
||||||
Some(mock) => mock.send(request).await,
|
Some(mock) => mock.send(request).await,
|
||||||
None => self.inner.send(request).await,
|
None => self.inner.send(request).await,
|
||||||
|
@ -73,29 +68,17 @@ impl Net {
|
||||||
pub struct MockNet {
|
pub struct MockNet {
|
||||||
inner: InnerNet<Mocked>,
|
inner: InnerNet<Mocked>,
|
||||||
}
|
}
|
||||||
impl MockNet {
|
impl Deref for MockNet {
|
||||||
pub fn client(&self) -> Client {
|
type Target = InnerNet<Mocked>;
|
||||||
self.inner.client()
|
|
||||||
}
|
fn deref(&self) -> &Self::Target {
|
||||||
pub fn on(&self, request: impl Into<reqwest::Request>) -> OnRequest {
|
&self.inner
|
||||||
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 {
|
impl From<MockNet> for Net {
|
||||||
fn from(mock_net: MockNet) -> Self {
|
fn from(mock_net: MockNet) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: InnerNet::<Unmocked>::new(),
|
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),
|
mock: Some(mock_net.inner),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,11 +97,8 @@ impl InnerNet<Unmocked> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(
|
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
|
||||||
&self,
|
request.send().await.map_err(Error::from)
|
||||||
request: impl Into<reqwest::RequestBuilder>,
|
|
||||||
) -> Result<reqwest::Response> {
|
|
||||||
request.into().send().await.map_err(Error::from)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,11 +115,8 @@ impl InnerNet<Mocked> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(
|
pub async fn send(&self, request: reqwest::RequestBuilder) -> Result<reqwest::Response> {
|
||||||
&self,
|
let request = request.build()?;
|
||||||
request: impl Into<reqwest::RequestBuilder>,
|
|
||||||
) -> Result<reqwest::Response> {
|
|
||||||
let request = request.into().build()?;
|
|
||||||
let read_plans = self.plans.read().map_err(|_| Error::RwLockLocked)?;
|
let read_plans = self.plans.read().map_err(|_| Error::RwLockLocked)?;
|
||||||
let index = read_plans.iter().position(|plan| {
|
let index = read_plans.iter().position(|plan| {
|
||||||
// METHOD
|
// METHOD
|
||||||
|
@ -186,10 +163,10 @@ impl InnerNet<Mocked> {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on(&self, request: impl Into<reqwest::Request>) -> OnRequest {
|
pub fn on(&self, request: reqwest::Request) -> OnRequest {
|
||||||
OnRequest {
|
OnRequest {
|
||||||
net: self,
|
net: self,
|
||||||
request: request.into(),
|
request,
|
||||||
match_on: vec![MatchOn::Method, MatchOn::Url],
|
match_on: vec![MatchOn::Method, MatchOn::Url],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,7 +214,7 @@ impl<'net> OnRequest<'net> {
|
||||||
match_on,
|
match_on,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn respond(self, response: impl Into<reqwest::Response>) -> Result<()> {
|
pub fn respond(self, response: reqwest::Response) -> Result<()> {
|
||||||
self.net._on(self.request, response.into(), self.match_on)
|
self.net._on(self.request, response, self.match_on)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
16
tests/net.rs
16
tests/net.rs
|
@ -17,7 +17,7 @@ async fn test_get_url() {
|
||||||
.expect("request body");
|
.expect("request body");
|
||||||
|
|
||||||
net.on(request)
|
net.on(request)
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -46,7 +46,7 @@ async fn test_get_wrong_url() {
|
||||||
.expect("request body");
|
.expect("request body");
|
||||||
|
|
||||||
net.on(request)
|
net.on(request)
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -77,7 +77,7 @@ async fn test_post_url() {
|
||||||
.expect("request body");
|
.expect("request body");
|
||||||
|
|
||||||
net.on(request)
|
net.on(request)
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -110,7 +110,7 @@ async fn test_post_by_method() {
|
||||||
MatchOn::Method,
|
MatchOn::Method,
|
||||||
// MatchOn::Url
|
// MatchOn::Url
|
||||||
])
|
])
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -144,7 +144,7 @@ async fn test_post_by_url() {
|
||||||
// MatchOn::Method,
|
// MatchOn::Method,
|
||||||
MatchOn::Url,
|
MatchOn::Url,
|
||||||
])
|
])
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -183,7 +183,7 @@ async fn test_post_by_body() {
|
||||||
// MatchOn::Url
|
// MatchOn::Url
|
||||||
MatchOn::Body,
|
MatchOn::Body,
|
||||||
])
|
])
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -226,7 +226,7 @@ async fn test_post_by_headers() {
|
||||||
// MatchOn::Url
|
// MatchOn::Url
|
||||||
MatchOn::Headers,
|
MatchOn::Headers,
|
||||||
])
|
])
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -265,7 +265,7 @@ async fn test_unused_post() {
|
||||||
.expect("request body");
|
.expect("request body");
|
||||||
|
|
||||||
net.on(request)
|
net.on(request)
|
||||||
.respond(my_response)
|
.respond(my_response.into())
|
||||||
.expect("on request, respond");
|
.expect("on request, respond");
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
|
Loading…
Reference in a new issue