Compare commits
No commits in common. "a6d93b9b145945675185824fbe9c6a6ce6024304" and "2ddc79d82679485bc80d8238e713a40557df3327" have entirely different histories.
a6d93b9b14
...
2ddc79d826
8 changed files with 179 additions and 509 deletions
|
@ -14,7 +14,6 @@ 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]
|
||||||
bytes = "1.8"
|
|
||||||
derive_more = { version = "1.0", features = [
|
derive_more = { version = "1.0", features = [
|
||||||
"constructor",
|
"constructor",
|
||||||
"display",
|
"display",
|
||||||
|
|
|
@ -61,6 +61,10 @@ async fn download_and_save_to_file(
|
||||||
) -> kxio::Result<()> {
|
) -> kxio::Result<()> {
|
||||||
println!("fetching: {url}");
|
println!("fetching: {url}");
|
||||||
|
|
||||||
|
// Uses the network abstraction to create a perfectly normal `reqwest::ResponseBuilder`.
|
||||||
|
// `kxio::net::RequestBuilder` is an alias.
|
||||||
|
let request: kxio::net::RequestBuilder = net.client().get(url);
|
||||||
|
|
||||||
// Rather than calling `.build().send()?` on the request, pass it to the `net`
|
// Rather than calling `.build().send()?` on the request, pass it to the `net`
|
||||||
// This allows the `net` to either make the network request as normal, or, if we are
|
// This allows the `net` to either make the network request as normal, or, if we are
|
||||||
// under test, to handle the request as the test dictates.
|
// under test, to handle the request as the test dictates.
|
||||||
|
@ -68,13 +72,7 @@ async fn download_and_save_to_file(
|
||||||
// a real network request being made, even under test conditions. Only ever use the
|
// a real network request being made, even under test conditions. Only ever use the
|
||||||
// `net.send(...)` function to keep your code testable.
|
// `net.send(...)` function to keep your code testable.
|
||||||
// `kxio::net::Response` is an alias for `reqwest::Response`.
|
// `kxio::net::Response` is an alias for `reqwest::Response`.
|
||||||
let response: kxio::net::Response = net.get(url).header("key", "value").send().await?;
|
let response: kxio::net::Response = net.send(request).await?;
|
||||||
// Other options:
|
|
||||||
// Uses the network abstraction to create a perfectly normal `reqwest::ResponseBuilder`.
|
|
||||||
// `kxio::net::RequestBuilder` is an alias.
|
|
||||||
// let response = net.send(net.client().get(url)).await?;
|
|
||||||
//
|
|
||||||
// let response = net.post(url).body("{}").send().await?;
|
|
||||||
|
|
||||||
let body = response.text().await?;
|
let body = response.text().await?;
|
||||||
println!("fetched {} bytes", body.bytes().len());
|
println!("fetched {} bytes", body.bytes().len());
|
||||||
|
@ -104,10 +102,10 @@ fn delete_file(file_path: &Path, fs: &kxio::fs::FileSystem) -> kxio::Result<()>
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use http::StatusCode;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
use kxio::net::{Method, Url};
|
||||||
|
|
||||||
// This test demonstrates how to use the `kxio` to test your program.
|
// This test demonstrates how to use the `kxio` to test your program.
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn should_save_remote_body() {
|
async fn should_save_remote_body() {
|
||||||
|
@ -120,12 +118,11 @@ mod tests {
|
||||||
let url = "http://localhost:8080";
|
let url = "http://localhost:8080";
|
||||||
|
|
||||||
// declare what response should be made for a given request
|
// declare what response should be made for a given request
|
||||||
|
let response = mock_net.response().body("contents").expect("response body");
|
||||||
mock_net
|
mock_net
|
||||||
.on()
|
.on(Method::GET)
|
||||||
.get(url)
|
.url(Url::parse(url).expect("parse url"))
|
||||||
.respond(StatusCode::OK)
|
.respond(response);
|
||||||
.body("contents")
|
|
||||||
.expect("valid mock");
|
|
||||||
|
|
||||||
// Create a temporary directory that will be deleted with `fs` goes out of scope
|
// Create a temporary directory that will be deleted with `fs` goes out of scope
|
||||||
let fs = kxio::fs::temp().expect("temp fs");
|
let fs = kxio::fs::temp().expect("temp fs");
|
||||||
|
|
|
@ -21,11 +21,6 @@ impl TempFileSystem {
|
||||||
_temp_dir: temp_dir,
|
_temp_dir: temp_dir,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a clone of the wrapped [FileSystem].
|
|
||||||
pub fn as_real(&self) -> FileSystem {
|
|
||||||
self.real.clone()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
impl std::ops::Deref for TempFileSystem {
|
impl std::ops::Deref for TempFileSystem {
|
||||||
type Target = FileSystem;
|
type Target = FileSystem;
|
||||||
|
|
|
@ -79,19 +79,28 @@
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use kxio::net;
|
//! use kxio::net;
|
||||||
//! use kxio::net::StatusCode;
|
//! use kxio::net::{Method, Url};
|
||||||
|
//!
|
||||||
//! # #[tokio::main]
|
//! # #[tokio::main]
|
||||||
//! # async fn main() -> net::Result<()> {
|
//! # async fn main() -> net::Result<()> {
|
||||||
//! # let mock_net = net::mock();
|
//! # let mock_net = net::mock();
|
||||||
//! mock_net.on().get("https://example.com")
|
//! mock_net.on(Method::GET)
|
||||||
//! .respond(StatusCode::OK).body("");
|
//! .url(Url::parse("https://example.com")?)
|
||||||
//! mock_net.on().get("https://example.com/foo")
|
//! .respond(mock_net.response().status(200).body("")?);
|
||||||
//! .respond(StatusCode::INTERNAL_SERVER_ERROR).body("Mocked response");
|
//! mock_net.on(Method::GET)
|
||||||
|
//! .url(Url::parse("https://example.com/foo")?)
|
||||||
|
//! .respond(mock_net.response().status(500).body("Mocked response")?);
|
||||||
//! # mock_net.reset();
|
//! # mock_net.reset();
|
||||||
//! # Ok(())
|
//! # Ok(())
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
//! All [MatchOn] options:
|
||||||
|
//! - [MatchOn::Method] (default)
|
||||||
|
//! - [MatchOn::Url] (default)
|
||||||
|
//! - [MatchOn::Headers]
|
||||||
|
//! - [MatchOn::Body].
|
||||||
|
//!
|
||||||
//! Once you have defined all your expected responses, convert the [MockNet] into a [Net].
|
//! Once you have defined all your expected responses, convert the [MockNet] into a [Net].
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
|
@ -146,14 +155,14 @@ mod system;
|
||||||
|
|
||||||
pub use result::{Error, Result};
|
pub use result::{Error, Result};
|
||||||
|
|
||||||
pub use system::{MockNet, Net};
|
pub use system::{MatchOn, MockNet, Net};
|
||||||
|
|
||||||
pub use http::HeaderMap;
|
pub use http::Method;
|
||||||
pub use http::StatusCode;
|
|
||||||
pub use reqwest::Client;
|
pub use reqwest::Client;
|
||||||
pub use reqwest::Request;
|
pub use reqwest::Request;
|
||||||
pub use reqwest::RequestBuilder;
|
pub use reqwest::RequestBuilder;
|
||||||
pub use reqwest::Response;
|
pub use reqwest::Response;
|
||||||
|
pub use url::Url;
|
||||||
|
|
||||||
/// Creates a new `Net`.
|
/// Creates a new `Net`.
|
||||||
pub const fn new() -> Net {
|
pub const fn new() -> Net {
|
||||||
|
|
|
@ -3,8 +3,6 @@ use derive_more::derive::From;
|
||||||
|
|
||||||
use crate::net::Request;
|
use crate::net::Request;
|
||||||
|
|
||||||
use super::system::MockError;
|
|
||||||
|
|
||||||
/// The Errors that may occur within [kxio::net][crate::net].
|
/// The Errors that may occur within [kxio::net][crate::net].
|
||||||
#[derive(Debug, From, derive_more::Display)]
|
#[derive(Debug, From, derive_more::Display)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
@ -34,10 +32,6 @@ pub enum Error {
|
||||||
|
|
||||||
/// Attempted to extract a [MockNet][super::MockNet] from a [Net][super::Net] that does not contain one.
|
/// Attempted to extract a [MockNet][super::MockNet] from a [Net][super::Net] that does not contain one.
|
||||||
NetIsNotAMock,
|
NetIsNotAMock,
|
||||||
|
|
||||||
InvalidMock(MockError),
|
|
||||||
|
|
||||||
MockResponseHasNoBody,
|
|
||||||
}
|
}
|
||||||
impl std::error::Error for Error {}
|
impl std::error::Error for Error {}
|
||||||
impl Clone for Error {
|
impl Clone for Error {
|
||||||
|
|
|
@ -1,35 +1,45 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
use std::{
|
use std::{cell::RefCell, ops::Deref, rc::Rc, sync::Arc};
|
||||||
cell::RefCell, collections::HashMap, marker::PhantomData, ops::Deref, rc::Rc, sync::Arc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use bytes::Bytes;
|
use reqwest::{Body, Client};
|
||||||
use derive_more::derive::{Display, From};
|
|
||||||
use http::StatusCode;
|
|
||||||
use reqwest::{Client, RequestBuilder};
|
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use url::Url;
|
|
||||||
|
|
||||||
use crate::net::{Request, Response};
|
use crate::net::{Method, Request, RequestBuilder, Response, Url};
|
||||||
|
|
||||||
use super::{Error, Result};
|
use super::{Error, Result};
|
||||||
|
|
||||||
/// A list of planned requests and responses
|
/// A list of planned requests and responses
|
||||||
type Plans = Vec<Plan>;
|
type Plans = Vec<Plan>;
|
||||||
|
|
||||||
|
/// The different ways to match a request.
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum MatchOn {
|
||||||
|
/// The request must have a specific HTTP Request Method.
|
||||||
|
Method,
|
||||||
|
|
||||||
|
/// The request must have a specific URL.
|
||||||
|
Url,
|
||||||
|
|
||||||
|
/// The request must have a specify HTTP Body.
|
||||||
|
Body,
|
||||||
|
|
||||||
|
/// The request must have a specific set of HTTP Headers.
|
||||||
|
Headers,
|
||||||
|
}
|
||||||
|
|
||||||
/// A planned request and the response to return
|
/// A planned request and the response to return
|
||||||
///
|
///
|
||||||
/// Contains a list of the criteria that a request must meet before being considered a match.
|
/// Contains a list of the criteria that a request must meet before being considered a match.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Plan {
|
struct Plan {
|
||||||
match_request: Vec<MatchRequest>,
|
match_request: Vec<MatchRequest>,
|
||||||
response: reqwest::Response,
|
response: Response,
|
||||||
}
|
}
|
||||||
impl Plan {
|
impl Plan {
|
||||||
fn matches(&self, request: &Request) -> bool {
|
fn matches(&self, request: &Request) -> bool {
|
||||||
self.match_request.iter().all(|criteria| match criteria {
|
self.match_request.iter().all(|criteria| match criteria {
|
||||||
MatchRequest::Method(method) => request.method() == http::Method::from(method),
|
MatchRequest::Method(method) => request.method() == method,
|
||||||
MatchRequest::Url(uri) => request.url() == uri,
|
MatchRequest::Url(uri) => request.url() == uri,
|
||||||
MatchRequest::Header { name, value } => {
|
MatchRequest::Header { name, value } => {
|
||||||
request
|
request
|
||||||
|
@ -43,7 +53,7 @@ impl Plan {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
MatchRequest::Body(body) => {
|
MatchRequest::Body(body) => {
|
||||||
request.body().and_then(reqwest::Body::as_bytes) == Some(body)
|
request.body().and_then(Body::as_bytes) == Some(body.as_bytes())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -71,7 +81,6 @@ impl Net {
|
||||||
/// let client = net.client();
|
/// let client = net.client();
|
||||||
/// let request = client.get("https://hyper.rs");
|
/// let request = client.get("https://hyper.rs");
|
||||||
/// ```
|
/// ```
|
||||||
#[must_use]
|
|
||||||
pub fn client(&self) -> Client {
|
pub fn client(&self) -> Client {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
@ -79,10 +88,6 @@ impl Net {
|
||||||
/// Constructs the Request and sends it to the target URL, returning a
|
/// Constructs the Request and sends it to the target URL, returning a
|
||||||
/// future Response.
|
/// future Response.
|
||||||
///
|
///
|
||||||
/// However, if this request is from a [Net] that was created from a [MockNet],
|
|
||||||
/// then the request will be matched and any stored response returned, or an
|
|
||||||
/// error if no matched request was found.
|
|
||||||
///
|
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// This method fails if there was an error while sending request,
|
/// This method fails if there was an error while sending request,
|
||||||
|
@ -119,42 +124,6 @@ impl Net {
|
||||||
None => Err(Error::UnexpectedMockRequest(request)),
|
None => Err(Error::UnexpectedMockRequest(request)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Starts building an http DELETE request for the URL.
|
|
||||||
#[must_use]
|
|
||||||
pub fn delete(&self, url: impl Into<String>) -> ReqBuilder {
|
|
||||||
ReqBuilder::new(self, NetMethod::Delete, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts building an http GET request for the URL.
|
|
||||||
#[must_use]
|
|
||||||
pub fn get(&self, url: impl Into<String>) -> ReqBuilder {
|
|
||||||
ReqBuilder::new(self, NetMethod::Get, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts building an http HEAD request for the URL.
|
|
||||||
#[must_use]
|
|
||||||
pub fn head(&self, url: impl Into<String>) -> ReqBuilder {
|
|
||||||
ReqBuilder::new(self, NetMethod::Head, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts building an http PATCH request for the URL.
|
|
||||||
#[must_use]
|
|
||||||
pub fn patch(&self, url: impl Into<String>) -> ReqBuilder {
|
|
||||||
ReqBuilder::new(self, NetMethod::Patch, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts building an http POST request for the URL.
|
|
||||||
#[must_use]
|
|
||||||
pub fn post(&self, url: impl Into<String>) -> ReqBuilder {
|
|
||||||
ReqBuilder::new(self, NetMethod::Post, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts building an http PUT request for the URL.
|
|
||||||
#[must_use]
|
|
||||||
pub fn put(&self, url: impl Into<String>) -> ReqBuilder {
|
|
||||||
ReqBuilder::new(self, NetMethod::Put, url)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
impl MockNet {
|
impl MockNet {
|
||||||
pub async fn try_from(net: Net) -> std::result::Result<Self, super::Error> {
|
pub async fn try_from(net: Net) -> std::result::Result<Self, super::Error> {
|
||||||
|
@ -167,111 +136,6 @@ impl MockNet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub enum NetMethod {
|
|
||||||
Delete,
|
|
||||||
Get,
|
|
||||||
Head,
|
|
||||||
Patch,
|
|
||||||
Post,
|
|
||||||
Put,
|
|
||||||
}
|
|
||||||
impl From<&NetMethod> for http::Method {
|
|
||||||
fn from(value: &NetMethod) -> Self {
|
|
||||||
match value {
|
|
||||||
NetMethod::Delete => http::Method::DELETE,
|
|
||||||
NetMethod::Get => http::Method::GET,
|
|
||||||
NetMethod::Head => http::Method::HEAD,
|
|
||||||
NetMethod::Patch => http::Method::PATCH,
|
|
||||||
NetMethod::Post => http::Method::POST,
|
|
||||||
NetMethod::Put => http::Method::PUT,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A builder for an http request.
|
|
||||||
pub struct ReqBuilder<'net> {
|
|
||||||
net: &'net Net,
|
|
||||||
url: String,
|
|
||||||
method: NetMethod,
|
|
||||||
headers: Vec<(String, String)>,
|
|
||||||
body: Option<Bytes>,
|
|
||||||
}
|
|
||||||
impl<'net> ReqBuilder<'net> {
|
|
||||||
#[must_use]
|
|
||||||
fn new(net: &'net Net, method: NetMethod, url: impl Into<String>) -> Self {
|
|
||||||
Self {
|
|
||||||
net,
|
|
||||||
url: url.into(),
|
|
||||||
method,
|
|
||||||
headers: vec![],
|
|
||||||
body: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Constructs the Request and sends it to the target URL, returning a
|
|
||||||
/// future Response.
|
|
||||||
///
|
|
||||||
/// However, if this request is from a [Net] that was created from a [MockNet],
|
|
||||||
/// then the request will be matched and any stored response returned, or an
|
|
||||||
/// error if no matched request was found.
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// This method fails if there was an error while sending request,
|
|
||||||
/// redirect loop was detected or redirect limit was exhausted.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```no_run
|
|
||||||
/// # use kxio::net::Result;
|
|
||||||
/// # async fn run() -> Result<()> {
|
|
||||||
/// let net = kxio::net::new();
|
|
||||||
/// let response = net.get("https://hyper.rs")
|
|
||||||
/// .header("foo", "bar")
|
|
||||||
/// .body("{}")
|
|
||||||
/// .send().await?;
|
|
||||||
/// # Ok(())
|
|
||||||
/// # }
|
|
||||||
/// ```
|
|
||||||
pub async fn send(self) -> Result<Response> {
|
|
||||||
let client = self.net.client();
|
|
||||||
// Method
|
|
||||||
let mut req = match self.method {
|
|
||||||
NetMethod::Delete => client.delete(self.url),
|
|
||||||
NetMethod::Get => client.get(self.url),
|
|
||||||
NetMethod::Head => client.head(self.url),
|
|
||||||
NetMethod::Patch => client.patch(self.url),
|
|
||||||
NetMethod::Post => client.post(self.url),
|
|
||||||
NetMethod::Put => client.put(self.url),
|
|
||||||
};
|
|
||||||
// Headers
|
|
||||||
for (name, value) in self.headers.into_iter() {
|
|
||||||
req = req.header(name, value);
|
|
||||||
}
|
|
||||||
// Body
|
|
||||||
if let Some(bytes) = self.body {
|
|
||||||
req = req.body(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.net.send(req).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Adds the header and value to the request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
|
|
||||||
self.headers.push((name.into(), value.into()));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the request body.
|
|
||||||
#[must_use]
|
|
||||||
pub fn body(mut self, bytes: impl Into<Bytes>) -> Self {
|
|
||||||
self.body = Some(bytes.into());
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A struct for defining the expected requests and their responses that should be made
|
/// A struct for defining the expected requests and their responses that should be made
|
||||||
/// during a test.
|
/// during a test.
|
||||||
///
|
///
|
||||||
|
@ -281,15 +145,16 @@ impl<'net> ReqBuilder<'net> {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
/// use kxio::net::{Method, Url};
|
||||||
/// # use kxio::net::Result;
|
/// # use kxio::net::Result;
|
||||||
/// use kxio::net::StatusCode;
|
|
||||||
/// # #[tokio::main]
|
/// # #[tokio::main]
|
||||||
/// # async fn run() -> Result<()> {
|
/// # async fn run() -> Result<()> {
|
||||||
/// let mock_net = kxio::net::mock();
|
/// let mock_net = kxio::net::mock();
|
||||||
/// let client = mock_net.client();
|
/// let client = mock_net.client();
|
||||||
/// // define an expected requet, and the response that should be returned
|
/// // define an expected requet, and the response that should be returned
|
||||||
/// mock_net.on().get("https://hyper.rs")
|
/// mock_net.on(Method::GET)
|
||||||
/// .respond(StatusCode::OK).body("Ok");
|
/// .url(Url::parse("https://hyper.rs")?)
|
||||||
|
/// .respond(mock_net.response().status(200).body("Ok")?);
|
||||||
/// let net: kxio::net::Net = mock_net.into();
|
/// let net: kxio::net::Net = mock_net.into();
|
||||||
/// // use 'net' in your program, by passing it as a reference
|
/// // use 'net' in your program, by passing it as a reference
|
||||||
///
|
///
|
||||||
|
@ -323,25 +188,30 @@ impl MockNet {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use kxio::net::StatusCode;
|
/// use kxio::net::{Method, Url};
|
||||||
/// # use kxio::net::Result;
|
/// # use kxio::net::Result;
|
||||||
/// # fn run() -> Result<()> {
|
/// # fn run() -> Result<()> {
|
||||||
/// let mock_net = kxio::net::mock();
|
/// let mock_net = kxio::net::mock();
|
||||||
/// let client = mock_net.client();
|
/// let client = mock_net.client();
|
||||||
/// mock_net.on().get("https://hyper.rs")
|
/// mock_net.on(Method::GET)
|
||||||
/// .respond(StatusCode::OK).body("Ok");
|
/// .url(Url::parse("https://hyper.rs")?)
|
||||||
|
/// .respond(mock_net.response().status(200).body("Ok")?);
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[must_use]
|
pub fn on(&self, method: impl Into<Method>) -> WhenRequest {
|
||||||
pub fn on(&self) -> WhenRequest<WhenBuildRequest> {
|
WhenRequest::new(self, method)
|
||||||
WhenRequest::new(self)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _when(&self, plan: Plan) {
|
fn _when(&self, plan: Plan) {
|
||||||
self.plans.borrow_mut().push(plan);
|
self.plans.borrow_mut().push(plan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a [http::response::Builder] to be extended and returned by a mocked network request.
|
||||||
|
pub fn response(&self) -> http::response::Builder {
|
||||||
|
Default::default()
|
||||||
|
}
|
||||||
|
|
||||||
/// Clears all the expected requests and responses from the [MockNet].
|
/// Clears all the expected requests and responses from the [MockNet].
|
||||||
///
|
///
|
||||||
/// When the [MockNet] goes out of scope it will assert that all expected requests and
|
/// When the [MockNet] goes out of scope it will assert that all expected requests and
|
||||||
|
@ -389,110 +259,23 @@ impl Drop for Net {
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum MatchRequest {
|
pub enum MatchRequest {
|
||||||
Method(NetMethod),
|
Method(Method),
|
||||||
Url(Url),
|
Url(Url),
|
||||||
Header { name: String, value: String },
|
Header { name: String, value: String },
|
||||||
Body(bytes::Bytes),
|
Body(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub enum RespondWith {
|
|
||||||
Status(StatusCode),
|
|
||||||
Header { name: String, value: String },
|
|
||||||
Body(bytes::Bytes),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Display, From)]
|
|
||||||
pub enum MockError {
|
|
||||||
#[display("url parse: {}", 0)]
|
|
||||||
UrlParse(#[from] url::ParseError),
|
|
||||||
}
|
|
||||||
impl std::error::Error for MockError {}
|
|
||||||
|
|
||||||
pub trait WhenState {}
|
|
||||||
|
|
||||||
pub struct WhenBuildRequest;
|
|
||||||
impl WhenState for WhenBuildRequest {}
|
|
||||||
|
|
||||||
pub struct WhenBuildResponse;
|
|
||||||
impl WhenState for WhenBuildResponse {}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct WhenRequest<'net, State>
|
pub struct WhenRequest<'net> {
|
||||||
where
|
|
||||||
State: WhenState,
|
|
||||||
{
|
|
||||||
_state: PhantomData<State>,
|
|
||||||
net: &'net MockNet,
|
net: &'net MockNet,
|
||||||
match_on: Vec<MatchRequest>,
|
match_on: Vec<MatchRequest>,
|
||||||
respond_with: Vec<RespondWith>,
|
|
||||||
error: Option<MockError>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'net> WhenRequest<'net, WhenBuildRequest> {
|
impl<'net> WhenRequest<'net> {
|
||||||
fn new(net: &'net MockNet) -> Self {
|
pub fn url(mut self, url: impl Into<Url>) -> Self {
|
||||||
Self {
|
self.match_on.push(MatchRequest::Url(url.into()));
|
||||||
_state: PhantomData,
|
|
||||||
net,
|
|
||||||
match_on: vec![],
|
|
||||||
respond_with: vec![],
|
|
||||||
error: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts mocking a GET http request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn get(self, url: impl Into<String>) -> Self {
|
|
||||||
self._url(NetMethod::Get, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts mocking a POST http request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn post(self, url: impl Into<String>) -> Self {
|
|
||||||
self._url(NetMethod::Post, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts mocking a PUT http request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn put(self, url: impl Into<String>) -> Self {
|
|
||||||
self._url(NetMethod::Put, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts mocking a DELETE http request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn delete(self, url: impl Into<String>) -> Self {
|
|
||||||
self._url(NetMethod::Delete, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts mocking a HEAD http request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn head(self, url: impl Into<String>) -> Self {
|
|
||||||
self._url(NetMethod::Head, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Starts mocking a PATCH http request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn patch(self, url: impl Into<String>) -> Self {
|
|
||||||
self._url(NetMethod::Patch, url)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn _url(mut self, method: NetMethod, url: impl Into<String>) -> Self {
|
|
||||||
self.match_on.push(MatchRequest::Method(method));
|
|
||||||
match Url::parse(&url.into()) {
|
|
||||||
Ok(url) => {
|
|
||||||
self.match_on.push(MatchRequest::Url(url));
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
self.error.replace(err.into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies a header that the mock will match against.
|
|
||||||
///
|
|
||||||
/// Any request that does not have this header will not match the mock.
|
|
||||||
#[must_use]
|
|
||||||
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
|
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
|
||||||
self.match_on.push(MatchRequest::Header {
|
self.match_on.push(MatchRequest::Header {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
|
@ -500,81 +283,25 @@ impl<'net> WhenRequest<'net, WhenBuildRequest> {
|
||||||
});
|
});
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
pub fn body(mut self, body: impl Into<String>) -> Self {
|
||||||
/// Specifies the body that the mock will match against.
|
|
||||||
///
|
|
||||||
/// Any request that does not have this body will not match the mock.
|
|
||||||
#[must_use]
|
|
||||||
pub fn body(mut self, body: impl Into<bytes::Bytes>) -> Self {
|
|
||||||
self.match_on.push(MatchRequest::Body(body.into()));
|
self.match_on.push(MatchRequest::Body(body.into()));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
pub fn respond<T>(self, response: http::Response<T>)
|
||||||
/// Specifies the http Status Code that will be returned for the matching request.
|
where
|
||||||
#[must_use]
|
T: Into<reqwest::Body>,
|
||||||
pub fn respond(self, status: StatusCode) -> WhenRequest<'net, WhenBuildResponse> {
|
{
|
||||||
WhenRequest::<WhenBuildResponse> {
|
|
||||||
_state: PhantomData,
|
|
||||||
net: self.net,
|
|
||||||
match_on: self.match_on,
|
|
||||||
respond_with: vec![RespondWith::Status(status)],
|
|
||||||
error: self.error,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<'net> WhenRequest<'net, WhenBuildResponse> {
|
|
||||||
/// Specifies a header that will be on the response sent for the matching request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
|
|
||||||
let name = name.into();
|
|
||||||
let value = value.into();
|
|
||||||
self.respond_with.push(RespondWith::Header { name, value });
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Specifies headers that will be on the response sent for the matching request.
|
|
||||||
#[must_use]
|
|
||||||
pub fn headers(mut self, headers: impl Into<HashMap<String, String>>) -> Self {
|
|
||||||
let h: HashMap<String, String> = headers.into();
|
|
||||||
for (name, value) in h.into_iter() {
|
|
||||||
self.respond_with.push(RespondWith::Header { name, value });
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Specifies the body of the response sent for the matching request.
|
|
||||||
pub fn body(mut self, body: impl Into<bytes::Bytes>) -> Result<()> {
|
|
||||||
self.respond_with.push(RespondWith::Body(body.into()));
|
|
||||||
self.mock()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Marks a response that has no body as complete.
|
|
||||||
pub fn mock(self) -> Result<()> {
|
|
||||||
if let Some(error) = self.error {
|
|
||||||
return Err(crate::net::Error::InvalidMock(error));
|
|
||||||
}
|
|
||||||
let mut builder = http::response::Builder::default();
|
|
||||||
let mut response_body = None;
|
|
||||||
for part in self.respond_with {
|
|
||||||
builder = match part {
|
|
||||||
RespondWith::Status(status) => builder.status(status),
|
|
||||||
RespondWith::Header { name, value } => builder.header(name, value),
|
|
||||||
RespondWith::Body(body) => {
|
|
||||||
response_body.replace(body);
|
|
||||||
builder
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let Some(body) = response_body else {
|
|
||||||
return Err(crate::net::Error::MockResponseHasNoBody);
|
|
||||||
};
|
|
||||||
let response = builder.body(body)?;
|
|
||||||
self.net._when(Plan {
|
self.net._when(Plan {
|
||||||
match_request: self.match_on,
|
match_request: self.match_on,
|
||||||
response: response.into(),
|
response: response.into(),
|
||||||
});
|
});
|
||||||
Ok(())
|
}
|
||||||
|
|
||||||
|
fn new(net: &'net MockNet, method: impl Into<Method>) -> Self {
|
||||||
|
Self {
|
||||||
|
net,
|
||||||
|
match_on: vec![MatchRequest::Method(method.into())],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,13 +9,6 @@ mod path {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn temp_as_real() {
|
|
||||||
let fs = fs::temp().expect("temp fs");
|
|
||||||
let read = fs.as_real();
|
|
||||||
|
|
||||||
assert_eq!(read.base(), fs.base());
|
|
||||||
}
|
|
||||||
mod is_link {
|
mod is_link {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
236
tests/net.rs
236
tests/net.rs
|
@ -1,8 +1,5 @@
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use http::StatusCode;
|
|
||||||
//
|
//
|
||||||
use kxio::net::{Error, MockNet, Net};
|
use kxio::net::{Error, Method, MockNet, Net, Url};
|
||||||
|
|
||||||
use assert2::let_assert;
|
use assert2::let_assert;
|
||||||
|
|
||||||
|
@ -10,141 +7,50 @@ use assert2::let_assert;
|
||||||
async fn test_get_url() {
|
async fn test_get_url() {
|
||||||
//given
|
//given
|
||||||
let mock_net = kxio::net::mock();
|
let mock_net = kxio::net::mock();
|
||||||
|
let client = mock_net.client();
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
let url = "https://www.example.com";
|
||||||
|
let my_response = mock_net
|
||||||
|
.response()
|
||||||
|
.status(200)
|
||||||
|
.body("Get OK")
|
||||||
|
.expect("body");
|
||||||
|
|
||||||
mock_net
|
mock_net
|
||||||
.on()
|
.on(Method::GET)
|
||||||
.get(url)
|
.url(Url::parse(url).expect("parse url"))
|
||||||
.respond(StatusCode::OK)
|
.respond(my_response);
|
||||||
.header("foo", "bar")
|
|
||||||
.headers(HashMap::new())
|
|
||||||
.body("Get OK");
|
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let response = Net::from(mock_net).get(url).send().await.expect("response");
|
let response = Net::from(mock_net)
|
||||||
|
.send(client.get(url))
|
||||||
|
.await
|
||||||
|
.expect("response");
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
assert_eq!(response.status(), http::StatusCode::OK);
|
||||||
assert_eq!(response.bytes().await.expect("response body"), "Get OK");
|
assert_eq!(response.bytes().await.expect("response body"), "Get OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_post_url() {
|
|
||||||
//given
|
|
||||||
let net = kxio::net::mock();
|
|
||||||
let client = net.client();
|
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
|
||||||
|
|
||||||
net.on().post(url).respond(StatusCode::OK).body("post OK");
|
|
||||||
|
|
||||||
//when
|
|
||||||
let response = Net::from(net)
|
|
||||||
.send(client.post(url))
|
|
||||||
.await
|
|
||||||
.expect("reponse");
|
|
||||||
|
|
||||||
//then
|
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
|
||||||
assert_eq!(response.bytes().await.expect("response body"), "post OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_put_url() {
|
|
||||||
//given
|
|
||||||
let net = kxio::net::mock();
|
|
||||||
let client = net.client();
|
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
|
||||||
|
|
||||||
net.on().put(url).respond(StatusCode::OK).body("put OK");
|
|
||||||
|
|
||||||
//when
|
|
||||||
let response = Net::from(net).send(client.put(url)).await.expect("reponse");
|
|
||||||
|
|
||||||
//then
|
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
|
||||||
assert_eq!(response.bytes().await.expect("response body"), "put OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_delete_url() {
|
|
||||||
//given
|
|
||||||
let net = kxio::net::mock();
|
|
||||||
let client = net.client();
|
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
|
||||||
|
|
||||||
net.on()
|
|
||||||
.delete(url)
|
|
||||||
.respond(StatusCode::OK)
|
|
||||||
.body("delete OK");
|
|
||||||
|
|
||||||
//when
|
|
||||||
let response = Net::from(net)
|
|
||||||
.send(client.delete(url))
|
|
||||||
.await
|
|
||||||
.expect("reponse");
|
|
||||||
|
|
||||||
//then
|
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
|
||||||
assert_eq!(response.bytes().await.expect("response body"), "delete OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_head_url() {
|
|
||||||
//given
|
|
||||||
let net = kxio::net::mock();
|
|
||||||
let client = net.client();
|
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
|
||||||
|
|
||||||
net.on().head(url).respond(StatusCode::OK).body("head OK");
|
|
||||||
|
|
||||||
//when
|
|
||||||
let response = Net::from(net)
|
|
||||||
.send(client.head(url))
|
|
||||||
.await
|
|
||||||
.expect("reponse");
|
|
||||||
|
|
||||||
//then
|
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
|
||||||
assert_eq!(response.bytes().await.expect("response body"), "head OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_patch_url() {
|
|
||||||
//given
|
|
||||||
let net = kxio::net::mock();
|
|
||||||
let client = net.client();
|
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
|
||||||
|
|
||||||
net.on().patch(url).respond(StatusCode::OK).body("patch OK");
|
|
||||||
|
|
||||||
//when
|
|
||||||
let response = Net::from(net)
|
|
||||||
.send(client.patch(url))
|
|
||||||
.await
|
|
||||||
.expect("reponse");
|
|
||||||
|
|
||||||
//then
|
|
||||||
assert_eq!(response.status(), http::StatusCode::OK);
|
|
||||||
assert_eq!(response.bytes().await.expect("response body"), "patch OK");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_get_wrong_url() {
|
async fn test_get_wrong_url() {
|
||||||
//given
|
//given
|
||||||
let net = kxio::net::mock();
|
let mock_net = kxio::net::mock();
|
||||||
let client = net.client();
|
let client = mock_net.client();
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
let url = "https://www.example.com";
|
||||||
|
let my_response = mock_net
|
||||||
|
.response()
|
||||||
|
.status(200)
|
||||||
|
.body("Get OK")
|
||||||
|
.expect("body");
|
||||||
|
|
||||||
net.on().get(url).respond(StatusCode::OK).body("Get OK");
|
mock_net
|
||||||
|
.on(Method::GET)
|
||||||
|
.url(Url::parse(url).expect("parse url"))
|
||||||
|
.respond(my_response);
|
||||||
|
|
||||||
let net = Net::from(net);
|
let net = Net::from(mock_net);
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let_assert!(
|
let_assert!(
|
||||||
|
@ -160,14 +66,41 @@ async fn test_get_wrong_url() {
|
||||||
mock_net.reset();
|
mock_net.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_post_url() {
|
||||||
|
//given
|
||||||
|
let net = kxio::net::mock();
|
||||||
|
let client = net.client();
|
||||||
|
|
||||||
|
let url = "https://www.example.com";
|
||||||
|
let my_response = net.response().status(200).body("Post OK").expect("body");
|
||||||
|
|
||||||
|
net.on(Method::POST)
|
||||||
|
.url(Url::parse(url).expect("parse url"))
|
||||||
|
.respond(my_response);
|
||||||
|
|
||||||
|
//when
|
||||||
|
let response = Net::from(net)
|
||||||
|
.send(client.post(url))
|
||||||
|
.await
|
||||||
|
.expect("reponse");
|
||||||
|
|
||||||
|
//then
|
||||||
|
assert_eq!(response.status(), http::StatusCode::OK);
|
||||||
|
assert_eq!(response.bytes().await.expect("response body"), "Post OK");
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_post_by_method() {
|
async fn test_post_by_method() {
|
||||||
//given
|
//given
|
||||||
let net = kxio::net::mock();
|
let net = kxio::net::mock();
|
||||||
let client = net.client();
|
let client = net.client();
|
||||||
|
|
||||||
// NOTE: No URL specified - so should match any URL
|
let my_response = net.response().status(200).body("").expect("response body");
|
||||||
net.on().respond(StatusCode::OK).body("");
|
|
||||||
|
net.on(Method::POST)
|
||||||
|
// NOTE: No URL specified - so shou∂ match any URL
|
||||||
|
.respond(my_response);
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let response = Net::from(net)
|
let response = Net::from(net)
|
||||||
|
@ -186,11 +119,16 @@ async fn test_post_by_body() {
|
||||||
let net = kxio::net::mock();
|
let net = kxio::net::mock();
|
||||||
let client = net.client();
|
let client = net.client();
|
||||||
|
|
||||||
// No URL - so any POST with a matching body
|
let my_response = net
|
||||||
net.on()
|
.response()
|
||||||
|
.status(200)
|
||||||
|
.body("response body")
|
||||||
|
.expect("body");
|
||||||
|
|
||||||
|
net.on(Method::POST)
|
||||||
|
// No URL - so any POST with a matching body
|
||||||
.body("match on body")
|
.body("match on body")
|
||||||
.respond(StatusCode::OK)
|
.respond(my_response);
|
||||||
.body("response body");
|
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let response = Net::from(net)
|
let response = Net::from(net)
|
||||||
|
@ -212,10 +150,15 @@ async fn test_post_by_header() {
|
||||||
let net = kxio::net::mock();
|
let net = kxio::net::mock();
|
||||||
let client = net.client();
|
let client = net.client();
|
||||||
|
|
||||||
net.on()
|
let my_response = net
|
||||||
|
.response()
|
||||||
|
.status(200)
|
||||||
|
.body("response body")
|
||||||
|
.expect("body");
|
||||||
|
|
||||||
|
net.on(Method::POST)
|
||||||
.header("test", "match")
|
.header("test", "match")
|
||||||
.respond(StatusCode::OK)
|
.respond(my_response);
|
||||||
.body("response body");
|
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let response = Net::from(net)
|
let response = Net::from(net)
|
||||||
|
@ -242,11 +185,16 @@ async fn test_post_by_header_wrong_value() {
|
||||||
let mock_net = kxio::net::mock();
|
let mock_net = kxio::net::mock();
|
||||||
let client = mock_net.client();
|
let client = mock_net.client();
|
||||||
|
|
||||||
|
let my_response = mock_net
|
||||||
|
.response()
|
||||||
|
.status(200)
|
||||||
|
.body("response body")
|
||||||
|
.expect("body");
|
||||||
|
|
||||||
mock_net
|
mock_net
|
||||||
.on()
|
.on(Method::POST)
|
||||||
.header("test", "match")
|
.header("test", "match")
|
||||||
.respond(StatusCode::OK)
|
.respond(my_response);
|
||||||
.body("response body");
|
|
||||||
let net = Net::from(mock_net);
|
let net = Net::from(mock_net);
|
||||||
|
|
||||||
//when
|
//when
|
||||||
|
@ -272,12 +220,16 @@ async fn test_unused_post_as_net() {
|
||||||
let mock_net = kxio::net::mock();
|
let mock_net = kxio::net::mock();
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
let url = "https://www.example.com";
|
||||||
|
let my_response = mock_net
|
||||||
|
.response()
|
||||||
|
.status(200)
|
||||||
|
.body("Post OK")
|
||||||
|
.expect("body");
|
||||||
|
|
||||||
mock_net
|
mock_net
|
||||||
.on()
|
.on(Method::POST)
|
||||||
.post(url)
|
.url(Url::parse(url).expect("prase url"))
|
||||||
.respond(StatusCode::OK)
|
.respond(my_response);
|
||||||
.body("Post OK");
|
|
||||||
|
|
||||||
let _net = Net::from(mock_net);
|
let _net = Net::from(mock_net);
|
||||||
|
|
||||||
|
@ -296,12 +248,16 @@ async fn test_unused_post_as_mocknet() {
|
||||||
let mock_net = kxio::net::mock();
|
let mock_net = kxio::net::mock();
|
||||||
|
|
||||||
let url = "https://www.example.com";
|
let url = "https://www.example.com";
|
||||||
|
let my_response = mock_net
|
||||||
|
.response()
|
||||||
|
.status(200)
|
||||||
|
.body("Post OK")
|
||||||
|
.expect("body");
|
||||||
|
|
||||||
mock_net
|
mock_net
|
||||||
.on()
|
.on(Method::POST)
|
||||||
.post(url)
|
.url(Url::parse(url).expect("parse url"))
|
||||||
.respond(StatusCode::OK)
|
.respond(my_response);
|
||||||
.body("Post OK");
|
|
||||||
|
|
||||||
//when
|
//when
|
||||||
// don't send the planned request
|
// don't send the planned request
|
||||||
|
|
Loading…
Reference in a new issue