Compare commits
3 commits
aad02be6cb
...
e8a4b04533
Author | SHA1 | Date | |
---|---|---|---|
e8a4b04533 | |||
e4c695501c | |||
f1cfae7153 |
3 changed files with 20 additions and 34 deletions
|
@ -110,44 +110,35 @@ mod tests {
|
|||
// When `net` goes out of scope it will check that all the expected network requests (see
|
||||
// `net.on(...)` below) were all made. If there are any that were not made, the test will
|
||||
// be failed. If you want to avoid this, then call `net.reset()` before your test ends.
|
||||
let mock_net: kxio::net::MockNet = kxio::net::mock();
|
||||
let net: kxio::net::MockNet = kxio::net::mock();
|
||||
let url = "http://localhost:8080";
|
||||
|
||||
// declare what response should be made for a given request
|
||||
let response: http::Response<&str> =
|
||||
mock_net.response().body("contents").expect("response body");
|
||||
let request = mock_net.client().get(url).build().expect("request");
|
||||
mock_net
|
||||
.on(request)
|
||||
net.response().body("contents").expect("response body");
|
||||
let request = net.client().get(url).build().expect("request");
|
||||
net.on(request)
|
||||
// By default, the METHOD and URL must match, equivalent to:
|
||||
//.match_on(vec![MatchOn::Method, MatchOn::Url])
|
||||
.respond(response)
|
||||
.respond(response.into())
|
||||
.expect("mock");
|
||||
|
||||
// Create a temporary directory that will be deleted with `fs` goes out of scope
|
||||
let fs = kxio::fs::temp().expect("temp fs");
|
||||
let file_path = fs.base().join("foo");
|
||||
|
||||
// Create a [Net] from the [MockNet] to pass to the system under tets
|
||||
let net = kxio::net::Net::from(mock_net);
|
||||
|
||||
//when
|
||||
// Pass the file sytsem and network abstractions to the code to be tested
|
||||
download_and_save_to_file(url, &file_path, &fs, &net)
|
||||
download_and_save_to_file(url, &file_path, &fs, &net.into())
|
||||
.await
|
||||
.expect("system under test");
|
||||
|
||||
//then
|
||||
// Read the file
|
||||
// Open a file and read it
|
||||
let file = fs.file(&file_path);
|
||||
let reader = file.reader().expect("reader");
|
||||
let contents = reader.as_str();
|
||||
|
||||
assert_eq!(contents, "contents");
|
||||
|
||||
// not needed for this test, but should it be needed, we can avoid checking for any
|
||||
// unconsumed request matches.
|
||||
let mock_net = kxio::net::MockNet::try_from(net).expect("recover mock");
|
||||
mock_net.reset().expect("reset mock");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ pub enum Error {
|
|||
UnexpectedMockRequest(reqwest::Request),
|
||||
RwLockLocked,
|
||||
Http(http::Error),
|
||||
NetIsNotAMock,
|
||||
}
|
||||
impl std::error::Error for Error {}
|
||||
impl Clone for Error {
|
||||
|
|
|
@ -68,16 +68,6 @@ impl Net {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl TryFrom<Net> for MockNet {
|
||||
type Error = super::Error;
|
||||
|
||||
fn try_from(net: Net) -> std::result::Result<Self, Self::Error> {
|
||||
match net.mock {
|
||||
Some(inner_mock) => Ok(MockNet { inner: inner_mock }),
|
||||
None => Err(Self::Error::NetIsNotAMock),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MockNet {
|
||||
|
@ -85,19 +75,15 @@ pub struct MockNet {
|
|||
}
|
||||
impl MockNet {
|
||||
pub fn client(&self) -> Client {
|
||||
Default::default()
|
||||
self.inner.client()
|
||||
}
|
||||
pub fn on(&self, request: impl Into<reqwest::Request>) -> OnRequest {
|
||||
self.inner.on(request)
|
||||
}
|
||||
/// Creates a [http::response::Builder] to be extended and returned by a mocked network request.
|
||||
pub fn response(&self) -> http::response::Builder {
|
||||
Default::default()
|
||||
self.inner.response()
|
||||
}
|
||||
pub async fn send(
|
||||
&self,
|
||||
request: impl Into<reqwest::RequestBuilder>,
|
||||
) -> Result<reqwest::Response> {
|
||||
pub async fn send(&self, request: impl Into<reqwest::RequestBuilder>) -> Result<reqwest::Response> {
|
||||
self.inner.send(request).await
|
||||
}
|
||||
pub fn reset(&self) -> Result<()> {
|
||||
|
@ -136,6 +122,11 @@ impl InnerNet<Unmocked> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: NetType> InnerNet<T> {
|
||||
pub fn client(&self) -> reqwest::Client {
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
impl InnerNet<Mocked> {
|
||||
const fn new() -> Self {
|
||||
Self {
|
||||
|
@ -190,6 +181,11 @@ impl InnerNet<Mocked> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates a [http::response::Builder] to be extended and returned by a mocked network request.
|
||||
pub fn response(&self) -> http::response::Builder {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
pub fn on(&self, request: impl Into<reqwest::Request>) -> OnRequest {
|
||||
OnRequest {
|
||||
net: self,
|
||||
|
|
Loading…
Reference in a new issue