2024-11-17 11:24:28 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use http::StatusCode;
|
2024-11-04 10:22:31 +00:00
|
|
|
//
|
2024-11-17 09:18:18 +00:00
|
|
|
use kxio::net::{Error, MockNet, Net};
|
2024-11-15 09:06:45 +00:00
|
|
|
|
|
|
|
use assert2::let_assert;
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_url() {
|
|
|
|
//given
|
2024-11-12 07:13:59 +00:00
|
|
|
let mock_net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-12 07:13:59 +00:00
|
|
|
mock_net
|
2024-11-17 09:18:18 +00:00
|
|
|
.on()
|
2024-11-17 09:18:18 +00:00
|
|
|
.get(url)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
|
|
|
.header("foo", "bar")
|
|
|
|
.headers(HashMap::new())
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("Get OK")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-17 09:18:18 +00:00
|
|
|
let response = Net::from(mock_net).get(url).send().await.expect("response");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "Get OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-17 09:18:18 +00:00
|
|
|
async fn test_post_url() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-17 09:18:18 +00:00
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
|
|
|
.post(url)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("post OK")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
//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)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("put OK")
|
|
|
|
.expect("mock");
|
2024-11-11 07:44:43 +00:00
|
|
|
|
2024-11-04 10:22:31 +00:00
|
|
|
//when
|
2024-11-17 09:18:18 +00:00
|
|
|
let response = Net::from(net).send(client.put(url)).await.expect("reponse");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
2024-11-17 09:18:18 +00:00
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "put OK");
|
|
|
|
}
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
#[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)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("delete OK")
|
|
|
|
.expect("mock");
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
//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");
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-17 09:18:18 +00:00
|
|
|
async fn test_head_url() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
let client = net.client();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
|
|
|
.head(url)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("head OK")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-17 09:18:18 +00:00
|
|
|
.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)
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("patch OK")
|
|
|
|
.expect("mock");
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
let response = Net::from(net)
|
|
|
|
.send(client.patch(url))
|
2024-11-09 12:12:11 +00:00
|
|
|
.await
|
|
|
|
.expect("reponse");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
2024-11-17 09:18:18 +00:00
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "patch OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_get_wrong_url() {
|
|
|
|
//given
|
2024-11-17 11:24:28 +00:00
|
|
|
let net = kxio::net::mock();
|
|
|
|
let client = net.client();
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
net.on()
|
|
|
|
.get(url)
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("Get OK")
|
|
|
|
.expect("mock");
|
2024-11-17 09:18:18 +00:00
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
let net = Net::from(net);
|
2024-11-17 09:18:18 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
let_assert!(
|
|
|
|
Err(Error::UnexpectedMockRequest(invalid_request)) =
|
|
|
|
net.send(client.get("https://some.other.url/")).await
|
|
|
|
);
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(invalid_request.url().to_string(), "https://some.other.url/");
|
|
|
|
|
|
|
|
// remove pending unmatched request - we never meant to match against it
|
|
|
|
let mock_net = MockNet::try_from(net).await.expect("recover net");
|
|
|
|
mock_net.reset();
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_post_by_method() {
|
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
let client = net.client();
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
// NOTE: No URL specified - so should match any URL
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on().respond(StatusCode::OK).body("").expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-04 10:22:31 +00:00
|
|
|
.send(client.post("https://some.other.url"))
|
|
|
|
.await
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
2024-11-10 17:11:54 +00:00
|
|
|
assert_eq!(response.bytes().await.expect("response body"), "");
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-12 07:13:59 +00:00
|
|
|
async fn test_post_by_body() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
let client = net.client();
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
// No URL - so any POST with a matching body
|
2024-11-17 09:18:18 +00:00
|
|
|
net.on()
|
2024-11-12 07:13:59 +00:00
|
|
|
.body("match on body")
|
2024-11-17 11:24:28 +00:00
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("response body")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-12 07:13:59 +00:00
|
|
|
.send(client.post("https://some.other.url").body("match on body"))
|
2024-11-09 12:12:11 +00:00
|
|
|
.await
|
|
|
|
.expect("response");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
2024-11-12 07:13:59 +00:00
|
|
|
assert_eq!(
|
|
|
|
response.bytes().await.expect("response body"),
|
|
|
|
"response body"
|
|
|
|
);
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-12 07:13:59 +00:00
|
|
|
async fn test_post_by_header() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-09 12:13:06 +00:00
|
|
|
let net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
let client = net.client();
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
net.on()
|
|
|
|
.header("test", "match")
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("response body")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-09 12:12:11 +00:00
|
|
|
let response = Net::from(net)
|
2024-11-12 07:13:59 +00:00
|
|
|
.send(
|
|
|
|
client
|
|
|
|
.post("https://some.other.url")
|
|
|
|
.body("nay body")
|
|
|
|
.header("test", "match"),
|
|
|
|
)
|
2024-11-04 10:22:31 +00:00
|
|
|
.await
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
//then
|
|
|
|
assert_eq!(response.status(), http::StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
response.bytes().await.expect("response body"),
|
|
|
|
"response body"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-11-12 07:13:59 +00:00
|
|
|
async fn test_post_by_header_wrong_value() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-12 07:13:59 +00:00
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
let client = mock_net.client();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.header("test", "match")
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("response body")
|
|
|
|
.expect("mock");
|
2024-11-12 07:13:59 +00:00
|
|
|
let net = Net::from(mock_net);
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//when
|
2024-11-12 07:13:59 +00:00
|
|
|
let response = net
|
2024-11-04 10:22:31 +00:00
|
|
|
.send(
|
|
|
|
client
|
2024-11-12 07:13:59 +00:00
|
|
|
.post("https://some.other.url")
|
|
|
|
.body("nay body")
|
|
|
|
.header("test", "no match"),
|
2024-11-04 10:22:31 +00:00
|
|
|
)
|
2024-11-12 07:13:59 +00:00
|
|
|
.await;
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
2024-11-12 07:13:59 +00:00
|
|
|
let_assert!(Err(kxio::net::Error::UnexpectedMockRequest(_)) = response);
|
|
|
|
|
feat: Add Debug, Clone, Default, PartialEq, Eq, Send, Sync to as many or our types as possible.
- adds tokio::sync as a dependency to provide an async Mutex for Clone of Net and MockNet
## ƒs
- adds `Clone` to `DirItem`
- adds `Default`, `PartialEq` and `Eq` to `FileSystem`, `PathMarker`, `FileMarker` and `DirMarker`
- adds `Default` to `PathReal`
- adds `Clone`, `Debug`, `Default`, `PartialEq` and `Eq` to `Reader`
## net
- `MockNet::try_from` now returns a `Future`, so should be `await`ed
- adds `Debug` to `Plan`
- adds `Debug` and `Default` to `Net`
- adds `Debug`, `Clone` and `Default` to `MockNet`
- adds `Debug`, `Clone`, `PartialEq` and `Eq` to `MatchRequest`
- adds `Debug` and `Clone` to `WhenRequest`
WIP: mutation tests
2024-11-16 08:46:07 +00:00
|
|
|
MockNet::try_from(net).await.expect("recover mock").reset();
|
2024-11-04 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[should_panic]
|
2024-11-11 07:44:43 +00:00
|
|
|
async fn test_unused_post_as_net() {
|
2024-11-04 10:22:31 +00:00
|
|
|
//given
|
2024-11-11 07:44:43 +00:00
|
|
|
let mock_net = kxio::net::mock();
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.post(url)
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("Post OK")
|
|
|
|
.expect("mock");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
2024-11-11 07:44:43 +00:00
|
|
|
let _net = Net::from(mock_net);
|
|
|
|
|
2024-11-04 10:22:31 +00:00
|
|
|
//when
|
|
|
|
// don't send the planned request
|
2024-11-09 12:12:11 +00:00
|
|
|
// let _response = Net::from(net).send(client.post(url)).await.expect("send");
|
2024-11-04 10:22:31 +00:00
|
|
|
|
|
|
|
//then
|
|
|
|
// Drop implementation for net should panic
|
|
|
|
}
|
2024-11-11 07:44:43 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[should_panic]
|
|
|
|
async fn test_unused_post_as_mocknet() {
|
|
|
|
//given
|
|
|
|
let mock_net = kxio::net::mock();
|
|
|
|
|
|
|
|
let url = "https://www.example.com";
|
|
|
|
|
2024-11-17 11:24:28 +00:00
|
|
|
mock_net
|
|
|
|
.on()
|
|
|
|
.post(url)
|
|
|
|
.respond(StatusCode::OK)
|
2024-11-17 09:18:18 +00:00
|
|
|
.body("Post OK")
|
|
|
|
.expect("mock");
|
2024-11-11 07:44:43 +00:00
|
|
|
|
|
|
|
//when
|
|
|
|
// don't send the planned request
|
|
|
|
// let _response = Net::from(net).send(client.post(url)).await.expect("send");
|
|
|
|
|
|
|
|
//then
|
|
|
|
// Drop implementation for mock_net should panic
|
|
|
|
}
|